Mbed OS Reference
Loading...
Searching...
No Matches
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"
28#include "rtos/EventFlags.h"
29
30
31/** TCP socket connection
32 */
33class TCPSocket : public InternetSocket {
34public:
35 /** Create an uninitialized socket
36 *
37 * Must call open to initialize the socket on a network stack.
38 */
40
41 /** Override multicast functions to return error for TCP
42 *
43 */
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 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 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 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 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 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 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 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 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 * Control info is not supported for TCPSocket, use sendto() instead.
151 */
153 const void *data, nsapi_size_t size,
154 nsapi_msghdr_t *control, nsapi_size_t control_size) override;
155
156 /** Receive a packet with ancillary data from a socket
157 *
158 * Control info is not supported for TCPSocket, use recvfrom() instead.
159 */
161 void *data, nsapi_size_t size,
162 nsapi_msghdr_t *control, nsapi_size_t control_size) override;
163
164 /** Accepts a connection on a socket.
165 *
166 * The server socket must be bound and set to listen for connections.
167 * On a new connection, returns connected network socket which user is expected to call close()
168 * and that deallocates the resources. Referencing a returned pointer after a close()
169 * call is not allowed and leads to undefined behavior.
170 *
171 * By default, accept blocks until incoming connection occurs. If socket is set to
172 * non-blocking or times out, error is set to NSAPI_ERROR_WOULD_BLOCK.
173 *
174 * @param error pointer to storage of the error value or NULL:
175 * NSAPI_ERROR_OK on success
176 * NSAPI_ERROR_WOULD_BLOCK if socket is set to non-blocking and would block
177 * NSAPI_ERROR_NO_SOCKET if the socket was not open
178 * @return pointer to a socket
179 */
181
182 /** Listen for incoming connections.
183 *
184 * Marks the socket as a passive socket that can be used to accept
185 * incoming connections.
186 *
187 * @param backlog Number of pending connections that can be queued
188 * simultaneously, defaults to 1
189 * @retval NSAPI_ERROR_OK on success
190 * @retval NSAPI_ERROR_NO_SOCKET in case socket was not created correctly
191 * @retval int Other negative error codes for stack-related failures.
192 * See @ref NetworkStack::socket_listen.
193 */
194 nsapi_error_t listen(int backlog = 1) override;
195
196protected:
197 nsapi_protocol_t get_proto() override;
198
199private:
200 /** Create a socket out of a given socket
201 *
202 * To be used within accept() function. Close() will clean this up.
203 */
204 TCPSocket(TCPSocket *parent, nsapi_socket_t socket, SocketAddress address);
205};
206
207
208#endif
209
210/** @}*/
Network Interface base class.
NetworkStack class.
Socket implementation that uses IP network stack.
SocketAddress class.
Definition: SocketAddress.h:37
TCP socket connection.
Definition: TCPSocket.h:33
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.
nsapi_error_t connect(const SocketAddress &address) override
Connects TCP socket to a remote host.
nsapi_error_t listen(int backlog=1) override
Listen for incoming connections.
TCPSocket()
Create an uninitialized socket.
nsapi_size_or_error_t sendto(const SocketAddress &address, const void *data, nsapi_size_t size) override
Send data on a socket.
int join_multicast_group(const SocketAddress &address)
Override multicast functions to return error for TCP.
Definition: TCPSocket.h:44
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.
nsapi_size_or_error_t recv(void *data, nsapi_size_t size) override
Receive data over a TCP socket.
nsapi_size_or_error_t recvfrom(SocketAddress *address, void *data, nsapi_size_t size) override
Receive a data from a socket.
TCPSocket * accept(nsapi_error_t *error=NULL) override
Accepts a connection on 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:153
signed int nsapi_error_t
Type used to represent error codes.
Definition: nsapi_types.h:142
void * nsapi_socket_t
Opaque handle for network sockets.
Definition: nsapi_types.h:254
unsigned int nsapi_size_t
Type used to represent the size of data passed through sockets.
Definition: nsapi_types.h:146
@ NSAPI_ERROR_UNSUPPORTED
Definition: nsapi_types.h:87
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.
Header structure for control info.
Definition: nsapi_types.h:416