[gnet-dev] [PATCH] gnet_conn_write_direct



Tim Müller wrote:
I suppose that would do as well, yes. Although it forces you to keep
track of things when you mix different types of buffers. Also, it makes
it easy to implement gnet_conn_write() as

gnet_conn_write_direct (conn, g_memdup (buf, len), len, g_free);

hmmm. That's a pretty good point. Here's a patch that does this (attached), diff'd against CVS.

	Jeff


? ChangeLog.cvs
? dolog
? src/base64.lo
? src/conn-http.lo
? src/ipv6.lo
? src/libgnet-2.0.la
? src/socks-private.lo
? src/socks.lo
? src/unix.lo
? src/uri.lo
? src/usagi_ifaddrs.lo
Index: ChangeLog
===================================================================
RCS file: /cvs/gnome/gnet/ChangeLog,v
retrieving revision 1.295
diff -u -r1.295 ChangeLog
--- ChangeLog	27 Oct 2005 17:03:22 -0000	1.295
+++ ChangeLog	9 Nov 2005 11:18:41 -0000
@@ -1,3 +1,9 @@
+2005-11-7   Jeff Garzik  <jgarzik pobox com>
+
+	* src/conn.[ch]:
+	  Add gnet_conn_write_direct(), to allow the caller to
+	  control the write buffer.
+
 2005-10-27  Tim-Philipp Müller  <tim at centricular dot net>
 
 	* src/pack.h:
Index: src/conn.c
===================================================================
RCS file: /cvs/gnome/gnet/src/conn.c,v
retrieving revision 1.21
diff -u -r1.21 conn.c
--- src/conn.c	17 Jan 2004 19:36:49 -0000	1.21
+++ src/conn.c	9 Nov 2005 11:18:42 -0000
@@ -56,7 +56,7 @@
 {
   gchar* 	buffer;
   gint 		length;
-
+  GDestroyNotify buffer_destroy_cb;
 } Write;
 
 
@@ -418,6 +418,14 @@
 }
 
 
+static void
+conn_write_free(Write *write)
+{
+  if (write->buffer_destroy_cb)
+    write->buffer_destroy_cb(write->buffer);
+  g_free (write);
+}
+
 
 /**
  *  gnet_conn_disconnect
@@ -468,10 +476,7 @@
 
   for (i = conn->write_queue; i != NULL; i = i->next)
     {
-      Write* write = i->data;
-      
-      g_free (write->buffer);
-      g_free (write);
+      conn_write_free(i->data);
     }
   g_list_free (conn->write_queue);
   conn->write_queue = NULL;
@@ -1088,18 +1093,20 @@
 
 
 /**
- *  gnet_conn_write
+ *  gnet_conn_write_direct
  *  @conn: a #GConn
  *  @buffer: buffer to write from
  *  @length: length of @buffer
+ *  @buffer_destroy_cb: called when buffer is no longer needed
  *
  *  Sets up an asynchronous write to @conn from @buffer.  The buffer is
- *  copied, so it may be deleted by the caller.  This function can be
- *  called again before the asynchronous write completes.
+ *  created by the caller.  This function can be called again before the
+ *  asynchronous write completes.  @buffer_destroy_cb may be %NULL.
  *
  **/
 void
-gnet_conn_write (GConn* conn, gchar* buffer, gint length)
+gnet_conn_write_direct (GConn* conn, gchar* buffer, gint length,
+			GDestroyNotify buffer_destroy_cb)
 {
   Write* write;
 
@@ -1108,13 +1115,31 @@
 
   /* Add to queue */
   write = g_new0 (Write, 1);
-  write->buffer = g_memdup (buffer, length);
+  write->buffer = buffer;
   write->length = length;
+  write->buffer_destroy_cb = buffer_destroy_cb;
   conn->write_queue = g_list_append (conn->write_queue, write);
 
   conn_check_write_queue (conn);
 }
 
+/**
+ *  gnet_conn_write
+ *  @conn: a #GConn
+ *  @buffer: buffer to write from
+ *  @length: length of @buffer
+ *
+ *  Sets up an asynchronous write to @conn from @buffer.  The buffer is
+ *  copied, so it may be deleted by the caller.  This function can be
+ *  called again before the asynchronous write completes.
+ *
+ **/
+void
+gnet_conn_write (GConn* conn, gchar* buffer, gint length)
+{
+  gnet_conn_write_direct(conn, g_memdup(buffer, length), length, g_free);
+}
+
 
 static void
 conn_check_write_queue (GConn* conn)
@@ -1171,8 +1196,7 @@
     {
       /* Remove and delete the queued write */
       conn->write_queue = g_list_remove (conn->write_queue, write);
-      g_free (write->buffer);
-      g_free (write);
+      conn_write_free(write);
 
       conn->bytes_written = 0;
 
Index: src/conn.h
===================================================================
RCS file: /cvs/gnome/gnet/src/conn.h,v
retrieving revision 1.14
diff -u -r1.14 conn.h
--- src/conn.h	5 Feb 2003 23:40:40 -0000	1.14
+++ src/conn.h	9 Nov 2005 11:18:42 -0000
@@ -138,7 +138,7 @@
  *  object.  The buffer is caller owned.
  *
  *  %GNET_CONN_WRITE: Data has been written.  This event occurs as a
- *  result of calling gnet_conn_write().
+ *  result of calling gnet_conn_write() or gnet_conn_write_direct().
  *
  *  %GNET_CONN_READABLE: The connection is readable.
  *
@@ -231,6 +231,8 @@
 void	   gnet_conn_readline (GConn* conn);
 
 void	   gnet_conn_write (GConn* conn, gchar* buffer, gint length);
+void	   gnet_conn_write_direct (GConn* conn, gchar* buffer, gint length,
+				   GDestroyNotify buffer_destroy_cb);
 
 void	   gnet_conn_set_watch_readable (GConn* conn, gboolean enable);
 void	   gnet_conn_set_watch_writable (GConn* conn, gboolean enable);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]