Skip to content

Commit

Permalink
Fix missing of stream time out callback when network disconnected.
Browse files Browse the repository at this point in the history
  • Loading branch information
mobizt committed Jun 14, 2023
1 parent 8607b31 commit 4e154f0
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 21 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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 `<FirebaseData>.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 `<FirebaseData>.httpConnected()` which will return true when connection to the server is still alive.
Expand Down
1 change: 1 addition & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ clear KEYWORD2
fileTransferError KEYWORD2
payload KEYWORD2
keepAlive KEYWORD2
isKeepAlive KEYWORD2
dataTypeEnum KEYWORD2
queryFilter KEYWORD2
empty KEYWORD2
Expand Down
2 changes: 0 additions & 2 deletions src/Firebase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down
11 changes: 11 additions & 0 deletions src/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
8 changes: 5 additions & 3 deletions src/rtdb/FB_RTDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;

Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/rtdb/FB_RTDB.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 6 additions & 4 deletions src/session/FB_Session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -588,9 +588,6 @@ bool FirebaseData::httpConnected()
if (tcpClient.isKeepAliveSet())
session.connected = tcpClient.connected();

if (!session.connected)
closeSession();

return session.connected;
}

Expand All @@ -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
Expand Down
8 changes: 7 additions & 1 deletion src/session/FB_Session.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 5 additions & 3 deletions src/signer/Signer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions src/signer/Signer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 */
Expand Down
5 changes: 5 additions & 0 deletions src/wcs/base/FB_TCP_Client_Base.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; };
Expand Down Expand Up @@ -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.
Expand Down
9 changes: 5 additions & 4 deletions src/wcs/esp32/FB_TCP_Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 4e154f0

Please sign in to comment.