From acf813c4aa96b92a771cba649c557319f2a44bb8 Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Wed, 24 Jan 2024 23:22:19 -0600 Subject: [PATCH] Ignore some socket errors that may be caused by transient network interruptions This allows the ENet connection to survive WiFi network roaming. --- unix.c | 22 +++++++++++++++++++--- win32.c | 22 +++++++++++++++++++--- 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/unix.c b/unix.c index 0903dad..ac60aca 100644 --- a/unix.c +++ b/unix.c @@ -655,10 +655,26 @@ enet_socket_send (ENetSocket socket, if (sentLength == -1) { - if (errno == EWOULDBLOCK) - return 0; + switch (errno) + { + case EWOULDBLOCK: + return 0; - return -1; + // These errors are treated as possible transient + // conditions that could be caused by a network + // interruption. We'll ignore them and allow the + // socket timeout to kill us if the connection + // is permanently interrupted. + case EADDRNOTAVAIL: + case ENETDOWN: + case ENETUNREACH: + case EHOSTDOWN: + case EHOSTUNREACH: + return 0; + + default: + return -1; + } } return sentLength; diff --git a/win32.c b/win32.c index ffdb220..1e8c60e 100644 --- a/win32.c +++ b/win32.c @@ -496,10 +496,26 @@ enet_socket_send (ENetSocket socket, NULL, NULL) == SOCKET_ERROR) { - if (WSAGetLastError () == WSAEWOULDBLOCK) - return 0; + switch (WSAGetLastError ()) + { + case WSAEWOULDBLOCK: + return 0; - return -1; + // These errors are treated as possible transient + // conditions that could be caused by a network + // interruption. We'll ignore them and allow the + // socket timeout to kill us if the connection + // is permanently interrupted. + case WSAEADDRNOTAVAIL: + case WSAENETDOWN: + case WSAENETUNREACH: + case WSAEHOSTDOWN: + case WSAEHOSTUNREACH: + return 0; + + default: + return -1; + } } return (int) sentLength;