Rtos API example

Committer:
marcozecchini
Date:
Sat Feb 23 12:13:36 2019 +0000
Revision:
0:9fca2b23d0ba
final commit

Who changed what in which revision?

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