shithub: choc

Download patch

ref: 58656316f7e115a321a155f905b2daaefcde537d
parent: f65633b5f50be8581ec2de7c06ca9a225d48b82a
author: Simon Howard <fraggle@gmail.com>
date: Mon Jan 2 16:04:10 EST 2006

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

--- 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 @@
 #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 @@
         NET_SV_Init();
 
         addr = net_loop_client_module.ResolveAddress("");
-
     }
 
     if (M_CheckParm("-client") > 0)
@@ -644,7 +649,7 @@
     {
         if (NET_CL_Connect(addr))
         {
-            printf("connected to local server\n");
+            printf("connected to %s\n", NET_AddrToString(addr));
         }
         else
         {
@@ -702,6 +707,7 @@
     if (debugfile)
 	fclose (debugfile);
 
+    NET_SV_Shutdown();
     NET_CL_Disconnect();
 
     if (!netgame || !usergame || consoleplayer == -1 || demoplayback)
--- 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 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 @@
 
     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 @@
         // 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 @@
         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 @@
 
         // 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);
         }
@@ -563,6 +567,67 @@
         {
             NET_SV_RunClient(&clients[i]);
         }
+    }
+}
+
+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);
     }
 }
 
--- 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_.
 //
@@ -42,6 +47,11 @@
 // run server: check for new packets received etc.
 
 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 */