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