foxygit / doom Log in
commit eb937f4d148a75c4f2b6d3f7687dae4ef2053105
Author:     Simon Howard <fraggle@gmail.com>
AuthorDate: Sun Apr 9 02:50:34 2006 +0000
Commit:     Simon Howard <fraggle@gmail.com>
CommitDate: Sun Apr 9 02:50:34 2006 +0000

    Broadcast searches (currently broken)

    Subversion-branch: /trunk/chocolate-doom
    Subversion-revision: 467
---
 src/d_main.c    |  8 ++++++--
 src/net_io.c    | 14 +++++++++++++-
 src/net_query.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++---
 src/net_sdl.c   | 21 ++++++++++++++++----
 4 files changed, 92 insertions(+), 10 deletions(-)

diff --git a/src/d_main.c b/src/d_main.c
index 6a60edc5..d9b5853b 100644
--- a/src/d_main.c
+++ b/src/d_main.c
@@ -1,7 +1,7 @@
 // Emacs style mode select   -*- C++ -*-
 //-----------------------------------------------------------------------------
 //
-// $Id: d_main.c 464 2006-04-06 20:48:35Z fraggle $
+// $Id: d_main.c 467 2006-04-09 02:50:34Z fraggle $
 //
 // Copyright(C) 1993-1996 Id Software, Inc.
 // Copyright(C) 2005 Simon Howard
@@ -184,7 +184,7 @@
 //-----------------------------------------------------------------------------


-static const char rcsid[] = "$Id: d_main.c 464 2006-04-06 20:48:35Z fraggle $";
+static const char rcsid[] = "$Id: d_main.c 467 2006-04-09 02:50:34Z fraggle $";

 #define	BGCOLOR		7
 #define	FGCOLOR		8
@@ -1368,6 +1368,10 @@ void D_DoomMain (void)
         NET_QueryAddress(myargv[p+1]);
     }

+    if (M_CheckParm("-search"))
+        NET_LANQuery();
+
+

 #ifdef FEATURE_DEHACKED
     printf("DEH_Init: Init Dehacked support.\n");
diff --git a/src/net_io.c b/src/net_io.c
index e4fb5aae..6067134d 100644
--- a/src/net_io.c
+++ b/src/net_io.c
@@ -1,7 +1,7 @@
 // Emacs style mode select   -*- C++ -*-
 //-----------------------------------------------------------------------------
 //
-// $Id: net_io.c 249 2006-01-02 21:02:16Z fraggle $
+// $Id: net_io.c 467 2006-04-09 02:50:34Z fraggle $
 //
 // Copyright(C) 2005 Simon Howard
 //
@@ -48,6 +48,8 @@ struct _net_context_s
     int num_modules;
 };

+net_addr_t net_broadcast_addr;
+
 net_context_t *NET_NewContext(void)
 {
     net_context_t *context;
@@ -94,6 +96,16 @@ void NET_SendPacket(net_addr_t *addr, net_packet_t *packet)
     addr->module->SendPacket(addr, packet);
 }

+void NET_SendBroadcast(net_context_t *context, net_packet_t *packet)
+{
+    int i;
+
+    for (i=0; i<context->num_modules; ++i)
+    {
+        context->modules[i]->SendPacket(&net_broadcast_addr, packet);
+    }
+}
+
 boolean NET_RecvPacket(net_context_t *context,
                        net_addr_t **addr,
                        net_packet_t **packet)
diff --git a/src/net_query.c b/src/net_query.c
index b6a604f8..4ee62298 100644
--- a/src/net_query.c
+++ b/src/net_query.c
@@ -46,7 +46,16 @@ static void NET_Query_SendQuery(net_addr_t *addr)

     request = NET_NewPacket(10);
     NET_WriteInt16(request, NET_PACKET_TYPE_QUERY);
-    NET_SendPacket(addr, request);
+
+    if (addr == NULL)
+    {
+        NET_SendBroadcast(query_context, request);
+    }
+    else
+    {
+        NET_SendPacket(addr, request);
+    }
+
     NET_FreePacket(request);
 }

@@ -164,6 +173,7 @@ void NET_Query_Init(void)
 void NET_QueryAddress(char *addr)
 {
     int start_time;
+    int last_send_time;
     net_addr_t *net_addr;

     NET_Query_Init();
@@ -177,12 +187,21 @@ void NET_QueryAddress(char *addr)

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

-    NET_Query_SendQuery(net_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);
     }
@@ -195,3 +214,37 @@ void NET_QueryAddress(char *addr)
     exit(0);
 }

+void NET_LANQuery(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
+
+        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)
+    {
+        I_Error("No servers found");
+    }
+
+    exit(0);
+}
+
diff --git a/src/net_sdl.c b/src/net_sdl.c
index 24be9627..25e79677 100644
--- a/src/net_sdl.c
+++ b/src/net_sdl.c
@@ -1,7 +1,7 @@
 // Emacs style mode select   -*- C++ -*-
 //-----------------------------------------------------------------------------
 //
-// $Id: net_sdl.c 465 2006-04-06 20:49:16Z fraggle $
+// $Id: net_sdl.c 467 2006-04-09 02:50:34Z fraggle $
 //
 // Copyright(C) 2005 Simon Howard
 //
@@ -43,6 +43,7 @@
 #include "i_system.h"
 #include "m_argv.h"
 #include "net_defs.h"
+#include "net_io.h"
 #include "net_packet.h"
 #include "net_sdl.h"
 #include "z_zone.h"
@@ -53,7 +54,9 @@

 #include <SDL_net.h>

-static int port = 2342;
+#define DEFAULT_PORT 2342
+
+static int port = DEFAULT_PORT;
 static UDPsocket udpsocket;
 static UDPpacket *recvpacket;

@@ -208,7 +211,17 @@ static boolean NET_SDL_InitServer(void)
 static void NET_SDL_SendPacket(net_addr_t *addr, net_packet_t *packet)
 {
     UDPpacket sdl_packet;
-    IPaddress *ip = (IPaddress *) addr->handle;
+    IPaddress ip;
+
+    if (addr == &net_broadcast_addr)
+    {
+        ip.host = INADDR_BROADCAST;
+        ip.port = DEFAULT_PORT;
+    }
+    else
+    {
+        ip = *((IPaddress *) addr->handle);
+    }

 #if 0
     {
@@ -229,7 +242,7 @@ static void NET_SDL_SendPacket(net_addr_t *addr, net_packet_t *packet)
     sdl_packet.channel = 0;
     sdl_packet.data = packet->data;
     sdl_packet.len = packet->len;
-    sdl_packet.address = *ip;
+    sdl_packet.address = ip;

     if (!SDLNet_UDP_Send(udpsocket, -1, &sdl_packet))
     {