Free (GPLv2) TCP/IP stack developed by TASS Belgium
Dependents: lpc1768-picotcp-demo ZeroMQ_PicoTCP_Publisher_demo TCPSocket_HelloWorld_PicoTCP Pico_TCP_UDP_Test ... more
TCPSocketServer.cpp
00001 /* 00002 * 00003 * PicoTCP Socket interface for mbed. 00004 * Copyright (C) 2013 TASS Belgium NV 00005 * 00006 * Released under GPL v2 00007 * 00008 * Other licensing models might apply at the sole discretion of the copyright holders. 00009 * 00010 * 00011 * This software is based on the mbed.org EthernetInterface implementation: 00012 * Copyright (C) 2012 mbed.org, MIT License 00013 * 00014 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 00015 * and associated documentation files (the "Software"), to deal in the Software without restriction, 00016 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 00017 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 00018 * furnished to do so, subject to the following conditions: 00019 * 00020 * The above copyright notice and this permission notice shall be included in all copies or 00021 * substantial portions of the Software. 00022 * 00023 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 00024 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00025 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 00026 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00027 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00028 */ 00029 #include "TCPSocketServer.h" 00030 #include "wrapper.h" 00031 00032 #include <cstring> 00033 00034 using std::memset; 00035 using std::memcpy; 00036 00037 TCPSocketServer::TCPSocketServer() { 00038 00039 } 00040 00041 int TCPSocketServer::bind(int port) { 00042 if (init_socket(SOCK_STREAM) < 0) 00043 return -1; 00044 00045 struct sockaddr_in localHost; 00046 memset(&localHost, 0, sizeof(localHost)); 00047 00048 localHost.sin_family = AF_INET; 00049 localHost.sin_port = short_be(port); 00050 localHost.sin_addr.s_addr = INADDR_ANY; 00051 00052 if (picotcp_bind(_ep, (struct sockaddr *) &localHost, sizeof(localHost)) < 0) { 00053 close(); 00054 return -1; 00055 } 00056 00057 return 0; 00058 } 00059 00060 int TCPSocketServer::listen(int max) { 00061 if (_ep < 0) 00062 return -1; 00063 00064 if (picotcp_listen(_ep, max) < 0) { 00065 close(); 00066 return -1; 00067 } 00068 00069 return 0; 00070 } 00071 00072 int TCPSocketServer::accept(TCPSocketConnection& connection) { 00073 char address[80]; 00074 if (_ep < 0) 00075 return -1; 00076 00077 if (!_blocking) { 00078 mbed_dbg("Not blocking...\n"); 00079 TimeInterval timeout(_timeout); 00080 if (wait_readable(timeout) != 0) 00081 return -1; 00082 } 00083 connection.reset_address(); 00084 socklen_t newSockRemoteHostLen = sizeof(connection._remoteHost); 00085 connection._ep = picotcp_accept(_ep, (struct sockaddr*) &connection._remoteHost, &newSockRemoteHostLen); 00086 if (!connection._ep) { 00087 return -1; //Accept failed 00088 } 00089 connection.set_blocking(true,1500); 00090 pico_ipv4_to_string(address, connection._remoteHost.sin_addr.s_addr); 00091 connection.set_address(address, connection._remoteHost.sin_port); 00092 return 0; 00093 }
Generated on Tue Jul 12 2022 15:59:22 by 1.7.2