Mistake on this page?
Report an issue in GitHub or email us
TCPSocket.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 ARM Limited
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 /** @file TCPSocket.h TCPSocket class */
19 /** \addtogroup netsocket
20  * @{*/
21 
22 #ifndef TCPSOCKET_H
23 #define TCPSOCKET_H
24 
25 #include "netsocket/InternetSocket.h"
26 #include "netsocket/NetworkStack.h"
28 #include "rtos/EventFlags.h"
29 
30 
31 /** TCP socket connection
32  */
33 class TCPSocket : public InternetSocket {
34 public:
35  /** Create an uninitialized socket
36  *
37  * Must call open to initialize the socket on a network stack.
38  */
39  TCPSocket();
40 
41  /** Override multicast functions to return error for TCP
42  *
43  */
44  int join_multicast_group(const SocketAddress &address)
45  {
47  }
48 
49  /** Connects TCP socket to a remote host
50  *
51  * Initiates a connection to a remote server specified by the
52  * indicated address.
53  *
54  * @param address The SocketAddress of the remote host
55  * @retval NSAPI_ERROR_OK on success
56  * @retval NSAPI_ERROR_IN_PROGRESS if the operation is ongoing
57  * @retval NSAPI_ERROR_NO_SOCKET if the socket has not been allocated
58  * @retval NSAPI_ERROR_DNS_FAILURE if the DNS address of host could not be resolved
59  * @retval NSAPI_ERROR_IS_CONNECTED if the connection is already established
60  * @retval int Other negative error codes for stack-related failures.
61  * See NetworkStack::socket_connect().
62  */
63  nsapi_error_t connect(const SocketAddress &address) override;
64 
65  /** Send data over a TCP socket
66  *
67  * The socket must be connected to a remote host. Returns the number of
68  * bytes sent from the buffer.
69  *
70  * By default, send blocks until all data is sent. If socket is set to
71  * non-blocking or times out, a partial amount can be written.
72  * NSAPI_ERROR_WOULD_BLOCK is returned if no data was written.
73  *
74  * @param data Buffer of data to send to the host
75  * @param size Size of the buffer in bytes
76  * @retval int Number of sent bytes on success
77  * @retval NSAPI_ERROR_NO_SOCKET in case socket was not created correctly
78  * @retval NSAPI_ERROR_WOULD_BLOCK in case non-blocking mode is enabled
79  * and send cannot be performed immediately
80  * @retval int Other negative error codes for stack-related failures.
81  * See @ref NetworkStack::socket_send.
82  */
83  nsapi_size_or_error_t send(const void *data, nsapi_size_t size) override;
84 
85  /** Receive data over a TCP socket
86  *
87  * The socket must be connected to a remote host. Returns the number of
88  * bytes received into the buffer.
89  *
90  * By default, recv blocks until some data is received. If socket is set to
91  * non-blocking or times out, NSAPI_ERROR_WOULD_BLOCK can be returned to
92  * indicate no data.
93  *
94  * @param data Destination buffer for data received from the host
95  * @param size Size of the buffer in bytes
96  * @retval int Number of received 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 int Other negative error codes for stack-related failures.
101  * See @ref NetworkStack::socket_recv.
102  */
103  nsapi_size_or_error_t recv(void *data, nsapi_size_t size) override;
104 
105  /** Send data on a socket.
106  *
107  * TCP socket is connection oriented protocol, so address is ignored.
108  *
109  * By default, sendto blocks until data is sent. If socket is set to
110  * non-blocking or times out, NSAPI_ERROR_WOULD_BLOCK is returned
111  * immediately.
112  *
113  * @param address Remote address
114  * @param data Buffer of data to send to the host
115  * @param size Size of the buffer in bytes
116  * @retval int Number of sent bytes on success
117  * @retval NSAPI_ERROR_NO_SOCKET in case socket was not created correctly
118  * @retval NSAPI_ERROR_WOULD_BLOCK in case non-blocking mode is enabled
119  * and send cannot be performed immediately
120  * @retval int Other negative error codes for stack-related failures.
121  * See @ref NetworkStack::socket_send.
122  */
124  const void *data, nsapi_size_t size) override;
125 
126  /** Receive a data from a socket
127  *
128  * Receives a data and stores the source address in address if address
129  * is not NULL. Returns the number of bytes written into the buffer.
130  *
131  * By default, recvfrom blocks until a data is received. If socket is set to
132  * non-blocking or times out with no datagram, NSAPI_ERROR_WOULD_BLOCK
133  * is returned.
134  *
135  * @param address Destination for the source address or NULL
136  * @param data Destination buffer for datagram received from the host
137  * @param size Size of the buffer in bytes
138  * @retval int Number of received bytes on success
139  * @retval NSAPI_ERROR_NO_SOCKET in case socket was not created correctly
140  * @retval NSAPI_ERROR_WOULD_BLOCK in case non-blocking mode is enabled
141  * and send cannot be performed immediately
142  * @retval int Other negative error codes for stack-related failures.
143  * See @ref NetworkStack::socket_recv.
144  */
146  void *data, nsapi_size_t size) override;
147 
148  /** Send data on a packet with ancillary datasocket.
149  *
150  * TCP socket is connection oriented protocol, so address is ignored.
151  *
152  * By default, sendto_control blocks until data is sent. If socket is set to
153  * non-blocking or times out, NSAPI_ERROR_WOULD_BLOCK is returned
154  * immediately.
155  *
156  * @param address Remote address
157  * @param data Buffer of data to send to the host
158  * @param size Size of the buffer in bytes
159  * @retval int Number of sent bytes on success
160  * @retval NSAPI_ERROR_NO_SOCKET in case socket was not created correctly
161  * @retval NSAPI_ERROR_WOULD_BLOCK in case non-blocking mode is enabled
162  * and send cannot be performed immediately
163  * @retval int Other negative error codes for stack-related failures.
164  * See @ref NetworkStack::socket_send.
165  */
167  const void *data, nsapi_size_t size,
168  nsapi_msghdr_t *control, nsapi_size_t control_size) override;
169 
170  /** Receive a packet with ancillary data from a socket
171  *
172  * Receives a data and stores the source address in address if address
173  * is not NULL. Returns the number of bytes written into the buffer.
174  *
175  * By default, recvfrom_control blocks until a data is received. If socket is set to
176  * non-blocking or times out with no datagram, NSAPI_ERROR_WOULD_BLOCK
177  * is returned.
178  *
179  * @param address Destination for the source address or NULL
180  * @param data Destination buffer for datagram received from the host
181  * @param size Size of the buffer in bytes
182  * @control Pointer to the control buffer
183  * @control_size Size of the control buffer in bytes
184  * @retval int Number of received bytes on success
185  * @retval NSAPI_ERROR_NO_SOCKET in case socket was not created correctly
186  * @retval NSAPI_ERROR_WOULD_BLOCK in case non-blocking mode is enabled
187  * and send cannot be performed immediately
188  * @retval int Other negative error codes for stack-related failures.
189  * See @ref NetworkStack::socket_recv.
190  */
192  void *data, nsapi_size_t size,
193  nsapi_msghdr_t *control, nsapi_size_t control_size) override;
194 
195  /** Accepts a connection on a socket.
196  *
197  * The server socket must be bound and set to listen for connections.
198  * On a new connection, returns connected network socket which user is expected to call close()
199  * and that deallocates the resources. Referencing a returned pointer after a close()
200  * call is not allowed and leads to undefined behavior.
201  *
202  * By default, accept blocks until incoming connection occurs. If socket is set to
203  * non-blocking or times out, error is set to NSAPI_ERROR_WOULD_BLOCK.
204  *
205  * @param error pointer to storage of the error value or NULL:
206  * NSAPI_ERROR_OK on success
207  * NSAPI_ERROR_WOULD_BLOCK if socket is set to non-blocking and would block
208  * NSAPI_ERROR_NO_SOCKET if the socket was not open
209  * @return pointer to a socket
210  */
211  TCPSocket *accept(nsapi_error_t *error = NULL) override;
212 
213  /** Listen for incoming connections.
214  *
215  * Marks the socket as a passive socket that can be used to accept
216  * incoming connections.
217  *
218  * @param backlog Number of pending connections that can be queued
219  * simultaneously, defaults to 1
220  * @retval NSAPI_ERROR_OK on success
221  * @retval NSAPI_ERROR_NO_SOCKET in case socket was not created correctly
222  * @retval int Other negative error codes for stack-related failures.
223  * See @ref NetworkStack::socket_listen.
224  */
225  nsapi_error_t listen(int backlog = 1) override;
226 
227 protected:
228  nsapi_protocol_t get_proto() override;
229 
230 private:
231  /** Create a socket out of a given socket
232  *
233  * To be used within accept() function. Close() will clean this up.
234  */
235  TCPSocket(TCPSocket *parent, nsapi_socket_t socket, SocketAddress address);
236 };
237 
238 
239 #endif
240 
241 /** @}*/
nsapi_msghdr
Definition: nsapi_types.h:414
Socket implementation that uses IP network stack.
nsapi_size_or_error_t recvfrom(SocketAddress *address, void *data, nsapi_size_t size) override
Receive a data from a socket.
int join_multicast_group(const SocketAddress &address)
Override multicast functions to return error for TCP.
Definition: TCPSocket.h:44
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.
void * nsapi_socket_t
Opaque handle for network sockets.
Definition: nsapi_types.h:254
signed int nsapi_error_t
Type used to represent error codes.
Definition: nsapi_types.h:142
TCPSocket()
Create an uninitialized socket.
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 data over a TCP socket.
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 data on a packet with ancillary datasocket.
TCPSocket * accept(nsapi_error_t *error=NULL) override
Accepts a connection on a socket.
nsapi_error_t connect(const SocketAddress &address) override
Connects TCP socket to a remote host.
SocketAddress class.
Definition: SocketAddress.h:37
TCP socket connection.
Definition: TCPSocket.h:33
nsapi_size_or_error_t recv(void *data, nsapi_size_t size) override
Receive data over a TCP socket.
nsapi_size_or_error_t sendto(const SocketAddress &address, const void *data, nsapi_size_t size) override
Send data on a socket.
unsigned int nsapi_size_t
Type used to represent the size of data passed through sockets.
Definition: nsapi_types.h:146
NetworkStack class.
nsapi_error_t listen(int backlog=1) override
Listen for incoming connections.
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 packet with ancillary data from a socket.
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.