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 uint8_t connected() = 0;
virtual operator bool() = 0;
virtual uint16_t localPort() = 0;
virtual IPAddress remoteIP() = 0;
virtual uint16_t remotePort() = 0;
protected:

View File

@ -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() {

View File

@ -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();

View File

@ -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();
}
}