From 46b811c5e8ead2768a498d3223e101e533cb2728 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 25 Jun 2024 12:26:12 +0700 Subject: [PATCH] Add support TCP session timeout for session reusage --- README.md | 7 ++++--- library.json | 2 +- library.properties | 2 +- src/Config.h | 4 ++++ src/core/AsyncClient/AsyncClient.h | 15 +++++++++++++-- src/core/Core.h | 2 +- 6 files changed, 24 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 19cbd1a..100fe18 100644 --- a/README.md +++ b/README.md @@ -2,11 +2,11 @@ ![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/mobizt/FirebaseClient/.github%2Fworkflows%2Fcompile_library.yml?logo=github&label=compile) [![Github Stars](https://img.shields.io/github/stars/mobizt/FirebaseClient?logo=github)](https://github.com/mobizt/FirebaseClient/stargazers) ![Github Issues](https://img.shields.io/github/issues/mobizt/FirebaseClient?logo=github) -![GitHub Release](https://img.shields.io/github/v/release/mobizt/FirebaseClient) ![Arduino](https://img.shields.io/badge/Arduino-v1.2.15-57C207?logo=arduino) ![PlatformIO](https://badges.registry.platformio.org/packages/mobizt/library/FirebaseClient.svg) ![GitHub Release Date](https://img.shields.io/github/release-date/mobizt/FirebaseClient) +![GitHub Release](https://img.shields.io/github/v/release/mobizt/FirebaseClient) ![Arduino](https://img.shields.io/badge/Arduino-v1.2.16-57C207?logo=arduino) ![PlatformIO](https://badges.registry.platformio.org/packages/mobizt/library/FirebaseClient.svg) ![GitHub Release Date](https://img.shields.io/github/release-date/mobizt/FirebaseClient) [![GitHub Sponsors](https://img.shields.io/github/sponsors/mobizt?logo=github)](https://github.com/sponsors/mobizt) -Revision `2024-06-25T01:54:21Z` +Revision `2024-06-25T05:25:05Z` ## Table of Contents @@ -507,7 +507,7 @@ In case using ESP8266 without `PSRAM` and you want to reduce the memory usage, y Note that, because the receive buffer size was set to minimum safe value, 1024, the large server response may not be able to handle. > [!WARNING] -> In ESP32, `WiFiClient` and `WiFiClientSecure` classes are unable to detect the server disconnection in case server session time out and the TCP session was kept alive for reuse in most tasks this library. The server session timed out will not happen if data was sent or received within the server time out period. User have to close the session manually by calling `WiFiClient::stop()` or `WiFiClientSecure::stop()` when no data was sent/received within 2-3 minutes before starting the new HTTP request. +> In ESP32, `WiFiClient` and `WiFiClientSecure` classes are unable to detect the server disconnection in case server session time out and the TCP session was kept alive for reuse in most tasks this library. The server session timed out will not happen if data was sent or received within the server time out period. The TCP session timeout in seconds in this library can be set via macro or build flag `FIREBASE_SESSION_TIMEOUT`. - ### Async Client @@ -2963,6 +2963,7 @@ ENABLE_ASYNC_TCP_CLIENT // For Async TCP Client usage. FIREBASE_ASYNC_QUEUE_LIMIT // For maximum async queue limit setting for an async client. FIREBASE_PRINTF_PORT // For Firebase.printf debug port. FIREBASE_PRINTF_BUFFER // Firebase.printf buffer size. +FIREBASE_SESSION_TIMEOUT // For TCP session timeout in seconds. The default TCP session timeout is 180 seconds (3 minutes) ``` You can assign the optional build options using one of the following methods. diff --git a/library.json b/library.json index 0b16b7e..107b7bc 100644 --- a/library.json +++ b/library.json @@ -1,6 +1,6 @@ { "name": "FirebaseClient", - "version": "1.2.15", + "version": "1.2.16", "keywords": "communication, REST, esp32, esp8266, arduino", "description": "Async Firebase Client library for Arduino.", "repository": { diff --git a/library.properties b/library.properties index f99224b..f8174df 100644 --- a/library.properties +++ b/library.properties @@ -1,6 +1,6 @@ name=FirebaseClient -version=1.2.15 +version=1.2.16 author=Mobizt diff --git a/src/Config.h b/src/Config.h index 3433bb2..bc3aa15 100644 --- a/src/Config.h +++ b/src/Config.h @@ -126,6 +126,10 @@ * * * 🏷️ For Firebase.printf buffer size. * #define FIREBASE_PRINTF_BUFFER 2048 + * + * * 🏷️ For TCP session timeout in seconds. + * The default TCP session timeout is 180 seconds (3 minutes) + * #define FIREBASE_SESSION_TIMEOUT */ #if __has_include("UserConfig.h") diff --git a/src/core/AsyncClient/AsyncClient.h b/src/core/AsyncClient/AsyncClient.h index e28a34a..24610d8 100644 --- a/src/core/AsyncClient/AsyncClient.h +++ b/src/core/AsyncClient/AsyncClient.h @@ -183,7 +183,7 @@ class AsyncClientClass : public ResultBase, RTDBResultBase AsyncResult *refResult = nullptr; AsyncResult aResult; int netErrState = 0; - uint32_t auth_ts = 0; + uint32_t auth_ts = 0, conn_ts = 0; uint32_t cvec_addr = 0; uint32_t result_addr = 0; uint32_t sync_send_timeout_sec = 0, sync_read_timeout_sec = 0; @@ -234,7 +234,13 @@ class AsyncClientClass : public ResultBase, RTDBResultBase void newCon(async_data_item_t *sData, const char *host, uint16_t port) { - if ((sse && !sData->sse) || (!sse && sData->sse) || (sData->auth_used && sData->state == async_state_undefined) || +#if defined(FIREBASE_SESSION_TIMEOUT) + int session_timeout_msec = FIREBASE_SESSION_TIMEOUT * 60 * 1000; +#else + int session_timeout_msec = 3 * 60 * 1000; +#endif + + if ((!sData->sse && conn_ts > 0 && millis() - conn_ts > session_timeout_msec) || (sse && !sData->sse) || (!sse && sData->sse) || (sData->auth_used && sData->state == async_state_undefined) || strcmp(this->host.c_str(), host) != 0 || this->port != port) { stop(sData); @@ -1425,6 +1431,10 @@ class AsyncClientClass : public ResultBase, RTDBResultBase this->host = host; this->port = port; + + if (client && !client->connected()) + conn_ts = millis(); + return sData->return_type; } @@ -2001,6 +2011,7 @@ class AsyncClientClass : public ResultBase, RTDBResultBase clear(host); port = 0; + conn_ts = 0; } async_data_item_t *createSlot(slot_options_t &options) diff --git a/src/core/Core.h b/src/core/Core.h index fd51194..fc84a87 100644 --- a/src/core/Core.h +++ b/src/core/Core.h @@ -7,7 +7,7 @@ #undef FIREBASE_CLIENT_VERSION #endif -#define FIREBASE_CLIENT_VERSION "1.2.15" +#define FIREBASE_CLIENT_VERSION "1.2.16" static void sys_idle() {