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