From 4e154f04dfbd6c26d42181875f4e5936edadd36f Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 14 Jun 2023 10:25:58 +0700 Subject: [PATCH] Fix missing of stream time out callback when network disconnected. --- README.md | 7 ++++++- keywords.txt | 1 + src/Firebase.cpp | 2 -- src/README.md | 11 +++++++++++ src/rtdb/FB_RTDB.cpp | 8 +++++--- src/rtdb/FB_RTDB.h | 2 +- src/session/FB_Session.cpp | 10 ++++++---- src/session/FB_Session.h | 8 +++++++- src/signer/Signer.cpp | 8 +++++--- src/signer/Signer.h | 4 ++-- src/wcs/base/FB_TCP_Client_Base.h | 5 +++++ src/wcs/esp32/FB_TCP_Client.cpp | 9 +++++---- 12 files changed, 54 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 9f068f88..296520fd 100644 --- a/README.md +++ b/README.md @@ -563,7 +563,7 @@ ENABLE_OTA_FIRMWARE_UPDATE ### About FirebaseData object -`FirebaseData` class used as the application and user data container. It used widely in this library to handle everything related to data in the server/client transmission. +`FirebaseData` class used as the application and user data container. It used widely in this library to handle everything related to data in the server/client data transmission. The WiFiClientSecure instance was created in `FirebaseData` object when connecting to server. The response payload will store in this object that allows user to acquire and process leter. @@ -585,8 +585,13 @@ Ex. ```cpp fbdo.keepAlive(5 /* tcp KeepAlive idle 5 seconds */, 5 /* tcp KeeAalive interval 5 seconds */, 1 /* tcp KeepAlive count 1 */); + +// If one of three arguments is zero, the KeepAlive will be disabled. ``` +To check the KeepAlive status, use `.isKeepAlive`. + + For the TCP (KeepAlive) options, see [here](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/lwip.html#tcp-options). You can check the server connecting status, by exexuting `.httpConnected()` which will return true when connection to the server is still alive. diff --git a/keywords.txt b/keywords.txt index 8ccce992..f961af2d 100644 --- a/keywords.txt +++ b/keywords.txt @@ -201,6 +201,7 @@ clear KEYWORD2 fileTransferError KEYWORD2 payload KEYWORD2 keepAlive KEYWORD2 +isKeepAlive KEYWORD2 dataTypeEnum KEYWORD2 queryFilter KEYWORD2 empty KEYWORD2 diff --git a/src/Firebase.cpp b/src/Firebase.cpp index df465cc0..8314bf1b 100644 --- a/src/Firebase.cpp +++ b/src/Firebase.cpp @@ -147,8 +147,6 @@ bool FIREBASE_CLASS::ready() // non-stream used session will stop if (Signer.isExpired() && fbdo && !fbdo->tcpClient.reserved && fbdo->session.con_mode != fb_esp_con_mode_rtdb_stream) fbdo->closeSession(); - - fbdo->httpConnected(); } } diff --git a/src/README.md b/src/README.md index 38f27d75..8c01b0bc 100644 --- a/src/README.md +++ b/src/README.md @@ -3577,6 +3577,17 @@ void keepAlive(int tcpKeepIdleSeconds, int tcpKeepIntervalSeconds, int tcpKeepCo + +#### Get TCP KeepAlive status. + +return **`Boolean`** status of TCP Keepalive. + +```cpp +bool isKeepAlive(); +``` + + + ## Firebase Cloud Messaging Object Functions diff --git a/src/rtdb/FB_RTDB.cpp b/src/rtdb/FB_RTDB.cpp index ac6609b7..03f574ba 100644 --- a/src/rtdb/FB_RTDB.cpp +++ b/src/rtdb/FB_RTDB.cpp @@ -8,7 +8,7 @@ * * This library supports Espressif ESP8266, ESP32 and RP2040 Pico * - * Created June 9, 2023 + * Created June 14, 2023 * * This work is a part of Firebase ESP Client library * Copyright (c) 2023 K. Suwatchai (Mobizt) @@ -639,7 +639,7 @@ bool FB_RTDB::handleStreamRead(FirebaseData *fbdo) if (fbdo->tcpClient.reserved) return false; - // prevent redundant calling + // prevent nested calling if (fbdo->session.streaming) return false; @@ -648,7 +648,9 @@ bool FB_RTDB::handleStreamRead(FirebaseData *fbdo) if (fbdo->session.rtdb.pause || fbdo->session.rtdb.stream_stop) return exitStream(fbdo, true); - if (!fbdo->tokenReady()) + // Check token status via checkToken(). + // Don't check from tokenReady() as it depends on network status too. + if (!Signer.checkToken()) return exitStream(fbdo, false); bool ret = false; diff --git a/src/rtdb/FB_RTDB.h b/src/rtdb/FB_RTDB.h index b7e7b1c6..9299bc0b 100644 --- a/src/rtdb/FB_RTDB.h +++ b/src/rtdb/FB_RTDB.h @@ -8,7 +8,7 @@ * * This library supports Espressif ESP8266, ESP32 and RP2040 Pico * - * Created June 9, 2023 + * Created June 14, 2023 * * This work is a part of Firebase ESP Client library * Copyright (c) 2023 K. Suwatchai (Mobizt) diff --git a/src/session/FB_Session.cpp b/src/session/FB_Session.cpp index 83a94c11..61334311 100644 --- a/src/session/FB_Session.cpp +++ b/src/session/FB_Session.cpp @@ -8,7 +8,7 @@ * * This library supports Espressif ESP8266, ESP32 and RP2040 Pico * - * Created June 9, 2023 + * Created June 14, 2023 * * This work is a part of Firebase ESP Client library * Copyright (c) 2023 K. Suwatchai (Mobizt) @@ -588,9 +588,6 @@ bool FirebaseData::httpConnected() if (tcpClient.isKeepAliveSet()) session.connected = tcpClient.connected(); - if (!session.connected) - closeSession(); - return session.connected; } @@ -612,6 +609,11 @@ void FirebaseData::keepAlive(int tcpKeepIdleSeconds, int tcpKeepIntervalSeconds, tcpClient.keepAlive(tcpKeepIdleSeconds, tcpKeepIntervalSeconds, tcpKeepCount); } +bool FirebaseData::isKeepAlive() +{ + return tcpClient.isKeepAlive; +} + String FirebaseData::payload() { #ifdef ENABLE_RTDB diff --git a/src/session/FB_Session.h b/src/session/FB_Session.h index ada5950f..c5407c2a 100644 --- a/src/session/FB_Session.h +++ b/src/session/FB_Session.h @@ -8,7 +8,7 @@ * * This library supports Espressif ESP8266, ESP32 and RP2040 Pico * - * Created June 9, 2023 + * Created June 14, 2023 * * This work is a part of Firebase ESP Client library * Copyright (c) 2023 K. Suwatchai (Mobizt) @@ -928,6 +928,12 @@ class FirebaseData */ void keepAlive(int tcpKeepIdleSeconds, int tcpKeepIntervalSeconds, int tcpKeepCount); + /** Get TCP KeepAlive status. + * + * @return Boolean status of TCP Keepalive. + */ + bool isKeepAlive(); + FB_TCP_CLIENT tcpClient; #if defined(FIREBASE_ESP32_CLIENT) || defined(FIREBASE_ESP8266_CLIENT) diff --git a/src/signer/Signer.cpp b/src/signer/Signer.cpp index d2e64a16..a401520f 100644 --- a/src/signer/Signer.cpp +++ b/src/signer/Signer.cpp @@ -8,7 +8,7 @@ * * This library supports Espressif ESP8266, ESP32 and Raspberry Pi Pico * - * Created June 7, 2023 + * Created June 14, 2023 * * This work is a part of Firebase ESP Client library * Copyright (c) 2023 K. Suwatchai (Mobizt) @@ -2140,13 +2140,15 @@ bool Firebase_Signer::handleEmailSending(MB_StringPtr payload, fb_esp_user_email return true; } -void Firebase_Signer::checkToken() +bool Firebase_Signer::checkToken() { if (!config || !auth) - return; + return false; if (isAuthToken(true) && isExpired()) handleToken(); + + return config->signer.tokens.status == token_status_ready; } bool Firebase_Signer::tokenReady() diff --git a/src/signer/Signer.h b/src/signer/Signer.h index 148296d3..e02996b6 100644 --- a/src/signer/Signer.h +++ b/src/signer/Signer.h @@ -8,7 +8,7 @@ * * This library supports Espressif ESP8266, ESP32 and Raspberry Pi Pico * - * Created June 7, 2023 + * Created June 14, 2023 * * This work is a part of Firebase ESP Client library * Copyright (c) 2023 K. Suwatchai (Mobizt) @@ -167,7 +167,7 @@ class Firebase_Signer /* request or refresh the token */ bool requestTokens(bool refresh); /* check the token ready status and process the token tasks */ - void checkToken(); + bool checkToken(); /* parse expiry time from string */ void getExpiration(const char *exp); /* send email */ diff --git a/src/wcs/base/FB_TCP_Client_Base.h b/src/wcs/base/FB_TCP_Client_Base.h index f030ffce..185663a1 100644 --- a/src/wcs/base/FB_TCP_Client_Base.h +++ b/src/wcs/base/FB_TCP_Client_Base.h @@ -269,9 +269,12 @@ class FB_TCP_Client_Base void keepAlive(int tcpKeepIdleSeconds, int tcpKeepIntervalSeconds, int tcpKeepCount) { +#if defined(USE_CONNECTION_KEEP_ALIVE_MODE) this->tcpKeepIdleSeconds = tcpKeepIdleSeconds; this->tcpKeepIntervalSeconds = tcpKeepIntervalSeconds; this->tcpKeepCount = tcpKeepCount; + isKeepAlive = tcpKeepIdleSeconds > 0 && tcpKeepIntervalSeconds > 0 && tcpKeepCount > 0; +#endif } bool isKeepAliveSet() { return tcpKeepIdleSeconds > -1 && tcpKeepIntervalSeconds > -1 && tcpKeepCount > -1; }; @@ -312,6 +315,8 @@ class FB_TCP_Client_Base // lwIP TCP Keepalive count. int tcpKeepCount = -1; + bool isKeepAlive = false; + // In esp8266, this is actually Arduino base Stream (char read) timeout. // This will override internally by WiFiClientSecureCtx::_connectSSL // to 5000 after SSL handshake was done with success. diff --git a/src/wcs/esp32/FB_TCP_Client.cpp b/src/wcs/esp32/FB_TCP_Client.cpp index 0f537462..cc385521 100644 --- a/src/wcs/esp32/FB_TCP_Client.cpp +++ b/src/wcs/esp32/FB_TCP_Client.cpp @@ -198,11 +198,12 @@ bool FB_TCP_Client::connect() tcpKeepIntervalSeconds = 0; tcpKeepCount = 0; } - - wcs->setOption(TCP_KEEPIDLE, &tcpKeepIdleSeconds); - wcs->setOption(TCP_KEEPINTVL, &tcpKeepIntervalSeconds); - wcs->setOption(TCP_KEEPCNT, &tcpKeepCount); + bool success = wcs->setOption(TCP_KEEPIDLE, &tcpKeepIdleSeconds) > -1 && + wcs->setOption(TCP_KEEPINTVL, &tcpKeepIntervalSeconds) > -1 && + wcs->setOption(TCP_KEEPCNT, &tcpKeepCount) > -1; + if (!success) + isKeepAlive = false; } #endif