ithewei 4 tahun lalu
induk
melakukan
b5ef532c28
6 mengubah file dengan 46 tambahan dan 15 penghapusan
  1. 3 0
      event/epoll.c
  2. 31 12
      event/hevent.c
  3. 5 0
      event/hevent.h
  4. 3 2
      event/hloop.c
  5. 3 0
      event/poll.c
  6. 1 1
      http/server/HttpHandler.cpp

+ 3 - 0
event/epoll.c

@@ -106,6 +106,9 @@ int iowatcher_poll_events(hloop_t* loop, int timeout) {
     if (epoll_ctx->events.size == 0) return 0;
     int nepoll = epoll_wait(epoll_ctx->epfd, epoll_ctx->events.ptr, epoll_ctx->events.size, timeout);
     if (nepoll < 0) {
+        if (errno == EINTR) {
+            return 0;
+        }
         perror("epoll");
         return nepoll;
     }

+ 31 - 12
event/hevent.c

@@ -237,17 +237,38 @@ void hio_set_heartbeat(hio_t* io, int interval_ms, hio_send_heartbeat_fn fn) {
     io->heartbeat_fn = fn;
 }
 
+bool hio_is_alloced_readbuf(hio_t* io) {
+    return  io->alloced_readbuf &&
+            io->readbuf.base &&
+            io->readbuf.len &&
+            io->readbuf.base != io->loop->readbuf.base;
+}
+
+void hio_alloc_readbuf(hio_t* io, int len) {
+    if (hio_is_alloced_readbuf(io)) {
+        io->readbuf.base = (char*)safe_realloc(io->readbuf.base, len, io->readbuf.len);
+    } else {
+        HV_ALLOC(io->readbuf.base, len);
+    }
+    io->readbuf.len = len;
+    io->alloced_readbuf = 1;
+}
+
+void hio_free_readbuf(hio_t* io) {
+    if (hio_is_alloced_readbuf(io)) {
+        HV_FREE(io->readbuf.base);
+        io->alloced_readbuf = 0;
+        // reset to loop->readbuf
+        io->readbuf.base = io->loop->readbuf.base;
+        io->readbuf.len = io->loop->readbuf.len;
+    }
+}
+
 void hio_unset_unpack(hio_t* io) {
     if (io->unpack_setting) {
-        // NOTE: unpack has own readbuf
-        if (io->readbuf.base && io->readbuf.len &&
-            io->readbuf.base != io->loop->readbuf.base) {
-            HV_FREE(io->readbuf.base);
-            // reset to loop->readbuf
-            io->readbuf.base = io->loop->readbuf.base;
-            io->readbuf.len = io->loop->readbuf.len;
-        }
         io->unpack_setting = NULL;
+        // NOTE: unpack has own readbuf
+        hio_free_readbuf(io);
     }
 }
 
@@ -278,9 +299,7 @@ void hio_set_unpack(hio_t* io, unpack_setting_t* setting) {
     if (io->unpack_setting->mode == UNPACK_BY_FIXED_LENGTH) {
         io->readbuf.len = io->unpack_setting->fixed_length;
     } else {
-        io->readbuf.len = 2;
+        io->readbuf.len = HLOOP_READ_BUFSIZE;
     }
-    assert(io->readbuf.len > 0);
-    // NOTE: free in hio_unset_unpack
-    HV_ALLOC(io->readbuf.base, io->readbuf.len);
+    hio_alloc_readbuf(io, io->readbuf.len);
 }

+ 5 - 0
event/hevent.h

@@ -98,6 +98,7 @@ struct hio_s {
     unsigned    recvfrom    :1;
     unsigned    sendto      :1;
     unsigned    close       :1;
+    unsigned    alloced_readbuf :1;
 // public:
     uint32_t    id; // fd cannot be used as unique identifier, so we provide an id
     int         fd;
@@ -155,6 +156,10 @@ void hio_del_close_timer(hio_t* io);
 void hio_del_keepalive_timer(hio_t* io);
 void hio_del_heartbeat_timer(hio_t* io);
 
+bool hio_is_alloced_readbuf(hio_t* io);
+void hio_alloc_readbuf(hio_t* io, int len);
+void hio_free_readbuf(hio_t* io);
+
 #define EVENT_ENTRY(p)          container_of(p, hevent_t, pending_node)
 #define IDLE_ENTRY(p)           container_of(p, hidle_t,  node)
 #define TIMER_ENTRY(p)          container_of(p, htimer_t, node)

+ 3 - 2
event/hloop.c

@@ -91,7 +91,7 @@ static int hloop_process_ios(hloop_t* loop, int timeout) {
     // That is to call IO multiplexing function such as select, poll, epoll, etc.
     int nevents = iowatcher_poll_events(loop, timeout);
     if (nevents < 0) {
-        hloge("poll_events error=%d", -nevents);
+        hlogd("poll_events error=%d", -nevents);
     }
     return nevents < 0 ? 0 : nevents;
 }
@@ -680,6 +680,7 @@ void hio_ready(hio_t* io) {
     io->error = 0;
     io->events = io->revents = 0;
     // readbuf
+    io->alloced_readbuf = 0;
     io->readbuf.base = io->loop->readbuf.base;
     io->readbuf.len = io->loop->readbuf.len;
     io->readbuf.offset = 0;
@@ -722,7 +723,7 @@ void hio_done(hio_t* io) {
     hio_del(io, HV_RDWR);
 
     // readbuf
-    hio_unset_unpack(io);
+    hio_free_readbuf(io);
 
     // write_queue
     offset_buf_t* pbuf = NULL;

+ 3 - 0
event/poll.c

@@ -104,6 +104,9 @@ int iowatcher_poll_events(hloop_t* loop, int timeout) {
     if (poll_ctx->fds.size == 0)   return 0;
     int npoll = poll(poll_ctx->fds.ptr, poll_ctx->fds.size, timeout);
     if (npoll < 0) {
+        if (errno == EINTR) {
+            return 0;
+        }
         perror("poll");
         return npoll;
     }

+ 1 - 1
http/server/HttpHandler.cpp

@@ -103,7 +103,7 @@ int HttpHandler::defaultRequestHandler() {
     else if (req->method == HTTP_GET || req->method == HTTP_HEAD) {
         // static handler
         if (service->staticHandler) {
-            customHttpHandler(service->staticHandler);
+            status_code = customHttpHandler(service->staticHandler);
         }
         else if (service->document_root.size() != 0) {
             status_code = defaultStaticHandler();