Fork of original library, increased Tx buffer to 16kB
Dependents: W5500-SNTPClient-example
Socket/TCPSocketServer.cpp@0:e9275bdfa393, 2019-05-02 (annotated)
- Committer:
- star297
- Date:
- Thu May 02 20:47:25 2019 +0000
- Revision:
- 0:e9275bdfa393
First commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
star297 | 0:e9275bdfa393 | 1 | /* Copyright (C) 2012 mbed.org, MIT License |
star297 | 0:e9275bdfa393 | 2 | * |
star297 | 0:e9275bdfa393 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
star297 | 0:e9275bdfa393 | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
star297 | 0:e9275bdfa393 | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
star297 | 0:e9275bdfa393 | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
star297 | 0:e9275bdfa393 | 7 | * furnished to do so, subject to the following conditions: |
star297 | 0:e9275bdfa393 | 8 | * |
star297 | 0:e9275bdfa393 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
star297 | 0:e9275bdfa393 | 10 | * substantial portions of the Software. |
star297 | 0:e9275bdfa393 | 11 | * |
star297 | 0:e9275bdfa393 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
star297 | 0:e9275bdfa393 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
star297 | 0:e9275bdfa393 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
star297 | 0:e9275bdfa393 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
star297 | 0:e9275bdfa393 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
star297 | 0:e9275bdfa393 | 17 | */ |
star297 | 0:e9275bdfa393 | 18 | |
star297 | 0:e9275bdfa393 | 19 | #include "TCPSocketServer.h" |
star297 | 0:e9275bdfa393 | 20 | |
star297 | 0:e9275bdfa393 | 21 | TCPSocketServer::TCPSocketServer() {} |
star297 | 0:e9275bdfa393 | 22 | |
star297 | 0:e9275bdfa393 | 23 | // Server initialization |
star297 | 0:e9275bdfa393 | 24 | int TCPSocketServer::bind(int port) |
star297 | 0:e9275bdfa393 | 25 | { |
star297 | 0:e9275bdfa393 | 26 | listen_port = port; |
star297 | 0:e9275bdfa393 | 27 | if (_sock_fd < 0) { |
star297 | 0:e9275bdfa393 | 28 | _sock_fd = eth->new_socket(); |
star297 | 0:e9275bdfa393 | 29 | if (_sock_fd < 0) { |
star297 | 0:e9275bdfa393 | 30 | return -1; |
star297 | 0:e9275bdfa393 | 31 | } |
star297 | 0:e9275bdfa393 | 32 | } |
star297 | 0:e9275bdfa393 | 33 | // set TCP protocol |
star297 | 0:e9275bdfa393 | 34 | eth->setProtocol(_sock_fd, WIZnet_Chip::TCP); |
star297 | 0:e9275bdfa393 | 35 | // set local port |
star297 | 0:e9275bdfa393 | 36 | eth->sreg<uint16_t>(_sock_fd, Sn_PORT, port); |
star297 | 0:e9275bdfa393 | 37 | // connect the network |
star297 | 0:e9275bdfa393 | 38 | eth->scmd(_sock_fd, WIZnet_Chip::OPEN); |
star297 | 0:e9275bdfa393 | 39 | return 0; |
star297 | 0:e9275bdfa393 | 40 | } |
star297 | 0:e9275bdfa393 | 41 | |
star297 | 0:e9275bdfa393 | 42 | int TCPSocketServer::listen(int backlog) |
star297 | 0:e9275bdfa393 | 43 | { |
star297 | 0:e9275bdfa393 | 44 | if (_sock_fd < 0) { |
star297 | 0:e9275bdfa393 | 45 | return -1; |
star297 | 0:e9275bdfa393 | 46 | } |
star297 | 0:e9275bdfa393 | 47 | if (backlog != 1) { |
star297 | 0:e9275bdfa393 | 48 | return -1; |
star297 | 0:e9275bdfa393 | 49 | } |
star297 | 0:e9275bdfa393 | 50 | eth->scmd(_sock_fd, WIZnet_Chip::LISTEN); |
star297 | 0:e9275bdfa393 | 51 | return 0; |
star297 | 0:e9275bdfa393 | 52 | } |
star297 | 0:e9275bdfa393 | 53 | |
star297 | 0:e9275bdfa393 | 54 | |
star297 | 0:e9275bdfa393 | 55 | int TCPSocketServer::accept(TCPSocketConnection& connection) |
star297 | 0:e9275bdfa393 | 56 | { |
star297 | 0:e9275bdfa393 | 57 | if (_sock_fd < 0) { |
star297 | 0:e9275bdfa393 | 58 | return -1; |
star297 | 0:e9275bdfa393 | 59 | } |
star297 | 0:e9275bdfa393 | 60 | Timer t; |
star297 | 0:e9275bdfa393 | 61 | t.reset(); |
star297 | 0:e9275bdfa393 | 62 | t.start(); |
star297 | 0:e9275bdfa393 | 63 | while(1) { |
star297 | 0:e9275bdfa393 | 64 | if (t.read_ms() > _timeout && _blocking == false) { |
star297 | 0:e9275bdfa393 | 65 | return -1; |
star297 | 0:e9275bdfa393 | 66 | } |
star297 | 0:e9275bdfa393 | 67 | if (eth->sreg<uint8_t>(_sock_fd, Sn_SR) == WIZnet_Chip::SOCK_ESTABLISHED) { |
star297 | 0:e9275bdfa393 | 68 | break; |
star297 | 0:e9275bdfa393 | 69 | } |
star297 | 0:e9275bdfa393 | 70 | } |
star297 | 0:e9275bdfa393 | 71 | uint32_t ip = eth->sreg<uint32_t>(_sock_fd, Sn_DIPR); |
star297 | 0:e9275bdfa393 | 72 | char host[16]; |
star297 | 0:e9275bdfa393 | 73 | snprintf(host, sizeof(host), "%d.%d.%d.%d", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff); |
star297 | 0:e9275bdfa393 | 74 | uint16_t port = eth->sreg<uint16_t>(_sock_fd, Sn_DPORT); |
star297 | 0:e9275bdfa393 | 75 | |
star297 | 0:e9275bdfa393 | 76 | // change this server socket to connection socket. |
star297 | 0:e9275bdfa393 | 77 | connection._sock_fd = _sock_fd; |
star297 | 0:e9275bdfa393 | 78 | connection._is_connected = true; |
star297 | 0:e9275bdfa393 | 79 | connection.set_address(host, port); |
star297 | 0:e9275bdfa393 | 80 | |
star297 | 0:e9275bdfa393 | 81 | // and then, for the next connection, server socket should be assigned new one. |
star297 | 0:e9275bdfa393 | 82 | _sock_fd = -1; // want to assign new available _sock_fd. |
star297 | 0:e9275bdfa393 | 83 | if(bind(listen_port) < 0) { |
star297 | 0:e9275bdfa393 | 84 | // modified by Patrick Pollet |
star297 | 0:e9275bdfa393 | 85 | error("No more socket for listening, bind error"); |
star297 | 0:e9275bdfa393 | 86 | return -1; |
star297 | 0:e9275bdfa393 | 87 | } else { |
star297 | 0:e9275bdfa393 | 88 | //return -1; |
star297 | 0:e9275bdfa393 | 89 | if(listen(1) < 0) { |
star297 | 0:e9275bdfa393 | 90 | // modified by Patrick Pollet |
star297 | 0:e9275bdfa393 | 91 | error("No more socket for listening, listen error"); |
star297 | 0:e9275bdfa393 | 92 | return -1; |
star297 | 0:e9275bdfa393 | 93 | } |
star297 | 0:e9275bdfa393 | 94 | } |
star297 | 0:e9275bdfa393 | 95 | return 0; |
star297 | 0:e9275bdfa393 | 96 | } |