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/Socket.h" 00024 #include "netsocket/NetworkStack.h" 00025 #include "netsocket/NetworkInterface.h" 00026 #include "rtos/Semaphore.h" 00027 00028 00029 /** TCP socket connection 00030 */ 00031 class TCPSocket : public Socket { 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 : _pending(0), _read_sem(0), _write_sem(0), 00049 _read_in_progress(false), _write_in_progress(false) 00050 { 00051 open(stack); 00052 } 00053 00054 /** Destroy a socket 00055 * 00056 * Closes socket if the socket is still open 00057 */ 00058 virtual ~TCPSocket(); 00059 00060 /** Connects TCP socket to a remote host 00061 * 00062 * Initiates a connection to a remote server specified by either 00063 * a domain name or an IP address and a port. 00064 * 00065 * @param host Hostname of the remote host 00066 * @param port Port of the remote host 00067 * @return 0 on success, negative error code on failure 00068 */ 00069 int connect(const char *host, uint16_t port); 00070 00071 /** Connects TCP socket to a remote host 00072 * 00073 * Initiates a connection to a remote server specified by the 00074 * indicated address. 00075 * 00076 * @param address The SocketAddress of the remote host 00077 * @return 0 on success, negative error code on failure 00078 */ 00079 int connect(const SocketAddress &address); 00080 00081 /** Send data over a TCP socket 00082 * 00083 * The socket must be connected to a remote host. Returns the number of 00084 * bytes sent from the buffer. 00085 * 00086 * By default, send blocks until data is sent. If socket is set to 00087 * non-blocking or times out, NSAPI_ERROR_WOULD_BLOCK is returned 00088 * immediately. 00089 * 00090 * @param data Buffer of data to send to the host 00091 * @param size Size of the buffer in bytes 00092 * @return Number of sent bytes on success, negative error 00093 * code on failure 00094 */ 00095 int send(const void *data, unsigned size); 00096 00097 /** Receive data over a TCP socket 00098 * 00099 * The socket must be connected to a remote host. Returns the number of 00100 * bytes received into the buffer. 00101 * 00102 * By default, recv blocks until data is sent. If socket is set to 00103 * non-blocking or times out, NSAPI_ERROR_WOULD_BLOCK is returned 00104 * immediately. 00105 * 00106 * @param data Destination buffer for data received from the host 00107 * @param size Size of the buffer in bytes 00108 * @return Number of received bytes on success, negative error 00109 * code on failure 00110 */ 00111 int recv(void *data, unsigned size); 00112 00113 protected: 00114 friend class TCPServer; 00115 00116 virtual nsapi_protocol_t get_proto(); 00117 virtual void event(); 00118 00119 volatile unsigned _pending; 00120 rtos::Semaphore _read_sem; 00121 rtos::Semaphore _write_sem; 00122 bool _read_in_progress; 00123 bool _write_in_progress; 00124 }; 00125 00126 00127 #endif 00128 00129 /** @}*/
Generated on Tue Jul 12 2022 17:34:56 by
