Skip to content

Commit

Permalink
Merge pull request #39 from nRF24/ConnectionTimeouts
Browse files Browse the repository at this point in the history
Simplify timeouts and enable for both servers and clients by default
  • Loading branch information
TMRh20 authored Oct 15, 2023
2 parents 3836331 + 778461e commit 792b0be
Show file tree
Hide file tree
Showing 9 changed files with 236 additions and 242 deletions.
185 changes: 90 additions & 95 deletions Dns.cpp

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions Dns.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
class DNSClient
{
public:

void begin(const IPAddress& aDNSServer);

/**
Expand All @@ -22,7 +21,7 @@ class DNSClient
* @result 1 if aIPAddrString was successfully converted to an IP address,
* else error code
*/
int inet_aton(const char *aIPAddrString, IPAddress& aResult);
int inet_aton(const char* aIPAddrString, IPAddress& aResult);

/**
* Resolve the given hostname to an IP address.
Expand Down
82 changes: 40 additions & 42 deletions RF24Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ int RF24Client::connect(IPAddress ip, uint16_t port)
{
#if UIP_ACTIVE_OPEN > 0

//do{
// do{

stop();
uip_ipaddr_t ipaddr;
Expand Down Expand Up @@ -75,10 +75,10 @@ int RF24Client::connect(IPAddress ip, uint16_t port)
#endif
}
}
//delay(25);
//}while(millis()-timer < 175);
// delay(25);
// }while(millis()-timer < 175);

#endif //Active open enabled
#endif // Active open enabled

return 0;
}
Expand All @@ -105,7 +105,7 @@ int RF24Client::connect(const char* host, uint16_t port)
return connect(remote_addr, port);
}
#else // ! UIP_UDP
//Do something with the input parameters to prevent compile time warnings
// Do something with the input parameters to prevent compile time warnings
if (host) {
};
if (port) {
Expand Down Expand Up @@ -214,7 +214,7 @@ size_t RF24Client::_write(uip_userdata_t* u, const uint8_t* buf, size_t size)
size_t remain = size - total_written;
payloadSize = rf24_min(remain, UIP_TCP_MSS);

//RF24EthernetClass::tick();
// RF24EthernetClass::tick();
goto test2;
}
return u->out_pos;
Expand All @@ -227,9 +227,9 @@ size_t RF24Client::_write(uip_userdata_t* u, const uint8_t* buf, size_t size)

void uip_log(char* msg)
{
//Serial.println();
//Serial.println("** UIP LOG **");
//Serial.println(msg);
// Serial.println();
// Serial.println("** UIP LOG **");
// Serial.println(msg);
if (msg)
{
};
Expand All @@ -241,12 +241,19 @@ void serialip_appcall(void)
{
uip_userdata_t* u = (uip_userdata_t*)uip_conn->appstate;

#if UIP_CONNECTION_TIMEOUT > 0
if (u && u->connectTimeout > 0) {
if (millis() - u->connectTimer > u->connectTimeout && u->initialData == false) {
uip_close();
if (!u->initialData) {
u->connectTimer = millis();
u->initialData = true;
}
else if (millis() - u->connectTimer > u->connectTimeout) {
u->state |= UIP_CLIENT_CLOSE;
u->connectTimer = millis();
IF_RF24ETHERNET_DEBUG_CLIENT(Serial.println(); Serial.print(millis()); Serial.println("UIP Client close(timeout)"););
}
}
#endif

/*******Connected**********/
if (!u && uip_connected())
Expand All @@ -257,6 +264,9 @@ void serialip_appcall(void)

if (u)
{
#if UIP_CONNECTION_TIMEOUT > 0
u->connectTimer = millis();
#endif
uip_conn->appstate = u;
IF_RF24ETHERNET_DEBUG_CLIENT(Serial.print(F("UIPClient allocated state: ")); Serial.println(u->state, BIN););
}
Expand All @@ -272,8 +282,9 @@ void serialip_appcall(void)
if (uip_newdata())
{
IF_RF24ETHERNET_DEBUG_CLIENT(Serial.println(); Serial.print(millis()); Serial.print(F(" UIPClient uip_newdata, uip_len:")); Serial.println(uip_len););

u->initialData = true;
#if UIP_CONNECTION_TIMEOUT > 0
u->connectTimer = millis();
#endif

if (u->sent)
{
Expand All @@ -284,7 +295,7 @@ void serialip_appcall(void)
uip_stop();
u->state &= ~UIP_CLIENT_RESTART;
u->windowOpened = false;
u->connAbortTime = u->restartTime = millis();
u->restartTime = millis();
memcpy(&u->myData[u->dataPos + u->dataCnt], uip_appdata, uip_datalen());
u->dataCnt += uip_datalen();

Expand Down Expand Up @@ -324,13 +335,16 @@ void serialip_appcall(void)
IF_RF24ETHERNET_DEBUG_CLIENT(Serial.println(); Serial.print(millis()); Serial.println(F(" UIPClient uip_acked")););
u->state &= ~UIP_CLIENT_RESTART;
u->hold = (u->out_pos = (u->windowOpened = (u->packets_out = false)));
u->connAbortTime = (u->restartTime = millis());
u->restartTime = millis();
#if UIP_CONNECTION_TIMEOUT > 0
u->connectTimer = millis();
#endif
}

/*******Polling**********/
if (uip_poll() || uip_rexmit())
{
//IF_RF24ETHERNET_DEBUG_CLIENT( Serial.println(); Serial.println(F("UIPClient uip_poll")); );
// IF_RF24ETHERNET_DEBUG_CLIENT( Serial.println(); Serial.println(F("UIPClient uip_poll")); );

if (u->packets_out != 0)
{
Expand All @@ -345,43 +359,24 @@ void serialip_appcall(void)
// Only call this if the TCP window has already been re-opened, the connection is being polled, but no data
// has been acked
if (!(u->state & (UIP_CLIENT_CLOSE | UIP_CLIENT_REMOTECLOSED)))
{

if (u->windowOpened == true && u->state & UIP_CLIENT_RESTART && millis() - u->restartTime > u->restartInterval)
{
u->restartTime = millis();
// Abort the connection if the connection is dead after a set timeout period (uip-conf.h)
#if defined UIP_CONNECTION_TIMEOUT
if (millis() - u->connAbortTime >= UIP_CONNECTION_TIMEOUT)
{
#if defined RF24ETHERNET_DEBUG_CLIENT || defined ETH_DEBUG_L1
Serial.println();
Serial.print(millis());
Serial.println(F(" *********** ABORTING CONNECTION ***************"));
#endif
u->windowOpened = false;
u->state = 0;
uip_conn->appstate = NULL;
uip_abort();
goto finish;
}
else

if (u->windowOpened == true && u->state & UIP_CLIENT_RESTART && millis() - u->restartTime > u->restartInterval)
{
#endif
u->restartTime = millis();
#if defined RF24ETHERNET_DEBUG_CLIENT || defined ETH_DEBUG_L1
Serial.println();
Serial.print(millis());
#if UIP_CONNECTION_TIMEOUT > 0
Serial.print(F(" UIPClient Re-Open TCP Window, time remaining before abort: "));
Serial.println((UIP_CONNECTION_TIMEOUT - (millis() - u->connAbortTime)) / 1000.00);
Serial.println(UIP_CONNECTION_TIMEOUT - (millis() - u->connectTimer));
#endif
#endif
u->restartInterval += 500;
u->restartInterval = rf24_min(u->restartInterval, 7000);
uip_restart();
#if defined UIP_CONNECTION_TIMEOUT
}
#endif
}
}
}

/*******Close**********/
Expand Down Expand Up @@ -416,7 +411,7 @@ finish:;
#endif
u->windowOpened = true;
u->restartInterval = UIP_WINDOW_REOPEN_DELAY; //.75 seconds
u->restartTime = u->connAbortTime = millis();
u->restartTime = millis();
}
}
}
Expand All @@ -439,7 +434,10 @@ uip_userdata_t* RF24Client::_allocateData()
data->out_pos = 0;
data->hold = 0;
data->initialData = false;
#if (UIP_CONNECTION_TIMEOUT > 0)
data->connectTimer = millis();
data->connectTimeout = UIP_CONNECTION_TIMEOUT;
#endif
return data;
}
}
Expand Down
3 changes: 2 additions & 1 deletion RF24Client.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ typedef struct
#endif
uint32_t restartTime;
uint32_t restartInterval;
uint32_t connAbortTime;
#if UIP_CONNECTION_TIMEOUT > 0
uint32_t connectTimeout;
uint32_t connectTimer;
#endif
uint8_t myData[OUTPUT_BUFFER_SIZE];
bool initialData;
} uip_userdata_t;
Expand Down
4 changes: 2 additions & 2 deletions RF24Ethernet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,8 @@ void RF24EthernetClass::tick()
yield();
#endif
#if defined(ARDUINO_ARCH_ESP32)
const TickType_t xDelay = 1 / portTICK_PERIOD_MS;
vTaskDelay( xDelay );
const TickType_t xDelay = 1 / portTICK_PERIOD_MS;
vTaskDelay(xDelay);
#endif

if (RF24Ethernet.network.update() == EXTERNAL_DATA_TYPE) {
Expand Down
Loading

0 comments on commit 792b0be

Please sign in to comment.