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  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #ifndef INTERNETDATAGRAMSOCKET_H
20 #define INTERNETDATAGRAMSOCKET_H
21 
22 #include "netsocket/InternetSocket.h"
23 #include "netsocket/NetworkStack.h"
25 #include "rtos/EventFlags.h"
26 
27 
28 /** InternetDatagramSocket socket implementation.
29  */
31 public:
32  /** Send data to the specified address.
33  *
34  * By default, sendto blocks until data is sent. If socket is set to
35  * nonblocking or times out, NSAPI_ERROR_WOULD_BLOCK is returned
36  * immediately.
37  *
38  * @param address The SocketAddress of the remote host.
39  * @param data Buffer of data to send to the host.
40  * @param size Size of the buffer in bytes.
41  * @retval NSAPI_ERROR_NO_SOCKET in case socket was not created correctly.
42  * @retval NSAPI_ERROR_WOULD_BLOCK in case non-blocking mode is enabled
43  * and send cannot be performed immediately.
44  * @retval int Other negative error codes for stack-related failures.
45  * See \ref NetworkStack::socket_send.
46  */
48  const void *data, nsapi_size_t size) override;
49 
50  /** Receive a datagram and store the source address in address if it's not NULL.
51  *
52  * By default, recvfrom blocks until a datagram is received. If socket is set to
53  * nonblocking or times out with no datagram, NSAPI_ERROR_WOULD_BLOCK
54  * is returned.
55  *
56  * @note If the datagram is larger than the buffer, the excess data is silently discarded.
57  *
58  * @note If socket is connected, only packets coming from connected peer address
59  * are accepted.
60  *
61  * @note recvfrom() is allowed write to address and data buffers even if error occurs.
62  *
63  * @param address Destination for the source address or NULL.
64  * @param data Destination buffer for RAW data to be received from the host.
65  * @param size Size of the buffer in bytes.
66  * @retval int Number of received bytes on success.
67  * @retval NSAPI_ERROR_NO_SOCKET in case socket was not created correctly.
68  * @retval NSAPI_ERROR_WOULD_BLOCK in case non-blocking mode is enabled
69  * and send cannot be performed immediately.
70  * @retval int Other negative error codes for stack-related failures.
71  * See \ref NetworkStack::socket_recv.
72  */
74  void *data, nsapi_size_t size) override;
75 
76  /** Set the remote address for next send() call and filtering
77  * of incoming packets. To reset the address, zero initialized
78  * SocketAddress must be in the address parameter.
79  *
80  * @param address The SocketAddress of the remote host.
81  * @return NSAPI_ERROR_OK on success.
82  */
83  nsapi_error_t connect(const SocketAddress &address) override;
84 
85  /** Send a raw data to connected remote address.
86  *
87  * By default, send blocks until all data is sent. If socket is set to
88  * nonblocking or times out, a partial amount can be written.
89  * NSAPI_ERROR_WOULD_BLOCK is returned if no data was written.
90  *
91  * @note The socket must be connected to a remote host before send() call.
92  *
93  * @param data Buffer of data to send to the host.
94  * @param size Size of the buffer in bytes.
95  * @retval int Number of sent bytes on success.
96  * @retval NSAPI_ERROR_NO_SOCKET in case socket was not created correctly.
97  * @retval NSAPI_ERROR_WOULD_BLOCK in case non-blocking mode is enabled
98  * and send cannot be performed immediately.
99  * #retval NSAPI_ERROR_NO_ADDRESS if the address was not set with connect().
100  * @retval int Other negative error codes for stack-related failures.
101  * See \ref NetworkStack::socket_send.
102  */
103  nsapi_size_or_error_t send(const void *data, nsapi_size_t size) override;
104 
105  /** Receive data from a socket.
106  *
107  * This is equivalent to calling recvfrom(NULL, data, size).
108  *
109  * By default, recv blocks until some data is received. If socket is set to
110  * nonblocking or times out, NSAPI_ERROR_WOULD_BLOCK can be returned to
111  * indicate no data.
112  *
113  * @note recv() is allowed write to data buffer even if error occurs.
114  *
115  * @param data Pointer to buffer for data received from the host.
116  * @param size Size of the buffer in bytes.
117  * @retval int Number of received bytes on success.
118  * @retval NSAPI_ERROR_NO_SOCKET in case socket was not created correctly.
119  * @retval NSAPI_ERROR_WOULD_BLOCK in case non-blocking mode is enabled
120  * and send cannot be performed immediately.
121  * @retval int Other negative error codes for stack-related failures.
122  * See \ref NetworkStack::socket_recv.
123  */
124  nsapi_size_or_error_t recv(void *data, nsapi_size_t size) override;
125 
126  /** Not implemented for InternetDatagramSocket.
127  *
128  * @param error Not used.
129  * @return NSAPI_ERROR_UNSUPPORTED
130  */
131  Socket *accept(nsapi_error_t *error = nullptr) override;
132 
133  /** Not implemented for InternetDatagramSocket.
134  *
135  * @param backlog Not used.
136  * @return NSAPI_ERROR_UNSUPPORTED
137  */
138  nsapi_error_t listen(int backlog = 1) override;
139 #if !defined(DOXYGEN_ONLY)
140 
141 protected:
142 
143  /** Create an uninitialized socket.
144  *
145  * @note Must call open to initialize the socket on a network stack.
146  */
147  InternetDatagramSocket() = default;
148 
149 #endif //!defined(DOXYGEN_ONLY)
150 };
151 
152 
153 #endif
154 
155 /** @}*/
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:95
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:106
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:36
Socket interface.
Definition: Socket.h:39
unsigned int nsapi_size_t
Type used to represent the size of data passed through sockets.
Definition: nsapi_types.h:99
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.