mirror of
https://github.com/moonlight-stream/moonlight-common-c.git
synced 2026-06-17 14:21:30 +00:00
Ensure graceful ENet disconnect after handling pending receives
This commit is contained in:
+51
-20
@@ -422,6 +422,8 @@ static void controlReceiveThreadFunc(void* context) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unsigned short terminationReason = -1;
|
||||||
|
|
||||||
while (!PltIsThreadInterrupted(&controlReceiveThread)) {
|
while (!PltIsThreadInterrupted(&controlReceiveThread)) {
|
||||||
ENetEvent event;
|
ENetEvent event;
|
||||||
|
|
||||||
@@ -433,16 +435,44 @@ static void controlReceiveThreadFunc(void* context) {
|
|||||||
// Handle a pending disconnect after unsuccessfully polling
|
// Handle a pending disconnect after unsuccessfully polling
|
||||||
// for new events to handle.
|
// for new events to handle.
|
||||||
if (disconnectPending) {
|
if (disconnectPending) {
|
||||||
Limelog("Dispatching pending ENet disconnect\n");
|
PltLockMutex(&enetMutex);
|
||||||
ListenerCallbacks.connectionTerminated(-1);
|
// Wait 100 ms for pending receives after a disconnect and
|
||||||
return;
|
// 1 second for the pending disconnect to be processed after
|
||||||
|
// removing the intercept callback.
|
||||||
|
err = serviceEnetHost(client, &event, client->intercept ? 100 : 1000);
|
||||||
|
if (err == 0) {
|
||||||
|
if (client->intercept) {
|
||||||
|
// Now that no pending receive events remain, we can
|
||||||
|
// remove our intercept hook and allow the server's
|
||||||
|
// disconnect to be processed as expected. We will wait
|
||||||
|
// 1 second for this disconnect to be processed before
|
||||||
|
// we tear down the connection anyway.
|
||||||
|
client->intercept = NULL;
|
||||||
|
PltUnlockMutex(&enetMutex);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// The 1 second timeout has expired with no disconnect event
|
||||||
|
// retransmission after the first notification. We can only
|
||||||
|
// assume the server died tragically, so go ahead and tear down.
|
||||||
|
PltUnlockMutex(&enetMutex);
|
||||||
|
Limelog("Disconnect event timeout expired\n");
|
||||||
|
ListenerCallbacks.connectionTerminated(-1);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
PltUnlockMutex(&enetMutex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// No events ready
|
||||||
|
PltSleepMs(100);
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// No events ready
|
|
||||||
PltSleepMs(100);
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
else if (err < 0) {
|
|
||||||
|
if (err < 0) {
|
||||||
Limelog("Control stream connection failed: %d\n", err);
|
Limelog("Control stream connection failed: %d\n", err);
|
||||||
ListenerCallbacks.connectionTerminated(err);
|
ListenerCallbacks.connectionTerminated(err);
|
||||||
return;
|
return;
|
||||||
@@ -478,27 +508,28 @@ static void controlReceiveThreadFunc(void* context) {
|
|||||||
|
|
||||||
BbInitializeWrappedBuffer(&bb, event.packet->data, sizeof(*ctlHdr), event.packet->dataLength - sizeof(*ctlHdr), BYTE_ORDER_LITTLE);
|
BbInitializeWrappedBuffer(&bb, event.packet->data, sizeof(*ctlHdr), event.packet->dataLength - sizeof(*ctlHdr), BYTE_ORDER_LITTLE);
|
||||||
|
|
||||||
unsigned short terminationReason;
|
|
||||||
|
|
||||||
BbGetShort(&bb, &terminationReason);
|
BbGetShort(&bb, &terminationReason);
|
||||||
|
|
||||||
Limelog("Server notified termination reason: %04x\n", terminationReason);
|
Limelog("Server notified termination reason: 0x%04x\n", terminationReason);
|
||||||
|
|
||||||
// SERVER_TERMINATED_INTENDED
|
// We don't actually notify the connection listener until we receive
|
||||||
if (terminationReason == 0x0100) {
|
// the disconnect event from the server that confirms the termination.
|
||||||
// Pass error code 0 to notify the client that this was not an error
|
|
||||||
ListenerCallbacks.connectionTerminated(0);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
ListenerCallbacks.connectionTerminated(terminationReason);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
enet_packet_destroy(event.packet);
|
enet_packet_destroy(event.packet);
|
||||||
}
|
}
|
||||||
else if (event.type == ENET_EVENT_TYPE_DISCONNECT) {
|
else if (event.type == ENET_EVENT_TYPE_DISCONNECT) {
|
||||||
Limelog("Control stream received disconnect event\n");
|
Limelog("Control stream received disconnect event\n");
|
||||||
ListenerCallbacks.connectionTerminated(-1);
|
|
||||||
|
// SERVER_TERMINATED_INTENDED
|
||||||
|
if (terminationReason == 0x0100) {
|
||||||
|
// Pass error code 0 to notify the client that this was not an error
|
||||||
|
ListenerCallbacks.connectionTerminated(0);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
ListenerCallbacks.connectionTerminated(terminationReason);
|
||||||
|
}
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user