foxygit / doom Log in
commit e4dcdc8be43cd9b07e7f3a7b54279f54f531c419
Author:     Simon Howard <fraggle@gmail.com>
AuthorDate: Mon May 29 20:55:20 2006 +0000
Commit:     Simon Howard <fraggle@gmail.com>
CommitDate: Mon May 29 20:55:20 2006 +0000

    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
---
 src/d_net.c     | 46 +++++++++++++++-------------
 src/net_query.c | 93 +++++++++++++++++++++++++++++++--------------------------
 src/net_query.h |  4 +++
 3 files changed, 80 insertions(+), 63 deletions(-)

diff --git a/src/d_net.c b/src/d_net.c
index ba433c9f..3fec28b5 100644
--- 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 @@ static const char rcsid[] = "$Id: d_net.c 484 2006-05-19 20:01:59Z fraggle $";
 #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 @@ void D_CheckNetGame (void)
 #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 @@ void D_CheckNetGame (void)
             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];
-            }
-        }
-
-        if (connect_module != NULL)
-        {
-            net_addr_t *addr;
-
-            connect_module->InitClient();
+                addr = NET_FindLANServer();

-            addr = connect_module->ResolveAddress(connect_addr);
+                if (addr == NULL)
+                {
+                    I_Error("No server found on local LAN");
+                }
+            }
+
+            i = M_CheckParm("-connect");

-            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",
diff --git a/src/net_query.c b/src/net_query.c
index 4ee62298..d4955061 100644
--- 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 @@ static void NET_Query_ParsePacket(net_addr_t *addr, net_packet_t *packet)
         for (i=0; i<70; ++i)
             putchar('=');
         putchar('\n');
+
+        first_response_addr = addr;
     }

     formatted_printf(18, "%s: ", NET_AddrToString(addr));
@@ -158,7 +161,44 @@ static void NET_Query_GetResponse(void)
     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)
@@ -167,13 +207,12 @@ void NET_Query_Init(void)
     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,26 +226,7 @@ void NET_QueryAddress(char *addr)

     printf("\nQuerying '%s'...\n\n", addr);

-    last_send_time = -1;
-    start_time = I_GetTimeMS();
-
-    while (num_responses <= 0 && 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(net_addr);
-            last_send_time = I_GetTimeMS();
-        }
-
-        // Check for a response
-
-        NET_Query_GetResponse();
-        I_Sleep(100);
-    }
-
-    if (num_responses <= 0)
+    if (!NET_Query_QueryLoop(net_addr, true))
     {
         I_Error("No response from '%s'", addr);
     }
@@ -214,33 +234,20 @@ void NET_QueryAddress(char *addr)
     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");
-
-    start_time = I_GetTimeMS();
-    last_send_time = -1;
-
-    while (num_responses <= 0 && I_GetTimeMS() < start_time + 5000)
-    {
-        // Send a query once every second
+    return NET_Query_QueryLoop(NULL, true);
+}

-        if (last_send_time < 0 || I_GetTimeMS() > last_send_time + 1000)
-        {
-            NET_Query_SendQuery(NULL);
-            last_send_time = I_GetTimeMS();
-        }
+void NET_LANQuery(void)
+{
+    NET_Query_Init();

-        NET_Query_GetResponse();
-        I_Sleep(100);
-    }
+    printf("\nSearching for servers on local LAN ...\n\n");

-    if (num_responses <= 0)
+    if (!NET_Query_QueryLoop(NULL, false))
     {
         I_Error("No servers found");
     }
diff --git a/src/net_query.h b/src/net_query.h
index f2b6cbc0..aa0eb70a 100644
--- 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 */