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.
Fork of WIZnet_Library by
TCPSocketServer.cpp
00001 /* Copyright (C) 2012 mbed.org, MIT License 00002 * 00003 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 00004 * and associated documentation files (the "Software"), to deal in the Software without restriction, 00005 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 00006 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 00007 * furnished to do so, subject to the following conditions: 00008 * 00009 * The above copyright notice and this permission notice shall be included in all copies or 00010 * substantial portions of the Software. 00011 * 00012 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 00013 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00014 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 00015 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00016 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00017 */ 00018 00019 #include "TCPSocketServer.h" 00020 00021 TCPSocketServer::TCPSocketServer() {} 00022 00023 // Server initialization 00024 int TCPSocketServer::bind(int port) { 00025 if (_sock_fd < 0) { 00026 _sock_fd = eth->new_socket(); 00027 if (_sock_fd < 0) { 00028 return -1; 00029 } 00030 } 00031 // set TCP protocol 00032 eth->setProtocol(_sock_fd, TCP); 00033 // set local port 00034 eth->sreg<uint16_t>(_sock_fd, Sn_PORT, port); 00035 // connect the network 00036 eth->scmd(_sock_fd, OPEN); 00037 return 0; 00038 } 00039 00040 int TCPSocketServer::listen(int backlog) { 00041 if (_sock_fd < 0) { 00042 return -1; 00043 } 00044 if (backlog != 1) { 00045 return -1; 00046 } 00047 eth->scmd(_sock_fd, LISTEN); 00048 return 0; 00049 } 00050 00051 00052 int TCPSocketServer::accept(TCPSocketConnection& connection) { 00053 if (_sock_fd < 0) { 00054 return -1; 00055 } 00056 Timer t; 00057 t.reset(); 00058 t.start(); 00059 while(1) { 00060 if (t.read_ms() > _timeout && _blocking == false) { 00061 return -1; 00062 } 00063 if (eth->sreg<uint8_t>(_sock_fd, Sn_SR) == SOCK_ESTABLISHED) { 00064 break; 00065 } 00066 } 00067 uint32_t ip = eth->sreg<uint32_t>(_sock_fd, Sn_DIPR); 00068 char host[16]; 00069 snprintf(host, sizeof(host), "%d.%d.%d.%d", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff); 00070 uint16_t port = eth->sreg<uint16_t>(_sock_fd, Sn_DPORT); 00071 connection._sock_fd = _sock_fd; 00072 connection.set_address(host, port); 00073 return 0; 00074 } 00075
Generated on Tue Jul 12 2022 20:45:54 by
1.7.2
