Improve ENet socket error propagation for better debuggability

This commit is contained in:
Cameron Gutman
2023-12-22 13:45:50 -06:00
parent 3ed3ba6253
commit 3aae4cdc59
3 changed files with 13 additions and 4 deletions

View File

@@ -1068,6 +1068,10 @@ static void controlReceiveThreadFunc(void* context) {
} }
if (err < 0) { 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); Limelog("Control stream connection failed: %d\n", err);
ListenerCallbacks.connectionTerminated(err); ListenerCallbacks.connectionTerminated(err);
return; return;

View File

@@ -9,6 +9,10 @@
static int serviceEnetHostInternal(ENetHost* client, ENetEvent* event, enet_uint32 timeoutMs, bool ignoreInterrupts) { static int serviceEnetHostInternal(ENetHost* client, ENetEvent* event, enet_uint32 timeoutMs, bool ignoreInterrupts) {
int ret; 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 // We need to call enet_host_service() multiple times to make sure retransmissions happen
for (;;) { for (;;) {
int selectedTimeout = timeoutMs < ENET_INTERNAL_TIMEOUT_MS ? timeoutMs : ENET_INTERNAL_TIMEOUT_MS; int selectedTimeout = timeoutMs < ENET_INTERNAL_TIMEOUT_MS ? timeoutMs : ENET_INTERNAL_TIMEOUT_MS;
@@ -16,6 +20,7 @@ static int serviceEnetHostInternal(ENetHost* client, ENetEvent* event, enet_uint
// We want to report an interrupt event if we are able to read data // We want to report an interrupt event if we are able to read data
if (!ignoreInterrupts && ConnectionInterrupted) { if (!ignoreInterrupts && ConnectionInterrupted) {
Limelog("ENet wait interrupted\n"); Limelog("ENet wait interrupted\n");
SetLastSocketError(EINTR);
ret = -1; ret = -1;
break; break;
} }
@@ -67,7 +72,7 @@ int gracefullyDisconnectEnetPeer(ENetHost* host, ENetPeer* peer, enet_uint32 lin
Limelog("Timed out waiting for ENet peer to acknowledge disconnection\n"); Limelog("Timed out waiting for ENet peer to acknowledge disconnection\n");
} }
else { else {
Limelog("Failed to receive ENet peer disconnection acknowledgement\n"); Limelog("Failed to receive ENet peer disconnection acknowledgement: %d\n", LastSocketFail());
} }
return -1; return -1;

View File

@@ -144,7 +144,7 @@ static bool transactRtspMessageEnet(PRTSP_MESSAGE request, PRTSP_MESSAGE respons
// Wait for a reply // Wait for a reply
if (serviceEnetHost(client, &event, RTSP_RECEIVE_TIMEOUT_SEC * 1000) <= 0 || if (serviceEnetHost(client, &event, RTSP_RECEIVE_TIMEOUT_SEC * 1000) <= 0 ||
event.type != ENET_EVENT_TYPE_RECEIVE) { event.type != ENET_EVENT_TYPE_RECEIVE) {
Limelog("Failed to receive RTSP reply\n"); Limelog("Failed to receive RTSP reply: %d\n", LastSocketFail());
goto Exit; goto Exit;
} }
@@ -165,7 +165,7 @@ static bool transactRtspMessageEnet(PRTSP_MESSAGE request, PRTSP_MESSAGE respons
// The payload comes in a second packet // The payload comes in a second packet
if (serviceEnetHost(client, &event, RTSP_RECEIVE_TIMEOUT_SEC * 1000) <= 0 || if (serviceEnetHost(client, &event, RTSP_RECEIVE_TIMEOUT_SEC * 1000) <= 0 ||
event.type != ENET_EVENT_TYPE_RECEIVE) { 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; goto Exit;
} }
@@ -851,7 +851,7 @@ int performRtspHandshake(PSERVER_INFORMATION serverInfo) {
// Wait for the connect to complete // Wait for the connect to complete
if (serviceEnetHost(client, &event, RTSP_CONNECT_TIMEOUT_SEC * 1000) <= 0 || if (serviceEnetHost(client, &event, RTSP_CONNECT_TIMEOUT_SEC * 1000) <= 0 ||
event.type != ENET_EVENT_TYPE_CONNECT) { 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); enet_peer_reset(peer);
peer = NULL; peer = NULL;
enet_host_destroy(client); enet_host_destroy(client);