Skip to content

Commit 5c3bd53

Browse files
committed
TcpServer: Move the client handling into tcpServerClientUpdate
1 parent 54c0955 commit 5c3bd53

File tree

1 file changed

+114
-107
lines changed

1 file changed

+114
-107
lines changed

Firmware/RTK_Everywhere/TcpServer.ino

Lines changed: 114 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,119 @@ int32_t tcpServerSendData(uint16_t dataHead)
319319
// TCP Server Routines
320320
//----------------------------------------
321321

322+
//----------------------------------------
323+
// Update the TCP server client state
324+
//----------------------------------------
325+
void tcpServerClientUpdate(uint8_t index)
326+
{
327+
bool clientConnected;
328+
bool dataSent;
329+
char response[512];
330+
int spot;
331+
332+
// Determine if the client data structure is in use
333+
if (tcpServerClientConnected & (1 << index))
334+
{
335+
// Data structure in use
336+
// Check for a working TCP server client connection
337+
clientConnected = tcpServerClient[index]->connected();
338+
dataSent = ((millis() - tcpServerTimer) < TCP_SERVER_CLIENT_DATA_TIMEOUT) ||
339+
(tcpServerClientDataSent & (1 << index));
340+
if (clientConnected && dataSent)
341+
{
342+
// Display this client connection
343+
if (PERIODIC_DISPLAY(PD_TCP_SERVER_DATA) && (!inMainMenu))
344+
{
345+
PERIODIC_CLEAR(PD_TCP_SERVER_DATA);
346+
systemPrintf("%s client %d connected to %s\r\n",
347+
tcpServerName, index,
348+
tcpServerClientIpAddress[index].toString().c_str());
349+
}
350+
}
351+
352+
// Shutdown the TCP server client link
353+
else
354+
tcpServerStopClient(index);
355+
}
356+
357+
// Determine if the client data structure is in use
358+
if (!(tcpServerClientConnected & (1 << index)))
359+
{
360+
if(tcpServerClient[index] == nullptr)
361+
tcpServerClient[index] = new NetworkClient;
362+
363+
// Data structure not in use
364+
// Check for another TCP server client
365+
*tcpServerClient[index] = tcpServer->accept();
366+
367+
// Exit if no TCP server client found
368+
if (*tcpServerClient[index])
369+
{
370+
// TCP server client found
371+
// Start processing the new TCP server client connection
372+
tcpServerClientIpAddress[index] = tcpServerClient[index]->remoteIP();
373+
374+
if ((settings.debugTcpServer || PERIODIC_DISPLAY(PD_TCP_SERVER_DATA)) && (!inMainMenu))
375+
{
376+
PERIODIC_CLEAR(PD_TCP_SERVER_DATA);
377+
systemPrintf("%s client %d connected to %s\r\n",
378+
tcpServerName, index,
379+
tcpServerClientIpAddress[index].toString().c_str());
380+
}
381+
382+
// If we are acting as an NTRIP Caster, intercept the initial communication from the client
383+
// and respond accordingly
384+
if (tcpServerInCasterMode)
385+
{
386+
// Read response from client
387+
spot = 0;
388+
while (tcpServerClient[index]->available())
389+
{
390+
response[spot++] = tcpServerClient[index]->read();
391+
if (spot == sizeof(response))
392+
spot = 0; // Wrap
393+
}
394+
response[spot] = '\0'; // Terminate string
395+
396+
if (strnstr(response, "GET / ", sizeof(response)) != NULL) // No mount point in header
397+
{
398+
if (settings.debugTcpServer)
399+
systemPrintln("Mount point table requested.");
400+
401+
// Respond with a single mountpoint
402+
const char fakeSourceTable[] =
403+
"SOURCETABLE 200 OK\r\nServer: SparkPNT Caster/1.0\r\nContent-Type: "
404+
"text/plain\r\nContent-Length: 96\r\n\r\nSTR;SparkBase;none;RTCM "
405+
"3.0;none;none;none;none;none;none;none;none;none;none;none;B;N;none;none";
406+
407+
tcpServerClient[index]->write(fakeSourceTable, strlen(fakeSourceTable));
408+
409+
tcpServerStopClient(index); // Disconnect from client
410+
}
411+
else if (strnstr(response, "GET /", sizeof(response)) != NULL) // Mount point in header
412+
{
413+
// NTRIP Client is sending us their mount point. Begin sending RTCM.
414+
if (settings.debugTcpServer)
415+
systemPrintln("NTRIP Client connected - Sending ICY 200 OK");
416+
417+
char confirmConnection[] = "ICY 200 OK\r\n";
418+
tcpServerClient[index]->write(confirmConnection, strlen(confirmConnection));
419+
}
420+
else
421+
{
422+
// Unknown response
423+
if (settings.debugTcpServer)
424+
systemPrintf("Unknown response: %s\r\n", response);
425+
}
426+
} // tcpServerInCasterMode
427+
428+
// Make client online after any NTRIP injections so ring buffer can start outputting data to it
429+
tcpServerClientConnected = tcpServerClientConnected | (1 << index);
430+
tcpServerClientDataSent = tcpServerClientDataSent | (1 << index);
431+
}
432+
}
433+
}
434+
322435
//----------------------------------------
323436
// Update the state of the TCP server state machine
324437
//----------------------------------------
@@ -467,9 +580,7 @@ void tcpServerStopClient(int index)
467580
//----------------------------------------
468581
void tcpServerUpdate()
469582
{
470-
bool clientConnected;
471583
bool connected;
472-
bool dataSent;
473584
bool enabled;
474585
int index;
475586
IPAddress ipAddress;
@@ -564,111 +675,7 @@ void tcpServerUpdate()
564675
// Walk the list of TCP server clients
565676
for (index = 0; index < TCP_SERVER_MAX_CLIENTS; index++)
566677
{
567-
// Determine if the client data structure is in use
568-
if (tcpServerClientConnected & (1 << index))
569-
{
570-
// Data structure in use
571-
// Check for a working TCP server client connection
572-
clientConnected = tcpServerClient[index]->connected();
573-
dataSent = ((millis() - tcpServerTimer) < TCP_SERVER_CLIENT_DATA_TIMEOUT) ||
574-
(tcpServerClientDataSent & (1 << index));
575-
if (clientConnected && dataSent)
576-
{
577-
// Display this client connection
578-
if (PERIODIC_DISPLAY(PD_TCP_SERVER_DATA) && (!inMainMenu))
579-
{
580-
PERIODIC_CLEAR(PD_TCP_SERVER_DATA);
581-
systemPrintf("%s client %d connected to %s\r\n",
582-
tcpServerName, index,
583-
tcpServerClientIpAddress[index].toString().c_str());
584-
}
585-
}
586-
587-
// Shutdown the TCP server client link
588-
else
589-
tcpServerStopClient(index);
590-
}
591-
}
592-
593-
// Walk the list of TCP server clients
594-
for (index = 0; index < TCP_SERVER_MAX_CLIENTS; index++)
595-
{
596-
// Determine if the client data structure is in use
597-
if (!(tcpServerClientConnected & (1 << index)))
598-
{
599-
if(tcpServerClient[index] == nullptr)
600-
tcpServerClient[index] = new NetworkClient;
601-
602-
// Data structure not in use
603-
// Check for another TCP server client
604-
*tcpServerClient[index] = tcpServer->accept();
605-
606-
// Exit if no TCP server client found
607-
if (! *tcpServerClient[index])
608-
break;
609-
610-
// Start processing the new TCP server client connection
611-
tcpServerClientIpAddress[index] = tcpServerClient[index]->remoteIP();
612-
613-
if ((settings.debugTcpServer || PERIODIC_DISPLAY(PD_TCP_SERVER_DATA)) && (!inMainMenu))
614-
{
615-
PERIODIC_CLEAR(PD_TCP_SERVER_DATA);
616-
systemPrintf("%s client %d connected to %s\r\n",
617-
tcpServerName, index,
618-
tcpServerClientIpAddress[index].toString().c_str());
619-
}
620-
621-
// If we are acting as an NTRIP Caster, intercept the initial communication from the client
622-
// and respond accordingly
623-
if (tcpServerInCasterMode)
624-
{
625-
// Read response from client
626-
char response[512];
627-
int spot = 0;
628-
while (tcpServerClient[index]->available())
629-
{
630-
response[spot++] = tcpServerClient[index]->read();
631-
if (spot == sizeof(response))
632-
spot = 0; // Wrap
633-
}
634-
response[spot] = '\0'; // Terminate string
635-
636-
if (strnstr(response, "GET / ", sizeof(response)) != NULL) // No mount point in header
637-
{
638-
if (settings.debugTcpServer)
639-
systemPrintln("Mount point table requested.");
640-
641-
// Respond with a single mountpoint
642-
const char fakeSourceTable[] =
643-
"SOURCETABLE 200 OK\r\nServer: SparkPNT Caster/1.0\r\nContent-Type: "
644-
"text/plain\r\nContent-Length: 96\r\n\r\nSTR;SparkBase;none;RTCM "
645-
"3.0;none;none;none;none;none;none;none;none;none;none;none;B;N;none;none";
646-
647-
tcpServerClient[index]->write(fakeSourceTable, strlen(fakeSourceTable));
648-
649-
tcpServerStopClient(index); // Disconnect from client
650-
}
651-
else if (strnstr(response, "GET /", sizeof(response)) != NULL) // Mount point in header
652-
{
653-
// NTRIP Client is sending us their mount point. Begin sending RTCM.
654-
if (settings.debugTcpServer)
655-
systemPrintln("NTRIP Client connected - Sending ICY 200 OK");
656-
657-
char confirmConnection[] = "ICY 200 OK\r\n";
658-
tcpServerClient[index]->write(confirmConnection, strlen(confirmConnection));
659-
}
660-
else
661-
{
662-
// Unknown response
663-
if (settings.debugTcpServer)
664-
systemPrintf("Unknown response: %s\r\n", response);
665-
}
666-
} // tcpServerInCasterMode
667-
668-
// Make client online after any NTRIP injections so ring buffer can start outputting data to it
669-
tcpServerClientConnected = tcpServerClientConnected | (1 << index);
670-
tcpServerClientDataSent = tcpServerClientDataSent | (1 << index);
671-
}
678+
tcpServerClientUpdate(index);
672679
}
673680

674681
// Check for data moving across the connections

0 commit comments

Comments
 (0)