foxygit / doom Log in
commit 58656316f7e115a321a155f905b2daaefcde537d
Author:     Simon Howard <fraggle@gmail.com>
AuthorDate: Mon Jan 2 21:04:10 2006 +0000
Commit:     Simon Howard <fraggle@gmail.com>
CommitDate: Mon Jan 2 21:04:10 2006 +0000

    Create NET_SV_Shutdown function to shut down the server. Call it
    when quitting the game. Print the IP of the server correctly when
    connecting.

    Subversion-branch: /trunk/chocolate-doom
    Subversion-revision: 250
---
 src/d_net.c      | 14 +++++---
 src/net_server.c | 99 ++++++++++++++++++++++++++++++++++++++++++++++----------
 src/net_server.h | 12 ++++++-
 3 files changed, 103 insertions(+), 22 deletions(-)

diff --git a/src/d_net.c b/src/d_net.c
index 3557a1a3..7af0cd01 100644
--- a/src/d_net.c
+++ b/src/d_net.c
@@ -1,7 +1,7 @@
 // Emacs style mode select   -*- C++ -*-
 //-----------------------------------------------------------------------------
 //
-// $Id: d_net.c 247 2006-01-02 20:14:29Z fraggle $
+// $Id: d_net.c 250 2006-01-02 21:04:10Z fraggle $
 //
 // Copyright(C) 1993-1996 Id Software, Inc.
 // Copyright(C) 2005 Simon Howard
@@ -22,6 +22,11 @@
 // 02111-1307, USA.
 //
 // $Log$
+// Revision 1.15  2006/01/02 21:04:10  fraggle
+// Create NET_SV_Shutdown function to shut down the server.  Call it
+// when quitting the game.  Print the IP of the server correctly when
+// connecting.
+//
 // Revision 1.14  2006/01/02 20:14:29  fraggle
 // Add a "-client" option to test connecting to a local server.
 //
@@ -80,7 +85,7 @@
 //-----------------------------------------------------------------------------


-static const char rcsid[] = "$Id: d_net.c 247 2006-01-02 20:14:29Z fraggle $";
+static const char rcsid[] = "$Id: d_net.c 250 2006-01-02 21:04:10Z fraggle $";


 #include "d_main.h"
@@ -94,6 +99,7 @@ static const char rcsid[] = "$Id: d_net.c 247 2006-01-02 20:14:29Z fraggle $";
 #include "doomstat.h"

 #include "net_client.h"
+#include "net_io.h"
 #include "net_server.h"
 #include "net_sdl.h"
 #include "net_loop.h"
@@ -632,7 +638,6 @@ void D_CheckNetGame (void)
         NET_SV_Init();

         addr = net_loop_client_module.ResolveAddress("");
-
     }

     if (M_CheckParm("-client") > 0)
@@ -644,7 +649,7 @@ void D_CheckNetGame (void)
     {
         if (NET_CL_Connect(addr))
         {
-            printf("connected to local server\n");
+            printf("connected to %s\n", NET_AddrToString(addr));
         }
         else
         {
@@ -702,6 +707,7 @@ void D_QuitNetGame (void)
     if (debugfile)
 	fclose (debugfile);

+    NET_SV_Shutdown();
     NET_CL_Disconnect();

     if (!netgame || !usergame || consoleplayer == -1 || demoplayback)
diff --git a/src/net_server.c b/src/net_server.c
index c004e804..e4dc4fe6 100644
--- a/src/net_server.c
+++ b/src/net_server.c
@@ -1,7 +1,7 @@
 // Emacs style mode select   -*- C++ -*-
 //-----------------------------------------------------------------------------
 //
-// $Id: net_server.c 245 2006-01-02 20:13:06Z fraggle $
+// $Id: net_server.c 250 2006-01-02 21:04:10Z fraggle $
 //
 // Copyright(C) 2005 Simon Howard
 //
@@ -21,6 +21,11 @@
 // 02111-1307, USA.
 //
 // $Log$
+// Revision 1.9  2006/01/02 21:04:10  fraggle
+// Create NET_SV_Shutdown function to shut down the server.  Call it
+// when quitting the game.  Print the IP of the server correctly when
+// connecting.
+//
 // Revision 1.8  2006/01/02 20:13:06  fraggle
 // Refer to connected clients by their AddrToString() output rather than just
 // the pointer to their struct.  Listen for IP connections as well as
@@ -58,6 +63,7 @@
 #include "doomdef.h"
 #include "doomstat.h"
 #include "i_system.h"
+#include "net_client.h"
 #include "net_defs.h"
 #include "net_io.h"
 #include "net_loop.h"
@@ -103,19 +109,16 @@ static boolean server_initialised = false;
 static net_client_t clients[MAXNETNODES];
 static net_context_t *server_context;

-static char *NET_SV_ClientAddress(net_client_t *client)
-{
-    static char addrbuf[128];
-
-    client->addr->module->AddrToString(client->addr, addrbuf, sizeof(addrbuf)-1);
-
-    return addrbuf;
-}
-
 static void NET_SV_DisconnectClient(net_client_t *client)
 {
-    client->state = CLIENT_STATE_DISCONNECTING;
-    client->last_send_time = -1;
+    if (client->active
+     && client->state != CLIENT_STATE_DISCONNECTING
+     && client->state != CLIENT_STATE_DISCONNECTED)
+    {
+        client->state = CLIENT_STATE_DISCONNECTING;
+        client->num_retries = 0;
+        client->last_send_time = -1;
+    }
 }

 static boolean ClientConnected(net_client_t *client)
@@ -304,7 +307,7 @@ static void NET_SV_ParseDisconnect(net_packet_t *packet, net_client_t *client)

     client->state = CLIENT_STATE_DISCONNECTED;

-    //printf("SV: %s: client disconnected\n", NET_SV_ClientAddress(client));
+    //printf("SV: %s: client disconnected\n", NET_AddrToString(client->addr));
 }

 // Parse a DISCONNECT_ACK packet
@@ -327,7 +330,7 @@ static void NET_SV_ParseDisconnectACK(net_packet_t *packet,
         // Place into the DISCONNECTED state to allow for cleanup.

         client->state = CLIENT_STATE_DISCONNECTED;
-        client->last_send_time = I_GetTimeMS();
+        client->last_send_time = -1;
     }
 }

@@ -351,7 +354,7 @@ static void NET_SV_Packet(net_packet_t *packet, net_addr_t *addr)
         return;
     }

-    //printf("SV: %s: %i\n", NET_SV_ClientAddress(client), packet_type);
+    //printf("SV: %s: %i\n", NET_AddrToString(addr), packet_type);

     switch (packet_type)
     {
@@ -502,9 +505,10 @@ static void NET_SV_RunClient(net_client_t *client)

         // Remove from the list after five seconds

-        if (I_GetTimeMS() - client->last_send_time > 5000)
+        if (client->last_send_time < 0
+         || I_GetTimeMS() - client->last_send_time > 5000)
         {
-            //printf("SV: %s: deactivated\n", NET_SV_ClientAddress(client));
+            //printf("SV: %s: deactivated\n", NET_AddrToString(client->addr));
             client->active = false;
             NET_FreeAddress(client->addr);
         }
@@ -566,3 +570,64 @@ void NET_SV_Run(void)
     }
 }

+void NET_SV_Shutdown(void)
+{
+    int i;
+    boolean running;
+    int start_time;
+
+    if (!server_initialised)
+    {
+        return;
+    }
+
+    fprintf(stderr, "SV: Shutting down server...\n");
+
+    // Disconnect all clients
+
+    for (i=0; i<MAXNETNODES; ++i)
+    {
+        if (clients[i].active)
+        {
+            NET_SV_DisconnectClient(&clients[i]);
+        }
+    }
+
+    // Wait for all clients to finish disconnecting
+
+    start_time = I_GetTimeMS();
+    running = true;
+
+    while (running)
+    {
+        // Check if any clients are still not finished
+
+        running = false;
+
+        for (i=0; i<MAXNETNODES; ++i)
+        {
+            if (clients[i].active)
+            {
+                running = true;
+            }
+        }
+
+        // Timed out?
+
+        if (I_GetTimeMS() - start_time > 5000)
+        {
+            running = false;
+            fprintf(stderr, "SV: Timed out waiting for clients to disconnect.\n");
+        }
+
+        // Run the client code in case this is a loopback client.
+
+        NET_CL_Run();
+        NET_SV_Run();
+
+        // Don't hog the CPU
+
+        I_Sleep(10);
+    }
+}
+
diff --git a/src/net_server.h b/src/net_server.h
index 21922919..81eaf4a4 100644
--- a/src/net_server.h
+++ b/src/net_server.h
@@ -1,7 +1,7 @@
 // Emacs style mode select   -*- C++ -*-
 //-----------------------------------------------------------------------------
 //
-// $Id: net_server.h 239 2006-01-02 00:00:08Z fraggle $
+// $Id: net_server.h 250 2006-01-02 21:04:10Z fraggle $
 //
 // Copyright(C) 2005 Simon Howard
 //
@@ -21,6 +21,11 @@
 // 02111-1307, USA.
 //
 // $Log$
+// Revision 1.3  2006/01/02 21:04:10  fraggle
+// Create NET_SV_Shutdown function to shut down the server.  Call it
+// when quitting the game.  Print the IP of the server correctly when
+// connecting.
+//
 // Revision 1.2  2006/01/02 00:00:08  fraggle
 // Neater prefixes: NET_Client -> NET_CL_.  NET_Server -> NET_SV_.
 //
@@ -43,5 +48,10 @@ void NET_SV_Init(void);

 void NET_SV_Run(void);

+// Shut down the server
+// Blocks until all clients disconnect, or until a 5 second timeout
+
+void NET_SV_Shutdown(void);
+
 #endif /* #ifndef NET_SERVER_H */