Committer:
borlanic
Date:
Fri Mar 30 14:07:05 2018 +0000
Revision:
4:75df35ef4fb6
Parent:
0:380207fcb5c1
commentar

Who changed what in which revision?

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