RTC auf true
features/netsocket/Socket.h@2:7aab896b1a3b, 2019-03-13 (annotated)
- Committer:
- kevman
- Date:
- Wed Mar 13 11:03:24 2019 +0000
- Revision:
- 2:7aab896b1a3b
- Parent:
- 0:38ceb79fef03
2019-03-13
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kevman | 0:38ceb79fef03 | 1 | |
kevman | 0:38ceb79fef03 | 2 | /** \addtogroup netsocket */ |
kevman | 0:38ceb79fef03 | 3 | /** @{*/ |
kevman | 0:38ceb79fef03 | 4 | /* Socket |
kevman | 0:38ceb79fef03 | 5 | * Copyright (c) 2015 ARM Limited |
kevman | 0:38ceb79fef03 | 6 | * |
kevman | 0:38ceb79fef03 | 7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
kevman | 0:38ceb79fef03 | 8 | * you may not use this file except in compliance with the License. |
kevman | 0:38ceb79fef03 | 9 | * You may obtain a copy of the License at |
kevman | 0:38ceb79fef03 | 10 | * |
kevman | 0:38ceb79fef03 | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
kevman | 0:38ceb79fef03 | 12 | * |
kevman | 0:38ceb79fef03 | 13 | * Unless required by applicable law or agreed to in writing, software |
kevman | 0:38ceb79fef03 | 14 | * distributed under the License is distributed on an "AS IS" BASIS, |
kevman | 0:38ceb79fef03 | 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
kevman | 0:38ceb79fef03 | 16 | * See the License for the specific language governing permissions and |
kevman | 0:38ceb79fef03 | 17 | * limitations under the License. |
kevman | 0:38ceb79fef03 | 18 | */ |
kevman | 0:38ceb79fef03 | 19 | |
kevman | 0:38ceb79fef03 | 20 | #ifndef SOCKET_H |
kevman | 0:38ceb79fef03 | 21 | #define SOCKET_H |
kevman | 0:38ceb79fef03 | 22 | |
kevman | 0:38ceb79fef03 | 23 | #include "netsocket/SocketAddress.h" |
kevman | 0:38ceb79fef03 | 24 | #include "Callback.h" |
kevman | 0:38ceb79fef03 | 25 | |
kevman | 0:38ceb79fef03 | 26 | /** Abstract socket class |
kevman | 0:38ceb79fef03 | 27 | */ |
kevman | 0:38ceb79fef03 | 28 | class Socket { |
kevman | 0:38ceb79fef03 | 29 | public: |
kevman | 0:38ceb79fef03 | 30 | /** Destroy a socket. |
kevman | 0:38ceb79fef03 | 31 | * |
kevman | 0:38ceb79fef03 | 32 | * Closes socket if the socket is still open |
kevman | 0:38ceb79fef03 | 33 | */ |
kevman | 0:38ceb79fef03 | 34 | virtual ~Socket() {} |
kevman | 0:38ceb79fef03 | 35 | |
kevman | 0:38ceb79fef03 | 36 | /** Close the socket. |
kevman | 0:38ceb79fef03 | 37 | * |
kevman | 0:38ceb79fef03 | 38 | * Closes any open connection and deallocates any memory associated |
kevman | 0:38ceb79fef03 | 39 | * with the socket. Called from destructor if socket is not closed. |
kevman | 0:38ceb79fef03 | 40 | * |
kevman | 0:38ceb79fef03 | 41 | * @return NSAPI_ERROR_OK on success, negative error code on failure |
kevman | 0:38ceb79fef03 | 42 | */ |
kevman | 0:38ceb79fef03 | 43 | virtual nsapi_error_t close() = 0; |
kevman | 0:38ceb79fef03 | 44 | |
kevman | 0:38ceb79fef03 | 45 | /** Connects socket to a remote address. |
kevman | 0:38ceb79fef03 | 46 | * |
kevman | 0:38ceb79fef03 | 47 | * Attempt to make connection on connection-mode protocol or set or reset |
kevman | 0:38ceb79fef03 | 48 | * the peer address on connectionless protocol. |
kevman | 0:38ceb79fef03 | 49 | * |
kevman | 0:38ceb79fef03 | 50 | * Also connectionless protocols use the connected address to filter |
kevman | 0:38ceb79fef03 | 51 | * incoming packets for recv() and recvfrom() calls. |
kevman | 0:38ceb79fef03 | 52 | * |
kevman | 0:38ceb79fef03 | 53 | * To reset the peer address, zero initialised(default constructor) SocketAddress |
kevman | 0:38ceb79fef03 | 54 | * object have to be in the address parameter. |
kevman | 0:38ceb79fef03 | 55 | * |
kevman | 0:38ceb79fef03 | 56 | * @param address The SocketAddress of the remote peer |
kevman | 0:38ceb79fef03 | 57 | * @return NSAPI_ERROR_OK on success, negative error code on failure |
kevman | 0:38ceb79fef03 | 58 | */ |
kevman | 0:38ceb79fef03 | 59 | virtual nsapi_error_t connect(const SocketAddress &address) = 0; |
kevman | 0:38ceb79fef03 | 60 | |
kevman | 0:38ceb79fef03 | 61 | /** Send data on a socket |
kevman | 0:38ceb79fef03 | 62 | * |
kevman | 0:38ceb79fef03 | 63 | * The socket must be connected to a remote host before send() call. |
kevman | 0:38ceb79fef03 | 64 | * Returns the number of bytes sent from the buffer. |
kevman | 0:38ceb79fef03 | 65 | * In case of connectionless socket, send data to pre-specified remote. |
kevman | 0:38ceb79fef03 | 66 | * |
kevman | 0:38ceb79fef03 | 67 | * By default, send blocks until all data is sent. If socket is set to |
kevman | 0:38ceb79fef03 | 68 | * non-blocking or times out, a partial amount can be written. |
kevman | 0:38ceb79fef03 | 69 | * NSAPI_ERROR_WOULD_BLOCK is returned if no data was written. |
kevman | 0:38ceb79fef03 | 70 | * |
kevman | 0:38ceb79fef03 | 71 | * @param data Buffer of data to send to the host |
kevman | 0:38ceb79fef03 | 72 | * @param size Size of the buffer in bytes |
kevman | 0:38ceb79fef03 | 73 | * @return Number of sent bytes on success, negative error |
kevman | 0:38ceb79fef03 | 74 | * code on failure. |
kevman | 0:38ceb79fef03 | 75 | */ |
kevman | 0:38ceb79fef03 | 76 | virtual nsapi_size_or_error_t send(const void *data, nsapi_size_t size) = 0; |
kevman | 0:38ceb79fef03 | 77 | |
kevman | 0:38ceb79fef03 | 78 | /** Receive data from a socket. |
kevman | 0:38ceb79fef03 | 79 | * |
kevman | 0:38ceb79fef03 | 80 | * Receive data from connected socket or in case of connectionless socket |
kevman | 0:38ceb79fef03 | 81 | * this is equivalent of calling recvfrom(NULL, data, size). |
kevman | 0:38ceb79fef03 | 82 | * |
kevman | 0:38ceb79fef03 | 83 | * If socket is connected, only packets coming from connected peer address |
kevman | 0:38ceb79fef03 | 84 | * are accepted. |
kevman | 0:38ceb79fef03 | 85 | * |
kevman | 0:38ceb79fef03 | 86 | * @note recv() is allowed write to data buffer even if error occurs. |
kevman | 0:38ceb79fef03 | 87 | * |
kevman | 0:38ceb79fef03 | 88 | * By default, recv blocks until some data is received. If socket is set to |
kevman | 0:38ceb79fef03 | 89 | * non-blocking or times out, NSAPI_ERROR_WOULD_BLOCK can be returned to |
kevman | 0:38ceb79fef03 | 90 | * indicate no data. |
kevman | 0:38ceb79fef03 | 91 | * |
kevman | 0:38ceb79fef03 | 92 | * @param data Destination buffer for data received from the host |
kevman | 0:38ceb79fef03 | 93 | * @param size Size of the buffer in bytes |
kevman | 0:38ceb79fef03 | 94 | * @return Number of received bytes on success, negative error |
kevman | 0:38ceb79fef03 | 95 | * code on failure. If no data is available to be received |
kevman | 0:38ceb79fef03 | 96 | * and the peer has performed an orderly shutdown, |
kevman | 0:38ceb79fef03 | 97 | * recv() returns 0. |
kevman | 0:38ceb79fef03 | 98 | */ |
kevman | 0:38ceb79fef03 | 99 | virtual nsapi_size_or_error_t recv(void *data, nsapi_size_t size) = 0; |
kevman | 0:38ceb79fef03 | 100 | |
kevman | 0:38ceb79fef03 | 101 | /** Send a message on a socket. |
kevman | 0:38ceb79fef03 | 102 | * |
kevman | 0:38ceb79fef03 | 103 | * The sendto() function shall send a message through a connection-mode or connectionless-mode socket. |
kevman | 0:38ceb79fef03 | 104 | * If the socket is connectionless-mode, the message shall be sent to the address specified. |
kevman | 0:38ceb79fef03 | 105 | * If the socket is connection-mode, address shall be ignored. |
kevman | 0:38ceb79fef03 | 106 | * |
kevman | 0:38ceb79fef03 | 107 | * By default, sendto blocks until data is sent. If socket is set to |
kevman | 0:38ceb79fef03 | 108 | * non-blocking or times out, NSAPI_ERROR_WOULD_BLOCK is returned |
kevman | 0:38ceb79fef03 | 109 | * immediately. |
kevman | 0:38ceb79fef03 | 110 | * |
kevman | 0:38ceb79fef03 | 111 | * @param address Remote address |
kevman | 0:38ceb79fef03 | 112 | * @param data Buffer of data to send to the host |
kevman | 0:38ceb79fef03 | 113 | * @param size Size of the buffer in bytes |
kevman | 0:38ceb79fef03 | 114 | * @return Number of sent bytes on success, negative error |
kevman | 0:38ceb79fef03 | 115 | * code on failure |
kevman | 0:38ceb79fef03 | 116 | */ |
kevman | 0:38ceb79fef03 | 117 | virtual nsapi_size_or_error_t sendto(const SocketAddress &address, |
kevman | 0:38ceb79fef03 | 118 | const void *data, nsapi_size_t size) = 0; |
kevman | 0:38ceb79fef03 | 119 | |
kevman | 0:38ceb79fef03 | 120 | /** Receive a data from a socket |
kevman | 0:38ceb79fef03 | 121 | * |
kevman | 0:38ceb79fef03 | 122 | * Receives a data and stores the source address in address if address |
kevman | 0:38ceb79fef03 | 123 | * is not NULL. Returns the number of bytes written into the buffer. |
kevman | 0:38ceb79fef03 | 124 | * |
kevman | 0:38ceb79fef03 | 125 | * If socket is connected, only packets coming from connected peer address |
kevman | 0:38ceb79fef03 | 126 | * are accepted. |
kevman | 0:38ceb79fef03 | 127 | * |
kevman | 0:38ceb79fef03 | 128 | * @note recvfrom() is allowed write to address and data buffers even if error occurs. |
kevman | 0:38ceb79fef03 | 129 | * |
kevman | 0:38ceb79fef03 | 130 | * By default, recvfrom blocks until a datagram is received. If socket is set to |
kevman | 0:38ceb79fef03 | 131 | * non-blocking or times out with no data, NSAPI_ERROR_WOULD_BLOCK |
kevman | 0:38ceb79fef03 | 132 | * is returned. |
kevman | 0:38ceb79fef03 | 133 | * |
kevman | 0:38ceb79fef03 | 134 | * @param address Destination for the source address or NULL |
kevman | 0:38ceb79fef03 | 135 | * @param data Destination buffer for datagram received from the host |
kevman | 0:38ceb79fef03 | 136 | * @param size Size of the buffer in bytes |
kevman | 0:38ceb79fef03 | 137 | * @return Number of received bytes on success, negative error |
kevman | 0:38ceb79fef03 | 138 | * code on failure |
kevman | 0:38ceb79fef03 | 139 | */ |
kevman | 0:38ceb79fef03 | 140 | virtual nsapi_size_or_error_t recvfrom(SocketAddress *address, |
kevman | 0:38ceb79fef03 | 141 | void *data, nsapi_size_t size) = 0; |
kevman | 0:38ceb79fef03 | 142 | |
kevman | 0:38ceb79fef03 | 143 | /** Bind a specific address to a socket. |
kevman | 0:38ceb79fef03 | 144 | * |
kevman | 0:38ceb79fef03 | 145 | * Binding a socket specifies the address and port on which to receive |
kevman | 0:38ceb79fef03 | 146 | * data. If the IP address is zeroed, only the port is bound. |
kevman | 0:38ceb79fef03 | 147 | * |
kevman | 0:38ceb79fef03 | 148 | * @param address Local address to bind |
kevman | 0:38ceb79fef03 | 149 | * @return NSAPI_ERROR_OK on success, negative error code on failure. |
kevman | 0:38ceb79fef03 | 150 | */ |
kevman | 0:38ceb79fef03 | 151 | virtual nsapi_error_t bind(const SocketAddress &address) = 0; |
kevman | 0:38ceb79fef03 | 152 | |
kevman | 0:38ceb79fef03 | 153 | /** Set blocking or non-blocking mode of the socket. |
kevman | 0:38ceb79fef03 | 154 | * |
kevman | 0:38ceb79fef03 | 155 | * Initially all sockets are in blocking mode. In non-blocking mode |
kevman | 0:38ceb79fef03 | 156 | * blocking operations such as send/recv/accept return |
kevman | 0:38ceb79fef03 | 157 | * NSAPI_ERROR_WOULD_BLOCK if they can not continue. |
kevman | 0:38ceb79fef03 | 158 | * |
kevman | 0:38ceb79fef03 | 159 | * set_blocking(false) is equivalent to set_timeout(0) |
kevman | 0:38ceb79fef03 | 160 | * set_blocking(true) is equivalent to set_timeout(-1) |
kevman | 0:38ceb79fef03 | 161 | * |
kevman | 0:38ceb79fef03 | 162 | * @param blocking true for blocking mode, false for non-blocking mode. |
kevman | 0:38ceb79fef03 | 163 | */ |
kevman | 0:38ceb79fef03 | 164 | virtual void set_blocking(bool blocking) = 0; |
kevman | 0:38ceb79fef03 | 165 | |
kevman | 0:38ceb79fef03 | 166 | /** Set timeout on blocking socket operations. |
kevman | 0:38ceb79fef03 | 167 | * |
kevman | 0:38ceb79fef03 | 168 | * Initially all sockets have unbounded timeouts. NSAPI_ERROR_WOULD_BLOCK |
kevman | 0:38ceb79fef03 | 169 | * is returned if a blocking operation takes longer than the specified |
kevman | 0:38ceb79fef03 | 170 | * timeout. A timeout of 0 removes the timeout from the socket. A negative |
kevman | 0:38ceb79fef03 | 171 | * value give the socket an unbounded timeout. |
kevman | 0:38ceb79fef03 | 172 | * |
kevman | 0:38ceb79fef03 | 173 | * set_timeout(0) is equivalent to set_blocking(false) |
kevman | 0:38ceb79fef03 | 174 | * set_timeout(-1) is equivalent to set_blocking(true) |
kevman | 0:38ceb79fef03 | 175 | * |
kevman | 0:38ceb79fef03 | 176 | * @param timeout Timeout in milliseconds |
kevman | 0:38ceb79fef03 | 177 | */ |
kevman | 0:38ceb79fef03 | 178 | virtual void set_timeout(int timeout) = 0; |
kevman | 0:38ceb79fef03 | 179 | |
kevman | 0:38ceb79fef03 | 180 | /** Register a callback on state change of the socket. |
kevman | 0:38ceb79fef03 | 181 | * |
kevman | 0:38ceb79fef03 | 182 | * The specified callback will be called on state changes such as when |
kevman | 0:38ceb79fef03 | 183 | * the socket can recv/send/accept successfully and on when an error |
kevman | 0:38ceb79fef03 | 184 | * occurs. The callback may also be called spuriously without reason. |
kevman | 0:38ceb79fef03 | 185 | * |
kevman | 0:38ceb79fef03 | 186 | * The callback may be called in an interrupt context and should not |
kevman | 0:38ceb79fef03 | 187 | * perform expensive operations such as recv/send calls. |
kevman | 0:38ceb79fef03 | 188 | * |
kevman | 0:38ceb79fef03 | 189 | * Note! This is not intended as a replacement for a poll or attach-like |
kevman | 0:38ceb79fef03 | 190 | * asynchronous api, but rather as a building block for constructing |
kevman | 0:38ceb79fef03 | 191 | * such functionality. The exact timing of when the registered function |
kevman | 0:38ceb79fef03 | 192 | * is called is not guaranteed and susceptible to change. |
kevman | 0:38ceb79fef03 | 193 | * |
kevman | 0:38ceb79fef03 | 194 | * @param func Function to call on state change. |
kevman | 0:38ceb79fef03 | 195 | */ |
kevman | 0:38ceb79fef03 | 196 | virtual void sigio(mbed::Callback<void()> func) = 0; |
kevman | 0:38ceb79fef03 | 197 | |
kevman | 0:38ceb79fef03 | 198 | /** Set socket options. |
kevman | 0:38ceb79fef03 | 199 | * |
kevman | 0:38ceb79fef03 | 200 | * setsockopt() allows an application to pass stack-specific options |
kevman | 0:38ceb79fef03 | 201 | * to the underlying stack using stack-specific level and option names, |
kevman | 0:38ceb79fef03 | 202 | * or to request generic options using levels from nsapi_socket_level_t. |
kevman | 0:38ceb79fef03 | 203 | * |
kevman | 0:38ceb79fef03 | 204 | * For unsupported options, NSAPI_ERROR_UNSUPPORTED is returned |
kevman | 0:38ceb79fef03 | 205 | * and the socket is unmodified. |
kevman | 0:38ceb79fef03 | 206 | * |
kevman | 0:38ceb79fef03 | 207 | * @param level Stack-specific protocol level or nsapi_socket_level_t. |
kevman | 0:38ceb79fef03 | 208 | * @param optname Level-specific option name. |
kevman | 0:38ceb79fef03 | 209 | * @param optval Option value. |
kevman | 0:38ceb79fef03 | 210 | * @param optlen Length of the option value. |
kevman | 0:38ceb79fef03 | 211 | * @return NSAPI_ERROR_OK on success, negative error code on failure. |
kevman | 0:38ceb79fef03 | 212 | */ |
kevman | 0:38ceb79fef03 | 213 | virtual nsapi_error_t setsockopt(int level, int optname, const void *optval, unsigned optlen) = 0; |
kevman | 0:38ceb79fef03 | 214 | |
kevman | 0:38ceb79fef03 | 215 | /** Get socket options. |
kevman | 0:38ceb79fef03 | 216 | * |
kevman | 0:38ceb79fef03 | 217 | * getsockopt() allows an application to retrieve stack-specific options |
kevman | 0:38ceb79fef03 | 218 | * from the underlying stack using stack-specific level and option names, |
kevman | 0:38ceb79fef03 | 219 | * or to request generic options using levels from nsapi_socket_level_t. |
kevman | 0:38ceb79fef03 | 220 | * |
kevman | 0:38ceb79fef03 | 221 | * For unsupported options, NSAPI_ERROR_UNSUPPORTED is returned |
kevman | 0:38ceb79fef03 | 222 | * and the socket is unmodified. |
kevman | 0:38ceb79fef03 | 223 | * |
kevman | 0:38ceb79fef03 | 224 | * @param level Stack-specific protocol level or nsapi_socket_level_t. |
kevman | 0:38ceb79fef03 | 225 | * @param optname Level-specific option name. |
kevman | 0:38ceb79fef03 | 226 | * @param optval Destination for option value. |
kevman | 0:38ceb79fef03 | 227 | * @param optlen Length of the option value. |
kevman | 0:38ceb79fef03 | 228 | * @return NSAPI_ERROR_OK on success, negative error code on failure. |
kevman | 0:38ceb79fef03 | 229 | */ |
kevman | 0:38ceb79fef03 | 230 | virtual nsapi_error_t getsockopt(int level, int optname, void *optval, unsigned *optlen) = 0; |
kevman | 0:38ceb79fef03 | 231 | |
kevman | 0:38ceb79fef03 | 232 | /** Accepts a connection on a socket. |
kevman | 0:38ceb79fef03 | 233 | * |
kevman | 0:38ceb79fef03 | 234 | * The server socket must be bound and set to listen for connections. |
kevman | 0:38ceb79fef03 | 235 | * On a new connection, returns connected network socket which user is expected to call close() |
kevman | 0:38ceb79fef03 | 236 | * and that deallocates the resources. Referencing a returned pointer after a close() |
kevman | 0:38ceb79fef03 | 237 | * call is not allowed and leads to undefined behaviour. |
kevman | 0:38ceb79fef03 | 238 | * |
kevman | 0:38ceb79fef03 | 239 | * By default, accept blocks until incomming connection occurs. If socket is set to |
kevman | 0:38ceb79fef03 | 240 | * non-blocking or times out, error is set to NSAPI_ERROR_WOULD_BLOCK. |
kevman | 0:38ceb79fef03 | 241 | * |
kevman | 0:38ceb79fef03 | 242 | * @param error pointer to storage of the error value or NULL |
kevman | 0:38ceb79fef03 | 243 | * @return pointer to a socket |
kevman | 0:38ceb79fef03 | 244 | */ |
kevman | 0:38ceb79fef03 | 245 | virtual Socket *accept(nsapi_error_t *error = NULL) = 0; |
kevman | 0:38ceb79fef03 | 246 | |
kevman | 0:38ceb79fef03 | 247 | /** Listen for incoming connections. |
kevman | 0:38ceb79fef03 | 248 | * |
kevman | 0:38ceb79fef03 | 249 | * Marks the socket as a passive socket that can be used to accept |
kevman | 0:38ceb79fef03 | 250 | * incoming connections. |
kevman | 0:38ceb79fef03 | 251 | * |
kevman | 0:38ceb79fef03 | 252 | * @param backlog Number of pending connections that can be queued |
kevman | 0:38ceb79fef03 | 253 | * simultaneously, defaults to 1 |
kevman | 0:38ceb79fef03 | 254 | * @return NSAPI_ERROR_OK on success, negative error code on failure |
kevman | 0:38ceb79fef03 | 255 | */ |
kevman | 0:38ceb79fef03 | 256 | virtual nsapi_error_t listen(int backlog = 1) = 0; |
kevman | 0:38ceb79fef03 | 257 | }; |
kevman | 0:38ceb79fef03 | 258 | |
kevman | 0:38ceb79fef03 | 259 | |
kevman | 0:38ceb79fef03 | 260 | #endif |
kevman | 0:38ceb79fef03 | 261 | |
kevman | 0:38ceb79fef03 | 262 | /** @}*/ |