@@ -137,35 +137,29 @@ void tcpServerDiscardBytes(RING_BUFFER_OFFSET previousTail, RING_BUFFER_OFFSET n
137
137
// ----------------------------------------
138
138
int32_t tcpServerClientSendData (int index, uint8_t *data, uint16_t length)
139
139
{
140
- length = tcpServerClient[index ]->write (data, length);
141
- if (length > 0 )
140
+ if (tcpServerClient[index ])
142
141
{
143
- // Update the data sent flag when data successfully sent
142
+ length = tcpServerClient[ index ]-> write ( data, length);
144
143
if (length > 0 )
145
- tcpServerClientDataSent = tcpServerClientDataSent | (1 << index );
146
- if ((settings.debugTcpServer || PERIODIC_DISPLAY (PD_TCP_SERVER_CLIENT_DATA)) && (!inMainMenu))
147
144
{
148
- PERIODIC_CLEAR (PD_TCP_SERVER_CLIENT_DATA);
149
- systemPrintf (" TCP server wrote %d bytes to %s\r\n " , length,
150
- tcpServerClientIpAddress[index ].toString ().c_str ());
145
+ // Update the data sent flag when data successfully sent
146
+ if (length > 0 )
147
+ tcpServerClientDataSent = tcpServerClientDataSent | (1 << index );
148
+ if ((settings.debugTcpServer || PERIODIC_DISPLAY (PD_TCP_SERVER_CLIENT_DATA)) && (!inMainMenu))
149
+ {
150
+ PERIODIC_CLEAR (PD_TCP_SERVER_CLIENT_DATA);
151
+ systemPrintf (" TCP server wrote %d bytes to %s\r\n " , length,
152
+ tcpServerClientIpAddress[index ].toString ().c_str ());
153
+ }
151
154
}
152
- }
153
155
154
- // Failed to write the data
155
- else
156
- {
157
- // Done with this client connection
158
- if ((settings.debugTcpServer || PERIODIC_DISPLAY (PD_TCP_SERVER_CLIENT_DATA)) && (!inMainMenu))
156
+ // Failed to write the data
157
+ else
159
158
{
160
- PERIODIC_CLEAR (PD_TCP_SERVER_CLIENT_DATA);
161
- systemPrintf ( " TCP server breaking connection %d with client %s \r\n " , index ,
162
- tcpServerClientIpAddress[ index ]. toString (). c_str ()) ;
159
+ // Done with this client connection
160
+ tcpServerStopClient ( index );
161
+ length = 0 ;
163
162
}
164
-
165
- tcpServerClient[index ]->stop ();
166
- tcpServerClientConnected = tcpServerClientConnected & (~(1 << index ));
167
- tcpServerClientWriteError = tcpServerClientWriteError | (1 << index );
168
- length = 0 ;
169
163
}
170
164
return length;
171
165
}
@@ -322,6 +316,9 @@ void tcpServerStop()
322
316
// Notify the rest of the system that the TCP server is shutting down
323
317
if (online.tcpServer )
324
318
{
319
+ if (settings.debugTcpServer && (!inMainMenu))
320
+ systemPrintf (" TcpServer: Notifying GNSS UART task to stop sending data\r\n " );
321
+
325
322
// Notify the GNSS UART tasks of the TCP server shutdown
326
323
online.tcpServer = false ;
327
324
delay (5 );
@@ -340,13 +337,15 @@ void tcpServerStop()
340
337
{
341
338
// Stop the TCP server
342
339
if (settings.debugTcpServer && (!inMainMenu))
343
- systemPrintln (" TCP server stopping " );
340
+ systemPrintln (" TcpServer: Stopping the server " );
344
341
tcpServer->stop ();
345
342
delete tcpServer;
346
343
tcpServer = nullptr ;
347
344
}
348
345
349
346
// Stop using the network
347
+ if (settings.debugTcpServer && (!inMainMenu))
348
+ systemPrintln (" TcpServer: Stopping network consumers" );
350
349
networkConsumerOffline (NETCONSUMER_TCP_SERVER);
351
350
if (tcpServerState != TCP_SERVER_STATE_OFF)
352
351
{
@@ -367,25 +366,32 @@ void tcpServerStopClient(int index)
367
366
bool connected;
368
367
bool dataSent;
369
368
370
- // Done with this client connection
371
- if ((settings. debugTcpServer || PERIODIC_DISPLAY (PD_TCP_SERVER_DATA)) && (!inMainMenu) )
369
+ // Determine if a client was allocated
370
+ if (tcpServerClient[ index ] )
372
371
{
373
- PERIODIC_CLEAR (PD_TCP_SERVER_DATA);
374
-
375
- // Determine the shutdown reason
376
- connected = tcpServerClient[index ]->connected () && (!(tcpServerClientWriteError & (1 << index )));
377
- dataSent =
378
- ((millis () - tcpServerTimer) < TCP_SERVER_CLIENT_DATA_TIMEOUT) || (tcpServerClientDataSent & (1 << index ));
379
- if (!dataSent)
380
- systemPrintf (" TCP Server: No data sent over %d seconds\r\n " , TCP_SERVER_CLIENT_DATA_TIMEOUT / 1000 );
381
- if (!connected)
382
- systemPrintf (" TCP Server: Link to client broken\r\n " );
383
- systemPrintf (" TCP server client %d disconnected from %s\r\n " , index ,
384
- tcpServerClientIpAddress[index ].toString ().c_str ());
385
- }
372
+ // Done with this client connection
373
+ if ((settings.debugTcpServer || PERIODIC_DISPLAY (PD_TCP_SERVER_DATA)) && (!inMainMenu))
374
+ {
375
+ PERIODIC_CLEAR (PD_TCP_SERVER_DATA);
376
+
377
+ // Determine the shutdown reason
378
+ connected = tcpServerClient[index ]->connected ()
379
+ && (!(tcpServerClientWriteError & (1 << index )));
380
+ dataSent = ((millis () - tcpServerTimer) < TCP_SERVER_CLIENT_DATA_TIMEOUT)
381
+ || (tcpServerClientDataSent & (1 << index ));
382
+ if (!dataSent)
383
+ systemPrintf (" TCP Server: No data sent over %d seconds\r\n " , TCP_SERVER_CLIENT_DATA_TIMEOUT / 1000 );
384
+ if (!connected)
385
+ systemPrintf (" TCP Server: Link to client broken\r\n " );
386
+ systemPrintf (" TCP server client %d disconnected from %s\r\n " , index ,
387
+ tcpServerClientIpAddress[index ].toString ().c_str ());
388
+ }
386
389
387
- // Shutdown the TCP server client link
388
- tcpServerClient[index ]->stop ();
390
+ // Shutdown the TCP server client link
391
+ tcpServerClient[index ]->stop ();
392
+ delete tcpServerClient[index ];
393
+ tcpServerClient[index ] = nullptr ;
394
+ }
389
395
tcpServerClientConnected = tcpServerClientConnected & (~(1 << index ));
390
396
tcpServerClientWriteError = tcpServerClientWriteError & (~(1 << index ));
391
397
}
0 commit comments