shithub: choc

Download patch

ref: e4dcdc8be43cd9b07e7f3a7b54279f54f531c419
parent: 847cd742d137f51868cfae978655d8658ecf788d
author: Simon Howard <[email protected]>
date: Mon May 29 16:55:20 EDT 2006

Add -autojoin command line parameter to automatically search a local LAN 
for a server and join it.

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

--- a/src/d_net.c
+++ b/src/d_net.c
@@ -1,7 +1,7 @@
 // Emacs style mode select   -*- C++ -*- 
 //-----------------------------------------------------------------------------
 //
-// $Id: d_net.c 484 2006-05-19 20:01:59Z fraggle $
+// $Id: d_net.c 544 2006-05-29 20:55:20Z fraggle $
 //
 // Copyright(C) 1993-1996 Id Software, Inc.
 // Copyright(C) 2005 Simon Howard
@@ -117,7 +117,7 @@
 //-----------------------------------------------------------------------------
 
 
-static const char rcsid[] = "$Id: d_net.c 484 2006-05-19 20:01:59Z fraggle $";
+static const char rcsid[] = "$Id: d_net.c 544 2006-05-29 20:55:20Z fraggle $";
 
 #include "doomfeatures.h"
 
@@ -136,6 +136,7 @@
 #include "net_client.h"
 #include "net_gui.h"
 #include "net_io.h"
+#include "net_query.h"
 #include "net_server.h"
 #include "net_sdl.h"
 #include "net_loop.h"
@@ -330,8 +331,7 @@
 #ifdef FEATURE_MULTIPLAYER
 
     {
-        net_module_t *connect_module = NULL;
-        char *connect_addr;
+        net_addr_t *addr = NULL;
 
         if (M_CheckParm("-server") > 0)
         {
@@ -339,33 +339,39 @@
             NET_SV_AddModule(&net_loop_server_module);
             NET_SV_AddModule(&net_sdl_module);
 
-            connect_module = &net_loop_client_module;
-            connect_addr = "";
+            net_loop_client_module.InitClient();
+            addr = net_loop_client_module.ResolveAddress(NULL);
         }
         else
         {
-            i = M_CheckParm("-connect");
+            i = M_CheckParm("-autojoin");
 
             if (i > 0)
             {
-                connect_module = &net_sdl_module;
-                connect_addr = myargv[i+1];
+                addr = NET_FindLANServer();
+
+                if (addr == NULL)
+                {
+                    I_Error("No server found on local LAN");
+                }
             }
-        }
+            
+            i = M_CheckParm("-connect");
 
-        if (connect_module != NULL)
-        {
-            net_addr_t *addr;
-
-            connect_module->InitClient();
-
-            addr = connect_module->ResolveAddress(connect_addr);
-
-            if (addr == NULL)
+            if (i > 0)
             {
-                I_Error("Unable to resolve \"%s\"", connect_addr);
+                net_sdl_module.InitClient();
+                addr = net_sdl_module.ResolveAddress(myargv[i+1]);
+
+                if (addr == NULL)
+                {
+                    I_Error("Unable to resolve '%s'\n", myargv[i+1]);
+                }
             }
+        }
 
+        if (addr != NULL)
+        {
             if (!NET_CL_Connect(addr))
             {
                 I_Error("D_CheckNetGame: Failed to connect to %s\n", 
--- a/src/net_query.c
+++ b/src/net_query.c
@@ -37,6 +37,7 @@
 #include "net_query.h"
 #include "net_sdl.h"
 
+static net_addr_t *first_response_addr;
 static net_context_t *query_context;
 static int num_responses;
 
@@ -130,6 +131,8 @@
         for (i=0; i<70; ++i)
             putchar('=');
         putchar('\n');
+
+        first_response_addr = addr;
     }
 
     formatted_printf(18, "%s: ", NET_AddrToString(addr));
@@ -158,9 +161,46 @@
     if (NET_RecvPacket(query_context, &addr, &packet))
     {
         NET_Query_ParsePacket(addr, packet);
+        NET_FreePacket(packet);
     }
 }
 
+static net_addr_t *NET_Query_QueryLoop(net_addr_t *addr, 
+                                       boolean find_one)
+{
+    int start_time;
+    int last_send_time;
+
+    last_send_time = -1;
+    start_time = I_GetTimeMS();
+
+    while (I_GetTimeMS() < start_time + 5000)
+    {
+        // Send a query once every second
+
+        if (last_send_time < 0 || I_GetTimeMS() > last_send_time + 1000)
+        {
+            NET_Query_SendQuery(addr);
+            last_send_time = I_GetTimeMS();
+        }
+
+        // Check for a response
+
+        NET_Query_GetResponse();
+
+        // Found a response?
+
+        if (find_one && num_responses > 0)
+            break;
+        
+        // Don't thrash the CPU
+        
+        I_Sleep(100);
+    }
+
+    return first_response_addr;
+}
+
 void NET_Query_Init(void)
 {
     query_context = NET_NewContext();
@@ -167,13 +207,12 @@
     NET_AddModule(query_context, &net_sdl_module);
     net_sdl_module.InitClient();
 
+    first_response_addr = NULL;
     num_responses = 0;
 }
 
 void NET_QueryAddress(char *addr)
 {
-    int start_time;
-    int last_send_time;
     net_addr_t *net_addr;
     
     NET_Query_Init();
@@ -187,27 +226,8 @@
 
     printf("\nQuerying '%s'...\n\n", addr);
 
-    last_send_time = -1;
-    start_time = I_GetTimeMS();
-
-    while (num_responses <= 0 && I_GetTimeMS() < start_time + 5000)
+    if (!NET_Query_QueryLoop(net_addr, true))
     {
-        // Send a query once every second
-
-        if (last_send_time < 0 || I_GetTimeMS() > last_send_time + 1000)
-        {
-            NET_Query_SendQuery(net_addr);
-            last_send_time = I_GetTimeMS();
-        }
-
-        // Check for a response
-
-        NET_Query_GetResponse();
-        I_Sleep(100);
-    }
-
-    if (num_responses <= 0)
-    {
         I_Error("No response from '%s'", addr);
     }
 
@@ -214,33 +234,20 @@
     exit(0);
 }
 
-void NET_LANQuery(void)
+net_addr_t *NET_FindLANServer(void)
 {
-    int start_time;
-    int last_send_time;
-    
     NET_Query_Init();
 
-    printf("\nPerforming broadcast scan for servers ...\n\n");
+    return NET_Query_QueryLoop(NULL, true);
+}
 
-    start_time = I_GetTimeMS();
-    last_send_time = -1;
+void NET_LANQuery(void)
+{
+    NET_Query_Init();
 
-    while (num_responses <= 0 && I_GetTimeMS() < start_time + 5000)
-    {
-        // Send a query once every second
+    printf("\nSearching for servers on local LAN ...\n\n");
 
-        if (last_send_time < 0 || I_GetTimeMS() > last_send_time + 1000)
-        {
-            NET_Query_SendQuery(NULL);
-            last_send_time = I_GetTimeMS();
-        }
-
-        NET_Query_GetResponse();
-        I_Sleep(100);
-    }
-
-    if (num_responses <= 0)
+    if (!NET_Query_QueryLoop(NULL, false))
     {
         I_Error("No servers found");
     }
--- a/src/net_query.h
+++ b/src/net_query.h
@@ -27,7 +27,11 @@
 #ifndef NET_QUERY_H
 #define NET_QUERY_H
 
+#include "net_defs.h"
+
 extern void NET_QueryAddress(char *addr);
+extern void NET_LANQuery(void);
+extern net_addr_t *NET_FindLANServer(void);
 
 #endif /* #ifndef NET_QUERY_H */