Be a little more abstract about which connection type use bufferevents

This commit is contained in:
Nick Mathewson 2009-08-09 10:53:06 -07:00
parent 9f8027abfd
commit 5279036148
3 changed files with 38 additions and 7 deletions

View File

@ -183,6 +183,21 @@ conn_state_to_string(int type, int state)
return buf; 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 /** Allocate and return a new dir_connection_t, initialized as by
* connection_init(). */ * connection_init(). */
dir_connection_t * dir_connection_t *
@ -308,7 +323,10 @@ connection_init(time_t now, connection_t *conn, int type, int socket_family)
conn->type = type; conn->type = type;
conn->socket_family = socket_family; 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->inbuf = buf_new();
conn->outbuf = buf_new(); conn->outbuf = buf_new();
} }
@ -3566,8 +3584,8 @@ assert_connection_ok(connection_t *conn, time_t now)
if (conn->bufev) { if (conn->bufev) {
tor_assert(conn->read_event == NULL); tor_assert(conn->read_event == NULL);
tor_assert(conn->write_event == NULL); tor_assert(conn->write_event == NULL);
/* XXX reinstate tor_assert(conn->inbuf == NULL); tor_assert(conn->inbuf == NULL);
tor_assert(conn->outbuf == NULL);*/ tor_assert(conn->outbuf == NULL);
} }
#endif #endif
@ -3609,10 +3627,10 @@ assert_connection_ok(connection_t *conn, time_t now)
* marked_for_close. */ * marked_for_close. */
/* buffers */ /* buffers */
if (!connection_is_listener(conn)) { if (conn->inbuf)
assert_buf_ok(conn->inbuf); assert_buf_ok(conn->inbuf);
if (conn->outbuf)
assert_buf_ok(conn->outbuf); assert_buf_ok(conn->outbuf);
}
if (conn->type == CONN_TYPE_OR) { if (conn->type == CONN_TYPE_OR) {
or_connection_t *or_conn = TO_OR_CONN(conn); or_connection_t *or_conn = TO_OR_CONN(conn);

View File

@ -134,7 +134,10 @@ void connection_dump_buffer_mem_stats(int severity);
void remove_file_if_very_old(const char *fname, time_t now); void remove_file_if_very_old(const char *fname, time_t now);
#ifdef USE_BUFFEREVENTS #ifdef USE_BUFFEREVENTS
int connection_type_uses_bufferevent(connection_t *conn);
void connection_configure_bufferevent_callbacks(connection_t *conn); void connection_configure_bufferevent_callbacks(connection_t *conn);
#else
#define connection_type_uses_bufferevent(c) (0)
#endif #endif
#endif #endif

View File

@ -173,12 +173,22 @@ connection_add(connection_t *conn)
smartlist_add(connection_array, conn); smartlist_add(connection_array, conn);
#ifdef USE_BUFFEREVENTS #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( conn->bufev = bufferevent_socket_new(
tor_libevent_get_base(), tor_libevent_get_base(),
conn->s, conn->s,
BEV_OPT_DEFER_CALLBACKS); 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); connection_configure_bufferevent_callbacks(conn);
} }
#endif #endif