Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: TYBLE16_simple_data_logger TYBLE16_MP3_Air
Socket.h
00001 /* 00002 * Copyright (c) 2015 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 /** @file Socket.h Abstract Socket interface */ 00018 /** @addtogroup netsocket 00019 * Mbed OS Socket API 00020 * @{ */ 00021 00022 #ifndef SOCKET_H 00023 #define SOCKET_H 00024 00025 #include "netsocket/SocketAddress.h" 00026 #include "Callback.h" 00027 00028 /** Socket interface. 00029 * 00030 * This class defines the Mbed OS Socket API. 00031 * Socket is an abstract interface for communicating with remote endpoints. 00032 * 00033 * This API is intended for applications and libraries instead of 00034 * protocol-specific implementations. For example, TCPSocket 00035 * and UDPSocket are implementations of the Socket interface. 00036 * Socket API is intentionally not protocol-specific, and allows all protocol 00037 * to provide the same API regardless of the underlying transport mechanism. 00038 */ 00039 class Socket { 00040 public: 00041 /** Destroy a socket. 00042 * 00043 * Closes socket if the socket is still open. 00044 */ 00045 virtual ~Socket() {} 00046 00047 /** Closes the socket. 00048 * 00049 * Closes any open connection and deallocates any memory associated 00050 * with the socket. Called from destructor if socket is not closed. 00051 * 00052 * @return NSAPI_ERROR_OK on success. 00053 * Negative subclass-dependent error code on failure. 00054 */ 00055 virtual nsapi_error_t close() = 0; 00056 00057 /** Connects socket to a remote address. 00058 * 00059 * Attempts to make connection on connection-mode protocol or set or reset 00060 * the peer address on connectionless protocol. 00061 * 00062 * Connectionless protocols also use the connected address to filter 00063 * incoming packets for recv() and recvfrom() calls. 00064 * 00065 * To reset the peer address, there must be zero initialized(default constructor) SocketAddress 00066 * objects in the address parameter. 00067 * 00068 * @note If connect() fails it is recommended to close the Socket and create 00069 * a new one before attempting to reconnect. 00070 * 00071 * @param address The SocketAddress of the remote peer. 00072 * @return NSAPI_ERROR_OK on success. 00073 * Negative subclass-dependent error code on failure. 00074 */ 00075 virtual nsapi_error_t connect(const SocketAddress &address) = 0; 00076 00077 /** Send data on a socket 00078 * 00079 * The socket must be connected to a remote host before send() call. 00080 * Returns the number of bytes sent from the buffer. 00081 * In case of connectionless socket, sends data to pre-specified remote. 00082 * 00083 * By default, send blocks until all data is sent. If socket is set to 00084 * non-blocking or times out, a partial amount can be written. 00085 * NSAPI_ERROR_WOULD_BLOCK is returned if no data was written. 00086 * 00087 * @param data Buffer of data to send to the host. 00088 * @param size Size of the buffer in bytes. 00089 * @return NSAPI_ERROR_OK on success. 00090 * Negative subclass-dependent error code on failure. 00091 */ 00092 virtual nsapi_size_or_error_t send(const void *data, nsapi_size_t size) = 0; 00093 00094 /** Receive data from a socket. 00095 * 00096 * Receive data from connected socket, or in the case of connectionless socket, 00097 * equivalent to calling recvfrom(NULL, data, size). 00098 * 00099 * If socket is connected, only packets coming from connected peer address 00100 * are accepted. 00101 * 00102 * @note recv() is allowed write to data buffer even if an error occurs. 00103 * 00104 * By default, recv blocks until some data is received. If socket is set to 00105 * non-blocking or times out, NSAPI_ERROR_WOULD_BLOCK can be returned to 00106 * indicate no data. 00107 * 00108 * @param data Destination buffer for data received from the host. 00109 * @param size Size of the buffer in bytes. 00110 * @return Number of received bytes on success, negative, subclass-dependent 00111 * error code on failure. If no data is available to be received 00112 * and the peer has performed an orderly shutdown, 00113 * recv() returns 0. 00114 */ 00115 virtual nsapi_size_or_error_t recv(void *data, nsapi_size_t size) = 0; 00116 00117 /** Send a message on a socket. 00118 * 00119 * The sendto() function sends a message through a connection-mode or connectionless-mode socket. 00120 * If the socket is a connectionless-mode socket, the message is sent to the address specified. 00121 * If the socket is a connected-mode socket, address is ignored. 00122 * 00123 * By default, sendto blocks until data is sent. If socket is set to 00124 * non-blocking or times out, NSAPI_ERROR_WOULD_BLOCK is returned 00125 * immediately. 00126 * 00127 * @param address Remote address 00128 * @param data Buffer of data to send to the host 00129 * @param size Size of the buffer in bytes 00130 * @return Number of sent bytes on success, negative subclass-dependent error 00131 * code on failure 00132 */ 00133 virtual nsapi_size_or_error_t sendto(const SocketAddress &address, 00134 const void *data, nsapi_size_t size) = 0; 00135 00136 /** Receive a data from a socket 00137 * 00138 * Receives a data and stores the source address in address if address 00139 * is not NULL. Returns the number of bytes written into the buffer. 00140 * 00141 * If socket is connected, only packets coming from connected peer address 00142 * are accepted. 00143 * 00144 * @note recvfrom() is allowed write to address and data buffers even if error occurs. 00145 * 00146 * By default, recvfrom blocks until a datagram is received. If socket is set to 00147 * non-blocking or times out with no data, NSAPI_ERROR_WOULD_BLOCK 00148 * is returned. 00149 * 00150 * @param address Destination for the source address or NULL 00151 * @param data Destination buffer for datagram received from the host 00152 * @param size Size of the buffer in bytes 00153 * @return Number of received bytes on success, negative subclass-dependent 00154 * error code on failure 00155 */ 00156 virtual nsapi_size_or_error_t recvfrom(SocketAddress *address, 00157 void *data, nsapi_size_t size) = 0; 00158 00159 /** Bind a specific address to a socket. 00160 * 00161 * Binding a socket specifies the address and port on which to receive 00162 * data. If the IP address is zeroed, only the port is bound. 00163 * 00164 * @param address Local address to bind. 00165 * @return NSAPI_ERROR_OK on success, negative subclass-dependent error 00166 * code on failure. 00167 */ 00168 virtual nsapi_error_t bind(const SocketAddress &address) = 0; 00169 00170 /** Set blocking or non-blocking mode of the socket. 00171 * 00172 * Initially all sockets are in blocking mode. In non-blocking mode 00173 * blocking operations such as send/recv/accept return 00174 * NSAPI_ERROR_WOULD_BLOCK if they cannot continue. 00175 * 00176 * set_blocking(false) is equivalent to set_timeout(0) 00177 * set_blocking(true) is equivalent to set_timeout(-1) 00178 * 00179 * @param blocking true for blocking mode, false for non-blocking mode. 00180 */ 00181 virtual void set_blocking(bool blocking) = 0; 00182 00183 /** Set timeout on blocking socket operations. 00184 * 00185 * Initially all sockets have unbounded timeouts. NSAPI_ERROR_WOULD_BLOCK 00186 * is returned if a blocking operation takes longer than the specified 00187 * timeout. A timeout of 0 removes the timeout from the socket. A negative 00188 * value gives the socket an unbounded timeout. 00189 * 00190 * set_timeout(0) is equivalent to set_blocking(false) 00191 * set_timeout(-1) is equivalent to set_blocking(true) 00192 * 00193 * @param timeout Timeout in milliseconds 00194 */ 00195 virtual void set_timeout(int timeout) = 0; 00196 00197 /** Register a callback on state change of the socket. 00198 * 00199 * The specified callback is called on state changes, such as when 00200 * the socket can receive/send/accept successfully and when an error 00201 * occurs. The callback may also be called spuriously without reason. 00202 * 00203 * The callback may be called in an interrupt context and should not 00204 * perform expensive operations such as receive/send calls. 00205 * 00206 * Note! This is not intended as a replacement for a poll or attach-like 00207 * asynchronous API, but rather as a building block for constructing 00208 * such functionality. The exact timing of the registered function 00209 * is not guaranteed and susceptible to change. 00210 * 00211 * @param func Function to call on state change. 00212 */ 00213 virtual void sigio(mbed::Callback<void()> func) = 0; 00214 00215 /** Set socket options. 00216 * 00217 * setsockopt() allows an application to pass stack-specific options 00218 * to the underlying stack using stack-specific level and option names, 00219 * or to request generic options using levels from nsapi_socket_level_t. 00220 * 00221 * For unsupported options, NSAPI_ERROR_UNSUPPORTED is returned 00222 * and the socket is unmodified. 00223 * 00224 * @param level Stack-specific protocol level or nsapi_socket_level_t. 00225 * @param optname Level-specific option name. 00226 * @param optval Option value. 00227 * @param optlen Length of the option value. 00228 * @retval NSAPI_ERROR_OK on success. 00229 * @retval NSAPI_ERROR_NO_SOCKET if socket is not open. 00230 * @retval int Negative error code on failure, see @ref NetworkStack::setsockopt. 00231 */ 00232 virtual nsapi_error_t setsockopt(int level, int optname, const void *optval, unsigned optlen) = 0; 00233 00234 /** Get socket options. 00235 * 00236 * getsockopt() allows an application to retrieve stack-specific options 00237 * from the underlying stack using stack-specific level and option names, 00238 * or to request generic options using levels from nsapi_socket_level_t. 00239 * 00240 * For unsupported options, NSAPI_ERROR_UNSUPPORTED is returned 00241 * and the socket is unmodified. 00242 * 00243 * @param level Stack-specific protocol level or nsapi_socket_level_t. 00244 * @param optname Level-specific option name. 00245 * @param optval Destination for option value. 00246 * @param optlen Length of the option value. 00247 * @retval NSAPI_ERROR_OK on success. 00248 * @retval NSAPI_ERROR_NO_SOCKET if socket is not open. 00249 * @retval int Negative error code on failure, see @ref NetworkStack::getsockopt. 00250 */ 00251 virtual nsapi_error_t getsockopt(int level, int optname, void *optval, unsigned *optlen) = 0; 00252 00253 /** Accepts a connection on a socket. 00254 * 00255 * The server socket must be bound and set to listen for connections. 00256 * On a new connection, returns connected network socket to call close() 00257 * that deallocates the resources. Referencing a returned pointer after a close() 00258 * call is not allowed and leads to undefined behavior. 00259 * 00260 * By default, accept blocks until incoming connection occurs. If socket is set to 00261 * non-blocking or times out, error is set to NSAPI_ERROR_WOULD_BLOCK. 00262 * 00263 * @param error Pointer to storage of the error value or NULL. 00264 * @return Pointer to a socket. 00265 */ 00266 virtual Socket *accept(nsapi_error_t *error = NULL) = 0; 00267 00268 /** Listen for incoming connections. 00269 * 00270 * Marks the socket as a passive socket that can be used to accept 00271 * incoming connections. 00272 * 00273 * @param backlog Number of pending connections that can be queued 00274 * simultaneously, defaults to 1. 00275 * @return NSAPI_ERROR_OK on success, negative error code on failure. 00276 */ 00277 virtual nsapi_error_t listen(int backlog = 1) = 0; 00278 00279 /** Get the remote-end peer associated with this socket. 00280 * 00281 * Copy the remote peer address to a SocketAddress structure pointed by 00282 * address parameter. Socket must be connected to have a peer address 00283 * associated. 00284 * 00285 * @param address Pointer to SocketAddress structure. 00286 * @retval NSAPI_ERROR_OK on success. 00287 * @retval NSAPI_ERROR_NO_SOCKET if socket is not connected. 00288 * @retval NSAPI_ERROR_NO_CONNECTION if the remote peer was not set. 00289 */ 00290 virtual nsapi_error_t getpeername(SocketAddress *address) = 0; 00291 }; 00292 00293 00294 #endif 00295 00296 /** @}*/
Generated on Tue Jul 12 2022 13:54:51 by
1.7.2