mbed-os for GR-LYCHEE

Dependents:   mbed-os-example-blinky-gr-lychee GR-Boads_Camera_sample GR-Boards_Audio_Recoder GR-Boads_Camera_DisplayApp ... more

Committer:
dkato
Date:
Fri Feb 02 05:42:23 2018 +0000
Revision:
0:f782d9c66c49
mbed-os for GR-LYCHEE

Who changed what in which revision?

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