Complete.Org: Mailing Lists: Archives: freeciv-dev: July 2004:
[Freeciv-Dev] Re: (PR#9513) Patch: connection id in chat
Home

[Freeciv-Dev] Re: (PR#9513) Patch: connection id in chat

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: undisclosed-recipients: ;
Subject: [Freeciv-Dev] Re: (PR#9513) Patch: connection id in chat
From: "Per I. Mathisen" <per@xxxxxxxxxxx>
Date: Wed, 28 Jul 2004 12:26:34 -0700
Reply-to: rt@xxxxxxxxxxx

<URL: http://rt.freeciv.org/Ticket/Display.html?id=9513 >

On Tue, 27 Jul 2004, Jason Short wrote:
> And this part is also quite bad
>
> +  remaining.lines++;
> +  remaining.line =
> +     fc_realloc(remaining.line,
> +              remaining.lines * sizeof(*remaining.line));
>
> since it will result in hideous amounts of reallocation. Instead a
> specvec should be used.

Or a speclist. Attached is a new patch with this and the capstr issue
fixed.

  - Per

Index: client/chatline_common.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/chatline_common.c,v
retrieving revision 1.5
diff -u -r1.5 chatline_common.c
--- client/chatline_common.c    28 Nov 2003 17:37:19 -0000      1.5
+++ client/chatline_common.c    28 Jul 2004 19:24:34 -0000
@@ -19,6 +19,7 @@
 #include <string.h>
 
 #include "astring.h"
+#include "log.h"
 #include "packets.h"
 #include "support.h"
 
@@ -27,6 +28,27 @@
 #include "chatline_common.h"
 #include "clinet.h"
 
+/* Stored up buffer of lines for the chatline */
+struct remaining {
+  char *text;
+  int conn_id;
+};
+#define SPECLIST_TAG remaining
+#include "speclist.h"
+#define remaining_list_iterate(rlist, pline) \
+  TYPED_LIST_ITERATE(struct remaining, rlist, pline)
+#define remaining_list_iterate_end LIST_ITERATE_END
+
+static struct remaining_list remains;
+
+/**************************************************************************
+  Initialize data structures.
+**************************************************************************/
+void chatline_common_init(void)
+{
+  remaining_list_init(&remains);
+}
+
 /**************************************************************************
   Send the message as a chat to the server.
 **************************************************************************/
@@ -36,7 +58,6 @@
 }
 
 static int frozen_level = 0;
-static struct astring remaining = ASTRING_INIT;
 
 /**************************************************************************
   Turn on buffering, using a counter so that calls may be nested.
@@ -46,9 +67,7 @@
   frozen_level++;
 
   if (frozen_level == 1) {
-    assert(remaining.str == NULL);
-    astr_minsize(&remaining, 1);
-    remaining.str[0] = '\0';
+    assert(remaining_list_size(&remains) == 0);
   }
 }
 
@@ -63,11 +82,11 @@
   assert(frozen_level >= 0);
 
   if (frozen_level == 0) {
-    if (remaining.n > 2) {
-      /* +1 to skip the initial '\n' */
-      append_output_window(remaining.str + 1);
-    }
-    astr_free(&remaining);
+    remaining_list_iterate(remains, pline) {
+      base_append_output_window(pline->text, pline->conn_id);
+      free(pline);
+    } remaining_list_iterate_end;
+    remaining_list_unlink_all(&remains);
   }
 }
 
@@ -83,24 +102,26 @@
 }
 
 /**************************************************************************
-...
+  Add a line of text to the output ("chatline") window.
 **************************************************************************/
 void append_output_window(const char *astring)
 {
+  base_append_output_window(astring, -1);
+}
+
+/**************************************************************************
+  Same as above, but here we know the connection id of the sender of the
+  text in question.
+**************************************************************************/
+void base_append_output_window(const char *astring, int conn_id)
+{
   if (frozen_level == 0) {
-    real_append_output_window(astring);
+    real_append_output_window(astring, conn_id);
   } else {
-    /* 
-     * len_src doesn't include the trailing '\0'
-     * len_dst does include the trailing '\0'
-     */
-    size_t len_src = strlen(astring), len_dst = remaining.n;
-
-    /* +1 for the "\n" */
-    astr_minsize(&remaining, len_dst + 1 + len_src);
-    remaining.str[len_dst - 1] = '\n';
+    struct remaining *premain = fc_malloc(sizeof(struct remaining));
 
-    /* +1 for the "\0" */
-    memcpy(&remaining.str[len_dst], astring, len_src + 1);
+    remaining_list_insert(&remains, premain);
+    premain->text = mystrdup(astring);
+    premain->conn_id = conn_id;
   }
 }
Index: client/chatline_common.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/chatline_common.h,v
retrieving revision 1.2
diff -u -r1.2 chatline_common.h
--- client/chatline_common.h    18 Jul 2003 22:02:24 -0000      1.2
+++ client/chatline_common.h    28 Jul 2004 19:24:34 -0000
@@ -17,6 +17,9 @@
 
 void send_chat(const char *message);
 
+void chatline_common_init(void);
+
+void base_append_output_window(const char *astring, int conn_id);
 void append_output_window(const char *astring);
 
 void output_window_freeze(void);
Index: client/civclient.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/civclient.c,v
retrieving revision 1.191
diff -u -r1.191 civclient.c
--- client/civclient.c  20 Jul 2004 17:13:52 -0000      1.191
+++ client/civclient.c  28 Jul 2004 19:24:34 -0000
@@ -204,6 +204,7 @@
 
   ui_init();
   my_init_network();
+  chatline_common_init();
   init_messages_where();
   init_city_report_data();
   init_player_dlg_common();
Index: client/climisc.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/climisc.c,v
retrieving revision 1.134
diff -u -r1.134 climisc.c
--- client/climisc.c    25 Jun 2004 23:35:55 -0000      1.134
+++ client/climisc.c    28 Jul 2004 19:24:35 -0000
@@ -860,7 +860,7 @@
   my_vsnprintf(message, sizeof(message), format, ap);
   va_end(ap);
 
-  handle_chat_msg(message, x, y, event);
+  handle_chat_msg(message, x, y, event, aconnection.id);
 }
 
 /**************************************************************************
Index: client/packhand.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/packhand.c,v
retrieving revision 1.390
diff -u -r1.390 packhand.c
--- client/packhand.c   20 Jul 2004 11:05:36 -0000      1.390
+++ client/packhand.c   28 Jul 2004 19:24:35 -0000
@@ -851,7 +851,7 @@
 /**************************************************************************
 ...
 **************************************************************************/
-void handle_chat_msg(char *message, int x, int y, enum event_type event)
+void handle_chat_msg(char *message, int x, int y, enum event_type event, int 
conn_id)
 {
   int where = MW_OUTPUT;       /* where to display the message */
   
@@ -863,7 +863,7 @@
   }
 
   if (BOOL_VAL(where & MW_OUTPUT)) {
-    append_output_window(message);
+    base_append_output_window(message,conn_id);
   }
   if (BOOL_VAL(where & MW_MESSAGES)) {
     add_notify_window(message, x, y, event);
Index: client/packhand_gen.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/packhand_gen.c,v
retrieving revision 1.5
diff -u -r1.5 packhand_gen.c
--- client/packhand_gen.c       6 Jun 2004 21:02:15 -0000       1.5
+++ client/packhand_gen.c       28 Jul 2004 19:24:35 -0000
@@ -102,7 +102,8 @@
       ((struct packet_chat_msg *)packet)->message,
       ((struct packet_chat_msg *)packet)->x,
       ((struct packet_chat_msg *)packet)->y,
-      ((struct packet_chat_msg *)packet)->event);
+      ((struct packet_chat_msg *)packet)->event,
+      ((struct packet_chat_msg *)packet)->conn_id);
     return TRUE;
 
   case PACKET_CITY_REMOVE:
Index: client/packhand_gen.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/packhand_gen.h,v
retrieving revision 1.5
diff -u -r1.5 packhand_gen.h
--- client/packhand_gen.h       6 Jun 2004 21:02:15 -0000       1.5
+++ client/packhand_gen.h       28 Jul 2004 19:24:35 -0000
@@ -34,7 +34,7 @@
 void handle_game_info(struct packet_game_info *packet);
 void handle_map_info(int xsize, int ysize, int topology_id);
 void handle_nuke_tile_info(int x, int y);
-void handle_chat_msg(char *message, int x, int y, enum event_type event);
+void handle_chat_msg(char *message, int x, int y, enum event_type event, int 
conn_id);
 void handle_city_remove(int city_id);
 struct packet_city_info;
 void handle_city_info(struct packet_city_info *packet);
Index: client/gui-gtk/chatline.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk/chatline.c,v
retrieving revision 1.21
diff -u -r1.21 chatline.c
--- client/gui-gtk/chatline.c   1 Apr 2004 01:28:56 -0000       1.21
+++ client/gui-gtk/chatline.c   28 Jul 2004 19:24:35 -0000
@@ -65,7 +65,7 @@
 /**************************************************************************
 ...
 **************************************************************************/
-void real_append_output_window(const char *astring)
+void real_append_output_window(const char *astring, int conn_id)
 {
   bool scroll;
   GtkAdjustment *slider =
Index: client/gui-gtk-2.0/chatline.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk-2.0/chatline.c,v
retrieving revision 1.9
diff -u -r1.9 chatline.c
--- client/gui-gtk-2.0/chatline.c       1 Apr 2004 01:20:20 -0000       1.9
+++ client/gui-gtk-2.0/chatline.c       28 Jul 2004 19:24:35 -0000
@@ -67,7 +67,7 @@
 /**************************************************************************
 ...
 **************************************************************************/
-void real_append_output_window(const char *astring)
+void real_append_output_window(const char *astring, int conn_id)
 {
  GtkWidget *sw;
  GtkAdjustment *slider;
Index: client/gui-mui/chatline.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-mui/chatline.c,v
retrieving revision 1.7
diff -u -r1.7 chatline.c
--- client/gui-mui/chatline.c   14 Nov 2002 09:14:56 -0000      1.7
+++ client/gui-mui/chatline.c   28 Jul 2004 19:24:35 -0000
@@ -34,7 +34,7 @@
 #include "gui_main.h"
 #include "muistuff.h"
 
-void real_append_output_window(const char *astring)
+void real_append_output_window(const char *astring, int conn_id)
 {
   DoMethod(main_output_listview, MUIM_NList_Insert, astring, -2, 
MUIV_List_Insert_Bottom);
   set(main_output_listview,MUIA_NList_First,  MUIV_NList_First_Bottom);
Index: client/gui-sdl/chatline.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-sdl/chatline.c,v
retrieving revision 1.14
diff -u -r1.14 chatline.c
--- client/gui-sdl/chatline.c   4 Dec 2003 13:53:12 -0000       1.14
+++ client/gui-sdl/chatline.c   28 Jul 2004 19:24:35 -0000
@@ -132,7 +132,7 @@
   Appends the string to the chat output window.
   Curretn it is wraper to message subsystem.
 **************************************************************************/
-void real_append_output_window(const char *astring)
+void real_append_output_window(const char *astring, int conn_id)
 {
   if (pConnDlg) {
     Uint16 *pUniStr;
Index: client/gui-stub/chatline.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-stub/chatline.c,v
retrieving revision 1.4
diff -u -r1.4 chatline.c
--- client/gui-stub/chatline.c  30 Nov 2002 19:27:36 -0000      1.4
+++ client/gui-stub/chatline.c  28 Jul 2004 19:24:35 -0000
@@ -23,7 +23,7 @@
   Appends the string to the chat output window.  The string should be
   inserted on its own line, although it will have no newline.
 **************************************************************************/
-void real_append_output_window(const char *astring)
+void real_append_output_window(const char *astring, int conn_id)
 {
   /* PORTME */
 }
Index: client/gui-win32/chatline.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-win32/chatline.c,v
retrieving revision 1.10
diff -u -r1.10 chatline.c
--- client/gui-win32/chatline.c 18 Jul 2003 22:02:25 -0000      1.10
+++ client/gui-win32/chatline.c 28 Jul 2004 19:24:35 -0000
@@ -85,10 +85,9 @@
 }
 
 /**************************************************************************
-
+  PS We need to add \r to lineends.
 **************************************************************************/
-void real_append_output_window(const char *astring) 
-     /* We need to add \r to lineends */ 
+void real_append_output_window(const char *astring, int conn_id)
 {
 
   const char *str;
Index: client/gui-xaw/chatline.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-xaw/chatline.c,v
retrieving revision 1.22
diff -u -r1.22 chatline.c
--- client/gui-xaw/chatline.c   24 Oct 2003 00:03:01 -0000      1.22
+++ client/gui-xaw/chatline.c   28 Jul 2004 19:24:35 -0000
@@ -72,7 +72,7 @@
 
  Now uses window's font size and width.  Assumes fixed-width font.  --jjm
 **************************************************************************/
-void real_append_output_window(const char *input_string)
+void real_append_output_window(const char *astring, int conn_id)
 {
   static int m_width=0;
 
Index: client/include/chatline_g.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/include/chatline_g.h,v
retrieving revision 1.2
diff -u -r1.2 chatline_g.h
--- client/include/chatline_g.h 27 Jun 2002 00:59:21 -0000      1.2
+++ client/include/chatline_g.h 28 Jul 2004 19:24:35 -0000
@@ -15,7 +15,7 @@
 
 #include "chatline_common.h"
 
-void real_append_output_window(const char *astring);
+void real_append_output_window(const char *astring, int conn_id);
 void log_output_window(void);
 void clear_output_window(void);
 
Index: client/include/diplodlg_g.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/include/diplodlg_g.h,v
retrieving revision 1.4
diff -u -r1.4 diplodlg_g.h
--- client/include/diplodlg_g.h 2 Feb 2004 07:23:45 -0000       1.4
+++ client/include/diplodlg_g.h 28 Jul 2004 19:24:35 -0000
@@ -13,9 +13,10 @@
 #ifndef FC__DIPLODLG_G_H
 #define FC__DIPLODLG_G_H
 
-#include "diptreaty.h"
 #include "shared.h"
 
+#include "diptreaty.h"
+
 void handle_diplomacy_init_meeting(int counterpart, int initiated_from);
 void handle_diplomacy_cancel_meeting(int counterpart, int initiated_from);
 void handle_diplomacy_create_clause(int counterpart, int giver,
Index: common/capstr.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/capstr.c,v
retrieving revision 1.173
diff -u -r1.173 capstr.c
--- common/capstr.c     20 Jul 2004 11:05:36 -0000      1.173
+++ common/capstr.c     28 Jul 2004 19:24:35 -0000
@@ -77,7 +77,8 @@
                    "+starter +union +iso_maps +big_map_size +orders2client " \
                    "+change_production +tilespec1 +no_earth +trans " \
                    "+want_hack invasions bombard +killstack2 spec +spec2 " \
-                   "+city_map startunits +turn_last_built +happyborders"
+                   "+city_map startunits +turn_last_built +happyborders " \
+                   "+connid"
 
 /* "+1.14.delta" is the new delta protocol for 1.14.0-dev.
  *
@@ -130,6 +131,9 @@
  * 
  * "happyborders" means that units may not cause unhappiness inside
  * our own borders.
+ * 
+ * "connid" adds the connection id of whoever sends a message to the 
+ * info sent to clients.
  */
 
 void init_our_capability(void)
Index: common/connection.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/connection.c,v
retrieving revision 1.41
diff -u -r1.41 connection.c
--- common/connection.c 18 Jul 2004 05:52:36 -0000      1.41
+++ common/connection.c 28 Jul 2004 19:24:35 -0000
@@ -55,6 +55,8 @@
    neccesary as removing a random connection while we are iterating through
    a connection list might corrupt the list. */
 int delayed_disconnect = 0;
+
+struct connection *current_connection;
   
 /**************************************************************************
   Command access levels for client-side use; at present, they are only
Index: common/connection.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/connection.h,v
retrieving revision 1.34
diff -u -r1.34 connection.h
--- common/connection.h 10 Apr 2004 03:47:49 -0000      1.34
+++ common/connection.h 28 Jul 2004 19:24:35 -0000
@@ -223,6 +223,7 @@
   } statistics;
 };
 
+extern struct connection *current_connection;
 
 const char *cmdlevel_name(enum cmdlevel_id lvl);
 enum cmdlevel_id cmdlevel_named(const char *token);
Index: common/packets.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/packets.c,v
retrieving revision 1.267
diff -u -r1.267 packets.c
--- common/packets.c    14 Jan 2004 11:58:12 -0000      1.267
+++ common/packets.c    28 Jul 2004 19:24:35 -0000
@@ -604,6 +604,12 @@
 void pre_send_packet_chat_msg(struct connection *pc,
                              struct packet_chat_msg *packet)
 {
+  if (current_connection) {
+    packet->conn_id = current_connection->id;
+  } else {
+    packet->conn_id = 255;
+  }
+
   if (packet->x == -1 && packet->y == -1) {
     /* since we can currently only send unsigned ints... */
     assert(!is_normal_map_pos(255, 255));
@@ -620,6 +626,9 @@
     packet->x = -1;
     packet->y = -1;
   }
+  if (packet->conn_id == 255) {
+    packet->conn_id = -1;
+  }
 }
 
 void pre_send_packet_player_attribute_chunk(struct connection *pc,
Index: common/packets.def
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/packets.def,v
retrieving revision 1.33
diff -u -r1.33 packets.def
--- common/packets.def  20 Jul 2004 11:05:37 -0000      1.33
+++ common/packets.def  28 Jul 2004 19:24:35 -0000
@@ -365,6 +365,7 @@
   STRING message[MAX_LEN_MSG];
   COORD x, y;
   EVENT event;
+  CONNECTION conn_id;
 end
 
 PACKET_CHAT_MSG_REQ=19;cs,handle-per-conn,dsend
Index: common/packets_gen.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/packets_gen.c,v
retrieving revision 1.37
diff -u -r1.37 packets_gen.c
--- common/packets_gen.c        20 Jul 2004 11:05:37 -0000      1.37
+++ common/packets_gen.c        28 Jul 2004 19:24:38 -0000
@@ -4116,7 +4116,7 @@
 
 #define cmp_packet_chat_msg_100 cmp_const
 
-BV_DEFINE(packet_chat_msg_100_fields, 4);
+BV_DEFINE(packet_chat_msg_100_fields, 5);
 
 static struct packet_chat_msg *receive_packet_chat_msg_100(struct connection 
*pc, enum packet_type type)
 {
@@ -4152,6 +4152,9 @@
   if (BV_ISSET(fields, 3)) {
     dio_get_sint16(&din, (int *) &real_packet->event);
   }
+  if (BV_ISSET(fields, 4)) {
+    dio_get_uint8(&din, (int *) &real_packet->conn_id);
+  }
 
   clone = fc_malloc(sizeof(*clone));
   *clone = *real_packet;
@@ -4211,6 +4214,10 @@
   if(differ) {different++;}
   if(differ) {BV_SET(fields, 3);}
 
+  differ = (old->conn_id != real_packet->conn_id);
+  if(differ) {different++;}
+  if(differ) {BV_SET(fields, 4);}
+
   if (different == 0 && !force_send_of_unchanged) {
 
   if (real_packet != packet) {
@@ -4233,6 +4240,9 @@
   if (BV_ISSET(fields, 3)) {
     dio_put_sint16(&dout, real_packet->event);
   }
+  if (BV_ISSET(fields, 4)) {
+    dio_put_uint8(&dout, real_packet->conn_id);
+  }
 
 
   if (old_from_hash) {
Index: common/packets_gen.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/packets_gen.h,v
retrieving revision 1.29
diff -u -r1.29 packets_gen.h
--- common/packets_gen.h        20 Jul 2004 11:05:37 -0000      1.29
+++ common/packets_gen.h        28 Jul 2004 19:24:38 -0000
@@ -153,6 +153,7 @@
   int x;
   int y;
   enum event_type event;
+  int conn_id;
 };
 
 struct packet_chat_msg_req {
Index: server/sernet.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/sernet.c,v
retrieving revision 1.121
diff -u -r1.121 sernet.c
--- server/sernet.c     20 Jul 2004 17:05:26 -0000      1.121
+++ server/sernet.c     28 Jul 2004 19:24:38 -0000
@@ -609,6 +609,7 @@
                  err = gettimeofday(&start, &tz);
                  assert(!err);
 #endif
+               current_connection = pconn;
                connection_do_buffer(pconn);
                start_processing_request(pconn,
                                         pconn->server.
@@ -621,6 +622,7 @@
 
                finish_processing_request(pconn);
                connection_do_unbuffer(pconn);
+               current_connection = NULL;
                if (!command_ok) {
                  close_connection(pconn);
                }

[Prev in Thread] Current Thread [Next in Thread]