1
0
ithewei 5 жил өмнө
parent
commit
97e61072eb
3 өөрчлөгдсөн 24 нэмэгдсэн , 5 устгасан
  1. 4 0
      event/hloop.c
  2. 18 3
      event/nio.c
  3. 2 2
      event/poll.c

+ 4 - 0
event/hloop.c

@@ -545,6 +545,10 @@ hio_t* hio_get(hloop_t* loop, int fd) {
 
 int hio_add(hio_t* io, hio_cb cb, int events) {
     printd("hio_add fd=%d events=%d\n", io->fd, events);
+#ifdef OS_WIN
+    // Windows iowatcher not work on stdio
+    if (io->fd < 3) return 0;
+#endif
     hloop_t* loop = io->loop;
     if (!io->ready) {
         hio_ready(io);

+ 18 - 3
event/nio.c

@@ -192,7 +192,7 @@ read:
         }
         else {
             io->error = socket_errno();
-            perror("read");
+            /* perror("read"); */
             goto read_error;
         }
     }
@@ -253,7 +253,7 @@ write:
         }
         else {
             io->error = socket_errno();
-            perror("write");
+            /* perror("write"); */
             goto write_error;
         }
     }
@@ -295,6 +295,9 @@ static void hio_handle_events(hio_t* io) {
             io->events &= ~WRITE_EVENT;
         }
         if (io->connect) {
+#ifdef OS_WIN
+            htimer_del((htimer_t*)io->userdata);
+#endif
             // NOTE: connect just do once
             // ONESHOT
             io->connect = 0;
@@ -314,6 +317,13 @@ int hio_accept(hio_t* io) {
     return 0;
 }
 
+#ifdef OS_WIN
+#define CONNECT_TIMEOUT     5000 // ms
+static void connect_timeout_cb(htimer_t* timer) {
+    hio_close((hio_t*)timer->userdata);
+}
+#endif
+
 int hio_connect(hio_t* io) {
     int ret = connect(io->fd, io->peeraddr, sizeof(sockaddr_un));
 #ifdef OS_WIN
@@ -332,6 +342,11 @@ int hio_connect(hio_t* io) {
         }
         return 0;
     }
+#ifdef OS_WIN
+    htimer_t* timer = htimer_add(io->loop, connect_timeout_cb, CONNECT_TIMEOUT, 1);
+    timer->userdata = io;
+    io->userdata = timer;
+#endif
     return hio_add(io, hio_handle_events, WRITE_EVENT);
 }
 
@@ -372,7 +387,7 @@ try_write:
                 goto enqueue;
             }
             else {
-                perror("write");
+                /* perror("write"); */
                 io->error = socket_errno();
                 goto write_error;
             }

+ 2 - 2
event/poll.c

@@ -116,10 +116,10 @@ int iowatcher_poll_events(hloop_t* loop, int timeout) {
             ++nevents;
             hio_t* io = loop->ios.ptr[fd];
             if (io) {
-                if (revents | POLLIN) {
+                if (revents & POLLIN) {
                     io->revents |= READ_EVENT;
                 }
-                if (revents | POLLOUT) {
+                if (revents & POLLOUT) {
                     io->revents |= WRITE_EVENT;
                 }
                 EVENT_PENDING(io);