From 937bce1a0bb2567f6d03b15df79525569377dabd Mon Sep 17 00:00:00 2001 From: ntruchsess Date: Wed, 27 Nov 2013 10:26:11 +0100 Subject: [PATCH] add localPort to EthernetClient, simplify operator== --- hardware/arduino/cores/arduino/Client.h | 1 + libraries/Ethernet/EthernetClient.cpp | 13 ++++++------- libraries/Ethernet/EthernetClient.h | 1 + .../Ethernet/examples/ChatServer/ChatServer.ino | 5 +---- 4 files changed, 9 insertions(+), 11 deletions(-) diff --git a/hardware/arduino/cores/arduino/Client.h b/hardware/arduino/cores/arduino/Client.h index 0222896bb..3f686f942 100644 --- a/hardware/arduino/cores/arduino/Client.h +++ b/hardware/arduino/cores/arduino/Client.h @@ -19,6 +19,7 @@ public: virtual void stop() = 0; virtual uint8_t connected() = 0; virtual operator bool() = 0; + virtual uint16_t localPort() = 0; virtual IPAddress remoteIP() = 0; virtual uint16_t remotePort() = 0; protected: diff --git a/libraries/Ethernet/EthernetClient.cpp b/libraries/Ethernet/EthernetClient.cpp index 10871e2c2..c14e60024 100644 --- a/libraries/Ethernet/EthernetClient.cpp +++ b/libraries/Ethernet/EthernetClient.cpp @@ -165,13 +165,12 @@ EthernetClient::operator bool() { } bool EthernetClient::operator==(const EthernetClient& rhs) { - if (_sock == MAX_SOCK_NUM || rhs._sock == MAX_SOCK_NUM) return false; - if (W5100.readSnDPORT(_sock)!=W5100.readSnDPORT(rhs._sock)) return false; - uint32_t a1; - uint32_t a2; - W5100.readSnDIPR(_sock,(uint8_t*) &a1); - W5100.readSnDIPR(rhs._sock,(uint8_t*) &a2); - return a1==a2; + return _sock == rhs._sock && _sock != MAX_SOCK_NUM && rhs._sock != MAX_SOCK_NUM; +} + +uint16_t EthernetClient::localPort() { + if (_sock == MAX_SOCK_NUM) return 0; + return W5100.readSnPORT(_sock); } IPAddress EthernetClient::remoteIP() { diff --git a/libraries/Ethernet/EthernetClient.h b/libraries/Ethernet/EthernetClient.h index 9c1b29767..8d51a49f2 100644 --- a/libraries/Ethernet/EthernetClient.h +++ b/libraries/Ethernet/EthernetClient.h @@ -25,6 +25,7 @@ public: virtual uint8_t connected(); virtual operator bool(); virtual bool operator==(const EthernetClient&); + virtual uint16_t localPort(); virtual IPAddress remoteIP(); virtual uint16_t remotePort(); diff --git a/libraries/Ethernet/examples/ChatServer/ChatServer.ino b/libraries/Ethernet/examples/ChatServer/ChatServer.ino index edd8fb3f8..281eaea96 100644 --- a/libraries/Ethernet/examples/ChatServer/ChatServer.ino +++ b/libraries/Ethernet/examples/ChatServer/ChatServer.ino @@ -72,8 +72,6 @@ void loop() { for (byte i=0;i<4;i++) { if (clients[i]!=client) { clients[i] = client; - Serial.print("found slot: "); - Serial.println(i); break; } } @@ -81,7 +79,7 @@ void loop() { // clead out the input buffer: client.flush(); Serial.println("We have a new client"); - client.println("Hello, client!"); + client.println("Hello, client!"); client.print("your IP: "); client.println(client.remoteIP()); client.print("your port: "); @@ -105,7 +103,6 @@ void loop() { for (byte i=0;i<4;i++) { if (!(clients[i].connected())) { clients[i].stop(); - ~clients[i]; clients[i]=EthernetClient(); } }