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  * @param address The SocketAddress of the remote host.
40  * @param data Buffer of data to send to the host.
41  * @param size Size of the buffer in bytes.
42  * @retval NSAPI_ERROR_NO_SOCKET in case socket was not created correctly.
43  * @retval NSAPI_ERROR_WOULD_BLOCK in case non-blocking mode is enabled
44  * and send cannot be performed immediately.
45  * @retval int Other negative error codes for stack-related failures.
46  * See \ref NetworkStack::socket_send.
47  */
49  const void *data, nsapi_size_t size) override;
50 
51  /** Receive a datagram and store the source address in address if it's not NULL.
52  *
53  * By default, recvfrom blocks until a datagram is received. If socket is set to
54  * nonblocking or times out with no datagram, NSAPI_ERROR_WOULD_BLOCK
55  * is returned.
56  *
57  * @note If the datagram is larger than the buffer, the excess data is silently discarded.
58  *
59  * @note If socket is connected, only packets coming from connected peer address
60  * are accepted.
61  *
62  * @note recvfrom() is allowed write to address and data buffers even if error occurs.
63  *
64  * @param address Destination for the source address or NULL.
65  * @param data Destination buffer for RAW data to be received from the host.
66  * @param size Size of the buffer in bytes.
67  * @retval int Number of received bytes on success.
68  * @retval NSAPI_ERROR_NO_SOCKET in case socket was not created correctly.
69  * @retval NSAPI_ERROR_WOULD_BLOCK in case non-blocking mode is enabled
70  * and send cannot be performed immediately.
71  * @retval int Other negative error codes for stack-related failures.
72  * See \ref NetworkStack::socket_recv.
73  */
75  void *data, nsapi_size_t size) override;
76 
77  /** Set the remote address for next send() call and filtering
78  * of incoming packets. To reset the address, zero initialized
79  * SocketAddress must be in the address parameter.
80  *
81  * @param address The SocketAddress of the remote host.
82  * @return NSAPI_ERROR_OK on success.
83  */
84  nsapi_error_t connect(const SocketAddress &address) override;
85 
86  /** Send a raw data to connected remote address.
87  *
88  * By default, send blocks until all data is sent. If socket is set to
89  * nonblocking or times out, a partial amount can be written.
90  * NSAPI_ERROR_WOULD_BLOCK is returned if no data was written.
91  *
92  * @note The socket must be connected to a remote host before send() call.
93  *
94  * @param data Buffer of data to send to the host.
95  * @param size Size of the buffer in bytes.
96  * @retval int Number of sent bytes on success.
97  * @retval NSAPI_ERROR_NO_SOCKET in case socket was not created correctly.
98  * @retval NSAPI_ERROR_WOULD_BLOCK in case non-blocking mode is enabled
99  * and send cannot be performed immediately.
100  * #retval NSAPI_ERROR_NO_ADDRESS if the address was not set with connect().
101  * @retval int Other negative error codes for stack-related failures.
102  * See \ref NetworkStack::socket_send.
103  */
104  nsapi_size_or_error_t send(const void *data, nsapi_size_t size) override;
105 
106  /** Receive data from a socket.
107  *
108  * This is equivalent to calling recvfrom(NULL, data, size).
109  *
110  * By default, recv blocks until some data is received. If socket is set to
111  * nonblocking or times out, NSAPI_ERROR_WOULD_BLOCK can be returned to
112  * indicate no data.
113  *
114  * @note recv() is allowed write to data buffer even if error occurs.
115  *
116  * @param data Pointer to buffer for data received from the host.
117  * @param size Size of the buffer in bytes.
118  * @retval int Number of received bytes on success.
119  * @retval NSAPI_ERROR_NO_SOCKET in case socket was not created correctly.
120  * @retval NSAPI_ERROR_WOULD_BLOCK in case non-blocking mode is enabled
121  * and send cannot be performed immediately.
122  * @retval int Other negative error codes for stack-related failures.
123  * See \ref NetworkStack::socket_recv.
124  */
125  nsapi_size_or_error_t recv(void *data, nsapi_size_t size) override;
126 
127  /** Not implemented for InternetDatagramSocket.
128  *
129  * @param error Not used.
130  * @return NSAPI_ERROR_UNSUPPORTED
131  */
132  Socket *accept(nsapi_error_t *error = nullptr) override;
133 
134  /** Not implemented for InternetDatagramSocket.
135  *
136  * @param backlog Not used.
137  * @return NSAPI_ERROR_UNSUPPORTED
138  */
139  nsapi_error_t listen(int backlog = 1) override;
140 #if !defined(DOXYGEN_ONLY)
141 
142 protected:
143 
144  /** Create an uninitialized socket.
145  *
146  * @note Must call open to initialize the socket on a network stack.
147  */
148  InternetDatagramSocket() = default;
149 
150 #endif //!defined(DOXYGEN_ONLY)
151 };
152 
153 
154 #endif
155 
156 /** @}*/
Socket implementation that uses IP network stack.
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:140
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:151
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:144
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.
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.