[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]
<URL: http://rt.freeciv.org/Ticket/Display.html?id=9513 >
Jason Short wrote:
> <URL: http://rt.freeciv.org/Ticket/Display.html?id=9513 >
>
> Two more problems:
>
> - There is a global server variable current_connection added into the
> common code. This is quite bad IMO. It is error-prone and makes the
> logic a lot more complicated.
This patch fixes this problem, as well as improving some other parts of
the code (IMO):
- pline->text is freed, not just pline (memory management would be
easier with specvecs since we wouldn't have to free pline).
- base_append_output_window is renamed append_output_window_full. Note
we already have a real_append_output_window. These names are ridiculous.
- Lots of comments added, and some style fixed.
jason
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 22:12:58 -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,12 @@
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) {
+ append_output_window_full(pline->text, pline->conn_id);
+ free(pline->text);
+ free(pline);
+ } remaining_list_iterate_end;
+ remaining_list_unlink_all(&remains);
}
}
@@ -83,24 +103,26 @@
}
/**************************************************************************
-...
+ Add a line of text to the output ("chatline") window.
**************************************************************************/
void append_output_window(const char *astring)
{
+ append_output_window_full(astring, -1);
+}
+
+/**************************************************************************
+ Same as above, but here we know the connection id of the sender of the
+ text in question.
+**************************************************************************/
+void append_output_window_full(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(*premain));
- /* +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 22:12:58 -0000
@@ -17,7 +17,10 @@
void send_chat(const char *message);
+void chatline_common_init(void);
+
void append_output_window(const char *astring);
+void append_output_window_full(const char *astring, int conn_id);
void output_window_freeze(void);
void output_window_thaw(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 22:12:58 -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 22:12:58 -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 22:12:59 -0000
@@ -849,9 +849,11 @@
}
/**************************************************************************
-...
+ Handle a message packet. This includes all messages - both
+ in-game messages and chats from other players.
**************************************************************************/
-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 +865,7 @@
}
if (BOOL_VAL(where & MW_OUTPUT)) {
- append_output_window(message);
+ append_output_window_full(message, conn_id);
}
if (BOOL_VAL(where & MW_MESSAGES)) {
add_notify_window(message, x, y, event);
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 22:12:59 -0000
@@ -63,9 +63,10 @@
}
/**************************************************************************
-...
+ 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)
{
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 22:12:59 -0000
@@ -65,9 +65,10 @@
}
/**************************************************************************
-...
+ 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)
{
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 22:12:59 -0000
@@ -34,7 +34,11 @@
#include "gui_main.h"
#include "muistuff.h"
-void real_append_output_window(const char *astring)
+/**************************************************************************
+ 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, 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 22:12:59 -0000
@@ -129,11 +129,12 @@
}
/**************************************************************************
- Appends the string to the chat output window.
- Curretn it is wraper to message subsystem.
+ 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)
{
+ /* Currently this is a wrapper to the message subsystem. */
if (pConnDlg) {
Uint16 *pUniStr;
size_t n = strlen(astring);
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 22:12:59 -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 22:12:59 -0000
@@ -85,18 +85,19 @@
}
/**************************************************************************
-
+ 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)
- /* We need to add \r to lineends */
+void real_append_output_window(const char *astring, int conn_id)
{
-
const char *str;
char *str2;
char buf[512];
+
str=astring;
while((str2=strchr(str,'\n')))
{
+ /* HACK: We need to add \r to lineends. */
strncpy(buf,str,str2-str);
buf[str2-str]=0;
strcat(buf,"\r\n");
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 22:12:59 -0000
@@ -55,25 +55,28 @@
}
/**************************************************************************
- this is properly a bad way to append to a text widget. Using the
- "useStringInPlace" resource and doubling mem alloc'ing would be better.
- Nope - tried it and many other variations and it wasn't any better.
- I'll replace this widget with a widget supportting hyperlinks later, so
- leth's forget the problem.
-
- There seems to be an Xaw problem with doing both wrap and scroll:
- its supposed to automatically scroll to end when we change the insertion
- point, but if a line is wrapped the scroll lags behind a line, and
- stays behind on subsequent additions, until the too-long line scrolls
- off the top. (I tried setting the insert position to the last char,
- instead of the start of line as below, but that didn't help.) So we
- split the line ourselves. I'm just using a fixed length split; should
- perhaps check and use width of output window (but font size?) -dwp
-
- Now uses window's font size and width. Assumes fixed-width font. --jjm
+ 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 *input_string)
+void real_append_output_window(const char *astring, int conn_id)
{
+ /* this is properly a bad way to append to a text widget. Using the
+ * "useStringInPlace" resource and doubling mem alloc'ing would be better.
+ * Nope - tried it and many other variations and it wasn't any better.
+ * I'll replace this widget with a widget supportting hyperlinks later, so
+ * leth's forget the problem.
+ *
+ * There seems to be an Xaw problem with doing both wrap and scroll:
+ * its supposed to automatically scroll to end when we change the insertion
+ * point, but if a line is wrapped the scroll lags behind a line, and
+ * stays behind on subsequent additions, until the too-long line scrolls
+ * off the top. (I tried setting the insert position to the last char,
+ * instead of the start of line as below, but that didn't help.) So we
+ * split the line ourselves. I'm just using a fixed length split; should
+ * perhaps check and use width of output window (but font size?) -dwp
+ *
+ * Now uses window's font size and width. Assumes fixed-width font. --jjm
+ */
static int m_width=0;
Dimension windowwth;
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 22:12:59 -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 22:12:59 -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 22:12:59 -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 22:12:59 -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/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 22:12:59 -0000
@@ -601,9 +601,19 @@
connection_do_unbuffer(pconn);
}
+/**************************************************************************
+ This function is a hack to convert the internal form for storing
+ connection IDs and map positions into the network form. It's called
+ directly on a packet before it's written to the network.
+**************************************************************************/
void pre_send_packet_chat_msg(struct connection *pc,
struct packet_chat_msg *packet)
{
+ if (packet->conn_id == -1) {
+ /* since we can currently only send unsigned ints... */
+ 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));
@@ -612,6 +622,11 @@
}
}
+/**************************************************************************
+ This function is a hack to convert the network form for storing
+ connection IDs and map positions into the internal form. It's
+ called directly on a packet after it's been received from the network.
+**************************************************************************/
void post_receive_packet_chat_msg(struct connection *pc,
struct packet_chat_msg *packet)
{
@@ -620,6 +635,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.34
diff -u -r1.34 packets.def
--- common/packets.def 28 Jul 2004 21:48:11 -0000 1.34
+++ common/packets.def 28 Jul 2004 22:13:00 -0000
@@ -369,6 +369,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: server/handchat.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/handchat.c,v
retrieving revision 1.30
diff -u -r1.30 handchat.c
--- server/handchat.c 28 Jul 2004 21:43:11 -0000 1.30
+++ server/handchat.c 28 Jul 2004 22:13:00 -0000
@@ -74,7 +74,7 @@
default:
assert(0);
}
- dsend_packet_chat_msg(pconn, message, -1, -1, E_NOEVENT);
+ dsend_packet_chat_msg(pconn, message, -1, -1, E_NOEVENT, -1);
}
/**************************************************************************
@@ -92,11 +92,11 @@
form_chat_name(dest, dest_name, sizeof(dest_name));
my_snprintf(message, sizeof(message), "->*%s* %s", dest_name, msg);
- dsend_packet_chat_msg(sender, message, -1, -1, E_NOEVENT);
+ dsend_packet_chat_msg(sender, message, -1, -1, E_NOEVENT, sender->id);
if (sender != dest) {
my_snprintf(message, sizeof(message), "*%s* %s", sender_name, msg);
- dsend_packet_chat_msg(dest, message, -1, -1, E_NOEVENT);
+ dsend_packet_chat_msg(dest, message, -1, -1, E_NOEVENT, sender->id);
}
}
@@ -113,12 +113,13 @@
form_chat_name(sender, sender_name, sizeof(sender_name));
my_snprintf(message, sizeof(message), "->[%s] %s", pdest->name, msg);
- dsend_packet_chat_msg(sender, message, -1, -1, E_NOEVENT);
+ dsend_packet_chat_msg(sender, message, -1, -1, E_NOEVENT, sender->id);
my_snprintf(message, sizeof(message), "[%s] %s", sender_name, msg);
conn_list_iterate(pdest->connections, dest_conn) {
if (dest_conn != sender) {
- dsend_packet_chat_msg(dest_conn, message, -1, -1, E_NOEVENT);
+ dsend_packet_chat_msg(dest_conn, message,
+ -1, -1, E_NOEVENT, sender->id);
}
} conn_list_iterate_end;
}
@@ -181,11 +182,14 @@
my_snprintf(chat, sizeof(chat),
_("%s to allies: %s"), sender_name,
skip_leading_spaces(message));
+ /* FIXME: there should be a special case for the sender, like in
+ * chat_msg_to_player_multi(). */
players_iterate(aplayer) {
if (!pplayers_allied(pconn->player, aplayer)) {
continue;
}
- dlsend_packet_chat_msg(&aplayer->connections, chat, -1, -1, E_NOEVENT);
+ dlsend_packet_chat_msg(&aplayer->connections, chat, -1, -1,
+ E_NOEVENT, pconn->id);
} players_iterate_end;
return;
}
@@ -271,7 +275,7 @@
/* Would have done something above if connected */
my_snprintf(chat, sizeof(chat),
_("Game: %s is not connected."), pdest->name);
- dsend_packet_chat_msg(pconn, chat, -1, -1, E_NOEVENT);
+ dsend_packet_chat_msg(pconn, chat, -1, -1, E_NOEVENT, -1);
return;
}
}
@@ -288,7 +292,7 @@
_("Game: There is no player nor connection by the name
%s."),
name);
}
- dsend_packet_chat_msg(pconn, chat, -1, -1, E_NOEVENT);
+ dsend_packet_chat_msg(pconn, chat, -1, -1, E_NOEVENT, -1);
return;
}
}
@@ -296,5 +300,6 @@
form_chat_name(pconn, sender_name, sizeof(sender_name));
my_snprintf(chat, sizeof(chat),
"<%s> %s", sender_name, message);
- dlsend_packet_chat_msg(&game.game_connections, chat, -1, -1, E_NOEVENT);
+ dlsend_packet_chat_msg(&game.game_connections, chat,
+ -1, -1, E_NOEVENT, pconn->id);
}
Index: server/plrhand.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/plrhand.c,v
retrieving revision 1.313
diff -u -r1.313 plrhand.c
--- server/plrhand.c 13 Jul 2004 21:54:17 -0000 1.313
+++ server/plrhand.c 28 Jul 2004 22:13:01 -0000
@@ -1145,6 +1145,7 @@
my_vsnprintf(genmsg.message, sizeof(genmsg.message), format, vargs);
genmsg.event = event;
+ genmsg.conn_id = -1;
conn_list_iterate(*dest, pconn) {
if (server_state >= RUN_GAME_STATE
@@ -1239,12 +1240,15 @@
{
struct packet_chat_msg genmsg;
va_list args;
+
va_start(args, format);
my_vsnprintf(genmsg.message, sizeof(genmsg.message), format, args);
va_end(args);
+
genmsg.x = -1;
genmsg.y = -1;
genmsg.event = E_NOEVENT;
+ genmsg.conn_id = -1;
players_iterate(other_player) {
if (player_has_embassy(other_player, pplayer)
|
|