1
0
mirror of https://github.com/arduino/Arduino.git synced 2025-01-18 07:52:14 +01:00

add localPort to EthernetClient, simplify operator==

This commit is contained in:
ntruchsess 2013-11-27 10:26:11 +01:00
parent ca37de4ba4
commit 937bce1a0b
4 changed files with 9 additions and 11 deletions

View File

@ -19,6 +19,7 @@ public:
virtual void stop() = 0; virtual void stop() = 0;
virtual uint8_t connected() = 0; virtual uint8_t connected() = 0;
virtual operator bool() = 0; virtual operator bool() = 0;
virtual uint16_t localPort() = 0;
virtual IPAddress remoteIP() = 0; virtual IPAddress remoteIP() = 0;
virtual uint16_t remotePort() = 0; virtual uint16_t remotePort() = 0;
protected: protected:

View File

@ -165,13 +165,12 @@ EthernetClient::operator bool() {
} }
bool EthernetClient::operator==(const EthernetClient& rhs) { bool EthernetClient::operator==(const EthernetClient& rhs) {
if (_sock == MAX_SOCK_NUM || rhs._sock == MAX_SOCK_NUM) return false; return _sock == rhs._sock && _sock != MAX_SOCK_NUM && rhs._sock != MAX_SOCK_NUM;
if (W5100.readSnDPORT(_sock)!=W5100.readSnDPORT(rhs._sock)) return false; }
uint32_t a1;
uint32_t a2; uint16_t EthernetClient::localPort() {
W5100.readSnDIPR(_sock,(uint8_t*) &a1); if (_sock == MAX_SOCK_NUM) return 0;
W5100.readSnDIPR(rhs._sock,(uint8_t*) &a2); return W5100.readSnPORT(_sock);
return a1==a2;
} }
IPAddress EthernetClient::remoteIP() { IPAddress EthernetClient::remoteIP() {

View File

@ -25,6 +25,7 @@ public:
virtual uint8_t connected(); virtual uint8_t connected();
virtual operator bool(); virtual operator bool();
virtual bool operator==(const EthernetClient&); virtual bool operator==(const EthernetClient&);
virtual uint16_t localPort();
virtual IPAddress remoteIP(); virtual IPAddress remoteIP();
virtual uint16_t remotePort(); virtual uint16_t remotePort();

View File

@ -72,8 +72,6 @@ void loop() {
for (byte i=0;i<4;i++) { for (byte i=0;i<4;i++) {
if (clients[i]!=client) { if (clients[i]!=client) {
clients[i] = client; clients[i] = client;
Serial.print("found slot: ");
Serial.println(i);
break; break;
} }
} }
@ -81,7 +79,7 @@ void loop() {
// clead out the input buffer: // clead out the input buffer:
client.flush(); client.flush();
Serial.println("We have a new client"); Serial.println("We have a new client");
client.println("Hello, client!"); client.println("Hello, client!");
client.print("your IP: "); client.print("your IP: ");
client.println(client.remoteIP()); client.println(client.remoteIP());
client.print("your port: "); client.print("your port: ");
@ -105,7 +103,6 @@ void loop() {
for (byte i=0;i<4;i++) { for (byte i=0;i<4;i++) {
if (!(clients[i].connected())) { if (!(clients[i].connected())) {
clients[i].stop(); clients[i].stop();
~clients[i];
clients[i]=EthernetClient(); clients[i]=EthernetClient();
} }
} }