Mistake on this page?
Report an issue in GitHub or email us
UDPSocket.h
1 
2 /** \addtogroup netsocket */
3 /** @{*/
4 /* UDPSocket
5  * Copyright (c) 2015 ARM Limited
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 UDPSOCKET_H
21 #define UDPSOCKET_H
22 
23 #include "netsocket/InternetSocket.h"
24 #include "netsocket/NetworkStack.h"
26 #include "rtos/EventFlags.h"
27 
28 
29 /** UDP socket implementation.
30  */
31 class UDPSocket : public InternetSocket {
32 public:
33  /** Create an uninitialized socket.
34  *
35  * @note Must call open to initialize the socket on a network stack.
36  */
37  UDPSocket();
38 
39  /** Create and open a socket on the network stack of the given
40  * network interface.
41  *
42  * @tparam S Type of the Network stack.
43  * @param stack Network stack as target for socket.
44  * @deprecated since mbed-os-5.11
45  */
46  template <typename S>
47  MBED_DEPRECATED_SINCE("mbed-os-5.11",
48  "The UDPSocket(S *stack) constructor is deprecated"
49  "It discards the open() call return value."
50  "Use another constructor and call open() explicitly, instead.")
51  UDPSocket(S *stack)
52  {
53  open(stack);
54  }
55 
56  /** Destroy a socket.
57  *
58  * @note Closes socket if the socket is still open.
59  */
60  virtual ~UDPSocket();
61 
62  /** Send data to the specified host and port.
63  *
64  * By default, sendto blocks until data is sent. If socket is set to
65  * nonblocking or times out, NSAPI_ERROR_WOULD_BLOCK is returned
66  * immediately.
67  *
68  * @param host Domain name of the remote host or a dotted notation IP address.
69  * @param port Port of the remote host.
70  * @param data Buffer of data to send to the host.
71  * @param size Size of the buffer in bytes.
72  * @return Number of sent bytes on success, negative error
73  * code on failure.
74  */
75  virtual nsapi_size_or_error_t sendto(const char *host, uint16_t port,
76  const void *data, nsapi_size_t size);
77 
78  /** Send 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  * @param address The SocketAddress of the remote host.
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 sendto(const SocketAddress &address,
91  const void *data, nsapi_size_t size);
92 
93  /** Receive a datagram and store the source address in address if it's not NULL.
94  *
95  * By default, recvfrom blocks until a datagram is received. If socket is set to
96  * nonblocking or times out with no datagram, NSAPI_ERROR_WOULD_BLOCK
97  * is returned.
98  *
99  * @note If the datagram is larger than the buffer, the excess data is silently discarded.
100  *
101  * @note If socket is connected, only packets coming from connected peer address
102  * are accepted.
103  *
104  * @note recvfrom() is allowed write to address and data buffers even if error occurs.
105  *
106  * @param address Destination for the source address or NULL.
107  * @param data Destination buffer for datagram received from the host.
108  * @param size Size of the buffer in bytes.
109  * @return Number of received bytes on success, negative error
110  * code on failure.
111  */
113  void *data, nsapi_size_t size);
114 
115  /** Set the remote address for next send() call and filtering
116  * of incoming packets. To reset the address, zero initialized
117  * SocketAddress must be in the address parameter.
118  *
119  * @param address The SocketAddress of the remote host.
120  * @return 0 on success, negative error code on failure.
121  */
122  virtual nsapi_error_t connect(const SocketAddress &address);
123 
124  /** Send a datagram to connected remote address.
125  *
126  * By default, send blocks until all data is sent. If socket is set to
127  * nonblocking or times out, a partial amount can be written.
128  * NSAPI_ERROR_WOULD_BLOCK is returned if no data was written.
129  *
130  * @note The socket must be connected to a remote host before send() call.
131  *
132  * @param data Buffer of data to send to the host.
133  * @param size Size of the buffer in bytes.
134  * @return Number of sent bytes on success, negative error
135  * code on failure.
136  */
137  virtual nsapi_size_or_error_t send(const void *data, nsapi_size_t size);
138 
139  /** Receive data from a socket.
140  *
141  * This is equivalent to calling recvfrom(NULL, data, size).
142  *
143  * If the socket is connected, only packets coming from a connected peer address
144  * are accepted.
145  *
146  * By default, recv blocks until some data is received. If socket is set to
147  * nonblocking or times out, NSAPI_ERROR_WOULD_BLOCK can be returned to
148  * indicate no data.
149  *
150  * @note recv() is allowed write to data buffer even if error occurs.
151  *
152  * @param data Pointer to buffer for data received from the host.
153  * @param size Size of the buffer in bytes.
154  * @return Number of received bytes on success, negative error
155  * code on failure.
156  */
157  virtual nsapi_size_or_error_t recv(void *data, nsapi_size_t size);
158 
159  /** Not implemented for UDP.
160  *
161  * @param error Not used.
162  * @return NSAPI_ERROR_UNSUPPORTED
163  */
164  virtual Socket *accept(nsapi_error_t *error = NULL);
165 
166  /** Not implemented for UDP.
167  *
168  * @param backlog Not used.
169  * @return NSAPI_ERROR_UNSUPPORTED
170  */
171  virtual nsapi_error_t listen(int backlog = 1);
172 
173 #if !defined(DOXYGEN_ONLY)
174 
175 protected:
176  virtual nsapi_protocol_t get_proto();
177 
178 #endif //!defined(DOXYGEN_ONLY)
179 };
180 
181 
182 #endif
183 
184 /** @}*/
nsapi_error_t open(NetworkStack *stack)
Open a network socket on the network stack of the given network interface.
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.
UDPSocket()
Create an uninitialized socket.
UDP socket implementation.
Definition: UDPSocket.h:31
virtual nsapi_size_or_error_t send(const void *data, nsapi_size_t size)
Send a datagram to connected remote address.
signed int nsapi_error_t
Type used to represent error codes.
Definition: nsapi_types.h:95
virtual Socket * accept(nsapi_error_t *error=NULL)
Not implemented for UDP.
virtual nsapi_error_t connect(const SocketAddress &address)
Set the remote address for next send() call and filtering of incoming packets.
virtual nsapi_size_or_error_t recv(void *data, nsapi_size_t size)
Receive data from a socket.
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 ~UDPSocket()
Destroy a socket.
virtual nsapi_error_t listen(int backlog=1)
Not implemented for UDP.
virtual nsapi_size_or_error_t recvfrom(SocketAddress *address, void *data, nsapi_size_t size)
Receive a datagram and store the source address in address if it&#39;s not NULL.
SocketAddress class.
Definition: SocketAddress.h:35
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.
#define MBED_DEPRECATED_SINCE(D, M)
MBED_DEPRECATED("message string") Mark a function declaration as deprecated, if it used then a warnin...
virtual nsapi_size_or_error_t sendto(const char *host, uint16_t port, const void *data, nsapi_size_t size)
Send data to the specified host and port.
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.