Skip to content

Commit 1cd6feb

Browse files
committed
refactoring
1 parent 388ba75 commit 1cd6feb

File tree

4 files changed

+25
-24
lines changed

4 files changed

+25
-24
lines changed

src/core/AppBase.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class AppBase
4242
void resultSetDebug(AsyncResult *aResult, app_debug_t *app_debug) { aResult->app_debug = app_debug; }
4343
void resultSetEvent(AsyncResult *aResult, app_event_t *app_event) { aResult->app_event = app_event; }
4444
app_event_t *getAppEvent(AsyncClientClass *aClient) { return &aClient->app_event; }
45-
void stopAsync(AsyncClientClass *aClient, async_data *sData) { aClient->stop(sData); }
45+
void stopAsync(AsyncClientClass *aClient) { aClient->stop(); }
4646
async_data *createSlotBase(AsyncClientClass *aClient, slot_options_t &soption)
4747
{
4848
if (aClient)

src/core/AsyncClient/AsyncClient.h

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,6 @@ class AsyncClientClass : public ResultBase, RTDBResultBase
163163
uint32_t addr = 0, auth_ts = 0, cvec_addr = 0, result_addr = 0, sync_send_timeout_sec = 0, sync_read_timeout_sec = 0, session_timeout_sec = 0;
164164
Timer session_timer;
165165
Client *client = nullptr;
166-
bool client_changed = false, network_changed = false;
167166
#if defined(ENABLE_ASYNC_TCP_CLIENT)
168167
AsyncTCPConfig *atcp_config = nullptr;
169168
#else
@@ -188,7 +187,7 @@ class AsyncClientClass : public ResultBase, RTDBResultBase
188187
if ((!sData->sse && session_timeout_sec >= FIREBASE_SESSION_TIMEOUT_SEC && session_timer.remaining() == 0) || (sse && !sData->sse) || (!sse && sData->sse) || (sData->auth_used && sData->state == astate_undefined) ||
189188
strcmp(this->host.c_str(), host) != 0 || this->port != port)
190189
{
191-
stop(sData);
190+
stop();
192191
getResult()->clear();
193192
}
194193

@@ -867,7 +866,6 @@ class AsyncClientClass : public ResultBase, RTDBResultBase
867866
sData->request.file_data.resumable.updateRange();
868867
}
869868
#endif
870-
871869
if (sData->request.method == reqns::http_delete && sData->response.httpCode == FIREBASE_ERROR_HTTP_CODE_NO_CONTENT)
872870
setDebugBase(app_debug, FPSTR("Delete operation complete"));
873871
}
@@ -885,7 +883,6 @@ class AsyncClientClass : public ResultBase, RTDBResultBase
885883
// the next chunk data is the payload
886884
if (sData->response.httpCode != FIREBASE_ERROR_HTTP_CODE_NO_CONTENT)
887885
{
888-
889886
if (sData->response.flags.chunks)
890887
{
891888
// Use temporary String buffer for decodeChunks
@@ -1084,7 +1081,7 @@ class AsyncClientClass : public ResultBase, RTDBResultBase
10841081
}
10851082

10861083
if (sData->response.flags.chunks && sData->auth_used)
1087-
stop(sData);
1084+
stop();
10881085

10891086
// HTTP server error.
10901087
if (sData->response.httpCode >= FIREBASE_ERROR_HTTP_CODE_BAD_REQUEST)
@@ -1172,7 +1169,7 @@ class AsyncClientClass : public ResultBase, RTDBResultBase
11721169
void reset(async_data *sData, bool disconnect)
11731170
{
11741171
if (disconnect)
1175-
stop(sData);
1172+
stop();
11761173

11771174
sData->response.httpCode = 0;
11781175
sData->error.code = 0;
@@ -1353,17 +1350,14 @@ class AsyncClientClass : public ResultBase, RTDBResultBase
13531350
inStopAsync = false;
13541351
}
13551352

1356-
void stop(async_data *sData)
1353+
void stop()
13571354
{
13581355
if (conn.isConnected())
13591356
setDebugBase(app_debug, FPSTR("Terminating the server connection..."));
13601357

13611358
conn.stop();
1362-
13631359
clear(host);
13641360
this->port = 0;
1365-
client_changed = false;
1366-
network_changed = false;
13671361
}
13681362

13691363
async_data *createSlot(slot_options_t &options)
@@ -1549,9 +1543,9 @@ class AsyncClientClass : public ResultBase, RTDBResultBase
15491543
return exitProcess(false);
15501544

15511545
// Restart connection when authenticate, client or network changed
1552-
if ((sData->sse && sData->auth_ts != auth_ts) || client_changed || network_changed)
1546+
if ((sData->sse && sData->auth_ts != auth_ts) || conn.isChanged())
15531547
{
1554-
stop(sData);
1548+
stop();
15551549
sData->state = astate_send_header;
15561550
}
15571551

@@ -1689,16 +1683,14 @@ class AsyncClientClass : public ResultBase, RTDBResultBase
16891683

16901684
~AsyncClientClass()
16911685
{
1692-
stop(nullptr);
1693-
1686+
stop();
16941687
for (size_t i = 0; i < sVec.size(); i++)
16951688
{
16961689
reset(getData(i), true);
16971690
async_data *sData = getData(i);
16981691
delete sData;
16991692
sData = nullptr;
17001693
}
1701-
17021694
addRemoveClientVec(cvec_addr, false);
17031695
}
17041696

@@ -1825,8 +1817,7 @@ class AsyncClientClass : public ResultBase, RTDBResultBase
18251817
void setNetwork(Client &client, network_config_data &net)
18261818
{
18271819
// Check client changes.
1828-
client_changed = reinterpret_cast<uint32_t>(&client) != reinterpret_cast<uint32_t>(this->client);
1829-
network_changed = true;
1820+
bool client_changed = reinterpret_cast<uint32_t>(&client) != reinterpret_cast<uint32_t>(this->client);
18301821

18311822
// Some changes, stop the current network client.
18321823
if (client_changed && this->client)
@@ -1838,7 +1829,10 @@ class AsyncClientClass : public ResultBase, RTDBResultBase
18381829

18391830
// Change the client.
18401831
if (client_changed)
1832+
{
18411833
this->client = &client;
1834+
conn.setClientChange();
1835+
}
18421836
}
18431837
};
18441838

src/core/AsyncClient/ConnectionHandler.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ struct conn_handler : public firebase_ns::ConnBase
7474
Client *client = nullptr;
7575
void *atcp_config = nullptr;
7676
firebase_ns::app_debug_t *app_debug = nullptr;
77-
bool connected = false;
77+
bool connected = false, client_changed = false, network_changed = false;
7878
int netErrState = 0;
7979

8080
public:
@@ -85,7 +85,12 @@ struct conn_handler : public firebase_ns::ConnBase
8585
this->atcp_config = atcp_config;
8686
this->app_debug = app_debug;
8787
}
88-
void setNetwork(network_config_data &net) { this->net.copy(net); }
88+
void setNetwork(network_config_data &net)
89+
{
90+
network_changed = true;
91+
this->net.copy(net);
92+
}
93+
void setClientChange() { client_changed = true; }
8994
unsigned long networkLastSeen() { return this->net.disconnected_ms; }
9095
firebase_network_type getNetworkType() { return this->net.network_type; }
9196
bool isConnected()
@@ -143,7 +148,6 @@ struct conn_handler : public firebase_ns::ConnBase
143148

144149
void stop()
145150
{
146-
147151
if (client_type == tcpc_sync)
148152
{
149153
if (client)
@@ -158,8 +162,12 @@ struct conn_handler : public firebase_ns::ConnBase
158162
}
159163

160164
this->connected = false;
165+
client_changed = false;
166+
network_changed = false;
161167
}
162168

169+
bool isChanged() { return client_changed || network_changed; }
170+
163171
void setDebug(const String &msg)
164172
{
165173
if (app_debug)
@@ -461,7 +469,6 @@ struct conn_handler : public firebase_ns::ConnBase
461469

462470
if (recon && (self_connect || net.net_timer.remaining() == 0))
463471
{
464-
465472
if (net.network_type == firebase_network_generic)
466473
{
467474
if (generic_network_owner_addr > 0 && generic_network_owner_addr != reinterpret_cast<uint32_t>(this))

src/core/FirebaseApp.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* 2025-01-25
2+
* 2025-01-27
33
*
44
* The MIT License (MIT)
55
* Copyright (c) 2025 K. Suwatchai (Mobizt)
@@ -324,7 +324,7 @@ namespace firebase_ns
324324
if (!aClient)
325325
return;
326326

327-
stopAsync(aClient, sData);
327+
stopAsync(aClient);
328328

329329
if (sData)
330330
{

0 commit comments

Comments
 (0)