Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
TCPSocket.h
00001 00002 /** \addtogroup netsocket */ 00003 /** @{*/ 00004 /* TCPSocket 00005 * Copyright (c) 2015 ARM Limited 00006 * 00007 * Licensed under the Apache License, Version 2.0 (the "License"); 00008 * you may not use this file except in compliance with the License. 00009 * You may obtain a copy of the License at 00010 * 00011 * http://www.apache.org/licenses/LICENSE-2.0 00012 * 00013 * Unless required by applicable law or agreed to in writing, software 00014 * distributed under the License is distributed on an "AS IS" BASIS, 00015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00016 * See the License for the specific language governing permissions and 00017 * limitations under the License. 00018 */ 00019 00020 #ifndef TCPSOCKET_H 00021 #define TCPSOCKET_H 00022 00023 #include "netsocket/InternetSocket.h" 00024 #include "netsocket/NetworkStack.h" 00025 #include "netsocket/NetworkInterface.h" 00026 #include "rtos/EventFlags.h" 00027 00028 00029 /** TCP socket connection 00030 */ 00031 class TCPSocket : public InternetSocket { 00032 public: 00033 /** Create an uninitialized socket 00034 * 00035 * Must call open to initialize the socket on a network stack. 00036 */ 00037 TCPSocket(); 00038 00039 /** Create a socket on a network interface 00040 * 00041 * Creates and opens a socket on the network stack of the given 00042 * network interface. 00043 * 00044 * @param stack Network stack as target for socket 00045 */ 00046 template <typename S> 00047 TCPSocket(S *stack) 00048 { 00049 open(stack); 00050 } 00051 00052 /** Destroy a socket 00053 * 00054 * Closes socket if the socket is still open 00055 */ 00056 virtual ~TCPSocket(); 00057 00058 /** Override multicast functions to return error for TCP 00059 * 00060 */ 00061 virtual int join_multicast_group(const SocketAddress &address) 00062 { 00063 return NSAPI_ERROR_UNSUPPORTED ; 00064 } 00065 00066 /** Connects TCP socket to a remote host 00067 * 00068 * Initiates a connection to a remote server specified by either 00069 * a domain name or an IP address and a port. 00070 * 00071 * @param host Hostname of the remote host 00072 * @param port Port of the remote host 00073 * @return 0 on success, negative error code on failure 00074 */ 00075 nsapi_error_t connect(const char *host, uint16_t port); 00076 00077 /** Connects TCP socket to a remote host 00078 * 00079 * Initiates a connection to a remote server specified by the 00080 * indicated address. 00081 * 00082 * @param address The SocketAddress of the remote host 00083 * @return 0 on success, negative error code on failure 00084 */ 00085 virtual nsapi_error_t connect(const SocketAddress &address); 00086 00087 /** Send data over a TCP socket 00088 * 00089 * The socket must be connected to a remote host. Returns the number of 00090 * bytes sent from the buffer. 00091 * 00092 * By default, send blocks until all data is sent. If socket is set to 00093 * non-blocking or times out, a partial amount can be written. 00094 * NSAPI_ERROR_WOULD_BLOCK is returned if no data was written. 00095 * 00096 * @param data Buffer of data to send to the host 00097 * @param size Size of the buffer in bytes 00098 * @return Number of sent bytes on success, negative error 00099 * code on failure 00100 */ 00101 virtual nsapi_size_or_error_t send(const void *data, nsapi_size_t size); 00102 00103 /** Receive data over a TCP socket 00104 * 00105 * The socket must be connected to a remote host. Returns the number of 00106 * bytes received into the buffer. 00107 * 00108 * By default, recv blocks until some data is received. If socket is set to 00109 * non-blocking or times out, NSAPI_ERROR_WOULD_BLOCK can be returned to 00110 * indicate no data. 00111 * 00112 * @param data Destination buffer for data received from the host 00113 * @param size Size of the buffer in bytes 00114 * @return Number of received bytes on success, negative error 00115 * code on failure. If no data is available to be received 00116 * and the peer has performed an orderly shutdown, 00117 * recv() returns 0. 00118 */ 00119 virtual nsapi_size_or_error_t recv(void *data, nsapi_size_t size); 00120 00121 /** Send data on a socket. 00122 * 00123 * TCP socket is connection oriented protocol, so address is ignored. 00124 * 00125 * By default, sendto blocks until data is sent. If socket is set to 00126 * non-blocking or times out, NSAPI_ERROR_WOULD_BLOCK is returned 00127 * immediately. 00128 * 00129 * @param address Remote address 00130 * @param data Buffer of data to send to the host 00131 * @param size Size of the buffer in bytes 00132 * @return Number of sent bytes on success, negative error 00133 * code on failure 00134 */ 00135 virtual nsapi_size_or_error_t sendto(const SocketAddress &address, 00136 const void *data, nsapi_size_t size); 00137 00138 /** Receive a data from a socket 00139 * 00140 * Receives a data and stores the source address in address if address 00141 * is not NULL. Returns the number of bytes written into the buffer. 00142 * 00143 * By default, recvfrom blocks until a data is received. If socket is set to 00144 * non-blocking or times out with no datagram, NSAPI_ERROR_WOULD_BLOCK 00145 * is returned. 00146 * 00147 * @param address Destination for the source address or NULL 00148 * @param data Destination buffer for datagram received from the host 00149 * @param size Size of the buffer in bytes 00150 * @return Number of received bytes on success, negative error 00151 * code on failure 00152 */ 00153 virtual nsapi_size_or_error_t recvfrom(SocketAddress *address, 00154 void *data, nsapi_size_t size); 00155 00156 /** Accepts a connection on a socket. 00157 * 00158 * The server socket must be bound and set to listen for connections. 00159 * On a new connection, returns connected network socket which user is expected to call close() 00160 * and that deallocates the resources. Referencing a returned pointer after a close() 00161 * call is not allowed and leads to undefined behaviour. 00162 * 00163 * By default, accept blocks until incomming connection occurs. If socket is set to 00164 * non-blocking or times out, error is set to NSAPI_ERROR_WOULD_BLOCK. 00165 * 00166 * @param error pointer to storage of the error value or NULL 00167 * @return pointer to a socket 00168 */ 00169 virtual TCPSocket *accept(nsapi_error_t *error = NULL); 00170 00171 /** Listen for incoming connections. 00172 * 00173 * Marks the socket as a passive socket that can be used to accept 00174 * incoming connections. 00175 * 00176 * @param backlog Number of pending connections that can be queued 00177 * simultaneously, defaults to 1 00178 * @return 0 on success, negative error code on failure 00179 */ 00180 virtual nsapi_error_t listen(int backlog = 1); 00181 00182 protected: 00183 friend class TCPServer; 00184 virtual nsapi_protocol_t get_proto(); 00185 }; 00186 00187 00188 #endif 00189 00190 /** @}*/
Generated on Tue Aug 9 2022 00:37:21 by
