|
|
@ -700,7 +700,7 @@ bool BaseChatMesh::startConnection(const ContactInfo& contact, uint16_t keep_ali |
|
|
uint32_t interval = connections[use_idx].keep_alive_millis = ((uint32_t)keep_alive_secs)*1000; |
|
|
uint32_t interval = connections[use_idx].keep_alive_millis = ((uint32_t)keep_alive_secs)*1000; |
|
|
connections[use_idx].next_ping = futureMillis(interval); |
|
|
connections[use_idx].next_ping = futureMillis(interval); |
|
|
connections[use_idx].expected_ack = 0; |
|
|
connections[use_idx].expected_ack = 0; |
|
|
connections[use_idx].last_activity = getRTCClock()->getCurrentTime(); |
|
|
connections[use_idx].last_activity_ms = _ms->getMillis(); |
|
|
return true; // success
|
|
|
return true; // success
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -710,7 +710,7 @@ void BaseChatMesh::stopConnection(const uint8_t* pub_key) { |
|
|
connections[i].keep_alive_millis = 0; // mark slot as now free
|
|
|
connections[i].keep_alive_millis = 0; // mark slot as now free
|
|
|
connections[i].next_ping = 0; |
|
|
connections[i].next_ping = 0; |
|
|
connections[i].expected_ack = 0; |
|
|
connections[i].expected_ack = 0; |
|
|
connections[i].last_activity = 0; |
|
|
connections[i].last_activity_ms = 0; |
|
|
break; |
|
|
break; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
@ -726,7 +726,7 @@ bool BaseChatMesh::hasConnectionTo(const uint8_t* pub_key) { |
|
|
void BaseChatMesh::markConnectionActive(const ContactInfo& contact) { |
|
|
void BaseChatMesh::markConnectionActive(const ContactInfo& contact) { |
|
|
for (int i = 0; i < MAX_CONNECTIONS; i++) { |
|
|
for (int i = 0; i < MAX_CONNECTIONS; i++) { |
|
|
if (connections[i].keep_alive_millis > 0 && connections[i].server_id.matches(contact.id)) { |
|
|
if (connections[i].keep_alive_millis > 0 && connections[i].server_id.matches(contact.id)) { |
|
|
connections[i].last_activity = getRTCClock()->getCurrentTime(); |
|
|
connections[i].last_activity_ms = _ms->getMillis(); |
|
|
|
|
|
|
|
|
// re-schedule next KEEP_ALIVE, now that we have heard from server
|
|
|
// re-schedule next KEEP_ALIVE, now that we have heard from server
|
|
|
connections[i].next_ping = futureMillis(connections[i].keep_alive_millis); |
|
|
connections[i].next_ping = futureMillis(connections[i].keep_alive_millis); |
|
|
@ -740,7 +740,7 @@ ContactInfo* BaseChatMesh::checkConnectionsAck(const uint8_t* data) { |
|
|
if (connections[i].keep_alive_millis > 0 && memcmp(&connections[i].expected_ack, data, 4) == 0) { |
|
|
if (connections[i].keep_alive_millis > 0 && memcmp(&connections[i].expected_ack, data, 4) == 0) { |
|
|
// yes, got an ack for our keep_alive request!
|
|
|
// yes, got an ack for our keep_alive request!
|
|
|
connections[i].expected_ack = 0; |
|
|
connections[i].expected_ack = 0; |
|
|
connections[i].last_activity = getRTCClock()->getCurrentTime(); |
|
|
connections[i].last_activity_ms = _ms->getMillis(); |
|
|
|
|
|
|
|
|
// re-schedule next KEEP_ALIVE, now that we have heard from server
|
|
|
// re-schedule next KEEP_ALIVE, now that we have heard from server
|
|
|
connections[i].next_ping = futureMillis(connections[i].keep_alive_millis); |
|
|
connections[i].next_ping = futureMillis(connections[i].keep_alive_millis); |
|
|
@ -757,14 +757,17 @@ void BaseChatMesh::checkConnections() { |
|
|
for (int i = 0; i < MAX_CONNECTIONS; i++) { |
|
|
for (int i = 0; i < MAX_CONNECTIONS; i++) { |
|
|
if (connections[i].keep_alive_millis == 0) continue; // unused slot
|
|
|
if (connections[i].keep_alive_millis == 0) continue; // unused slot
|
|
|
|
|
|
|
|
|
uint32_t now = getRTCClock()->getCurrentTime(); |
|
|
// Monotonic time is immune to RTC clock changes (GPS, NTP, manual sync).
|
|
|
uint32_t expire_secs = (connections[i].keep_alive_millis / 1000) * 5 / 2; // 2.5 x keep_alive interval
|
|
|
// Assumes light sleep (millis() keeps incrementing). Deep sleep resets millis(),
|
|
|
if (now >= connections[i].last_activity + expire_secs) { |
|
|
// but BaseChatMesh is only used by companion_radio which uses light sleep.
|
|
|
|
|
|
unsigned long now = _ms->getMillis(); |
|
|
|
|
|
unsigned long expire_millis = (connections[i].keep_alive_millis * 5UL) / 2; // 2.5 x keep_alive interval
|
|
|
|
|
|
if ((now - connections[i].last_activity_ms) >= expire_millis) { |
|
|
// connection now lost
|
|
|
// connection now lost
|
|
|
connections[i].keep_alive_millis = 0; |
|
|
connections[i].keep_alive_millis = 0; |
|
|
connections[i].next_ping = 0; |
|
|
connections[i].next_ping = 0; |
|
|
connections[i].expected_ack = 0; |
|
|
connections[i].expected_ack = 0; |
|
|
connections[i].last_activity = 0; |
|
|
connections[i].last_activity_ms = 0; |
|
|
continue; |
|
|
continue; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|