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 *
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();
}
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
* 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);
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
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