Marco Zecchini
/
Example_RTOS
Rtos API example
mbed-os/features/netsocket/TCPSocket.h@0:9fca2b23d0ba, 2019-02-23 (annotated)
- Committer:
- marcozecchini
- Date:
- Sat Feb 23 12:13:36 2019 +0000
- Revision:
- 0:9fca2b23d0ba
final commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
marcozecchini | 0:9fca2b23d0ba | 1 | |
marcozecchini | 0:9fca2b23d0ba | 2 | /** \addtogroup netsocket */ |
marcozecchini | 0:9fca2b23d0ba | 3 | /** @{*/ |
marcozecchini | 0:9fca2b23d0ba | 4 | /* TCPSocket |
marcozecchini | 0:9fca2b23d0ba | 5 | * Copyright (c) 2015 ARM Limited |
marcozecchini | 0:9fca2b23d0ba | 6 | * |
marcozecchini | 0:9fca2b23d0ba | 7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
marcozecchini | 0:9fca2b23d0ba | 8 | * you may not use this file except in compliance with the License. |
marcozecchini | 0:9fca2b23d0ba | 9 | * You may obtain a copy of the License at |
marcozecchini | 0:9fca2b23d0ba | 10 | * |
marcozecchini | 0:9fca2b23d0ba | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
marcozecchini | 0:9fca2b23d0ba | 12 | * |
marcozecchini | 0:9fca2b23d0ba | 13 | * Unless required by applicable law or agreed to in writing, software |
marcozecchini | 0:9fca2b23d0ba | 14 | * distributed under the License is distributed on an "AS IS" BASIS, |
marcozecchini | 0:9fca2b23d0ba | 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
marcozecchini | 0:9fca2b23d0ba | 16 | * See the License for the specific language governing permissions and |
marcozecchini | 0:9fca2b23d0ba | 17 | * limitations under the License. |
marcozecchini | 0:9fca2b23d0ba | 18 | */ |
marcozecchini | 0:9fca2b23d0ba | 19 | |
marcozecchini | 0:9fca2b23d0ba | 20 | #ifndef TCPSOCKET_H |
marcozecchini | 0:9fca2b23d0ba | 21 | #define TCPSOCKET_H |
marcozecchini | 0:9fca2b23d0ba | 22 | |
marcozecchini | 0:9fca2b23d0ba | 23 | #include "netsocket/Socket.h" |
marcozecchini | 0:9fca2b23d0ba | 24 | #include "netsocket/NetworkStack.h" |
marcozecchini | 0:9fca2b23d0ba | 25 | #include "netsocket/NetworkInterface.h" |
marcozecchini | 0:9fca2b23d0ba | 26 | #include "rtos/EventFlags.h" |
marcozecchini | 0:9fca2b23d0ba | 27 | |
marcozecchini | 0:9fca2b23d0ba | 28 | |
marcozecchini | 0:9fca2b23d0ba | 29 | /** TCP socket connection |
marcozecchini | 0:9fca2b23d0ba | 30 | */ |
marcozecchini | 0:9fca2b23d0ba | 31 | class TCPSocket : public Socket { |
marcozecchini | 0:9fca2b23d0ba | 32 | public: |
marcozecchini | 0:9fca2b23d0ba | 33 | /** Create an uninitialized socket |
marcozecchini | 0:9fca2b23d0ba | 34 | * |
marcozecchini | 0:9fca2b23d0ba | 35 | * Must call open to initialize the socket on a network stack. |
marcozecchini | 0:9fca2b23d0ba | 36 | */ |
marcozecchini | 0:9fca2b23d0ba | 37 | TCPSocket(); |
marcozecchini | 0:9fca2b23d0ba | 38 | |
marcozecchini | 0:9fca2b23d0ba | 39 | /** Create a socket on a network interface |
marcozecchini | 0:9fca2b23d0ba | 40 | * |
marcozecchini | 0:9fca2b23d0ba | 41 | * Creates and opens a socket on the network stack of the given |
marcozecchini | 0:9fca2b23d0ba | 42 | * network interface. |
marcozecchini | 0:9fca2b23d0ba | 43 | * |
marcozecchini | 0:9fca2b23d0ba | 44 | * @param stack Network stack as target for socket |
marcozecchini | 0:9fca2b23d0ba | 45 | */ |
marcozecchini | 0:9fca2b23d0ba | 46 | template <typename S> |
marcozecchini | 0:9fca2b23d0ba | 47 | TCPSocket(S *stack) |
marcozecchini | 0:9fca2b23d0ba | 48 | : _pending(0), _event_flag(0), |
marcozecchini | 0:9fca2b23d0ba | 49 | _read_in_progress(false), _write_in_progress(false) |
marcozecchini | 0:9fca2b23d0ba | 50 | { |
marcozecchini | 0:9fca2b23d0ba | 51 | open(stack); |
marcozecchini | 0:9fca2b23d0ba | 52 | } |
marcozecchini | 0:9fca2b23d0ba | 53 | |
marcozecchini | 0:9fca2b23d0ba | 54 | /** Destroy a socket |
marcozecchini | 0:9fca2b23d0ba | 55 | * |
marcozecchini | 0:9fca2b23d0ba | 56 | * Closes socket if the socket is still open |
marcozecchini | 0:9fca2b23d0ba | 57 | */ |
marcozecchini | 0:9fca2b23d0ba | 58 | virtual ~TCPSocket(); |
marcozecchini | 0:9fca2b23d0ba | 59 | |
marcozecchini | 0:9fca2b23d0ba | 60 | /** Override multicast functions to return error for TCP |
marcozecchini | 0:9fca2b23d0ba | 61 | * |
marcozecchini | 0:9fca2b23d0ba | 62 | */ |
marcozecchini | 0:9fca2b23d0ba | 63 | int join_multicast_group(const SocketAddress &address) { return NSAPI_ERROR_UNSUPPORTED; } |
marcozecchini | 0:9fca2b23d0ba | 64 | |
marcozecchini | 0:9fca2b23d0ba | 65 | /** Connects TCP socket to a remote host |
marcozecchini | 0:9fca2b23d0ba | 66 | * |
marcozecchini | 0:9fca2b23d0ba | 67 | * Initiates a connection to a remote server specified by either |
marcozecchini | 0:9fca2b23d0ba | 68 | * a domain name or an IP address and a port. |
marcozecchini | 0:9fca2b23d0ba | 69 | * |
marcozecchini | 0:9fca2b23d0ba | 70 | * @param host Hostname of the remote host |
marcozecchini | 0:9fca2b23d0ba | 71 | * @param port Port of the remote host |
marcozecchini | 0:9fca2b23d0ba | 72 | * @return 0 on success, negative error code on failure |
marcozecchini | 0:9fca2b23d0ba | 73 | */ |
marcozecchini | 0:9fca2b23d0ba | 74 | nsapi_error_t connect(const char *host, uint16_t port); |
marcozecchini | 0:9fca2b23d0ba | 75 | |
marcozecchini | 0:9fca2b23d0ba | 76 | /** Connects TCP socket to a remote host |
marcozecchini | 0:9fca2b23d0ba | 77 | * |
marcozecchini | 0:9fca2b23d0ba | 78 | * Initiates a connection to a remote server specified by the |
marcozecchini | 0:9fca2b23d0ba | 79 | * indicated address. |
marcozecchini | 0:9fca2b23d0ba | 80 | * |
marcozecchini | 0:9fca2b23d0ba | 81 | * @param address The SocketAddress of the remote host |
marcozecchini | 0:9fca2b23d0ba | 82 | * @return 0 on success, negative error code on failure |
marcozecchini | 0:9fca2b23d0ba | 83 | */ |
marcozecchini | 0:9fca2b23d0ba | 84 | nsapi_error_t connect(const SocketAddress &address); |
marcozecchini | 0:9fca2b23d0ba | 85 | |
marcozecchini | 0:9fca2b23d0ba | 86 | /** Send data over a TCP socket |
marcozecchini | 0:9fca2b23d0ba | 87 | * |
marcozecchini | 0:9fca2b23d0ba | 88 | * The socket must be connected to a remote host. Returns the number of |
marcozecchini | 0:9fca2b23d0ba | 89 | * bytes sent from the buffer. |
marcozecchini | 0:9fca2b23d0ba | 90 | * |
marcozecchini | 0:9fca2b23d0ba | 91 | * By default, send blocks until data is sent. If socket is set to |
marcozecchini | 0:9fca2b23d0ba | 92 | * non-blocking or times out, NSAPI_ERROR_WOULD_BLOCK is returned |
marcozecchini | 0:9fca2b23d0ba | 93 | * immediately. |
marcozecchini | 0:9fca2b23d0ba | 94 | * |
marcozecchini | 0:9fca2b23d0ba | 95 | * @param data Buffer of data to send to the host |
marcozecchini | 0:9fca2b23d0ba | 96 | * @param size Size of the buffer in bytes |
marcozecchini | 0:9fca2b23d0ba | 97 | * @return Number of sent bytes on success, negative error |
marcozecchini | 0:9fca2b23d0ba | 98 | * code on failure |
marcozecchini | 0:9fca2b23d0ba | 99 | */ |
marcozecchini | 0:9fca2b23d0ba | 100 | nsapi_size_or_error_t send(const void *data, nsapi_size_t size); |
marcozecchini | 0:9fca2b23d0ba | 101 | |
marcozecchini | 0:9fca2b23d0ba | 102 | /** Receive data over a TCP socket |
marcozecchini | 0:9fca2b23d0ba | 103 | * |
marcozecchini | 0:9fca2b23d0ba | 104 | * The socket must be connected to a remote host. Returns the number of |
marcozecchini | 0:9fca2b23d0ba | 105 | * bytes received into the buffer. |
marcozecchini | 0:9fca2b23d0ba | 106 | * |
marcozecchini | 0:9fca2b23d0ba | 107 | * By default, recv blocks until data is sent. If socket is set to |
marcozecchini | 0:9fca2b23d0ba | 108 | * non-blocking or times out, NSAPI_ERROR_WOULD_BLOCK is returned |
marcozecchini | 0:9fca2b23d0ba | 109 | * immediately. |
marcozecchini | 0:9fca2b23d0ba | 110 | * |
marcozecchini | 0:9fca2b23d0ba | 111 | * @param data Destination buffer for data received from the host |
marcozecchini | 0:9fca2b23d0ba | 112 | * @param size Size of the buffer in bytes |
marcozecchini | 0:9fca2b23d0ba | 113 | * @return Number of received bytes on success, negative error |
marcozecchini | 0:9fca2b23d0ba | 114 | * code on failure |
marcozecchini | 0:9fca2b23d0ba | 115 | */ |
marcozecchini | 0:9fca2b23d0ba | 116 | nsapi_size_or_error_t recv(void *data, nsapi_size_t size); |
marcozecchini | 0:9fca2b23d0ba | 117 | |
marcozecchini | 0:9fca2b23d0ba | 118 | protected: |
marcozecchini | 0:9fca2b23d0ba | 119 | friend class TCPServer; |
marcozecchini | 0:9fca2b23d0ba | 120 | |
marcozecchini | 0:9fca2b23d0ba | 121 | virtual nsapi_protocol_t get_proto(); |
marcozecchini | 0:9fca2b23d0ba | 122 | virtual void event(); |
marcozecchini | 0:9fca2b23d0ba | 123 | |
marcozecchini | 0:9fca2b23d0ba | 124 | volatile unsigned _pending; |
marcozecchini | 0:9fca2b23d0ba | 125 | rtos::EventFlags _event_flag; |
marcozecchini | 0:9fca2b23d0ba | 126 | bool _read_in_progress; |
marcozecchini | 0:9fca2b23d0ba | 127 | bool _write_in_progress; |
marcozecchini | 0:9fca2b23d0ba | 128 | }; |
marcozecchini | 0:9fca2b23d0ba | 129 | |
marcozecchini | 0:9fca2b23d0ba | 130 | |
marcozecchini | 0:9fca2b23d0ba | 131 | #endif |
marcozecchini | 0:9fca2b23d0ba | 132 | |
marcozecchini | 0:9fca2b23d0ba | 133 | /** @}*/ |