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