Browse Source

add hio_close_async

hewei.it 4 years ago
parent
commit
89ccb84485
4 changed files with 21 additions and 16 deletions
  1. 18 0
      event/hloop.c
  2. 1 0
      event/hloop.h
  3. 1 15
      event/nio.c
  4. 1 1
      http/server/HttpServer.cpp

+ 18 - 0
event/hloop.c

@@ -810,6 +810,24 @@ int hio_del(hio_t* io, int events) {
     return 0;
     return 0;
 }
 }
 
 
+static void hio_close_event_cb(hevent_t* ev) {
+    hio_t* io = (hio_t*)ev->userdata;
+    uint32_t id = (uintptr_t)ev->privdata;
+    if (io->id != id) return;
+    hio_close(io);
+}
+
+int hio_close_async(hio_t* io) {
+    hevent_t ev;
+    memset(&ev, 0, sizeof(ev));
+    ev.cb = hio_close_event_cb;
+    ev.userdata = io;
+    ev.privdata = (void*)(uintptr_t)io->id;
+    ev.priority = HEVENT_HIGH_PRIORITY;
+    hloop_post_event(io->loop, &ev);
+    return 0;
+}
+
 hio_t* hread(hloop_t* loop, int fd, void* buf, size_t len, hread_cb read_cb) {
 hio_t* hread(hloop_t* loop, int fd, void* buf, size_t len, hread_cb read_cb) {
     hio_t* io = hio_get(loop, fd);
     hio_t* io = hio_get(loop, fd);
     assert(io != NULL);
     assert(io != NULL);

+ 1 - 0
event/hloop.h

@@ -268,6 +268,7 @@ HV_EXPORT int hio_write  (hio_t* io, const void* buf, size_t len);
 // NOTE: hio_close is thread-safe, if called by other thread, hloop_post_event(hio_close_event).
 // NOTE: hio_close is thread-safe, if called by other thread, hloop_post_event(hio_close_event).
 // hio_del(io, HV_RDWR) => close => hclose_cb
 // hio_del(io, HV_RDWR) => close => hclose_cb
 HV_EXPORT int hio_close  (hio_t* io);
 HV_EXPORT int hio_close  (hio_t* io);
+HV_EXPORT int hio_close_async(hio_t* io);
 
 
 //------------------high-level apis-------------------------------------------
 //------------------high-level apis-------------------------------------------
 // hio_get -> hio_set_readbuf -> hio_setcb_read -> hio_read
 // hio_get -> hio_set_readbuf -> hio_setcb_read -> hio_read

+ 1 - 15
event/nio.c

@@ -551,24 +551,10 @@ disconnect:
     return nwrite;
     return nwrite;
 }
 }
 
 
-static void hio_close_event_cb(hevent_t* ev) {
-    hio_t* io = (hio_t*)ev->userdata;
-    uint32_t id = (uintptr_t)ev->privdata;
-    if (io->id != id) return;
-    hio_close(io);
-}
-
 int hio_close (hio_t* io) {
 int hio_close (hio_t* io) {
     if (io->closed) return 0;
     if (io->closed) return 0;
     if (hv_gettid() != io->loop->tid) {
     if (hv_gettid() != io->loop->tid) {
-        hevent_t ev;
-        memset(&ev, 0, sizeof(ev));
-        ev.cb = hio_close_event_cb;
-        ev.userdata = io;
-        ev.privdata = (void*)(uintptr_t)io->id;
-        ev.priority = HEVENT_HIGH_PRIORITY;
-        hloop_post_event(io->loop, &ev);
-        return 0;
+        return hio_close_async(io);
     }
     }
     hrecursive_mutex_lock(&io->write_mutex);
     hrecursive_mutex_lock(&io->write_mutex);
     if (!write_queue_empty(&io->write_queue) && io->error == 0 && io->close == 0) {
     if (!write_queue_empty(&io->write_queue) && io->error == 0 && io->close == 0) {

+ 1 - 1
http/server/HttpServer.cpp

@@ -53,7 +53,7 @@ static void websocket_onmessage(int opcode, const std::string& msg, hio_t* io) {
     WebSocketHandler* ws = handler->ws.get();
     WebSocketHandler* ws = handler->ws.get();
     switch(opcode) {
     switch(opcode) {
     case WS_OPCODE_CLOSE:
     case WS_OPCODE_CLOSE:
-        hio_close(io);
+        hio_close_async(io);
         break;
         break;
     case WS_OPCODE_PING:
     case WS_OPCODE_PING:
         // printf("recv ping\n");
         // printf("recv ping\n");