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

[or-cvs] r9209: Refactor connection_write_to_buf_zlib back into connection_w (in tor/trunk: . src/or)



Author: nickm
Date: 2006-12-29 00:07:04 -0500 (Fri, 29 Dec 2006)
New Revision: 9209

Modified:
   tor/trunk/
   tor/trunk/src/or/connection.c
   tor/trunk/src/or/dirserv.c
   tor/trunk/src/or/or.h
Log:
 r11744@Kushana:  nickm | 2006-12-28 23:43:53 -0500
 Refactor connection_write_to_buf_zlib back into connection_write_to_buf.  Hooray for dumping duplicate code.



Property changes on: tor/trunk
___________________________________________________________________
 svk:merge ticket from /tor/trunk [r11744] on c95137ef-5f19-0410-b913-86e773d04f59

Modified: tor/trunk/src/or/connection.c
===================================================================
--- tor/trunk/src/or/connection.c	2006-12-29 05:06:47 UTC (rev 9208)
+++ tor/trunk/src/or/connection.c	2006-12-29 05:07:04 UTC (rev 9209)
@@ -1745,18 +1745,35 @@
 
 /** Append <b>len</b> bytes of <b>string</b> onto <b>conn</b>'s
  * outbuf, and ask it to start writing.
+ *
+ * If <b>zlib</b> is nonzero, this is a directory connection that should get
+ * its contents compressed or decompressed as they're written.  If zlib is
+ * negative, this is the last data to be compressed, and the connection's zlib
+ * state should be flushed.
  */
 void
-connection_write_to_buf(const char *string, size_t len, connection_t *conn)
+_connection_write_to_buf_impl(const char *string, size_t len,
+                              connection_t *conn, int zlib)
 {
   int r;
+  size_t old_datalen;
   if (!len)
     return;
   /* if it's marked for close, only allow write if we mean to flush it */
   if (conn->marked_for_close && !conn->hold_open_until_flushed)
     return;
 
-  CONN_LOG_PROTECT(conn, r = write_to_buf(string, len, conn->outbuf));
+  old_datalen = buf_datalen(conn->outbuf);
+  if (zlib) {
+    dir_connection_t *dir_conn = TO_DIR_CONN(conn);
+    int done = zlib < 0;
+    if (!dir_conn) return;
+    CONN_LOG_PROTECT(conn, r = write_to_buf_zlib(conn->outbuf,
+                                                 dir_conn->zlib_state,
+                                                 string, len, done));
+  } else {
+    CONN_LOG_PROTECT(conn, r = write_to_buf(string, len, conn->outbuf));
+  }
   if (r < 0) {
     if (CONN_IS_EDGE(conn)) {
       /* if it failed, it means we have our package/delivery windows set
@@ -1774,40 +1791,12 @@
   }
 
   connection_start_writing(conn);
-  conn->outbuf_flushlen += len;
+  if (zlib)
+    conn->outbuf_flushlen += buf_datalen(conn->outbuf) - old_datalen;
+  else
+    conn->outbuf_flushlen += len;
 }
 
-void
-connection_write_to_buf_zlib(dir_connection_t *dir_conn,
-                             const char *data, size_t data_len,
-                             int done)
-{
-  int r;
-  size_t old_datalen;
-  connection_t *conn;
-  if (!data_len)
-    return;
-  conn = TO_CONN(dir_conn);
-  /* if it's marked for close, only allow write if we mean to flush it */
-  if (conn->marked_for_close && !conn->hold_open_until_flushed)
-    return;
-
-  old_datalen = buf_datalen(conn->outbuf);
-  /* XXXX TOO much duplicate code! XXXX012NM */
-  CONN_LOG_PROTECT(conn, r = write_to_buf_zlib(
-                                conn->outbuf, dir_conn->zlib_state,
-                                data, data_len, done));
-  if (r < 0) {
-    log_warn(LD_NET,
-             "write_to_buf failed. Closing connection (fd %d).", conn->s);
-    connection_mark_for_close(conn);
-    return;
-  }
-
-  connection_start_writing(conn);
-  conn->outbuf_flushlen += buf_datalen(conn->outbuf) - old_datalen;
-}
-
 /** Return the conn to addr/port that has the most recent
  * timestamp_created, or NULL if no such conn exists. */
 or_connection_t *

Modified: tor/trunk/src/or/dirserv.c
===================================================================
--- tor/trunk/src/or/dirserv.c	2006-12-29 05:06:47 UTC (rev 9208)
+++ tor/trunk/src/or/dirserv.c	2006-12-29 05:07:04 UTC (rev 9209)
@@ -1939,7 +1939,7 @@
 connection_dirserv_finish_spooling(dir_connection_t *conn)
 {
   if (conn->zlib_state) {
-    connection_write_to_buf_zlib(conn, "", 0, 1);
+    connection_write_to_buf_zlib("", 0, conn, 1);
     tor_zlib_free(conn->zlib_state);
     conn->zlib_state = NULL;
   }
@@ -1975,8 +1975,8 @@
     body = signed_descriptor_get_body(sd);
     if (conn->zlib_state) {
       int last = ! smartlist_len(conn->fingerprint_stack);
-      connection_write_to_buf_zlib(conn, body,
-                                   sd->signed_descriptor_len, last);
+      connection_write_to_buf_zlib(body, sd->signed_descriptor_len, conn,
+                                   last);
       if (last) {
         tor_zlib_free(conn->zlib_state);
         conn->zlib_state = NULL;
@@ -2014,9 +2014,9 @@
     bytes = (ssize_t) remaining;
 
   if (conn->zlib_state) {
-    connection_write_to_buf_zlib(conn,
+    connection_write_to_buf_zlib(
                              conn->cached_dir->dir_z + conn->cached_dir_offset,
-                             bytes, bytes == remaining);
+                             bytes, conn, bytes == remaining);
   } else {
     connection_write_to_buf(conn->cached_dir->dir_z + conn->cached_dir_offset,
                             bytes, TO_CONN(conn));

Modified: tor/trunk/src/or/or.h
===================================================================
--- tor/trunk/src/or/or.h	2006-12-29 05:06:47 UTC (rev 9208)
+++ tor/trunk/src/or/or.h	2006-12-29 05:07:04 UTC (rev 9209)
@@ -2000,11 +2000,23 @@
 int connection_wants_to_flush(connection_t *conn);
 int connection_outbuf_too_full(connection_t *conn);
 int connection_handle_write(connection_t *conn, int force);
-void connection_write_to_buf(const char *string, size_t len,
-                             connection_t *conn);
-void connection_write_to_buf_zlib(dir_connection_t *conn,
-                                  const char *data, size_t data_len,
-                                  int done);
+void _connection_write_to_buf_impl(const char *string, size_t len,
+                                   connection_t *conn, int zlib);
+static void connection_write_to_buf(const char *string, size_t len,
+                                    connection_t *conn);
+static void connection_write_to_buf_zlib(const char *string, size_t len,
+                                         dir_connection_t *conn, int done);
+static INLINE void
+connection_write_to_buf(const char *string, size_t len, connection_t *conn)
+{
+  _connection_write_to_buf_impl(string, len, conn, 0);
+}
+static INLINE void
+connection_write_to_buf_zlib(const char *string, size_t len,
+                             dir_connection_t *conn, int done)
+{
+  _connection_write_to_buf_impl(string, len, TO_CONN(conn), done ? -1 : 1);
+}
 
 or_connection_t *connection_or_exact_get_by_addr_port(uint32_t addr,
                                                    uint16_t port);