foxygit / doom Log in
commit 25ae4973fab0cfffe47fbc8373dae8a8715786d7
Author:     Simon Howard <fraggle@soulsphere.org>
AuthorDate: Sat Feb 23 16:53:58 2019 -0500
Commit:     Simon Howard <fraggle@soulsphere.org>
CommitDate: Sat Feb 23 16:53:58 2019 -0500

    net: Display appropriate message on connect failure.

    Previously, the "reject reason" would not be set if the server simply
    never responded to the connection attempt. So ensure that an error
    string of some kind is always set when NET_CL_Connect is called.

    As part of this, the handling for REJECTED packets has been moved to
    the client code; only the server only sends such packets, so it makes
    no sense for it to be in the common code.
---
 src/net_client.c | 42 ++++++++++++++++++++++++++++++++++++++++++
 src/net_common.c | 29 -----------------------------
 2 files changed, 42 insertions(+), 29 deletions(-)

diff --git a/src/net_client.c b/src/net_client.c
index cf83450c..2d787a5d 100644
--- a/src/net_client.c
+++ b/src/net_client.c
@@ -109,6 +109,9 @@ static net_context_t *client_context;

 static net_gamesettings_t settings;

+// Why did the server reject us?
+char *net_client_reject_reason = NULL;
+
 // true if the client code is in use

 boolean net_client_connected;
@@ -462,6 +465,37 @@ static void NET_CL_ParseSYN(net_packet_t *packet)
     }
 }

+static void SetRejectReason(const char *s)
+{
+    free(net_client_reject_reason);
+    if (s != NULL)
+    {
+        net_client_reject_reason = strdup(s);
+    }
+    else
+    {
+        net_client_reject_reason = NULL;
+    }
+}
+
+static void NET_CL_ParseReject(net_packet_t *packet)
+{
+    char *msg;
+
+    msg = NET_ReadSafeString(packet);
+    if (msg == NULL)
+    {
+        return;
+    }
+
+    if (client_connection.state == NET_CONN_STATE_CONNECTING)
+    {
+        client_connection.state = NET_CONN_STATE_DISCONNECTED;
+        client_connection.disconnect_reason = NET_DISCONNECT_REMOTE;
+        SetRejectReason(msg);
+    }
+}
+
 // data received while we are waiting for the game to start

 static void NET_CL_ParseWaitingData(net_packet_t *packet)
@@ -919,6 +953,10 @@ static void NET_CL_ParsePacket(net_packet_t *packet)
                 NET_CL_ParseSYN(packet);
                 break;

+            case NET_PACKET_TYPE_REJECTED:
+                NET_CL_ParseReject(packet);
+                break;
+
             case NET_PACKET_TYPE_WAITING_DATA:
                 NET_CL_ParseWaitingData(packet);
                 break;
@@ -1040,6 +1078,7 @@ boolean NET_CL_Connect(net_addr_t *addr, net_connect_data_t *data)
     // initialize module for client mode
     if (!addr->module->InitClient())
     {
+        SetRejectReason("Failed to initialize client module");
         return false;
     }

@@ -1054,6 +1093,7 @@ boolean NET_CL_Connect(net_addr_t *addr, net_connect_data_t *data)
     // try to connect
     start_time = I_GetTimeMS();
     last_send_time = -1;
+    SetRejectReason("Unknown reason");

     while (client_connection.state == NET_CONN_STATE_CONNECTING)
     {
@@ -1069,6 +1109,7 @@ boolean NET_CL_Connect(net_addr_t *addr, net_connect_data_t *data)
         // time out after 5 seconds
         if (nowtime - start_time > 5000)
         {
+            SetRejectReason("No response from server");
             break;
         }

@@ -1093,6 +1134,7 @@ boolean NET_CL_Connect(net_addr_t *addr, net_connect_data_t *data)
     {
         // connected ok!
         NET_Log("client: connected successfully");
+        SetRejectReason(NULL);
         client_state = CLIENT_STATE_WAITING_LAUNCH;
         drone = data->drone;

diff --git a/src/net_common.c b/src/net_common.c
index 08eeb0ee..7b4eb344 100644
--- a/src/net_common.c
+++ b/src/net_common.c
@@ -50,9 +50,6 @@ struct net_reliable_packet_s

 static FILE *net_debug = NULL;

-// Why did the server reject us?
-char *net_client_reject_reason = NULL;
-
 static void NET_Conn_Init(net_connection_t *conn, net_addr_t *addr,
                           net_protocol_t protocol)
 {
@@ -129,29 +126,6 @@ static void NET_Conn_ParseDisconnectACK(net_connection_t *conn,
     }
 }

-static void NET_Conn_ParseReject(net_connection_t *conn, net_packet_t *packet)
-{
-    char *msg;
-
-    msg = NET_ReadSafeString(packet);
-
-    if (msg == NULL)
-    {
-        return;
-    }
-
-    if (conn->state == NET_CONN_STATE_CONNECTING)
-    {
-        // rejected by server
-
-        conn->state = NET_CONN_STATE_DISCONNECTED;
-        conn->disconnect_reason = NET_DISCONNECT_REMOTE;
-
-        free(net_client_reject_reason);
-        net_client_reject_reason = strdup(msg);
-    }
-}
-
 static void NET_Conn_ParseReliableACK(net_connection_t *conn, net_packet_t *packet)
 {
     unsigned int seq;
@@ -273,9 +247,6 @@ boolean NET_Conn_Packet(net_connection_t *conn, net_packet_t *packet,
         case NET_PACKET_TYPE_KEEPALIVE:
             // No special action needed.
             break;
-        case NET_PACKET_TYPE_REJECTED:
-            NET_Conn_ParseReject(conn, packet);
-            break;
         case NET_PACKET_TYPE_RELIABLE_ACK:
             NET_Conn_ParseReliableACK(conn, packet);
             break;