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

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.