Mistake on this page?
Report an issue in GitHub or email us
InternetDatagramSocket.h
1 /** \addtogroup netsocket */
2 /** @{*/
3 /* InternetDatagramSocket
4  * Copyright (c) 2015 ARM Limited
5  * SPDX-License-Identifier: Apache-2.0
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 
20 #ifndef INTERNETDATAGRAMSOCKET_H
21 #define INTERNETDATAGRAMSOCKET_H
22 
23 #include "netsocket/InternetSocket.h"
24 #include "netsocket/NetworkStack.h"
26 #include "rtos/EventFlags.h"
27 
28 
29 /** InternetDatagramSocket socket implementation.
30  */
32 public:
33  /** Send data to the specified address.
34  *
35  * By default, sendto blocks until data is sent. If socket is set to
36  * nonblocking or times out, NSAPI_ERROR_WOULD_BLOCK is returned
37  * immediately.
38  *
39  * It uses sendto_control with zero ancillary data
40  * @param address The SocketAddress of the remote host.
41  * @param data Buffer of data to send to the host.
42  * @param size Size of the buffer in bytes.
43  * @retval NSAPI_ERROR_NO_SOCKET in case socket was not created correctly.
44  * @retval NSAPI_ERROR_WOULD_BLOCK in case non-blocking mode is enabled
45  * and send cannot be performed immediately.
46  * @retval int Other negative error codes for stack-related failures.
47  * See \ref NetworkStack::socket_send.
48  */
50  const void *data, nsapi_size_t size) override;
51 
52  /** Receive a datagram and store the source address in address if it's not NULL.
53  *
54  * By default, recvfrom blocks until a datagram is received. If socket is set to
55  * nonblocking or times out with no datagram, NSAPI_ERROR_WOULD_BLOCK
56  * is returned.
57  *
58  * @note If the datagram is larger than the buffer, the excess data is silently discarded.
59  *
60  * @note If socket is connected, only packets coming from connected peer address
61  * are accepted.
62  *
63  * @note recvfrom() is allowed write to address and data buffers even if error occurs.
64  * It uses recvfrom_control with zero ancillary data
65  * @param address Destination for the source address or NULL.
66  * @param data Destination buffer for RAW data to be received from the host.
67  * @param size Size of the buffer in bytes.
68  * @retval int Number of received bytes on success.
69  * @retval NSAPI_ERROR_NO_SOCKET in case socket was not created correctly.
70  * @retval NSAPI_ERROR_WOULD_BLOCK in case non-blocking mode is enabled
71  * and send cannot be performed immediately.
72  * @retval int Other negative error codes for stack-related failures.
73  * See \ref NetworkStack::socket_recv.
74  */
76  void *data, nsapi_size_t size) override;
77 
78  /** Send datagram and ancillary data to the specified address.
79  *
80  * By default, sendto blocks until data is sent. If socket is set to
81  * nonblocking or times out, NSAPI_ERROR_WOULD_BLOCK is returned
82  * immediately.
83  *
84  * It uses sendto_control with zero ancillary data
85  * @param address The SocketAddress of the remote host.
86  * @param data Buffer of data to send to the host.
87  * @param size Size of the buffer in bytes.
88  * @param control Size of the buffer in bytes.
89  * @param control_size Size of the buffer in bytes.
90  * @retval NSAPI_ERROR_NO_SOCKET in case socket was not created correctly.
91  * @retval NSAPI_ERROR_WOULD_BLOCK in case non-blocking mode is enabled
92  * and send cannot be performed immediately.
93  * @retval int Other negative error codes for stack-related failures.
94  * See \ref NetworkStack::socket_send.
95  */
97  const void *data, nsapi_size_t size,
98  nsapi_msghdr_t *control, nsapi_size_t control_size) override;
99 
100 
101  /** Receive a datagram with ancillary data and store the source address in address if it's not NULL.
102  *
103  * By default, recvfrom blocks until a datagram is received. If socket is set to
104  * nonblocking or times out with no datagram, NSAPI_ERROR_WOULD_BLOCK
105  * is returned.
106  * Ancillary data is stored in msghdr struct
107  * @note If the datagram is larger than the buffer, the excess data is silently discarded.
108  *
109  * @note If socket is connected, only packets coming from connected peer address
110  * are accepted.
111  *
112  * @note recvfrom_control() is allowed write to address and data buffers even if error occurs.
113  *
114  * @param address Destination for the source address or NULL.
115  * @param data Destination buffer for RAW data to be received from the host.
116  * @param size Size of the buffer in bytes.
117  * @param control Size of the buffer in bytes.
118  * @param control_size Size of the buffer in bytes.
119  * @retval int Number of received bytes on success.
120  * @retval NSAPI_ERROR_NO_SOCKET in case socket was not created correctly.
121  * @retval NSAPI_ERROR_WOULD_BLOCK in case non-blocking mode is enabled
122  * and send cannot be performed immediately.
123  * @retval int Other negative error codes for stack-related failures.
124  * See \ref NetworkStack::socket_recv.
125  */
127  void *data, nsapi_size_t size,
128  nsapi_msghdr_t *control, nsapi_size_t control_size) override;
129 
130  /** Set the remote address for next send() call and filtering
131  * of incoming packets. To reset the address, zero initialized
132  * SocketAddress must be in the address parameter.
133  *
134  * @param address The SocketAddress of the remote host.
135  * @return NSAPI_ERROR_OK on success.
136  */
137  nsapi_error_t connect(const SocketAddress &address) override;
138 
139  /** Send a raw data to connected remote address.
140  *
141  * By default, send blocks until all data is sent. If socket is set to
142  * nonblocking or times out, a partial amount can be written.
143  * NSAPI_ERROR_WOULD_BLOCK is returned if no data was written.
144  *
145  * @note The socket must be connected to a remote host before send() call.
146  *
147  * @param data Buffer of data to send to the host.
148  * @param size Size of the buffer in bytes.
149  * @retval int Number of sent bytes on success.
150  * @retval NSAPI_ERROR_NO_SOCKET in case socket was not created correctly.
151  * @retval NSAPI_ERROR_WOULD_BLOCK in case non-blocking mode is enabled
152  * and send cannot be performed immediately.
153  * #retval NSAPI_ERROR_NO_ADDRESS if the address was not set with connect().
154  * @retval int Other negative error codes for stack-related failures.
155  * See \ref NetworkStack::socket_send.
156  */
157  nsapi_size_or_error_t send(const void *data, nsapi_size_t size) override;
158 
159  /** Receive data from a socket.
160  *
161  * This is equivalent to calling recvfrom(NULL, data, size).
162  *
163  * By default, recv blocks until some data is received. If socket is set to
164  * nonblocking or times out, NSAPI_ERROR_WOULD_BLOCK can be returned to
165  * indicate no data.
166  *
167  * @note recv() is allowed write to data buffer even if error occurs.
168  *
169  * @param data Pointer to buffer for data received from the host.
170  * @param size Size of the buffer in bytes.
171  * @retval int Number of received bytes on success.
172  * @retval NSAPI_ERROR_NO_SOCKET in case socket was not created correctly.
173  * @retval NSAPI_ERROR_WOULD_BLOCK in case non-blocking mode is enabled
174  * and send cannot be performed immediately.
175  * @retval int Other negative error codes for stack-related failures.
176  * See \ref NetworkStack::socket_recv.
177  */
178  nsapi_size_or_error_t recv(void *data, nsapi_size_t size) override;
179 
180  /** Not implemented for InternetDatagramSocket.
181  *
182  * @param error Not used.
183  * @return NSAPI_ERROR_UNSUPPORTED
184  */
185  Socket *accept(nsapi_error_t *error = nullptr) override;
186 
187  /** Not implemented for InternetDatagramSocket.
188  *
189  * @param backlog Not used.
190  * @return NSAPI_ERROR_UNSUPPORTED
191  */
192  nsapi_error_t listen(int backlog = 1) override;
193 #if !defined(DOXYGEN_ONLY)
194 
195 protected:
196 
197  /** Create an uninitialized socket.
198  *
199  * @note Must call open to initialize the socket on a network stack.
200  */
201  InternetDatagramSocket() = default;
202 
203 #endif //!defined(DOXYGEN_ONLY)
204 };
205 
206 
207 #endif
208 
209 /** @}*/
nsapi_msghdr
Definition: nsapi_types.h:414
Socket implementation that uses IP network stack.
nsapi_size_or_error_t recvfrom_control(SocketAddress *address, void *data, nsapi_size_t size, nsapi_msghdr_t *control, nsapi_size_t control_size) override
Receive a datagram with ancillary data and store the source address in address if it's not NULL...
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.
Network Interface base class.
Socket * accept(nsapi_error_t *error=nullptr) override
Not implemented for InternetDatagramSocket.
signed int nsapi_error_t
Type used to represent error codes.
Definition: nsapi_types.h:142
nsapi_size_or_error_t recvfrom(SocketAddress *address, void *data, nsapi_size_t size) override
Receive a datagram and store the source address in address if it's not NULL.
nsapi_size_or_error_t recv(void *data, nsapi_size_t size) override
Receive data from a socket.
nsapi_error_t connect(const SocketAddress &address) override
Set the remote address for next send() call and filtering of incoming packets.
signed int nsapi_size_or_error_t
Type used to represent either a size or error passed through sockets.
Definition: nsapi_types.h:153
nsapi_size_or_error_t send(const void *data, nsapi_size_t size) override
Send a raw data to connected remote address.
nsapi_error_t listen(int backlog=1) override
Not implemented for InternetDatagramSocket.
SocketAddress class.
Definition: SocketAddress.h:37
Socket interface.
Definition: Socket.h:40
unsigned int nsapi_size_t
Type used to represent the size of data passed through sockets.
Definition: nsapi_types.h:146
NetworkStack class.
nsapi_size_or_error_t sendto(const SocketAddress &address, const void *data, nsapi_size_t size) override
Send data to the specified address.
nsapi_size_or_error_t sendto_control(const SocketAddress &address, const void *data, nsapi_size_t size, nsapi_msghdr_t *control, nsapi_size_t control_size) override
Send datagram and ancillary data to the specified address.
InternetDatagramSocket socket implementation.
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.