Skip to content

Commit

Permalink
net: Display appropriate message on connect failure.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
fragglet committed Feb 23, 2019
1 parent 835d927 commit 25ae497
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 29 deletions.
42 changes: 42 additions & 0 deletions src/net_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand All @@ -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)
{
Expand All @@ -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;
}

Expand All @@ -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;

Expand Down
29 changes: 0 additions & 29 deletions src/net_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 25ae497

Please sign in to comment.