Skip to content

Commit 673fbe9

Browse files
authored
Fixed handling of upstream disconnects.
# Changes - Increased the socket receive timeout to better allow for variable data latency - Increased the default connection timeout in the C++ client # Fixes - Fixed handling of upstream socket disconnects in POSIX (behavioral difference between POSIX and FreeRTOS) Merge pull request #40.
2 parents d5f1388 + dcf96f8 commit 673fbe9

3 files changed

Lines changed: 40 additions & 14 deletions

File tree

c/src/point_one/polaris/polaris.c

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -490,21 +490,41 @@ int Polaris_Work(PolarisContext_t* context) {
490490
recv(context->socket, context->recv_buffer, POLARIS_RECV_BUFFER_SIZE, 0);
491491
#endif
492492

493+
#ifdef P1_FREERTOS
494+
// FreeRTOS does not use errno for recv() errors, but instead returns the
495+
// equivalent error value. On a socket timeout, it returns 0 instead of
496+
// ETIMEDOUT.
497+
//
498+
// We try to mimick the actual POSIX behavior to simplify the code below.
499+
if (bytes_read == 0) {
500+
bytes_read = -1;
501+
errno = ETIMEDOUT;
502+
} else if (bytes_read < 0) {
503+
errno = (int)bytes_read;
504+
}
505+
#endif
506+
493507
if (bytes_read < 0) {
494508
if (errno == EAGAIN || errno == ETIMEDOUT) {
495509
P1_DebugPrint("Socket timed out.\n");
496510
return 0;
497511
} else {
498-
P1_DebugPrint(
499-
"Connection terminated. [ret=%d, errno=%d, "
500-
"disconnected=%d]\n",
501-
(int)bytes_read, errno, context->disconnected);
502-
CloseSocket(context, 1);
512+
// Typically ENOTCONN, but could be another error condition.
513+
int ret;
503514
if (context->disconnected) {
504-
return 0;
515+
P1_DebugPrint(
516+
"Connection terminated by user request. [ret=%d, errno=%d]\n",
517+
(int)bytes_read, errno);
518+
ret = 0;
505519
} else {
506-
return POLARIS_CONNECTION_CLOSED;
520+
P1_DebugPrint(
521+
"Connection terminated upstream. [ret=%d, errno=%d]\n",
522+
(int)bytes_read, errno);
523+
ret = POLARIS_CONNECTION_CLOSED;
507524
}
525+
526+
CloseSocket(context, 1);
527+
return ret;
508528
}
509529
} else if (bytes_read == 0) {
510530
// If recv() times out before we've gotten anything, the socket was probably
@@ -516,11 +536,17 @@ int Polaris_Work(PolarisContext_t* context) {
516536
CloseSocket(context, 1);
517537
return POLARIS_FORBIDDEN;
518538
}
519-
// Otherwise, there may just not be new data available (e.g., user hasn't
520-
// sent a position yet, network connection temporarily broken, etc.).
539+
// A 0 return means the socket had an "orderly shutdown", i.e., it was
540+
// either closed by user request or disconnected upstream by the Polaris
541+
// service.
521542
else {
522-
P1_DebugPrint("Received 0 bytes/socket timed out.\n");
523-
return 0;
543+
if (context->disconnected) {
544+
P1_DebugPrint("Connection terminated by user request.\n");
545+
} else {
546+
P1_DebugPrint("Connection terminated upstream.\n");
547+
}
548+
CloseSocket(context, 1);
549+
return POLARIS_CONNECTION_CLOSED;
524550
}
525551
} else {
526552
P1_DebugPrint("Received %u bytes.\n", (unsigned)bytes_read);

c/src/point_one/polaris/polaris.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
* single receive call (@ref Polaris_Work()).
5959
*/
6060
#ifndef POLARIS_RECV_TIMEOUT_MS
61-
# define POLARIS_RECV_TIMEOUT_MS 1000
61+
# define POLARIS_RECV_TIMEOUT_MS 5000
6262
#endif
6363

6464
/**

src/point_one/polaris/polaris_client.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ class PolarisClient {
4949

5050
void RequestBeacon(const std::string& beacon_id);
5151

52-
void Run(double timeout_sec = 15.0);
52+
void Run(double timeout_sec = 30.0);
5353

54-
void RunAsync(double timeout_sec = 15.0);
54+
void RunAsync(double timeout_sec = 30.0);
5555

5656
void Disconnect();
5757

0 commit comments

Comments
 (0)