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 
33  /** Send data to the specified host and port.
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 host Domain name of the remote host or a dotted notation IP address.
40  * @param port Port 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  * @return Number of sent bytes on success, negative error
44  * code on failure.
45  */
46  virtual nsapi_size_or_error_t sendto(const char *host, uint16_t port,
47  const void *data, nsapi_size_t size);
48 
49  /** Send data to the specified address.
50  *
51  * By default, sendto blocks until data is sent. If socket is set to
52  * nonblocking or times out, NSAPI_ERROR_WOULD_BLOCK is returned
53  * immediately.
54  *
55  * @param address The SocketAddress of the remote host.
56  * @param data Buffer of data to send to the host.
57  * @param size Size of the buffer in bytes.
58  * @return Number of sent bytes on success, negative error
59  * code on failure.
60  */
61  virtual nsapi_size_or_error_t sendto(const SocketAddress &address,
62  const void *data, nsapi_size_t size);
63 
64  /** Receive a datagram and store the source address in address if it's not NULL.
65  *
66  * By default, recvfrom blocks until a datagram is received. If socket is set to
67  * nonblocking or times out with no datagram, NSAPI_ERROR_WOULD_BLOCK
68  * is returned.
69  *
70  * @note If the datagram is larger than the buffer, the excess data is silently discarded.
71  *
72  * @note If socket is connected, only packets coming from connected peer address
73  * are accepted.
74  *
75  * @note recvfrom() is allowed write to address and data buffers even if error occurs.
76  *
77  * @param address Destination for the source address or NULL.
78  * @param data Destination buffer for RAW data to be received from the host.
79  * @param size Size of the buffer in bytes.
80  * @return Number of received bytes on success, negative error
81  * code on failure.
82  */
84  void *data, nsapi_size_t size);
85 
86  /** Set the remote address for next send() call and filtering
87  * of incoming packets. To reset the address, zero initialized
88  * SocketAddress must be in the address parameter.
89  *
90  * @param address The SocketAddress of the remote host.
91  * @return 0 on success, negative error code on failure.
92  */
93  virtual nsapi_error_t connect(const SocketAddress &address);
94 
95  /** Send a raw data to connected remote address.
96  *
97  * By default, send blocks until all data is sent. If socket is set to
98  * nonblocking or times out, a partial amount can be written.
99  * NSAPI_ERROR_WOULD_BLOCK is returned if no data was written.
100  *
101  * @note The socket must be connected to a remote host before send() call.
102  *
103  * @param data Buffer of data to send to the host.
104  * @param size Size of the buffer in bytes.
105  * @return Number of sent bytes on success, negative error
106  * code on failure.
107  */
108  virtual nsapi_size_or_error_t send(const void *data, nsapi_size_t size);
109 
110  /** Receive data from a socket.
111  *
112  * This is equivalent to calling recvfrom(NULL, data, size).
113  *
114  * By default, recv blocks until some data is received. If socket is set to
115  * nonblocking or times out, NSAPI_ERROR_WOULD_BLOCK can be returned to
116  * indicate no data.
117  *
118  * @note recv() is allowed write to data buffer even if error occurs.
119  *
120  * @param data Pointer to buffer for data received from the host.
121  * @param size Size of the buffer in bytes.
122  * @return Number of received bytes on success, negative error
123  * code on failure.
124  */
125  virtual nsapi_size_or_error_t recv(void *data, nsapi_size_t size);
126 
127  /** Not implemented for InternetDatagramSocket.
128  *
129  * @param error Not used.
130  * @return NSAPI_ERROR_UNSUPPORTED
131  */
132  virtual Socket *accept(nsapi_error_t *error = NULL);
133 
134  /** Not implemented for InternetDatagramSocket.
135  *
136  * @param backlog Not used.
137  * @return NSAPI_ERROR_UNSUPPORTED
138  */
139  virtual nsapi_error_t listen(int backlog = 1);
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.
virtual nsapi_error_t connect(const SocketAddress &address)
Set the remote address for next send() call and filtering of incoming packets.
signed int nsapi_error_t
Type used to represent error codes.
Definition: nsapi_types.h:95
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_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.
SocketAddress class.
Definition: SocketAddress.h:35
virtual Socket * accept(nsapi_error_t *error=NULL)
Not implemented for InternetDatagramSocket.
Socket interface.
Definition: Socket.h:39
virtual nsapi_size_or_error_t send(const void *data, nsapi_size_t size)
Send a raw data to connected remote address.
unsigned int nsapi_size_t
Type used to represent the size of data passed through sockets.
Definition: nsapi_types.h:99
NetworkStack class.
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's not NULL.
virtual nsapi_error_t listen(int backlog=1)
Not implemented for InternetDatagramSocket.
virtual nsapi_size_or_error_t recv(void *data, nsapi_size_t size)
Receive data from a socket.
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.