exeption of receive(TCPSocketconnection)

Dependents:   FTP_SDCard_File_Client_WIZwiki-W7500 FTPClient_example Freedman DigitalCamera_OV5642_WIZwiki-W7500 ... more

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