shithub: choc

Download patch

ref: b65c4617a657cf2cc5848efcc8753bcc6f67fdca
parent: e48ea79eb19b130f2aa569401f57e460d6417091
author: Simon Howard <[email protected]>
date: Sun Sep 17 16:37:26 EDT 2006

Split off query data into a net_querydata_t structure in net_structrw.c

Subversion-branch: /trunk/chocolate-doom
Subversion-revision: 612

--- a/src/net_defs.h
+++ b/src/net_defs.h
@@ -1,7 +1,7 @@
 // Emacs style mode select   -*- C++ -*- 
 //-----------------------------------------------------------------------------
 //
-// $Id: net_defs.h 593 2006-09-01 20:45:45Z fraggle $
+// $Id: net_defs.h 612 2006-09-17 20:37:26Z fraggle $
 //
 // Copyright(C) 2005 Simon Howard
 //
@@ -205,6 +205,19 @@
     boolean playeringame[MAXPLAYERS];
     net_ticdiff_t cmds[MAXPLAYERS];
 } net_full_ticcmd_t;
+
+// Data sent in response to server queries
+
+typedef struct
+{
+    char *version;
+    int server_state;
+    int num_players;
+    int max_players;
+    int gamemode;
+    int gamemission;
+    char *description;
+} net_querydata_t;
 
 #endif /* #ifndef NET_DEFS_H */
 
--- a/src/net_query.c
+++ b/src/net_query.c
@@ -35,32 +35,47 @@
 #include "net_io.h"
 #include "net_packet.h"
 #include "net_query.h"
+#include "net_structrw.h"
 #include "net_sdl.h"
 
+typedef struct 
+{
+    net_addr_t *addr;
+    net_querydata_t data;
+} queryresponse_t;
+
 static net_context_t *query_context;
-static net_addr_t **responders;
+static queryresponse_t *responders;
 static int num_responses;
 
 // Add a new address to the list of hosts that has responded
 
-static void NET_Query_AddResponder(net_addr_t *addr)
+static queryresponse_t *AddResponder(net_addr_t *addr, 
+                                     net_querydata_t *data)
 {
+    queryresponse_t *response;
+
     responders = realloc(responders, 
-                         sizeof(net_addr_t *) * (num_responses + 1));
-    responders[num_responses] = addr;
+                         sizeof(queryresponse_t) * (num_responses + 1));
+
+    response = &responders[num_responses];
+    response->addr = addr;
+    response->data = *data;
     ++num_responses;
+
+    return response;
 }
 
 // Returns true if the reply is from a host that has not previously
 // responded.
 
-static boolean NET_Query_CheckResponder(net_addr_t *addr)
+static boolean CheckResponder(net_addr_t *addr)
 {
     int i;
 
     for (i=0; i<num_responses; ++i)
     {
-        if (responders[i] == addr)
+        if (responders[i].addr == addr)
         {
             return false;
         }
@@ -128,64 +143,77 @@
     }
 }
 
+static void PrintHeader(void)
+{
+    int i;
+
+    formatted_printf(18, "Address");
+    formatted_printf(8, "Players");
+    puts("Description");
+
+    for (i=0; i<70; ++i)
+        putchar('=');
+    putchar('\n');
+}
+
+static void PrintResponse(queryresponse_t *response)
+{
+    formatted_printf(18, "%s: ", NET_AddrToString(response->addr));
+    formatted_printf(8, "%i/%i", response->data.num_players, 
+                                 response->data.max_players);
+
+    if (response->data.gamemode != indetermined)
+    {
+        printf("(%s) ", GameDescription(response->data.gamemode, 
+                                        response->data.gamemission));
+    }
+
+    if (response->data.server_state)
+    {
+        printf("(game running) ");
+    }
+
+    NET_SafePuts(response->data.description);
+}
+
 static void NET_Query_ParsePacket(net_addr_t *addr, net_packet_t *packet)
 {
     unsigned int packet_type;
-    char *server_version;
-    unsigned int in_game;
-    unsigned int num_players, max_players;
-    unsigned int servermode, servermission;
-    char *server_description;
-    int i;
+    net_querydata_t querydata;
+    queryresponse_t *response;
 
     // Have we already received a packet from this host?
 
-    if (!NET_Query_CheckResponder(addr))
+    if (!CheckResponder(addr))
     {
         return;
     }
 
+    // Read the header
+
     if (!NET_ReadInt16(packet, &packet_type)
-     || !(server_version = NET_ReadString(packet))
-     || !NET_ReadInt8(packet, &in_game)
-     || !NET_ReadInt8(packet, &num_players)
-     || !NET_ReadInt8(packet, &max_players)
-     || !NET_ReadInt8(packet, &servermode)
-     || !NET_ReadInt8(packet, &servermission)
-     || !(server_description = NET_ReadString(packet)))
+     || packet_type != NET_PACKET_TYPE_QUERY_RESPONSE)
     {
         return;
     }
 
-    if (num_responses <= 0)
-    {
-        // If this is the first response, print the table header
+    // Read query data
 
-        formatted_printf(18, "Address");
-        formatted_printf(8, "Players");
-        puts("Description");
-
-        for (i=0; i<70; ++i)
-            putchar('=');
-        putchar('\n');
-    }
-
-    formatted_printf(18, "%s: ", NET_AddrToString(addr));
-    formatted_printf(8, "%i/%i", num_players, max_players);
-
-    if (servermode != indetermined)
+    if (!NET_ReadQueryData(packet, &querydata))
     {
-        printf("(%s) ", GameDescription(servermode, servermission));
+        return;
     }
 
-    if (in_game)
+    if (num_responses <= 0)
     {
-        printf("(game running) ");
+        // If this is the first response, print the table header
+
+        PrintHeader();
     }
 
-    NET_SafePuts(server_description);
+    response = AddResponder(addr, &querydata);
 
-    NET_Query_AddResponder(addr);
+    PrintResponse(response);
 }
 
 static void NET_Query_GetResponse(void)
@@ -234,7 +262,7 @@
     }
 
     if (num_responses > 0)
-        return responders[0];
+        return responders[0].addr;
     else
         return NULL;
 }
--- a/src/net_server.c
+++ b/src/net_server.c
@@ -1,7 +1,7 @@
 // Emacs style mode select   -*- C++ -*- 
 //-----------------------------------------------------------------------------
 //
-// $Id: net_server.c 464 2006-04-06 20:48:35Z fraggle $
+// $Id: net_server.c 612 2006-09-17 20:37:26Z fraggle $
 //
 // Copyright(C) 2005 Simon Howard
 //
@@ -1063,34 +1063,35 @@
 void NET_SV_SendQueryResponse(net_addr_t *addr)
 {
     net_packet_t *reply;
+    net_querydata_t querydata;
 
-    reply = NET_NewPacket(64);
-    NET_WriteInt16(reply, NET_PACKET_TYPE_QUERY_RESPONSE);
-
     // Version
 
-    NET_WriteString(reply, PACKAGE_STRING);
+    querydata.version = PACKAGE_STRING;
 
     // Server state
 
-    NET_WriteInt8(reply, server_state);
+    querydata.server_state = server_state;
 
     // Number of players/maximum players
 
-    NET_WriteInt8(reply, NET_SV_NumClients());
-    NET_WriteInt8(reply, MAXPLAYERS);
+    querydata.num_players = NET_SV_NumClients();
+    querydata.max_players = MAXPLAYERS;
 
     // Game mode/mission
 
-    NET_WriteInt8(reply, sv_gamemode);
-    NET_WriteInt8(reply, sv_gamemission);
+    querydata.gamemode = sv_gamemode;
+    querydata.gamemission = sv_gamemission;
 
     // Server description.  This is currently hard-coded.
 
-    NET_WriteString(reply, "Chocolate Doom server");
+    querydata.description = "Chocolate Doom server";
 
     // Send it and we're done.
 
+    reply = NET_NewPacket(64);
+    NET_WriteInt16(reply, NET_PACKET_TYPE_QUERY_RESPONSE);
+    NET_WriteQueryData(reply, &querydata);
     NET_SendPacket(addr, reply);
     NET_FreePacket(reply);
 }
--- a/src/net_structrw.c
+++ b/src/net_structrw.c
@@ -1,7 +1,7 @@
 // Emacs style mode select   -*- C++ -*- 
 //-----------------------------------------------------------------------------
 //
-// $Id: net_structrw.c 593 2006-09-01 20:45:45Z fraggle $
+// $Id: net_structrw.c 612 2006-09-17 20:37:26Z fraggle $
 //
 // Copyright(C) 2005 Simon Howard
 //
@@ -98,6 +98,42 @@
         && NET_ReadInt8(packet, (unsigned int *) &settings->new_sync)
         && NET_ReadInt32(packet, (unsigned int *) &settings->timelimit)
         && NET_ReadSInt8(packet, (signed int *) &settings->loadgame);
+}
+
+boolean NET_ReadQueryData(net_packet_t *packet, net_querydata_t *query)
+{
+    boolean result;
+
+    query->version = NET_ReadString(packet);
+
+    result = query->version != NULL
+          && NET_ReadInt8(packet, (unsigned int *) &query->server_state)
+          && NET_ReadInt8(packet, (unsigned int *) &query->num_players)
+          && NET_ReadInt8(packet, (unsigned int *) &query->max_players)
+          && NET_ReadInt8(packet, (unsigned int *) &query->gamemode)
+          && NET_ReadInt8(packet, (unsigned int *) &query->gamemission);
+    
+    if (result)
+    {
+        query->description = NET_ReadString(packet);
+
+        return query->description != NULL;
+    }   
+    else
+    {
+        return false;
+    } 
+}
+
+void NET_WriteQueryData(net_packet_t *packet, net_querydata_t *query)
+{
+    NET_WriteString(packet, query->version);
+    NET_WriteInt8(packet, query->server_state);
+    NET_WriteInt8(packet, query->num_players);
+    NET_WriteInt8(packet, query->max_players);
+    NET_WriteInt8(packet, query->gamemode);
+    NET_WriteInt8(packet, query->gamemission);
+    NET_WriteString(packet, query->description);
 }
 
 void NET_WriteTiccmdDiff(net_packet_t *packet, net_ticdiff_t *diff, 
--- a/src/net_structrw.h
+++ b/src/net_structrw.h
@@ -1,7 +1,7 @@
 // Emacs style mode select   -*- C++ -*- 
 //-----------------------------------------------------------------------------
 //
-// $Id: net_structrw.h 376 2006-02-23 18:19:05Z fraggle $
+// $Id: net_structrw.h 612 2006-09-17 20:37:26Z fraggle $
 //
 // Copyright(C) 2005 Simon Howard
 //
@@ -52,6 +52,9 @@
 
 extern void NET_WriteSettings(net_packet_t *packet, net_gamesettings_t *settings);
 extern boolean NET_ReadSettings(net_packet_t *packet, net_gamesettings_t *settings);
+
+extern void NET_WriteQueryData(net_packet_t *packet, net_querydata_t *querydata);
+extern boolean NET_ReadQueryData(net_packet_t *packet, net_querydata_t *querydata);
 
 extern void NET_WriteTiccmdDiff(net_packet_t *packet, net_ticdiff_t *diff, boolean lowres_turn);
 extern boolean NET_ReadTiccmdDiff(net_packet_t *packet, net_ticdiff_t *diff, boolean lowres_turn);