Partial fix for #11. Correctly calls connection handler on timeout now,

but doesn't yet set the error.
This commit is contained in:
Jack Moffitt
2005-12-01 23:09:20 +00:00
parent 003b0528cd
commit 68c9a71774
3 changed files with 22 additions and 3 deletions

View File

@@ -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 */

View File

@@ -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);

View File

@@ -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);