Browse Source

HV_READ HV_WRITE

ithewei 5 years ago
parent
commit
992ea237c2
8 changed files with 67 additions and 67 deletions
  1. 12 12
      event/epoll.c
  2. 5 5
      event/hloop.h
  3. 8 8
      event/kqueue.c
  4. 14 14
      event/nio.c
  5. 11 11
      event/overlapio.c
  6. 9 9
      event/poll.c
  7. 7 7
      event/select.c
  8. 1 1
      examples/nc.c

+ 12 - 12
event/epoll.c

@@ -46,17 +46,17 @@ int iowatcher_add_event(hloop_t* loop, int fd, int events) {
     ee.events = 0;
     ee.data.fd = fd;
     // pre events
-    if (io->events & READ_EVENT) {
+    if (io->events & HV_READ) {
         ee.events |= EPOLLIN;
     }
-    if (io->events & WRITE_EVENT) {
+    if (io->events & HV_WRITE) {
         ee.events |= EPOLLOUT;
     }
     // now events
-    if (events & READ_EVENT) {
+    if (events & HV_READ) {
         ee.events |= EPOLLIN;
     }
-    if (events & WRITE_EVENT) {
+    if (events & HV_WRITE) {
         ee.events |= EPOLLOUT;
     }
     int op = io->events == 0 ? EPOLL_CTL_ADD : EPOLL_CTL_MOD;
@@ -79,17 +79,17 @@ int iowatcher_del_event(hloop_t* loop, int fd, int events) {
     ee.events = 0;
     ee.data.fd = fd;
     // pre events
-    if (io->events & READ_EVENT) {
+    if (io->events & HV_READ) {
         ee.events |= EPOLLIN;
     }
-    if (io->events & WRITE_EVENT) {
+    if (io->events & HV_WRITE) {
         ee.events |= EPOLLOUT;
     }
     // now events
-    if (events & READ_EVENT) {
+    if (events & HV_READ) {
         ee.events &= ~EPOLLIN;
     }
-    if (events & WRITE_EVENT) {
+    if (events & HV_WRITE) {
         ee.events &= ~EPOLLOUT;
     }
     int op = ee.events == 0 ? EPOLL_CTL_DEL : EPOLL_CTL_MOD;
@@ -119,11 +119,11 @@ int iowatcher_poll_events(hloop_t* loop, int timeout) {
             ++nevents;
             hio_t* io = loop->ios.ptr[fd];
             if (io) {
-                if (revents & EPOLLIN) {
-                    io->revents |= READ_EVENT;
+                if (revents & (EPOLLIN | EPOLLHUP | EPOLLERR)) {
+                    io->revents |= HV_READ;
                 }
-                if (revents & EPOLLOUT) {
-                    io->revents |= WRITE_EVENT;
+                if (revents & (EPOLLOUT | EPOLLHUP | EPOLLERR)) {
+                    io->revents |= HV_WRITE;
                 }
                 EVENT_PENDING(io);
             }

+ 5 - 5
event/hloop.h

@@ -144,9 +144,9 @@ void        htimer_reset(htimer_t* timer);
 
 // io
 //-----------------------low-level apis---------------------------------------
-#define READ_EVENT  0x0001
-#define WRITE_EVENT 0x0004
-#define ALL_EVENTS  READ_EVENT|WRITE_EVENT
+#define HV_READ  0x0001
+#define HV_WRITE 0x0004
+#define HV_RDWR  (HV_READ|HV_WRITE)
 /*
 const char* hio_engine() {
 #ifdef EVENT_SELECT
@@ -168,8 +168,8 @@ const char* hio_engine() {
 */
 const char* hio_engine();
 hio_t* hio_get(hloop_t* loop, int fd);
-int    hio_add(hio_t* io, hio_cb cb, int events DEFAULT(READ_EVENT));
-int    hio_del(hio_t* io, int events DEFAULT(ALL_EVENTS));
+int    hio_add(hio_t* io, hio_cb cb, int events DEFAULT(HV_READ));
+int    hio_del(hio_t* io, int events DEFAULT(HV_RDWR));
 
 int hio_fd    (hio_t* io);
 int hio_error (hio_t* io);

+ 8 - 8
event/kqueue.c

@@ -82,10 +82,10 @@ static int __add_event(hloop_t* loop, int fd, int event) {
 }
 
 int iowatcher_add_event(hloop_t* loop, int fd, int events) {
-    if (events & READ_EVENT) {
+    if (events & HV_READ) {
         __add_event(loop, fd, EVFILT_READ);
     }
-    if (events & WRITE_EVENT) {
+    if (events & HV_WRITE) {
         __add_event(loop, fd, EVFILT_WRITE);
     }
     return 0;
@@ -121,10 +121,10 @@ static int __del_event(hloop_t* loop, int fd, int event) {
 }
 
 int iowatcher_del_event(hloop_t* loop, int fd, int events) {
-    if (events & READ_EVENT) {
+    if (events & HV_READ) {
         __del_event(loop, fd, EVFILT_READ);
     }
-    if (events & WRITE_EVENT) {
+    if (events & HV_WRITE) {
         __del_event(loop, fd, EVFILT_WRITE);
     }
     return 0;
@@ -159,11 +159,11 @@ int iowatcher_poll_events(hloop_t* loop, int timeout) {
         int revents = kqueue_ctx->events[i].filter;
         hio_t* io = loop->ios.ptr[fd];
         if (io) {
-            if (revents | EVFILT_READ) {
-                io->revents |= EVFILT_READ;
+            if (revents & EVFILT_READ) {
+                io->revents |= HV_READ;
             }
-            if (revents | EVFILT_WRITE) {
-                io->revents |= EVFILT_WRITE;
+            if (revents & EVFILT_WRITE) {
+                io->revents |= HV_WRITE;
             }
             EVENT_PENDING(io);
         }

+ 14 - 14
event/nio.c

@@ -15,8 +15,8 @@ static void ssl_do_handshark(hio_t* io) {
     int ret = SSL_do_handshake(ssl);
     if (ret == 1) {
         // handshark finish
-        iowatcher_del_event(io->loop, io->fd, READ_EVENT);
-        io->events &= ~READ_EVENT;
+        iowatcher_del_event(io->loop, io->fd, HV_READ);
+        io->events &= ~HV_READ;
         io->cb = NULL;
         printd("ssl handshark finished.\n");
         if (io->accept_cb) {
@@ -29,8 +29,8 @@ static void ssl_do_handshark(hio_t* io) {
     else {
         int errcode = SSL_get_error(ssl, ret);
         if (errcode == SSL_ERROR_WANT_READ) {
-            if ((io->events & READ_EVENT) == 0) {
-                hio_add(io, ssl_do_handshark, READ_EVENT);
+            if ((io->events & HV_READ) == 0) {
+                hio_add(io, ssl_do_handshark, HV_READ);
             }
         }
         else {
@@ -279,7 +279,7 @@ disconnect:
 }
 
 static void hio_handle_events(hio_t* io) {
-    if ((io->events & READ_EVENT) && (io->revents & READ_EVENT)) {
+    if ((io->events & HV_READ) && (io->revents & HV_READ)) {
         if (io->accept) {
             nio_accept(io);
         }
@@ -288,11 +288,11 @@ static void hio_handle_events(hio_t* io) {
         }
     }
 
-    if ((io->events & WRITE_EVENT) && (io->revents & WRITE_EVENT)) {
-        // NOTE: del WRITE_EVENT, if write_queue empty
+    if ((io->events & HV_WRITE) && (io->revents & HV_WRITE)) {
+        // NOTE: del HV_WRITE, if write_queue empty
         if (write_queue_empty(&io->write_queue)) {
-            iowatcher_del_event(io->loop, io->fd, WRITE_EVENT);
-            io->events &= ~WRITE_EVENT;
+            iowatcher_del_event(io->loop, io->fd, HV_WRITE);
+            io->events &= ~HV_WRITE;
         }
         if (io->connect) {
             // NOTE: connect just do once
@@ -314,7 +314,7 @@ static void hio_handle_events(hio_t* io) {
 }
 
 int hio_accept(hio_t* io) {
-    hio_add(io, hio_handle_events, READ_EVENT);
+    hio_add(io, hio_handle_events, HV_READ);
     return 0;
 }
 
@@ -344,11 +344,11 @@ int hio_connect(hio_t* io) {
     htimer_t* timer = htimer_add(io->loop, connect_timeout_cb, CONNECT_TIMEOUT, 1);
     timer->userdata = io;
     io->timer = timer;
-    return hio_add(io, hio_handle_events, WRITE_EVENT);
+    return hio_add(io, hio_handle_events, HV_WRITE);
 }
 
 int hio_read (hio_t* io) {
-    return hio_add(io, hio_handle_events, READ_EVENT);
+    return hio_add(io, hio_handle_events, HV_READ);
 }
 
 int hio_write (hio_t* io, const void* buf, size_t len) {
@@ -401,7 +401,7 @@ try_write:
             //goto write_done;
             return nwrite;
         }
-        hio_add(io, hio_handle_events, WRITE_EVENT);
+        hio_add(io, hio_handle_events, HV_WRITE);
     }
 enqueue:
     if (nwrite < len) {
@@ -427,7 +427,7 @@ int hio_close (hio_t* io) {
     printd("close fd=%d\n", io->fd);
     if (io->closed) return 0;
     io->closed = 1;
-    hio_del(io, ALL_EVENTS);
+    hio_del(io, HV_RDWR);
 #ifdef OS_UNIX
     close(io->fd);
 #else

+ 11 - 11
event/overlapio.c

@@ -225,11 +225,11 @@ static void hio_handle_events(hio_t* io) {
         }
     }
 
-    if ((io->events & WRITE_EVENT) && (io->revents & WRITE_EVENT)) {
-        // NOTE: WRITE_EVENT just do once
+    if ((io->events & HV_WRITE) && (io->revents & HV_WRITE)) {
+        // NOTE: HV_WRITE just do once
         // ONESHOT
-        iowatcher_del_event(io->loop, io->fd, WRITE_EVENT);
-        io->events &= ~WRITE_EVENT;
+        iowatcher_del_event(io->loop, io->fd, HV_WRITE);
+        io->events &= ~HV_WRITE;
         if (io->connect) {
             io->connect = 0;
 
@@ -277,7 +277,7 @@ int hio_connect (hio_t* io) {
     hoverlapped_t* hovlp;
     SAFE_ALLOC_SIZEOF(hovlp);
     hovlp->fd = io->fd;
-    hovlp->event = WRITE_EVENT;
+    hovlp->event = HV_WRITE;
     hovlp->io = io;
     if (ConnectEx(io->fd, io->peeraddr, sizeof(struct sockaddr_in6), NULL, 0, &dwbytes, &hovlp->ovlp) != TRUE) {
         int err = WSAGetLastError();
@@ -286,7 +286,7 @@ int hio_connect (hio_t* io) {
             goto error;
         }
     }
-    return hio_add(io, hio_handle_events, WRITE_EVENT);
+    return hio_add(io, hio_handle_events, HV_WRITE);
 error:
     hio_close(io);
     return 0;
@@ -341,7 +341,7 @@ WSASend:
         hoverlapped_t* hovlp;
         SAFE_ALLOC_SIZEOF(hovlp);
         hovlp->fd = io->fd;
-        hovlp->event = WRITE_EVENT;
+        hovlp->event = HV_WRITE;
         hovlp->buf.len = len - nwrite;
         // NOTE: free on_send_complete
         SAFE_ALLOC(hovlp->buf.buf, hovlp->buf.len);
@@ -368,7 +368,7 @@ WSASend:
                 return ret;
             }
         }
-        return hio_add(io, hio_handle_events, WRITE_EVENT);
+        return hio_add(io, hio_handle_events, HV_WRITE);
     }
 write_error:
 disconnect:
@@ -380,7 +380,7 @@ int hio_close (hio_t* io) {
     printd("close fd=%d\n", io->fd);
     if (io->closed) return 0;
     io->closed = 1;
-    hio_del(io, ALL_EVENTS);
+    hio_del(io, HV_RDWR);
 #ifdef USE_DISCONNECTEX
     // DisconnectEx reuse socket
     if (io->connectex) {
@@ -409,9 +409,9 @@ int hio_close (hio_t* io) {
         SAFE_FREE(io->hovlp);
     }
     if (io->close_cb) {
-        printd("close_cb------\n");
+        //printd("close_cb------\n");
         io->close_cb(io);
-        printd("close_cb======\n");
+        //printd("close_cb======\n");
     }
     return 0;
 }

+ 9 - 9
event/poll.c

@@ -62,10 +62,10 @@ int iowatcher_add_event(hloop_t* loop, int fd, int events) {
         pfd = poll_ctx->fds.ptr + idx;
         assert(pfd->fd == fd);
     }
-    if (events & READ_EVENT) {
+    if (events & HV_READ) {
         pfd->events |= POLLIN;
     }
-    if (events & WRITE_EVENT) {
+    if (events & HV_WRITE) {
         pfd->events |= POLLOUT;
     }
     return 0;
@@ -80,15 +80,15 @@ int iowatcher_del_event(hloop_t* loop, int fd, int events) {
     if (idx < 0) return 0;
     struct pollfd* pfd = poll_ctx->fds.ptr + idx;
     assert(pfd->fd == fd);
-    if (events & READ_EVENT) {
+    if (events & HV_READ) {
         pfd->events &= ~POLLIN;
     }
-    if (events & WRITE_EVENT) {
+    if (events & HV_WRITE) {
         pfd->events &= ~POLLOUT;
     }
     if (pfd->events == 0) {
         pollfds_del_nomove(&poll_ctx->fds, idx);
-        // NOTE: correct event_idex
+        // NOTE: correct event_index
         if (idx < poll_ctx->fds.size) {
             hio_t* last = loop->ios.ptr[poll_ctx->fds.ptr[idx].fd];
             last->event_index[0] = idx;
@@ -116,11 +116,11 @@ int iowatcher_poll_events(hloop_t* loop, int timeout) {
             ++nevents;
             hio_t* io = loop->ios.ptr[fd];
             if (io) {
-                if (revents & POLLIN) {
-                    io->revents |= READ_EVENT;
+                if (revents & (POLLIN | POLLHUP | POLLERR)) {
+                    io->revents |= HV_READ;
                 }
-                if (revents & POLLOUT) {
-                    io->revents |= WRITE_EVENT;
+                if (revents & (POLLOUT | POLLHUP | POLLERR)) {
+                    io->revents |= HV_WRITE;
                 }
                 EVENT_PENDING(io);
             }

+ 7 - 7
event/select.c

@@ -40,13 +40,13 @@ int iowatcher_add_event(hloop_t* loop, int fd, int events) {
     if (fd > select_ctx->max_fd) {
         select_ctx->max_fd = fd;
     }
-    if (events & READ_EVENT) {
+    if (events & HV_READ) {
         if (!FD_ISSET(fd, &select_ctx->readfds)) {
             FD_SET(fd, &select_ctx->readfds);
             select_ctx->nread++;
         }
     }
-    if (events & WRITE_EVENT) {
+    if (events & HV_WRITE) {
         if (!FD_ISSET(fd, &select_ctx->writefds)) {
             FD_SET(fd, &select_ctx->writefds);
             select_ctx->nwrite++;
@@ -61,13 +61,13 @@ int iowatcher_del_event(hloop_t* loop, int fd, int events) {
     if (fd == select_ctx->max_fd) {
         select_ctx->max_fd = -1;
     }
-    if (events & READ_EVENT) {
+    if (events & HV_READ) {
         if (FD_ISSET(fd, &select_ctx->readfds)) {
             FD_CLR(fd, &select_ctx->readfds);
             select_ctx->nread--;
         }
     }
-    if (events & WRITE_EVENT) {
+    if (events & HV_WRITE) {
         if (FD_ISSET(fd, &select_ctx->writefds)) {
             FD_CLR(fd, &select_ctx->writefds);
             select_ctx->nwrite--;
@@ -100,7 +100,7 @@ static int remove_bad_fds(hloop_t* loop) {
                 ++badfds;
                 hio_t* io = loop->ios.ptr[fd];
                 if (io) {
-                    hio_del(io, ALL_EVENTS);
+                    hio_del(io, HV_RDWR);
                 }
             }
         }
@@ -149,11 +149,11 @@ int iowatcher_poll_events(hloop_t* loop, int timeout) {
         revents = 0;
         if (FD_ISSET(fd, &readfds)) {
             ++nevents;
-            revents |= READ_EVENT;
+            revents |= HV_READ;
         }
         if (FD_ISSET(fd, &writefds)) {
             ++nevents;
-            revents |= WRITE_EVENT;
+            revents |= HV_WRITE;
         }
         if (revents) {
             hio_t* io = loop->ios.ptr[fd];

+ 1 - 1
examples/nc.c

@@ -49,7 +49,7 @@ void on_stdin(hio_t* io, void* buf, int readbytes) {
 
 void on_close(hio_t* io) {
     //printf("on_close fd=%d error=%d\n", hio_fd(io), hio_error(io));
-    hio_del(stdinio, READ_EVENT);
+    hio_del(stdinio, HV_READ);
 }
 
 void on_connect(hio_t* io) {