You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a program which needs to always update outputs in a timely manner, even when calling EthernetClient::connect() (which can block for the number of milliseconds set with setConnectionTimeout()).
I have modified the current code:
intEthernetClient::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()?
}
_sockindex = MAX_SOCK_NUM;
}
#if defined(ESP8266) || defined(ESP32)
if (ip == IPAddress((uint32_t)0) || ip == IPAddress(0xFFFFFFFFul)) return0;
#elseif (ip == IPAddress(0ul) || ip == IPAddress(0xFFFFFFFFul)) return0;
#endif
_sockindex = Ethernet.socketBegin(SnMR::TCP, 0);
if (_sockindex >= MAX_SOCK_NUM) return0;
Ethernet.socketConnect(_sockindex, rawIPAddress(ip), port);
uint32_t start = millis();
while (1) {
uint8_t stat = Ethernet.socketStatus(_sockindex);
if (stat == SnSR::ESTABLISHED) return1;
if (stat == SnSR::CLOSE_WAIT) return1;
if (stat == SnSR::CLOSED) return0;
if (millis() - start > _timeout) break;
delay(1);
}
Ethernet.socketClose(_sockindex);
_sockindex = MAX_SOCK_NUM;
return0;
}
to the following:
intEthernetClient::connect(IPAddress ip, uint16_t port)
{
int ret = beginConnect(ip, port);
if (ret >= 0) return ret;
uint32_t start = millis();
while (1) {
ret = endConnect();
if (ret >= 0) return ret;
if (millis() - start > _timeout) break;
delay(1);
}
Ethernet.socketClose(_sockindex);
_sockindex = MAX_SOCK_NUM;
return0;
}
intEthernetClient::beginConnect(IPAddress ip, uint16_t port)
{
if (_sockindex < MAX_SOCK_NUM) {
if (Ethernet.socketStatus(_sockindex) != SnSR::CLOSED) {
Ethernet.socketDisconnect(_sockindex); // TODO: should we call stop()?
}
_sockindex = MAX_SOCK_NUM;
}
if (ip == IPAddress(0ul) || ip == IPAddress(0xFFFFFFFFul)) return0;
_sockindex = Ethernet.socketBegin(SnMR::TCP, 0);
if (_sockindex >= MAX_SOCK_NUM) return0;
Ethernet.socketConnect(_sockindex, rawIPAddress(ip), port);
return -1;
}
intEthernetClient::endConnect()
{
uint8_t stat = Ethernet.socketStatus(_sockindex);
if (stat == SnSR::ESTABLISHED) return1;
if (stat == SnSR::CLOSE_WAIT) return1;
if (stat == SnSR::CLOSED) return0;
return -1;
}
Now instead of calling connect() I can call beginConnect() and then repeatedly call endConnect() during the main loop until it returns either 1 on success or 0 on failure.
If there is interest to include this into the library I can update the documentation (which is not up-to-date for connect() anyway) and create a PR. Please let me know.
The text was updated successfully, but these errors were encountered:
I have a program which needs to always update outputs in a timely manner, even when calling
EthernetClient::connect()
(which can block for the number of milliseconds set withsetConnectionTimeout()
).I have modified the current code:
to the following:
Now instead of calling
connect()
I can callbeginConnect()
and then repeatedly callendConnect()
during the main loop until it returns either 1 on success or 0 on failure.If there is interest to include this into the library I can update the documentation (which is not up-to-date for
connect()
anyway) and create a PR. Please let me know.The text was updated successfully, but these errors were encountered: