WIZnetInterface using namespace

Dependents:   DualNetworkInterface-Basic

Fork of WIZnetInterface by WIZnet

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TCPSocketServer.cpp Source File

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.hpp"
00020 
00021 using namespace wiznet_space;
00022     
00023 TCPSocketServer::TCPSocketServer() {}
00024 
00025 // Server initialization
00026 int TCPSocketServer::bind(int port)
00027 {
00028    // set the listen_port for next connection. 
00029    listen_port = port;
00030     if (_sock_fd < 0) {
00031         _sock_fd = eth->new_socket();
00032         if (_sock_fd < 0) {
00033             return -1;
00034         }
00035     }
00036     // set TCP protocol
00037     eth->setProtocol(_sock_fd, WIZnet_Chip::TCP);
00038     // set local port
00039     eth->sreg<uint16_t>(_sock_fd, Sn_PORT, port);
00040     // connect the network
00041     eth->scmd(_sock_fd, WIZnet_Chip::OPEN);
00042     return 0;
00043 }
00044 
00045 int TCPSocketServer::listen(int backlog)
00046 {
00047     if (_sock_fd < 0) {
00048         return -1;
00049     }
00050     if (backlog != 1) {
00051         return -1;
00052     }
00053     eth->scmd(_sock_fd, WIZnet_Chip::LISTEN);
00054     return 0;
00055 }
00056 
00057 
00058 int TCPSocketServer::accept(TCPSocketConnection& connection)
00059 {
00060     if (_sock_fd < 0) {
00061         return -1;
00062     }
00063     Timer t;
00064     t.reset();
00065     t.start();
00066     while(1) {
00067         if (t.read_ms() > _timeout && _blocking == false) {
00068             return -1;
00069         }
00070         if (eth->sreg<uint8_t>(_sock_fd, Sn_SR) == WIZnet_Chip::SOCK_ESTABLISHED) {
00071             break;
00072         }
00073     }
00074     uint32_t ip = eth->sreg<uint32_t>(_sock_fd, Sn_DIPR);
00075     char host[16];
00076     snprintf(host, sizeof(host), "%d.%d.%d.%d", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff);
00077     uint16_t port = eth->sreg<uint16_t>(_sock_fd, Sn_DPORT);
00078 
00079     // change this server socket to connection socket.
00080     connection._sock_fd = _sock_fd;
00081     connection._is_connected = true;
00082     connection.set_address(host, port);
00083 
00084     // and then, for the next connection, server socket should be assigned new one.
00085     _sock_fd = -1; // want to assign new available _sock_fd.
00086     if(bind(listen_port) < 0) {
00087         // modified by Patrick Pollet
00088         error("No more socket for listening, bind error");
00089         return -1;
00090     } else {
00091         //return -1;
00092         if(listen(1) < 0) {
00093             // modified by Patrick Pollet
00094             error("No more socket for listening, listen error");
00095             return -1;
00096         }
00097     }
00098 
00099     return 0;
00100 }