diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/net_defs.h | 15 | ||||
-rw-r--r-- | src/net_query.c | 112 | ||||
-rw-r--r-- | src/net_server.c | 23 | ||||
-rw-r--r-- | src/net_structrw.c | 38 | ||||
-rw-r--r-- | src/net_structrw.h | 5 |
5 files changed, 137 insertions, 56 deletions
diff --git a/src/net_defs.h b/src/net_defs.h index 928212fe..978cfba5 100644 --- 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 // @@ -206,5 +206,18 @@ typedef struct 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 */ diff --git a/src/net_query.c b/src/net_query.c index 07d947b5..9a6b223b 100644 --- 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 char *GameDescription(GameMode_t mode, GameMission_t mission) } } -static void NET_Query_ParsePacket(net_addr_t *addr, net_packet_t *packet) +static void PrintHeader(void) { - 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; - // Have we already received a packet from this host? + formatted_printf(18, "Address"); + formatted_printf(8, "Players"); + puts("Description"); - if (!NET_Query_CheckResponder(addr)) + 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) { - return; + printf("(%s) ", GameDescription(response->data.gamemode, + response->data.gamemission)); } - 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))) + if (response->data.server_state) { - return; + printf("(game running) "); } - if (num_responses <= 0) + NET_SafePuts(response->data.description); +} + +static void NET_Query_ParsePacket(net_addr_t *addr, net_packet_t *packet) +{ + unsigned int packet_type; + net_querydata_t querydata; + queryresponse_t *response; + + // Have we already received a packet from this host? + + if (!CheckResponder(addr)) { - // If this is the first response, print the table header + return; + } - formatted_printf(18, "Address"); - formatted_printf(8, "Players"); - puts("Description"); + // Read the header - for (i=0; i<70; ++i) - putchar('='); - putchar('\n'); + if (!NET_ReadInt16(packet, &packet_type) + || packet_type != NET_PACKET_TYPE_QUERY_RESPONSE) + { + return; } - formatted_printf(18, "%s: ", NET_AddrToString(addr)); - formatted_printf(8, "%i/%i", num_players, max_players); + // Read query data - 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 @@ static net_addr_t *NET_Query_QueryLoop(net_addr_t *addr, } if (num_responses > 0) - return responders[0]; + return responders[0].addr; else return NULL; } diff --git a/src/net_server.c b/src/net_server.c index c57ef61a..992a10d0 100644 --- 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 @@ static void NET_SV_ParseResendRequest(net_packet_t *packet, net_client_t *client void NET_SV_SendQueryResponse(net_addr_t *addr) { net_packet_t *reply; - - reply = NET_NewPacket(64); - NET_WriteInt16(reply, NET_PACKET_TYPE_QUERY_RESPONSE); + net_querydata_t querydata; // 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); } diff --git a/src/net_structrw.c b/src/net_structrw.c index 4f5890e9..c947fb30 100644 --- 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 // @@ -100,6 +100,42 @@ boolean NET_ReadSettings(net_packet_t *packet, net_gamesettings_t *settings) && 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, boolean lowres_turn) { diff --git a/src/net_structrw.h b/src/net_structrw.h index 6f8ec59b..920dd851 100644 --- 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 // @@ -53,6 +53,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); extern void NET_TiccmdDiff(ticcmd_t *tic1, ticcmd_t *tic2, net_ticdiff_t *diff); |