@@ -137,35 +137,29 @@ void tcpServerDiscardBytes(RING_BUFFER_OFFSET previousTail, RING_BUFFER_OFFSET n
137137// ----------------------------------------
138138int32_t tcpServerClientSendData (int index, uint8_t *data, uint16_t length)
139139{
140- length = tcpServerClient[index]->write (data, length);
141- if (length > 0 )
140+ if (tcpServerClient[index])
142141 {
143- // Update the data sent flag when data successfully sent
142+ length = tcpServerClient[index]-> write ( data, length);
144143 if (length > 0 )
145- tcpServerClientDataSent = tcpServerClientDataSent | (1 << index);
146- if ((settings.debugTcpServer || PERIODIC_DISPLAY (PD_TCP_SERVER_CLIENT_DATA)) && (!inMainMenu))
147144 {
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+ }
151154 }
152- }
153155
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
159158 {
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 ;
163162 }
164-
165- tcpServerClient[index]->stop ();
166- tcpServerClientConnected = tcpServerClientConnected & (~(1 << index));
167- tcpServerClientWriteError = tcpServerClientWriteError | (1 << index);
168- length = 0 ;
169163 }
170164 return length;
171165}
@@ -322,6 +316,9 @@ void tcpServerStop()
322316 // Notify the rest of the system that the TCP server is shutting down
323317 if (online.tcpServer )
324318 {
319+ if (settings.debugTcpServer && (!inMainMenu))
320+ systemPrintf (" TcpServer: Notifying GNSS UART task to stop sending data\r\n " );
321+
325322 // Notify the GNSS UART tasks of the TCP server shutdown
326323 online.tcpServer = false ;
327324 delay (5 );
@@ -340,13 +337,15 @@ void tcpServerStop()
340337 {
341338 // Stop the TCP server
342339 if (settings.debugTcpServer && (!inMainMenu))
343- systemPrintln (" TCP server stopping " );
340+ systemPrintln (" TcpServer: Stopping the server " );
344341 tcpServer->stop ();
345342 delete tcpServer;
346343 tcpServer = nullptr ;
347344 }
348345
349346 // Stop using the network
347+ if (settings.debugTcpServer && (!inMainMenu))
348+ systemPrintln (" TcpServer: Stopping network consumers" );
350349 networkConsumerOffline (NETCONSUMER_TCP_SERVER);
351350 if (tcpServerState != TCP_SERVER_STATE_OFF)
352351 {
@@ -367,25 +366,32 @@ void tcpServerStopClient(int index)
367366 bool connected;
368367 bool dataSent;
369368
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] )
372371 {
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+ }
386389
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+ }
389395 tcpServerClientConnected = tcpServerClientConnected & (~(1 << index));
390396 tcpServerClientWriteError = tcpServerClientWriteError & (~(1 << index));
391397}
0 commit comments