Partial fix for #11. Correctly calls connection handler on timeout now,
but doesn't yet set the error.
This commit is contained in:
@@ -153,6 +153,7 @@ struct _xmpp_conn_t {
|
|||||||
xmpp_conn_type_t type;
|
xmpp_conn_type_t type;
|
||||||
|
|
||||||
xmpp_conn_state_t state;
|
xmpp_conn_state_t state;
|
||||||
|
uint64_t timeout_stamp;
|
||||||
int error;
|
int error;
|
||||||
xmpp_stream_error_t *stream_error;
|
xmpp_stream_error_t *stream_error;
|
||||||
sock_t sock;
|
sock_t sock;
|
||||||
@@ -184,6 +185,9 @@ struct _xmpp_conn_t {
|
|||||||
int depth;
|
int depth;
|
||||||
xmpp_stanza_t *stanza;
|
xmpp_stanza_t *stanza;
|
||||||
|
|
||||||
|
/* timeouts */
|
||||||
|
unsigned int connect_timeout;
|
||||||
|
|
||||||
/* event handlers */
|
/* event handlers */
|
||||||
|
|
||||||
/* stream open handler */
|
/* stream open handler */
|
||||||
|
|||||||
@@ -18,6 +18,7 @@
|
|||||||
|
|
||||||
#include "strophe.h"
|
#include "strophe.h"
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#define vsnprintf _vsnprintf
|
#define vsnprintf _vsnprintf
|
||||||
@@ -25,6 +26,7 @@
|
|||||||
|
|
||||||
#define DEFAULT_SEND_QUEUE_MAX 64
|
#define DEFAULT_SEND_QUEUE_MAX 64
|
||||||
#define DISCONNECT_TIMEOUT 2000 /* 2 seconds */
|
#define DISCONNECT_TIMEOUT 2000 /* 2 seconds */
|
||||||
|
#define CONNECT_TIMEOUT 5000 /* 5 seconds */
|
||||||
|
|
||||||
static int _disconnect_cleanup(xmpp_conn_t * const conn,
|
static int _disconnect_cleanup(xmpp_conn_t * const conn,
|
||||||
void * const userdata);
|
void * const userdata);
|
||||||
@@ -43,6 +45,7 @@ xmpp_conn_t *xmpp_conn_new(xmpp_ctx_t * const ctx)
|
|||||||
|
|
||||||
conn->type = XMPP_UNKNOWN;
|
conn->type = XMPP_UNKNOWN;
|
||||||
conn->sock = -1;
|
conn->sock = -1;
|
||||||
|
conn->timeout_stamp = 0;
|
||||||
conn->error = 0;
|
conn->error = 0;
|
||||||
conn->stream_error = NULL;
|
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_head = NULL;
|
||||||
conn->send_queue_tail = NULL;
|
conn->send_queue_tail = NULL;
|
||||||
|
|
||||||
|
/* default timeouts */
|
||||||
|
conn->connect_timeout = CONNECT_TIMEOUT;
|
||||||
|
|
||||||
conn->lang = xmpp_strdup(conn->ctx, "en");
|
conn->lang = xmpp_strdup(conn->ctx, "en");
|
||||||
if (!conn->lang) {
|
if (!conn->lang) {
|
||||||
xmpp_free(conn->ctx, conn);
|
xmpp_free(conn->ctx, conn);
|
||||||
@@ -260,6 +266,7 @@ int xmpp_connect_client(xmpp_conn_t * const conn,
|
|||||||
* from within the event loop */
|
* from within the event loop */
|
||||||
|
|
||||||
conn->state = XMPP_STATE_CONNECTING;
|
conn->state = XMPP_STATE_CONNECTING;
|
||||||
|
conn->timeout_stamp = time_stamp();
|
||||||
xmpp_debug(conn->ctx, "xmpp", "attempting to connect to %s", conn->domain);
|
xmpp_debug(conn->ctx, "xmpp", "attempting to connect to %s", conn->domain);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
@@ -278,7 +285,7 @@ void conn_disconnect_clean(xmpp_conn_t * const conn)
|
|||||||
|
|
||||||
void conn_disconnect(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;
|
conn->state = XMPP_STATE_DISCONNECTED;
|
||||||
sock_close(conn->sock);
|
sock_close(conn->sock);
|
||||||
|
|
||||||
|
|||||||
12
src/event.c
12
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
|
make sure we don't wait past the time when timed handlers need
|
||||||
to be called */
|
to be called */
|
||||||
next = handler_fire_timed(ctx);
|
next = handler_fire_timed(ctx);
|
||||||
|
|
||||||
tv.tv_sec = 0;
|
tv.tv_sec = 0;
|
||||||
tv.tv_usec = ((next < timeout) ? next : timeout) * 1000;
|
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:
|
case XMPP_STATE_CONNECTING:
|
||||||
/* connect has been called and we're waiting for it to complete */
|
/* connect has been called and we're waiting for it to complete */
|
||||||
/* connection will give us write or error events */
|
/* 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;
|
break;
|
||||||
case XMPP_STATE_CONNECTED:
|
case XMPP_STATE_CONNECTED:
|
||||||
FD_SET(conn->sock, &rfds);
|
FD_SET(conn->sock, &rfds);
|
||||||
|
|||||||
Reference in New Issue
Block a user