Entrega 3er corte - sistemas embebidos

Committer:
Bethory
Date:
Wed May 30 04:46:28 2018 +0000
Revision:
1:fcdb45ee95b9
Parent:
0:6ad07c9019fd
Entrega Final

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Bethory 0:6ad07c9019fd 1
Bethory 0:6ad07c9019fd 2 /* TCPServer
Bethory 0:6ad07c9019fd 3 * Copyright (c) 2015 ARM Limited
Bethory 0:6ad07c9019fd 4 *
Bethory 0:6ad07c9019fd 5 * Licensed under the Apache License, Version 2.0 (the "License");
Bethory 0:6ad07c9019fd 6 * you may not use this file except in compliance with the License.
Bethory 0:6ad07c9019fd 7 * You may obtain a copy of the License at
Bethory 0:6ad07c9019fd 8 *
Bethory 0:6ad07c9019fd 9 * http://www.apache.org/licenses/LICENSE-2.0
Bethory 0:6ad07c9019fd 10 *
Bethory 0:6ad07c9019fd 11 * Unless required by applicable law or agreed to in writing, software
Bethory 0:6ad07c9019fd 12 * distributed under the License is distributed on an "AS IS" BASIS,
Bethory 0:6ad07c9019fd 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Bethory 0:6ad07c9019fd 14 * See the License for the specific language governing permissions and
Bethory 0:6ad07c9019fd 15 * limitations under the License.
Bethory 0:6ad07c9019fd 16 */
Bethory 0:6ad07c9019fd 17
Bethory 0:6ad07c9019fd 18 #ifndef TCPSERVER_H
Bethory 0:6ad07c9019fd 19 #define TCPSERVER_H
Bethory 0:6ad07c9019fd 20
Bethory 0:6ad07c9019fd 21 #include "netsocket/Socket.h"
Bethory 0:6ad07c9019fd 22 #include "netsocket/TCPSocket.h"
Bethory 0:6ad07c9019fd 23 #include "netsocket/NetworkStack.h"
Bethory 0:6ad07c9019fd 24 #include "netsocket/NetworkInterface.h"
Bethory 0:6ad07c9019fd 25 #include "rtos/Semaphore.h"
Bethory 0:6ad07c9019fd 26
Bethory 0:6ad07c9019fd 27
Bethory 0:6ad07c9019fd 28 /** TCP socket server
Bethory 0:6ad07c9019fd 29 * @addtogroup netsocket
Bethory 0:6ad07c9019fd 30 */
Bethory 0:6ad07c9019fd 31 class TCPServer : public Socket {
Bethory 0:6ad07c9019fd 32 public:
Bethory 0:6ad07c9019fd 33 /** Create an uninitialized socket
Bethory 0:6ad07c9019fd 34 *
Bethory 0:6ad07c9019fd 35 * Must call open to initialize the socket on a network stack.
Bethory 0:6ad07c9019fd 36 */
Bethory 0:6ad07c9019fd 37 TCPServer();
Bethory 0:6ad07c9019fd 38
Bethory 0:6ad07c9019fd 39 /** Create a socket on a network interface
Bethory 0:6ad07c9019fd 40 *
Bethory 0:6ad07c9019fd 41 * Creates and opens a socket on the network stack of the given
Bethory 0:6ad07c9019fd 42 * network interface.
Bethory 0:6ad07c9019fd 43 *
Bethory 0:6ad07c9019fd 44 * @param stack Network stack as target for socket
Bethory 0:6ad07c9019fd 45 */
Bethory 0:6ad07c9019fd 46 template <typename S>
Bethory 0:6ad07c9019fd 47 TCPServer(S *stack)
Bethory 0:6ad07c9019fd 48 : _pending(0), _accept_sem(0)
Bethory 0:6ad07c9019fd 49 {
Bethory 0:6ad07c9019fd 50 open(stack);
Bethory 0:6ad07c9019fd 51 }
Bethory 0:6ad07c9019fd 52
Bethory 0:6ad07c9019fd 53 /** Destroy a socket
Bethory 0:6ad07c9019fd 54 *
Bethory 0:6ad07c9019fd 55 * Closes socket if the socket is still open
Bethory 0:6ad07c9019fd 56 */
Bethory 0:6ad07c9019fd 57 virtual ~TCPServer();
Bethory 0:6ad07c9019fd 58
Bethory 0:6ad07c9019fd 59 /** Listen for connections on a TCP socket
Bethory 0:6ad07c9019fd 60 *
Bethory 0:6ad07c9019fd 61 * Marks the socket as a passive socket that can be used to accept
Bethory 0:6ad07c9019fd 62 * incoming connections.
Bethory 0:6ad07c9019fd 63 *
Bethory 0:6ad07c9019fd 64 * @param backlog Number of pending connections that can be queued
Bethory 0:6ad07c9019fd 65 * simultaneously, defaults to 1
Bethory 0:6ad07c9019fd 66 * @return 0 on success, negative error code on failure
Bethory 0:6ad07c9019fd 67 */
Bethory 0:6ad07c9019fd 68 nsapi_error_t listen(int backlog = 1);
Bethory 0:6ad07c9019fd 69
Bethory 0:6ad07c9019fd 70 /** Accepts a connection on a TCP socket
Bethory 0:6ad07c9019fd 71 *
Bethory 0:6ad07c9019fd 72 * The server socket must be bound and set to listen for connections.
Bethory 0:6ad07c9019fd 73 * On a new connection, creates a network socket using the specified
Bethory 0:6ad07c9019fd 74 * socket instance.
Bethory 0:6ad07c9019fd 75 *
Bethory 0:6ad07c9019fd 76 * By default, accept blocks until data is sent. If socket is set to
Bethory 0:6ad07c9019fd 77 * non-blocking or times out, NSAPI_ERROR_WOULD_BLOCK is returned
Bethory 0:6ad07c9019fd 78 * immediately.
Bethory 0:6ad07c9019fd 79 *
Bethory 0:6ad07c9019fd 80 * @param connection TCPSocket instance that will handle the incoming connection.
Bethory 0:6ad07c9019fd 81 * @param address Destination for the remote address or NULL
Bethory 0:6ad07c9019fd 82 * @return 0 on success, negative error code on failure
Bethory 0:6ad07c9019fd 83 */
Bethory 0:6ad07c9019fd 84 nsapi_error_t accept(TCPSocket *connection, SocketAddress *address = NULL);
Bethory 0:6ad07c9019fd 85
Bethory 0:6ad07c9019fd 86 protected:
Bethory 0:6ad07c9019fd 87 virtual nsapi_protocol_t get_proto();
Bethory 0:6ad07c9019fd 88 virtual void event();
Bethory 0:6ad07c9019fd 89
Bethory 0:6ad07c9019fd 90 volatile unsigned _pending;
Bethory 0:6ad07c9019fd 91 rtos::Semaphore _accept_sem;
Bethory 0:6ad07c9019fd 92 };
Bethory 0:6ad07c9019fd 93
Bethory 0:6ad07c9019fd 94
Bethory 0:6ad07c9019fd 95 #endif