ithewei 4 years ago
parent
commit
952d458fc9
4 changed files with 68 additions and 42 deletions
  1. 57 14
      event/hevent.c
  2. 5 0
      event/hevent.h
  3. 1 0
      event/hloop.h
  4. 5 28
      event/nio.c

+ 57 - 14
event/hevent.c

@@ -116,6 +116,10 @@ int hio_enable_ssl(hio_t* io) {
     return 0;
     return 0;
 }
 }
 
 
+bool hio_is_ssl(hio_t* io) {
+    return io->io_type == HIO_TYPE_SSL;
+}
+
 hssl_t hio_get_ssl(hio_t* io) {
 hssl_t hio_get_ssl(hio_t* io) {
     return io->ssl;
     return io->ssl;
 }
 }
@@ -141,6 +145,39 @@ void hio_set_readbuf(hio_t* io, void* buf, size_t len) {
     }
     }
 }
 }
 
 
+void hio_del_connect_timer(hio_t* io) {
+    if (io->connect_timer) {
+        htimer_del(io->connect_timer);
+        io->connect_timer = NULL;
+        io->connect_timeout = 0;
+    }
+}
+
+void hio_del_close_timer(hio_t* io) {
+    if (io->close_timer) {
+        htimer_del(io->close_timer);
+        io->close_timer = NULL;
+        io->close_timeout = 0;
+    }
+}
+
+void hio_del_keepalive_timer(hio_t* io) {
+    if (io->keepalive_timer) {
+        htimer_del(io->keepalive_timer);
+        io->keepalive_timer = NULL;
+        io->keepalive_timeout = 0;
+    }
+}
+
+void hio_del_heartbeat_timer(hio_t* io) {
+    if (io->heartbeat_timer) {
+        htimer_del(io->heartbeat_timer);
+        io->heartbeat_timer = NULL;
+        io->heartbeat_interval = 0;
+        io->heartbeat_fn = NULL;
+    }
+}
+
 void hio_set_connect_timeout(hio_t* io, int timeout_ms) {
 void hio_set_connect_timeout(hio_t* io, int timeout_ms) {
     io->connect_timeout = timeout_ms;
     io->connect_timeout = timeout_ms;
 }
 }
@@ -163,15 +200,18 @@ static void __keepalive_timeout_cb(htimer_t* timer) {
 }
 }
 
 
 void hio_set_keepalive_timeout(hio_t* io, int timeout_ms) {
 void hio_set_keepalive_timeout(hio_t* io, int timeout_ms) {
+    if (timeout_ms == 0) {
+        // del
+        hio_del_keepalive_timer(io);
+        return;
+    }
+
     if (io->keepalive_timer) {
     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);
-        }
+        // reset
+        ((struct htimeout_s*)io->keepalive_timer)->timeout = timeout_ms;
+        htimer_reset(io->keepalive_timer);
     } else {
     } else {
+        // add
         io->keepalive_timer = htimer_add(io->loop, __keepalive_timeout_cb, timeout_ms, 1);
         io->keepalive_timer = htimer_add(io->loop, __keepalive_timeout_cb, timeout_ms, 1);
         io->keepalive_timer->privdata = io;
         io->keepalive_timer->privdata = io;
     }
     }
@@ -186,15 +226,18 @@ static void __heartbeat_timer_cb(htimer_t* timer) {
 }
 }
 
 
 void hio_set_heartbeat(hio_t* io, int interval_ms, hio_send_heartbeat_fn fn) {
 void hio_set_heartbeat(hio_t* io, int interval_ms, hio_send_heartbeat_fn fn) {
+    if (interval_ms == 0) {
+        // del
+        hio_del_heartbeat_timer(io);
+        return;
+    }
+
     if (io->heartbeat_timer) {
     if (io->heartbeat_timer) {
-        if (interval_ms == 0) {
-            htimer_del(io->heartbeat_timer);
-            io->heartbeat_timer = NULL;
-        } else {
-            ((struct htimeout_s*)io->heartbeat_timer)->timeout = interval_ms;
-            htimer_reset(io->heartbeat_timer);
-        }
+        // reset
+        ((struct htimeout_s*)io->heartbeat_timer)->timeout = interval_ms;
+        htimer_reset(io->heartbeat_timer);
     } else {
     } else {
+        // add
         io->heartbeat_timer = htimer_add(io->loop, __heartbeat_timer_cb, interval_ms, INFINITE);
         io->heartbeat_timer = htimer_add(io->loop, __heartbeat_timer_cb, interval_ms, INFINITE);
         io->heartbeat_timer->privdata = io;
         io->heartbeat_timer->privdata = io;
     }
     }

+ 5 - 0
event/hevent.h

@@ -148,6 +148,11 @@ void hio_done(hio_t* io);
 void hio_free(hio_t* io);
 void hio_free(hio_t* io);
 uint32_t hio_next_id();
 uint32_t hio_next_id();
 
 
+void hio_del_connect_timer(hio_t* io);
+void hio_del_close_timer(hio_t* io);
+void hio_del_keepalive_timer(hio_t* io);
+void hio_del_heartbeat_timer(hio_t* io);
+
 #define EVENT_ENTRY(p)          container_of(p, hevent_t, pending_node)
 #define EVENT_ENTRY(p)          container_of(p, hevent_t, pending_node)
 #define IDLE_ENTRY(p)           container_of(p, hidle_t,  node)
 #define IDLE_ENTRY(p)           container_of(p, hidle_t,  node)
 #define TIMER_ENTRY(p)          container_of(p, htimer_t, node)
 #define TIMER_ENTRY(p)          container_of(p, htimer_t, node)

+ 1 - 0
event/hloop.h

@@ -234,6 +234,7 @@ HV_EXPORT hclose_cb   hio_getcb_close(hio_t* io);
 // some useful settings
 // some useful settings
 // Enable SSL/TLS is so easy :)
 // Enable SSL/TLS is so easy :)
 HV_EXPORT int  hio_enable_ssl(hio_t* io);
 HV_EXPORT int  hio_enable_ssl(hio_t* io);
+HV_EXPORT bool hio_is_ssl(hio_t* io);
 HV_EXPORT hssl_t hio_get_ssl(hio_t* io);
 HV_EXPORT hssl_t hio_get_ssl(hio_t* io);
 HV_EXPORT int  hio_set_ssl(hio_t* io, hssl_t ssl);
 HV_EXPORT int  hio_set_ssl(hio_t* io, hssl_t ssl);
 // TODO: One loop per thread, one readbuf per loop.
 // TODO: One loop per thread, one readbuf per loop.

+ 5 - 28
event/nio.c

@@ -56,11 +56,7 @@ static void __connect_cb(hio_t* io) {
             SOCKADDR_STR(io->localaddr, localaddrstr),
             SOCKADDR_STR(io->localaddr, localaddrstr),
             SOCKADDR_STR(io->peeraddr, peeraddrstr));
             SOCKADDR_STR(io->peeraddr, peeraddrstr));
     */
     */
-    if (io->connect_timer) {
-        htimer_del(io->connect_timer);
-        io->connect_timer = NULL;
-        io->connect_timeout = 0;
-    }
+    hio_del_connect_timer(io);
 
 
     if (io->connect_cb) {
     if (io->connect_cb) {
         // printd("connect_cb------\n");
         // printd("connect_cb------\n");
@@ -97,30 +93,11 @@ static void __write_cb(hio_t* io, const void* buf, int writebytes) {
 
 
 static void __close_cb(hio_t* io) {
 static void __close_cb(hio_t* io) {
     // printd("close fd=%d\n", io->fd);
     // printd("close fd=%d\n", io->fd);
-    if (io->connect_timer) {
-        htimer_del(io->connect_timer);
-        io->connect_timer = NULL;
-        io->connect_timeout = 0;
-    }
-
-    if (io->close_timer) {
-        htimer_del(io->close_timer);
-        io->close_timer = NULL;
-        io->close_timeout = 0;
-    }
 
 
-    if (io->keepalive_timer) {
-        htimer_del(io->keepalive_timer);
-        io->keepalive_timer = NULL;
-        io->keepalive_timeout = 0;
-    }
-
-    if (io->heartbeat_timer) {
-        htimer_del(io->heartbeat_timer);
-        io->heartbeat_timer = NULL;
-        io->heartbeat_interval = 0;
-        io->heartbeat_fn = NULL;
-    }
+    hio_del_connect_timer(io);
+    hio_del_close_timer(io);
+    hio_del_keepalive_timer(io);
+    hio_del_heartbeat_timer(io);
 
 
     if (io->close_cb) {
     if (io->close_cb) {
         // printd("close_cb------\n");
         // printd("close_cb------\n");