Selaa lähdekoodia

More flexible hio_set_keepalive_timeout, hio_set_heartbeat

ithewei 4 vuotta sitten
vanhempi
commit
30042e6889
2 muutettua tiedostoa jossa 45 lisäystä ja 40 poistoa
  1. 45 0
      event/hevent.c
  2. 0 40
      event/nio.c

+ 45 - 0
event/hevent.c

@@ -1,6 +1,7 @@
 #include "hevent.h"
 #include "hsocket.h"
 #include "hatomic.h"
+#include "hlog.h"
 
 uint64_t hloop_next_event_id() {
     static hatomic_t s_id = HATOMIC_VAR_INIT(0);
@@ -118,11 +119,55 @@ void hio_set_close_timeout(hio_t* io, int timeout_ms) {
     io->close_timeout = timeout_ms;
 }
 
+static void __keepalive_timeout_cb(htimer_t* timer) {
+    hio_t* io = (hio_t*)timer->privdata;
+    if (io) {
+        char localaddrstr[SOCKADDR_STRLEN] = {0};
+        char peeraddrstr[SOCKADDR_STRLEN] = {0};
+        hlogw("keepalive timeout [%s] <=> [%s]",
+                SOCKADDR_STR(io->localaddr, localaddrstr),
+                SOCKADDR_STR(io->peeraddr, peeraddrstr));
+        io->error = ETIMEDOUT;
+        hio_close(io);
+    }
+}
+
 void hio_set_keepalive_timeout(hio_t* io, int timeout_ms) {
+    if (io->keepalive_timer) {
+        if (timeout_ms == 0) {
+            htimer_del(io->keepalive_timer);
+            io->keepalive_timer = NULL;
+        } else {
+            ((struct htimeout_s*)io->keepalive_timer)->timeout = timeout_ms;
+            htimer_reset(io->keepalive_timer);
+        }
+    } else {
+        io->keepalive_timer = htimer_add(io->loop, __keepalive_timeout_cb, timeout_ms, 1);
+        io->keepalive_timer->privdata = io;
+    }
     io->keepalive_timeout = timeout_ms;
 }
 
+static void __heartbeat_timer_cb(htimer_t* timer) {
+    hio_t* io = (hio_t*)timer->privdata;
+    if (io && io->heartbeat_fn) {
+        io->heartbeat_fn(io);
+    }
+}
+
 void hio_set_heartbeat(hio_t* io, int interval_ms, hio_send_heartbeat_fn fn) {
+    if (io->heartbeat_timer) {
+        if (interval_ms == 0) {
+            htimer_del(io->heartbeat_timer);
+            io->heartbeat_timer = NULL;
+        } else {
+            ((struct htimeout_s*)io->heartbeat_fn)->timeout = interval_ms;
+            htimer_reset(io->keepalive_timer);
+        }
+    } else {
+        io->heartbeat_timer = htimer_add(io->loop, __heartbeat_timer_cb, interval_ms, INFINITE);
+        io->heartbeat_timer->privdata = io;
+    }
     io->heartbeat_interval = interval_ms;
     io->heartbeat_fn = fn;
 }

+ 0 - 40
event/nio.c

@@ -32,26 +32,6 @@ static void __close_timeout_cb(htimer_t* timer) {
     }
 }
 
-static void __keepalive_timeout_cb(htimer_t* timer) {
-    hio_t* io = (hio_t*)timer->privdata;
-    if (io) {
-        char localaddrstr[SOCKADDR_STRLEN] = {0};
-        char peeraddrstr[SOCKADDR_STRLEN] = {0};
-        hlogw("keepalive timeout [%s] <=> [%s]",
-                SOCKADDR_STR(io->localaddr, localaddrstr),
-                SOCKADDR_STR(io->peeraddr, peeraddrstr));
-        io->error = ETIMEDOUT;
-        hio_close(io);
-    }
-}
-
-static void __heartbeat_timer_cb(htimer_t* timer) {
-    hio_t* io = (hio_t*)timer->privdata;
-    if (io && io->heartbeat_fn) {
-        io->heartbeat_fn(io);
-    }
-}
-
 static void __accept_cb(hio_t* io) {
     /*
     char localaddrstr[SOCKADDR_STRLEN] = {0};
@@ -66,16 +46,6 @@ static void __accept_cb(hio_t* io) {
         io->accept_cb(io);
         // printd("accept_cb======\n");
     }
-
-    if (io->keepalive_timeout > 0) {
-        io->keepalive_timer = htimer_add(io->loop, __keepalive_timeout_cb, io->keepalive_timeout, 1);
-        io->keepalive_timer->privdata = io;
-    }
-
-    if (io->heartbeat_interval > 0) {
-        io->heartbeat_timer = htimer_add(io->loop, __heartbeat_timer_cb, io->heartbeat_interval, INFINITE);
-        io->heartbeat_timer->privdata = io;
-    }
 }
 
 static void __connect_cb(hio_t* io) {
@@ -97,16 +67,6 @@ static void __connect_cb(hio_t* io) {
         io->connect_cb(io);
         // printd("connect_cb======\n");
     }
-
-    if (io->keepalive_timeout > 0) {
-        io->keepalive_timer = htimer_add(io->loop, __keepalive_timeout_cb, io->keepalive_timeout, 1);
-        io->keepalive_timer->privdata = io;
-    }
-
-    if (io->heartbeat_interval > 0) {
-        io->heartbeat_timer = htimer_add(io->loop, __heartbeat_timer_cb, io->heartbeat_interval, INFINITE);
-        io->heartbeat_timer->privdata = io;
-    }
 }
 
 static void __read_cb(hio_t* io, void* buf, int readbytes) {