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

[or-cvs] [tor/master 15/38] Be a little more abstract about which connection type use bufferevents



Author: Nick Mathewson <nickm@xxxxxxxxxxxxxx>
Date: Sun, 9 Aug 2009 10:53:06 -0700
Subject: Be a little more abstract about which connection type use bufferevents
Commit: 5279036148ca158f7c60f793f401604060b1c1ba

---
 src/or/connection.c |   28 +++++++++++++++++++++++-----
 src/or/connection.h |    3 +++
 src/or/main.c       |   14 ++++++++++++--
 3 files changed, 38 insertions(+), 7 deletions(-)

diff --git a/src/or/connection.c b/src/or/connection.c
index 3788348..54b9911 100644
--- a/src/or/connection.c
+++ b/src/or/connection.c
@@ -183,6 +183,21 @@ conn_state_to_string(int type, int state)
   return buf;
 }
 
+#ifdef USE_BUFFEREVENTS
+/** Return true iff the connection's type is one that can use a
+    bufferevent-based implementation. */
+int
+connection_type_uses_bufferevent(connection_t *conn)
+{
+  switch (conn->type) {
+    case CONN_TYPE_AP:
+      return 1;
+    default:
+      return 0;
+  }
+}
+#endif
+
 /** Allocate and return a new dir_connection_t, initialized as by
  * connection_init(). */
 dir_connection_t *
@@ -308,7 +323,10 @@ connection_init(time_t now, connection_t *conn, int type, int socket_family)
 
   conn->type = type;
   conn->socket_family = socket_family;
-  if (!connection_is_listener(conn)) { /* listeners never use their buf */
+  if (!connection_is_listener(conn)) {
+    /* listeners never use their buf */
+    /* XXX and bufferevents don't either, but for now we leave this here
+     * so that linked connections can still work. */
     conn->inbuf = buf_new();
     conn->outbuf = buf_new();
   }
@@ -3566,8 +3584,8 @@ assert_connection_ok(connection_t *conn, time_t now)
   if (conn->bufev) {
     tor_assert(conn->read_event == NULL);
     tor_assert(conn->write_event == NULL);
-    /* XXX reinstate tor_assert(conn->inbuf == NULL);
-       tor_assert(conn->outbuf == NULL);*/
+    tor_assert(conn->inbuf == NULL);
+    tor_assert(conn->outbuf == NULL);
   }
 #endif
 
@@ -3609,10 +3627,10 @@ assert_connection_ok(connection_t *conn, time_t now)
    * marked_for_close. */
 
   /* buffers */
-  if (!connection_is_listener(conn)) {
+  if (conn->inbuf)
     assert_buf_ok(conn->inbuf);
+  if (conn->outbuf)
     assert_buf_ok(conn->outbuf);
-  }
 
   if (conn->type == CONN_TYPE_OR) {
     or_connection_t *or_conn = TO_OR_CONN(conn);
diff --git a/src/or/connection.h b/src/or/connection.h
index 906f9ba..af21481 100644
--- a/src/or/connection.h
+++ b/src/or/connection.h
@@ -134,7 +134,10 @@ void connection_dump_buffer_mem_stats(int severity);
 void remove_file_if_very_old(const char *fname, time_t now);
 
 #ifdef USE_BUFFEREVENTS
+int connection_type_uses_bufferevent(connection_t *conn);
 void connection_configure_bufferevent_callbacks(connection_t *conn);
+#else
+#define connection_type_uses_bufferevent(c) (0)
 #endif
 
 #endif
diff --git a/src/or/main.c b/src/or/main.c
index 85c99ff..18473c3 100644
--- a/src/or/main.c
+++ b/src/or/main.c
@@ -173,12 +173,22 @@ connection_add(connection_t *conn)
   smartlist_add(connection_array, conn);
 
 #ifdef USE_BUFFEREVENTS
-  if (conn->type == CONN_TYPE_AP && conn->s >= 0 && !conn->linked) {
+  if (connection_type_uses_bufferevent(conn) &&
+      conn->s >= 0 && !conn->linked) {
     conn->bufev = bufferevent_socket_new(
                          tor_libevent_get_base(),
                          conn->s,
                          BEV_OPT_DEFER_CALLBACKS);
-
+    if (conn->inbuf) {
+      /* XXX Instead we should assert that there is no inbuf, once we
+       * have linked connections using bufferevents. */
+      tor_assert(conn->outbuf);
+      tor_assert(buf_datalen(conn->inbuf) == 0);
+      tor_assert(buf_datalen(conn->outbuf) == 0);
+      buf_free(conn->inbuf);
+      buf_free(conn->outbuf);
+      conn->inbuf = conn->outbuf = NULL;
+    }
     connection_configure_bufferevent_callbacks(conn);
   }
 #endif
-- 
1.7.1