No more update~~ please use W5500Interface.

Fork of EthernetInterfaceW5500 by Bongjun Hur

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