Skip to content

Commit

Permalink
Fix bug: Repeated connections will cause timeout detection errors
Browse files Browse the repository at this point in the history
Signed-off-by: Jianhui Zhao <jianhuizhao329@gmail.com>
  • Loading branch information
Jianhui Zhao committed Sep 10, 2018
1 parent 832ea32 commit c70e821
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 20 deletions.
28 changes: 10 additions & 18 deletions src/uwsc.c
Original file line number Diff line number Diff line change
Expand Up @@ -453,18 +453,10 @@ static void uwsc_handshake(struct uwsc_client *cl, const char *host, int port, c
static void uwsc_timer_cb(struct ev_loop *loop, struct ev_timer *w, int revents)
{
struct uwsc_client *cl = container_of(w, struct uwsc_client, timer);
static time_t connect_time;
static time_t last_ping;
static int ntimeout;
time_t now = time(NULL);
ev_tstamp now = ev_now(loop);

if (unlikely(cl->state == CLIENT_STATE_CONNECTING)) {
if (connect_time == 0) {
connect_time = now;
return;
}

if (now - connect_time > 5) {
if (now - cl->start_time > UWSC_MAX_CONNECT_TIME) {
uwsc_error(cl, UWSC_ERROR_CONNECT, "Connect timeout");
return;
}
Expand All @@ -473,28 +465,27 @@ static void uwsc_timer_cb(struct ev_loop *loop, struct ev_timer *w, int revents)
if (unlikely(cl->state != CLIENT_STATE_MESSAGE))
return;

if (cl->ping_interval == 0)
if (cl->ping_interval < 1)
return;

if (unlikely(cl->wait_pong)) {
if (now - last_ping < 3)
if (now - cl->last_ping < 3)
return;

uwsc_log_err("ping timeout %d\n", ++ntimeout);
if (ntimeout > 2) {
uwsc_log_err("ping timeout %d\n", ++cl->ntimeout);
if (cl->ntimeout > 2) {
uwsc_error(cl, UWSC_ERROR_PING_TIMEOUT, "ping timeout");
return;
}
} else {
ntimeout = 0;
cl->ntimeout = 0;
}


if (now - last_ping < cl->ping_interval)
if (now - cl->last_ping < cl->ping_interval)
return;
last_ping = now;

cl->ping(cl);
cl->last_ping = now;
cl->wait_pong = true;
}

Expand Down Expand Up @@ -539,6 +530,7 @@ struct uwsc_client *uwsc_new(struct ev_loop *loop, const char *url, int ping_int
cl->sock = sock;
cl->send = uwsc_send;
cl->ping = uwsc_ping;
cl->start_time = ev_now(loop);
cl->ping_interval = ping_interval;

if (ssl) {
Expand Down
8 changes: 6 additions & 2 deletions src/uwsc.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "buffer.h"

#define HTTP_HEAD_LIMIT 4096
#define UWSC_MAX_CONNECT_TIME 5 /* second */

/* WebSocket close status codes defined in RFC 6455, section 11.7 */
enum {
Expand Down Expand Up @@ -89,8 +90,11 @@ struct uwsc_client {
struct ev_timer timer;
bool wait_pong;
int ping_interval;
char key[256]; /* Sec-WebSocket-Key */
void *ssl;
ev_tstamp start_time; /* Time stamp of begin connect */
ev_tstamp last_ping; /* Time stamp of last ping */
int ntimeout; /* Number of timeouts */
char key[256]; /* Sec-WebSocket-Key */
void *ssl; /* Context wrap of openssl, wolfssl and mbedtls */

void (*onopen)(struct uwsc_client *cl);
void (*set_ping_interval)(struct uwsc_client *cl, int interval);
Expand Down

0 comments on commit c70e821

Please sign in to comment.