1
0
ithewei 5 жил өмнө
parent
commit
51886bdb64
6 өөрчлөгдсөн 61 нэмэгдсэн , 32 устгасан
  1. 12 0
      event/hevent.h
  2. 3 14
      event/hloop.c
  3. 2 0
      event/hloop.h
  4. 1 1
      event/nio.c
  5. 15 15
      event/overlapio.c
  6. 28 2
      examples/nc.c

+ 12 - 0
event/hevent.h

@@ -131,6 +131,18 @@ struct hio_s {
     void*       hovlp;          // for iocp/overlapio
     void*       ssl;            // for SSL
 };
+/*
+ * hio lifeline:
+ * fd =>
+ * hio_get => HV_ALLOC_SIZEOF(io) => hio_init =>
+ * hio_ready => hio_add => hio_del => hio_done =>
+ * hio_close => hclose_cb =>
+ * hio_free => HV_FREE(io)
+ */
+void hio_init(hio_t* io);
+void hio_ready(hio_t* io);
+void hio_done(hio_t* io);
+void hio_free(hio_t* io);
 
 #define EVENT_ENTRY(p)          container_of(p, hevent_t, pending_node)
 #define IDLE_ENTRY(p)           container_of(p, hidle_t,  node)

+ 3 - 14
event/hloop.c

@@ -15,17 +15,6 @@
 #define IO_ARRAY_INIT_SIZE              1024
 #define CUSTOM_EVENT_QUEUE_INIT_SIZE    16
 
-/*
- * hio lifeline:
- * hio_get => HV_ALLOC_SIZEOF(io) => hio_init =>
- * hio_ready => hio_add => hio_del => hio_done =>
- * hio_free => HV_FREE(io)
- */
-static void hio_init(hio_t* io);
-static void hio_ready(hio_t* io);
-static void hio_done(hio_t* io);
-static void hio_free(hio_t* io);
-
 static void __hidle_del(hidle_t* idle);
 static void __htimer_del(htimer_t* timer);
 
@@ -566,6 +555,8 @@ void hio_done(hio_t* io) {
     if (!io->ready) return;
     io->ready = 0;
 
+    hio_del(io, HV_RDWR);
+
     offset_buf_t* pbuf = NULL;
     while (!write_queue_empty(&io->write_queue)) {
         pbuf = write_queue_front(&io->write_queue);
@@ -579,6 +570,7 @@ void hio_free(hio_t* io) {
     if (io == NULL) return;
     // NOTE: call hio_done to cleanup write_queue
     hio_done(io);
+    // NOTE: call hio_close to call hclose_cb
     hio_close(io);
     HV_FREE(io->localaddr);
     HV_FREE(io->peeraddr);
@@ -652,9 +644,6 @@ int hio_del(hio_t* io, int events) {
             EVENT_INACTIVE(io);
         }
     }
-    if (!io->active) {
-        hio_done(io);
-    }
     return 0;
 }
 

+ 2 - 0
event/hloop.h

@@ -231,6 +231,8 @@ HV_EXPORT int hio_accept (hio_t* io);
 HV_EXPORT int hio_connect(hio_t* io);
 // hio_add(io, HV_READ) => read => hread_cb
 HV_EXPORT int hio_read   (hio_t* io);
+#define hio_read_start(io) hio_read(io)
+#define hio_read_stop(io)  hio_del(io, HV_READ)
 // hio_try_write => hio_add(io, HV_WRITE) => write => hwrite_cb
 HV_EXPORT int hio_write  (hio_t* io, const void* buf, size_t len);
 // hio_del(io, HV_RDWR) => close => hclose_cb

+ 1 - 1
event/nio.c

@@ -552,7 +552,7 @@ int hio_close (hio_t* io) {
     }
 
     io->closed = 1;
-    hio_del(io, HV_RDWR);
+    hio_done(io);
     __close_cb(io);
     if (io->ssl) {
         hssl_free(io->ssl);

+ 15 - 15
event/overlapio.c

@@ -381,7 +381,21 @@ disconnect:
 int hio_close (hio_t* io) {
     if (io->closed) return 0;
     io->closed = 1;
-    hio_del(io, HV_RDWR);
+    hio_done(io);
+    if (io->hovlp) {
+        hoverlapped_t* hovlp = (hoverlapped_t*)io->hovlp;
+        // NOTE: hread buf provided by caller
+        if (hovlp->buf.buf != io->readbuf.base) {
+            HV_FREE(hovlp->buf.buf);
+        }
+        HV_FREE(hovlp->addr);
+        HV_FREE(io->hovlp);
+    }
+    if (io->close_cb) {
+        //printd("close_cb------\n");
+        io->close_cb(io);
+        //printd("close_cb======\n");
+    }
     if (io->io_type & HIO_TYPE_SOCKET) {
 #ifdef USE_DISCONNECTEX
         // DisconnectEx reuse socket
@@ -402,20 +416,6 @@ int hio_close (hio_t* io) {
         closesocket(io->fd);
 #endif
     }
-    if (io->hovlp) {
-        hoverlapped_t* hovlp = (hoverlapped_t*)io->hovlp;
-        // NOTE: hread buf provided by caller
-        if (hovlp->buf.buf != io->readbuf.base) {
-            HV_FREE(hovlp->buf.buf);
-        }
-        HV_FREE(hovlp->addr);
-        HV_FREE(io->hovlp);
-    }
-    if (io->close_cb) {
-        //printd("close_cb------\n");
-        io->close_cb(io);
-        //printd("close_cb======\n");
-    }
     return 0;
 }
 

+ 28 - 2
examples/nc.c

@@ -37,8 +37,33 @@ void on_stdin(hio_t* io, void* buf, int readbytes) {
     //printf("on_stdin fd=%d readbytes=%d\n", hio_fd(io), readbytes);
     //printf("> %s\n", buf);
 
-    // CR|LF => CRLF for test most protocols
     char* str = (char*)buf;
+
+    // test hio_read_start/hio_read_stop/hio_close/hloop_stop
+#if 1
+    if (strncmp(str, "start", 5) == 0) {
+        printf("call hio_read_start\n");
+        hio_read_start(sockio);
+        return;
+    }
+    else if (strncmp(str, "stop", 4) == 0) {
+        printf("call hio_read_stop\n");
+        hio_read_stop(sockio);
+        return;
+    }
+    else if (strncmp(str, "close", 5) == 0) {
+        printf("call hio_close\n");
+        hio_close(sockio);
+        return;
+    }
+    else if (strncmp(str, "quit", 4) == 0) {
+        printf("call hloop_stop\n");
+        hloop_stop(hevent_loop(io));
+        return;
+    }
+#endif
+
+    // CR|LF => CRLF for test most protocols
     char eol = str[readbytes-1];
     if (eol == '\n' || eol == '\r') {
         if (readbytes > 1 && str[readbytes-2] == '\r' && eol == '\n') {
@@ -50,6 +75,7 @@ void on_stdin(hio_t* io, void* buf, int readbytes) {
             str[readbytes - 1] = '\n';
         }
     }
+
     hio_write(sockio, buf, readbytes);
 }
 
@@ -68,7 +94,7 @@ void on_connect(hio_t* io) {
             SOCKADDR_STR(hio_peeraddr(io), peeraddrstr));
     }
 
-    hio_read(io);
+    hio_read_start(io);
 }
 
 int main(int argc, char** argv) {