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