Skip to content

Commit

Permalink
Simplify timeouts
Browse files Browse the repository at this point in the history
- Add timeouts for both clients and servers by default
- Change default timeout period to 45 seconds
- Server-side timeouts were not working by default, now they are. Set to a higher value or 0 to disable
  • Loading branch information
TMRh20 committed Oct 14, 2023
1 parent 3836331 commit b92a75b
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 37 deletions.
68 changes: 33 additions & 35 deletions RF24Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,20 +241,30 @@ 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 (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)"););
}else{
if(!u->initialData){
u->connectTimer = millis();
u->initialData = true;
}
}
}
#endif

/*******Connected**********/
if (!u && uip_connected())
{
IF_RF24ETHERNET_DEBUG_CLIENT(Serial.println(); Serial.print(millis()); Serial.println(F(" UIPClient uip_connected")););

u = (uip_userdata_t*)EthernetClient::_allocateData();

#if UIP_CONNECTION_TIMEOUT > 0
u->connectTimer = millis();
#endif
if (u)
{
uip_conn->appstate = u;
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,7 +335,10 @@ 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**********/
Expand All @@ -350,36 +364,17 @@ void serialip_appcall(void)
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
{
#endif
#if defined RF24ETHERNET_DEBUG_CLIENT || defined ETH_DEBUG_L1
Serial.println();
Serial.print(millis());
Serial.print(F(" UIPClient Re-Open TCP Window, time remaining before abort: "));
Serial.println((UIP_CONNECTION_TIMEOUT - (millis() - u->connAbortTime)) / 1000.00);
#endif
u->restartInterval += 500;
u->restartInterval = rf24_min(u->restartInterval, 7000);
uip_restart();
#if defined UIP_CONNECTION_TIMEOUT
}
#if UIP_CONNECTION_TIMEOUT > 0
Serial.print(F(" UIPClient Re-Open TCP Window, time remaining before abort: "));
Serial.println( UIP_CONNECTION_TIMEOUT - (millis() - u->connectTimer));
#endif
#endif
u->restartInterval += 500;
u->restartInterval = rf24_min(u->restartInterval, 7000);
uip_restart();
}
}
}
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;
data->connectTimer = millis();
#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: 4 additions & 0 deletions RF24Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,16 @@ size_t RF24Server::write(const uint8_t* buf, size_t size)
return ret;
}

/*************************************************************/

void RF24Server::setTimeout(uint32_t timeout)
{
#if UIP_CONNECTION_TIMEOUT > 0
for (uint8_t i = 0; i < UIP_CONNS; i++) {
uip_userdata_t* data = &RF24Client::all_data[i];
if (data) {
data->connectTimeout = timeout;
}
}
#endif
}
2 changes: 1 addition & 1 deletion uip-conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
*
* If uIP is polling the established connection, but an ack or data is not received for this duration in ms, kill the connection.
*/
#define UIP_CONNECTION_TIMEOUT 30000
#define UIP_CONNECTION_TIMEOUT 45000

/**
* @brief SLIP/TUN - 14 for Ethernet/TAP & 0 for TUN/SLIP
Expand Down

0 comments on commit b92a75b

Please sign in to comment.