shithub: choc

Download patch

ref: edc20cc64e1930bdcc9466063e17f91519492a37
parent: 76b9a5e33a500e96789572b9e6deb6df4b392d1e
author: Simon Howard <fraggle@gmail.com>
date: Tue Mar 7 07:46:52 EST 2006

Generic console message mechanism. Inform all players when recording
a low-res demo.

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

--- a/src/net_client.c
+++ b/src/net_client.c
@@ -1,7 +1,7 @@
 // Emacs style mode select   -*- C++ -*- 
 //-----------------------------------------------------------------------------
 //
-// $Id: net_client.c 401 2006-03-01 20:02:53Z fraggle $
+// $Id: net_client.c 410 2006-03-07 12:46:52Z fraggle $
 //
 // Copyright(C) 2005 Simon Howard
 //
@@ -143,6 +143,7 @@
 // Network client code
 //
 
+#include <ctype.h>
 #include <stdlib.h>
 #include <string.h>
 
@@ -884,6 +885,33 @@
     NET_CL_SendTics(start, start + num_tics - 1);
 }
 
+// Console message that the server wants the client to print
+
+static void NET_CL_ParseConsoleMessage(net_packet_t *packet)
+{
+    char *msg;
+    char *p;
+
+    msg = NET_ReadString(packet);
+
+    if (msg == NULL)
+    {
+        return;
+    }
+
+    // Do not do a straight "puts" of the string, as this could be
+    // dangerous (sending control codes to terminals can do all
+    // kinds of things)
+
+    for (p=msg; *p; ++p)
+    {
+        if (isprint(*p))
+            putchar(*p);
+    }
+
+    putchar('\n');
+}
+
 // parse a received packet
 
 static void NET_CL_ParsePacket(net_packet_t *packet)
@@ -917,6 +945,10 @@
 
             case NET_PACKET_TYPE_GAMEDATA_RESEND:
                 NET_CL_ParseResendRequest(packet);
+                break;
+
+            case NET_PACKET_TYPE_CONSOLE_MESSAGE:
+                NET_CL_ParseConsoleMessage(packet);
                 break;
 
             default:
--- a/src/net_defs.h
+++ b/src/net_defs.h
@@ -1,7 +1,7 @@
 // Emacs style mode select   -*- C++ -*- 
 //-----------------------------------------------------------------------------
 //
-// $Id: net_defs.h 401 2006-03-01 20:02:53Z fraggle $
+// $Id: net_defs.h 410 2006-03-07 12:46:52Z fraggle $
 //
 // Copyright(C) 2005 Simon Howard
 //
@@ -166,6 +166,7 @@
     NET_PACKET_TYPE_DISCONNECT_ACK,
     NET_PACKET_TYPE_RELIABLE_ACK,
     NET_PACKET_TYPE_GAMEDATA_RESEND,
+    NET_PACKET_TYPE_CONSOLE_MESSAGE,
 } net_packet_type_t;
 
 typedef struct 
--- a/src/net_server.c
+++ b/src/net_server.c
@@ -1,7 +1,7 @@
 // Emacs style mode select   -*- C++ -*- 
 //-----------------------------------------------------------------------------
 //
-// $Id: net_server.c 409 2006-03-06 20:48:07Z fraggle $
+// $Id: net_server.c 410 2006-03-07 12:46:52Z fraggle $
 //
 // Copyright(C) 2005 Simon Howard
 //
@@ -158,6 +158,7 @@
 // Network server code
 //
 
+#include <stdarg.h>
 #include <stdlib.h>
 #include <string.h>
 
@@ -262,6 +263,48 @@
         && client->connection.state == NET_CONN_STATE_CONNECTED;
 }
 
+// Send a message to be displayed on a client's console
+
+static void NET_SV_SendConsoleMessage(net_client_t *client, char *s, ...)
+{
+    char buf[1024];
+    va_list args;
+    net_packet_t *packet;
+
+    va_start(args, s);
+    vsnprintf(buf, sizeof(buf), s, args);
+    va_end(args);
+    
+    packet = NET_Conn_NewReliable(&client->connection, 
+                                  NET_PACKET_TYPE_CONSOLE_MESSAGE);
+
+    NET_WriteString(packet, buf);
+}
+
+// Send a message to all clients
+
+static void NET_SV_BroadcastMessage(char *s, ...)
+{
+    char buf[1024];
+    va_list args;
+    int i;
+
+    va_start(args, s);
+    vsnprintf(buf, sizeof(buf), s, args);
+    va_end(args);
+    
+    for (i=0; i<MAXNETNODES; ++i)
+    {
+        if (ClientConnected(&clients[i]))
+        {
+            NET_SV_SendConsoleMessage(&clients[i], buf);
+        }
+    }
+}
+
+
+// Assign player numbers to connected clients
+
 static void NET_SV_AssignPlayers(void)
 {
     int i;
@@ -661,9 +704,9 @@
     {
         if (sv_players[i] != NULL && sv_players[i]->recording_lowres)
         {
-            printf("SV: Playing in low resolution turning mode, "
-                   "because player %i is recording a Vanilla demo.\n",
-                   i + 1);
+            NET_SV_BroadcastMessage("Playing in low resolution turning mode, "
+                                    "because player %i is recording a Vanilla demo.\n",
+                                    i + 1);
                    
             settings.lowres_turn = true;
         }