Skip to content

Commit

Permalink
rename sockindex to _sockindex
Browse files Browse the repository at this point in the history
private variable in this class begin with _
  • Loading branch information
Rotzbua authored and aentinger committed Dec 15, 2021
1 parent 7c46fb9 commit 1e2a6c2
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 52 deletions.
10 changes: 5 additions & 5 deletions src/Ethernet.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,8 @@ class EthernetUDP : public UDP {

class EthernetClient : public Client {
public:
EthernetClient() : sockindex(MAX_SOCK_NUM), _timeout(1000) { }
EthernetClient(uint8_t s) : sockindex(s), _timeout(1000) { }
EthernetClient() : _sockindex(MAX_SOCK_NUM), _timeout(1000) { }
EthernetClient(uint8_t s) : _sockindex(s), _timeout(1000) { }
virtual ~EthernetClient() {};

uint8_t status();
Expand All @@ -230,12 +230,12 @@ class EthernetClient : public Client {
virtual void flush();
virtual void stop();
virtual uint8_t connected();
virtual operator bool() { return sockindex < MAX_SOCK_NUM; }
virtual operator bool() { return _sockindex < MAX_SOCK_NUM; }
virtual bool operator==(const bool value) { return bool() == value; }
virtual bool operator!=(const bool value) { return bool() != value; }
virtual bool operator==(const EthernetClient&);
virtual bool operator!=(const EthernetClient& rhs) { return !this->operator==(rhs); }
uint8_t getSocketNumber() const { return sockindex; }
uint8_t getSocketNumber() const { return _sockindex; }
virtual uint16_t localPort();
virtual IPAddress remoteIP();
virtual uint16_t remotePort();
Expand All @@ -246,7 +246,7 @@ class EthernetClient : public Client {
using Print::write;

private:
uint8_t sockindex; // MAX_SOCK_NUM means client not in use
uint8_t _sockindex; // MAX_SOCK_NUM means client not in use
uint16_t _timeout;
};

Expand Down
94 changes: 47 additions & 47 deletions src/EthernetClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ int EthernetClient::connect(const char * host, uint16_t port)
DNSClient dns; // Look up the host first
IPAddress remote_addr;

if (sockindex < MAX_SOCK_NUM) {
if (Ethernet.socketStatus(sockindex) != SnSR::CLOSED) {
Ethernet.socketDisconnect(sockindex); // TODO: should we call stop()?
if (_sockindex < MAX_SOCK_NUM) {
if (Ethernet.socketStatus(_sockindex) != SnSR::CLOSED) {
Ethernet.socketDisconnect(_sockindex); // TODO: should we call stop()?
}
sockindex = MAX_SOCK_NUM;
_sockindex = MAX_SOCK_NUM;
}
dns.begin(Ethernet.dnsServerIP());
if (!dns.getHostByName(host, remote_addr)) return 0; // TODO: use _timeout
Expand All @@ -41,38 +41,38 @@ int EthernetClient::connect(const char * host, uint16_t port)

int EthernetClient::connect(IPAddress ip, uint16_t port)
{
if (sockindex < MAX_SOCK_NUM) {
if (Ethernet.socketStatus(sockindex) != SnSR::CLOSED) {
Ethernet.socketDisconnect(sockindex); // TODO: should we call stop()?
if (_sockindex < MAX_SOCK_NUM) {
if (Ethernet.socketStatus(_sockindex) != SnSR::CLOSED) {
Ethernet.socketDisconnect(_sockindex); // TODO: should we call stop()?
}
sockindex = MAX_SOCK_NUM;
_sockindex = MAX_SOCK_NUM;
}
#if defined(ESP8266) || defined(ESP32)
if (ip == IPAddress((uint32_t)0) || ip == IPAddress(0xFFFFFFFFul)) return 0;
#else
if (ip == IPAddress(0ul) || ip == IPAddress(0xFFFFFFFFul)) return 0;
#endif
sockindex = Ethernet.socketBegin(SnMR::TCP, 0);
if (sockindex >= MAX_SOCK_NUM) return 0;
Ethernet.socketConnect(sockindex, rawIPAddress(ip), port);
_sockindex = Ethernet.socketBegin(SnMR::TCP, 0);
if (_sockindex >= MAX_SOCK_NUM) return 0;
Ethernet.socketConnect(_sockindex, rawIPAddress(ip), port);
uint32_t start = millis();
while (1) {
uint8_t stat = Ethernet.socketStatus(sockindex);
uint8_t stat = Ethernet.socketStatus(_sockindex);
if (stat == SnSR::ESTABLISHED) return 1;
if (stat == SnSR::CLOSE_WAIT) return 1;
if (stat == SnSR::CLOSED) return 0;
if (millis() - start > _timeout) break;
delay(1);
}
Ethernet.socketClose(sockindex);
sockindex = MAX_SOCK_NUM;
Ethernet.socketClose(_sockindex);
_sockindex = MAX_SOCK_NUM;
return 0;
}

int EthernetClient::availableForWrite(void)
{
if (sockindex >= MAX_SOCK_NUM) return 0;
return Ethernet.socketSendAvailable(sockindex);
if (_sockindex >= MAX_SOCK_NUM) return 0;
return Ethernet.socketSendAvailable(_sockindex);
}

size_t EthernetClient::write(uint8_t b)
Expand All @@ -82,16 +82,16 @@ size_t EthernetClient::write(uint8_t b)

size_t EthernetClient::write(const uint8_t *buf, size_t size)
{
if (sockindex >= MAX_SOCK_NUM) return 0;
if (Ethernet.socketSend(sockindex, buf, size)) return size;
if (_sockindex >= MAX_SOCK_NUM) return 0;
if (Ethernet.socketSend(_sockindex, buf, size)) return size;
setWriteError();
return 0;
}

int EthernetClient::available()
{
if (sockindex >= MAX_SOCK_NUM) return 0;
return Ethernet.socketRecvAvailable(sockindex);
if (_sockindex >= MAX_SOCK_NUM) return 0;
return Ethernet.socketRecvAvailable(_sockindex);
// TODO: do the WIZnet chips automatically retransmit TCP ACK
// packets if they are lost by the network? Someday this should
// be checked by a man-in-the-middle test which discards certain
Expand All @@ -102,88 +102,88 @@ int EthernetClient::available()

int EthernetClient::read(uint8_t *buf, size_t size)
{
if (sockindex >= MAX_SOCK_NUM) return 0;
return Ethernet.socketRecv(sockindex, buf, size);
if (_sockindex >= MAX_SOCK_NUM) return 0;
return Ethernet.socketRecv(_sockindex, buf, size);
}

int EthernetClient::peek()
{
if (sockindex >= MAX_SOCK_NUM) return -1;
if (_sockindex >= MAX_SOCK_NUM) return -1;
if (!available()) return -1;
return Ethernet.socketPeek(sockindex);
return Ethernet.socketPeek(_sockindex);
}

int EthernetClient::read()
{
uint8_t b;
if (Ethernet.socketRecv(sockindex, &b, 1) > 0) return b;
if (Ethernet.socketRecv(_sockindex, &b, 1) > 0) return b;
return -1;
}

void EthernetClient::flush()
{
while (sockindex < MAX_SOCK_NUM) {
uint8_t stat = Ethernet.socketStatus(sockindex);
while (_sockindex < MAX_SOCK_NUM) {
uint8_t stat = Ethernet.socketStatus(_sockindex);
if (stat != SnSR::ESTABLISHED && stat != SnSR::CLOSE_WAIT) return;
if (Ethernet.socketSendAvailable(sockindex) >= W5100.SSIZE) return;
if (Ethernet.socketSendAvailable(_sockindex) >= W5100.SSIZE) return;
}
}

void EthernetClient::stop()
{
if (sockindex >= MAX_SOCK_NUM) return;
if (_sockindex >= MAX_SOCK_NUM) return;

// attempt to close the connection gracefully (send a FIN to other side)
Ethernet.socketDisconnect(sockindex);
Ethernet.socketDisconnect(_sockindex);
unsigned long start = millis();

// wait up to a second for the connection to close
do {
if (Ethernet.socketStatus(sockindex) == SnSR::CLOSED) {
sockindex = MAX_SOCK_NUM;
if (Ethernet.socketStatus(_sockindex) == SnSR::CLOSED) {
_sockindex = MAX_SOCK_NUM;
return; // exit the loop
}
delay(1);
} while (millis() - start < _timeout);

// if it hasn't closed, close it forcefully
Ethernet.socketClose(sockindex);
sockindex = MAX_SOCK_NUM;
Ethernet.socketClose(_sockindex);
_sockindex = MAX_SOCK_NUM;
}

uint8_t EthernetClient::connected()
{
if (sockindex >= MAX_SOCK_NUM) return 0;
if (_sockindex >= MAX_SOCK_NUM) return 0;

uint8_t s = Ethernet.socketStatus(sockindex);
uint8_t s = Ethernet.socketStatus(_sockindex);
return !(s == SnSR::LISTEN || s == SnSR::CLOSED || s == SnSR::FIN_WAIT ||
(s == SnSR::CLOSE_WAIT && !available()));
}

uint8_t EthernetClient::status()
{
if (sockindex >= MAX_SOCK_NUM) return SnSR::CLOSED;
return Ethernet.socketStatus(sockindex);
if (_sockindex >= MAX_SOCK_NUM) return SnSR::CLOSED;
return Ethernet.socketStatus(_sockindex);
}

// the next function allows us to use the client returned by
// EthernetServer::available() as the condition in an if-statement.
bool EthernetClient::operator==(const EthernetClient& rhs)
{
if (sockindex != rhs.sockindex) return false;
if (sockindex >= MAX_SOCK_NUM) return false;
if (rhs.sockindex >= MAX_SOCK_NUM) return false;
if (_sockindex != rhs._sockindex) return false;
if (_sockindex >= MAX_SOCK_NUM) return false;
if (rhs._sockindex >= MAX_SOCK_NUM) return false;
return true;
}

// https://github.com/per1234/EthernetMod
// from: https://github.com/ntruchsess/Arduino-1/commit/937bce1a0bb2567f6d03b15df79525569377dabd
uint16_t EthernetClient::localPort()
{
if (sockindex >= MAX_SOCK_NUM) return 0;
if (_sockindex >= MAX_SOCK_NUM) return 0;
uint16_t port;
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
port = W5100.readSnPORT(sockindex);
port = W5100.readSnPORT(_sockindex);
SPI.endTransaction();
return port;
}
Expand All @@ -192,10 +192,10 @@ uint16_t EthernetClient::localPort()
// returns the remote IP address: https://forum.arduino.cc/index.php?topic=82416.0
IPAddress EthernetClient::remoteIP()
{
if (sockindex >= MAX_SOCK_NUM) return IPAddress((uint32_t)0);
if (_sockindex >= MAX_SOCK_NUM) return IPAddress((uint32_t)0);
uint8_t remoteIParray[4];
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
W5100.readSnDIPR(sockindex, remoteIParray);
W5100.readSnDIPR(_sockindex, remoteIParray);
SPI.endTransaction();
return IPAddress(remoteIParray);
}
Expand All @@ -204,10 +204,10 @@ IPAddress EthernetClient::remoteIP()
// from: https://github.com/ntruchsess/Arduino-1/commit/ca37de4ba4ecbdb941f14ac1fe7dd40f3008af75
uint16_t EthernetClient::remotePort()
{
if (sockindex >= MAX_SOCK_NUM) return 0;
if (_sockindex >= MAX_SOCK_NUM) return 0;
uint16_t port;
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
port = W5100.readSnDPORT(sockindex);
port = W5100.readSnDPORT(_sockindex);
SPI.endTransaction();
return port;
}

0 comments on commit 1e2a6c2

Please sign in to comment.