Skip to content

Commit

Permalink
Improve ENet socket error propagation for better debuggability
Browse files Browse the repository at this point in the history
  • Loading branch information
cgutman committed Dec 22, 2023
1 parent 3ed3ba6 commit 3aae4cd
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/ControlStream.c
Original file line number Diff line number Diff line change
Expand Up @@ -1068,6 +1068,10 @@ static void controlReceiveThreadFunc(void* context) {
}

if (err < 0) {
// The error from serviceEnetHost() should be propagated via LastSocketError()
LC_ASSERT(err == -1);

err = LastSocketFail();
Limelog("Control stream connection failed: %d\n", err);
ListenerCallbacks.connectionTerminated(err);
return;
Expand Down
7 changes: 6 additions & 1 deletion src/Misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@
static int serviceEnetHostInternal(ENetHost* client, ENetEvent* event, enet_uint32 timeoutMs, bool ignoreInterrupts) {
int ret;

// Clear the last socket error to ensure the caller doesn't read a stale error upon a
// failure in non-socket-related processing in enet_host_service()
SetLastSocketError(0);

// We need to call enet_host_service() multiple times to make sure retransmissions happen
for (;;) {
int selectedTimeout = timeoutMs < ENET_INTERNAL_TIMEOUT_MS ? timeoutMs : ENET_INTERNAL_TIMEOUT_MS;

// We want to report an interrupt event if we are able to read data
if (!ignoreInterrupts && ConnectionInterrupted) {
Limelog("ENet wait interrupted\n");
SetLastSocketError(EINTR);
ret = -1;
break;
}
Expand Down Expand Up @@ -67,7 +72,7 @@ int gracefullyDisconnectEnetPeer(ENetHost* host, ENetPeer* peer, enet_uint32 lin
Limelog("Timed out waiting for ENet peer to acknowledge disconnection\n");
}
else {
Limelog("Failed to receive ENet peer disconnection acknowledgement\n");
Limelog("Failed to receive ENet peer disconnection acknowledgement: %d\n", LastSocketFail());
}

return -1;
Expand Down
6 changes: 3 additions & 3 deletions src/RtspConnection.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ static bool transactRtspMessageEnet(PRTSP_MESSAGE request, PRTSP_MESSAGE respons
// Wait for a reply
if (serviceEnetHost(client, &event, RTSP_RECEIVE_TIMEOUT_SEC * 1000) <= 0 ||
event.type != ENET_EVENT_TYPE_RECEIVE) {
Limelog("Failed to receive RTSP reply\n");
Limelog("Failed to receive RTSP reply: %d\n", LastSocketFail());
goto Exit;
}

Expand All @@ -165,7 +165,7 @@ static bool transactRtspMessageEnet(PRTSP_MESSAGE request, PRTSP_MESSAGE respons
// The payload comes in a second packet
if (serviceEnetHost(client, &event, RTSP_RECEIVE_TIMEOUT_SEC * 1000) <= 0 ||
event.type != ENET_EVENT_TYPE_RECEIVE) {
Limelog("Failed to receive RTSP reply payload\n");
Limelog("Failed to receive RTSP reply payload: %d\n", LastSocketFail());
goto Exit;
}

Expand Down Expand Up @@ -851,7 +851,7 @@ int performRtspHandshake(PSERVER_INFORMATION serverInfo) {
// Wait for the connect to complete
if (serviceEnetHost(client, &event, RTSP_CONNECT_TIMEOUT_SEC * 1000) <= 0 ||
event.type != ENET_EVENT_TYPE_CONNECT) {
Limelog("RTSP: Failed to connect to UDP port %u\n", RtspPortNumber);
Limelog("RTSP: Failed to connect to UDP port %u: error %d\n", RtspPortNumber, LastSocketFail());
enet_peer_reset(peer);
peer = NULL;
enet_host_destroy(client);
Expand Down

0 comments on commit 3aae4cd

Please sign in to comment.