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.
Dependencies: MAX44000 PWM_Tone_Library nexpaq_mdk
Fork of LED_Demo by
TCPSocket.h
00001 /* TCPSocket 00002 * Copyright (c) 2015 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #ifndef TCPSOCKET_H 00018 #define TCPSOCKET_H 00019 00020 #include "network-socket/Socket.h" 00021 #include "network-socket/NetworkStack.h" 00022 #include "network-socket/NetworkInterface.h" 00023 #include "rtos/Semaphore.h" 00024 00025 00026 /** TCP socket connection 00027 */ 00028 class TCPSocket : public Socket { 00029 public: 00030 /** Create an uninitialized socket 00031 * 00032 * Must call open to initialize the socket on a network stack. 00033 */ 00034 TCPSocket(); 00035 00036 /** Create a socket on a network interface 00037 * 00038 * Creates and opens a socket on the network stack of the given 00039 * network interface. 00040 * 00041 * @param stack Network stack as target for socket 00042 */ 00043 template <typename S> 00044 TCPSocket(S *stack) 00045 : _pending(0), _read_sem(0), _write_sem(0), 00046 _read_in_progress(false), _write_in_progress(false) 00047 { 00048 open(stack); 00049 } 00050 00051 /** Destroy a socket 00052 * 00053 * Closes socket if the socket is still open 00054 */ 00055 virtual ~TCPSocket(); 00056 00057 /** Connects TCP socket to a remote host 00058 * 00059 * Initiates a connection to a remote server specified by either 00060 * a domain name or an IP address and a port. 00061 * 00062 * @param host Hostname of the remote host 00063 * @param port Port of the remote host 00064 * @return 0 on success, negative error code on failure 00065 */ 00066 int connect(const char *host, uint16_t port); 00067 00068 /** Connects TCP socket to a remote host 00069 * 00070 * Initiates a connection to a remote server specified by the 00071 * indicated address. 00072 * 00073 * @param address The SocketAddress of the remote host 00074 * @return 0 on success, negative error code on failure 00075 */ 00076 int connect(const SocketAddress &address); 00077 00078 /** Send data over a TCP socket 00079 * 00080 * The socket must be connected to a remote host. Returns the number of 00081 * bytes sent from the buffer. 00082 * 00083 * By default, send blocks until data is sent. If socket is set to 00084 * non-blocking or times out, NSAPI_ERROR_WOULD_BLOCK is returned 00085 * immediately. 00086 * 00087 * @param data Buffer of data to send to the host 00088 * @param size Size of the buffer in bytes 00089 * @return Number of sent bytes on success, negative error 00090 * code on failure 00091 */ 00092 int send(const void *data, unsigned size); 00093 00094 /** Receive data over a TCP socket 00095 * 00096 * The socket must be connected to a remote host. Returns the number of 00097 * bytes received into the buffer. 00098 * 00099 * By default, recv blocks until data is sent. If socket is set to 00100 * non-blocking or times out, NSAPI_ERROR_WOULD_BLOCK is returned 00101 * immediately. 00102 * 00103 * @param data Destination buffer for data received from the host 00104 * @param size Size of the buffer in bytes 00105 * @return Number of received bytes on success, negative error 00106 * code on failure 00107 */ 00108 int recv(void *data, unsigned size); 00109 00110 protected: 00111 friend class TCPServer; 00112 00113 virtual nsapi_protocol_t get_proto(); 00114 virtual void event(); 00115 00116 volatile unsigned _pending; 00117 rtos::Semaphore _read_sem; 00118 rtos::Semaphore _write_sem; 00119 bool _read_in_progress; 00120 bool _write_in_progress; 00121 }; 00122 00123 00124 #endif
Generated on Tue Jul 12 2022 12:28:52 by
