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