From 68c9a71774e86852506676e92c9336ebb4cdffa0 Mon Sep 17 00:00:00 2001 From: Jack Moffitt Date: Thu, 1 Dec 2005 23:09:20 +0000 Subject: [PATCH] Partial fix for #11. Correctly calls connection handler on timeout now, but doesn't yet set the error. --- src/common.h | 4 ++++ src/conn.c | 9 ++++++++- src/event.c | 12 ++++++++++-- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/common.h b/src/common.h index 978afe3..6a65fbf 100644 --- a/src/common.h +++ b/src/common.h @@ -153,6 +153,7 @@ struct _xmpp_conn_t { xmpp_conn_type_t type; xmpp_conn_state_t state; + uint64_t timeout_stamp; int error; xmpp_stream_error_t *stream_error; sock_t sock; @@ -184,6 +185,9 @@ struct _xmpp_conn_t { int depth; xmpp_stanza_t *stanza; + /* timeouts */ + unsigned int connect_timeout; + /* event handlers */ /* stream open handler */ diff --git a/src/conn.c b/src/conn.c index 8c832bc..9ce7fd4 100644 --- a/src/conn.c +++ b/src/conn.c @@ -18,6 +18,7 @@ #include "strophe.h" #include "common.h" +#include "util.h" #ifdef _WIN32 #define vsnprintf _vsnprintf @@ -25,6 +26,7 @@ #define DEFAULT_SEND_QUEUE_MAX 64 #define DISCONNECT_TIMEOUT 2000 /* 2 seconds */ +#define CONNECT_TIMEOUT 5000 /* 5 seconds */ static int _disconnect_cleanup(xmpp_conn_t * const conn, void * const userdata); @@ -43,6 +45,7 @@ xmpp_conn_t *xmpp_conn_new(xmpp_ctx_t * const ctx) conn->type = XMPP_UNKNOWN; conn->sock = -1; + conn->timeout_stamp = 0; conn->error = 0; conn->stream_error = NULL; @@ -53,6 +56,9 @@ xmpp_conn_t *xmpp_conn_new(xmpp_ctx_t * const ctx) conn->send_queue_head = NULL; conn->send_queue_tail = NULL; + /* default timeouts */ + conn->connect_timeout = CONNECT_TIMEOUT; + conn->lang = xmpp_strdup(conn->ctx, "en"); if (!conn->lang) { xmpp_free(conn->ctx, conn); @@ -260,6 +266,7 @@ int xmpp_connect_client(xmpp_conn_t * const conn, * from within the event loop */ conn->state = XMPP_STATE_CONNECTING; + conn->timeout_stamp = time_stamp(); xmpp_debug(conn->ctx, "xmpp", "attempting to connect to %s", conn->domain); return 0; @@ -278,7 +285,7 @@ void conn_disconnect_clean(xmpp_conn_t * const conn) void conn_disconnect(xmpp_conn_t * const conn) { - xmpp_info(conn->ctx, "xmpp", "Disconnected from server"); + xmpp_debug(conn->ctx, "xmpp", "Closing socket."); conn->state = XMPP_STATE_DISCONNECTED; sock_close(conn->sock); diff --git a/src/event.c b/src/event.c index d4e0e70..a6d4c3a 100644 --- a/src/event.c +++ b/src/event.c @@ -101,7 +101,7 @@ void xmpp_run_once(xmpp_ctx_t *ctx, const unsigned long timeout) make sure we don't wait past the time when timed handlers need to be called */ next = handler_fire_timed(ctx); - + tv.tv_sec = 0; tv.tv_usec = ((next < timeout) ? next : timeout) * 1000; @@ -117,7 +117,15 @@ void xmpp_run_once(xmpp_ctx_t *ctx, const unsigned long timeout) case XMPP_STATE_CONNECTING: /* connect has been called and we're waiting for it to complete */ /* connection will give us write or error events */ - FD_SET(conn->sock, &wfds); + + /* make sure the timeout hasn't expired */ + if (time_elapsed(conn->timeout_stamp, time_stamp()) <= + conn->connect_timeout) + FD_SET(conn->sock, &wfds); + else { + xmpp_info(ctx, "xmpp", "Connection attempt timed out."); + conn_disconnect(conn); + } break; case XMPP_STATE_CONNECTED: FD_SET(conn->sock, &rfds);