Selaa lähdekoodia

connect timer

ithewei 5 vuotta sitten
vanhempi
commit
119a655257
4 muutettua tiedostoa jossa 15 lisäystä ja 15 poistoa
  1. 1 0
      event/hevent.h
  2. 2 0
      event/hloop.c
  3. 5 8
      event/nio.c
  4. 7 7
      event/overlapio.c

+ 1 - 0
event/hevent.h

@@ -116,6 +116,7 @@ struct hio_s {
     int         event_index[2]; // for poll,kqueue
     void*       hovlp;          // for iocp/overlapio
     void*       ssl;            // for SSL
+    htimer_t*   timer;         // for io timeout
 };
 
 #define EVENT_ENTRY(p)          container_of(p, hevent_t, pending_node)

+ 2 - 0
event/hloop.c

@@ -492,6 +492,8 @@ void hio_ready(hio_t* io) {
     io->connect_cb = 0;
     io->event_index[0] = io->event_index[1] = -1;
     io->hovlp = NULL;
+    io->ssl = NULL;
+    io->timer = NULL;
     fill_io_type(io);
     if (io->io_type & HIO_TYPE_SOCKET) {
         hio_socket_init(io);

+ 5 - 8
event/nio.c

@@ -295,12 +295,13 @@ 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;
+            if (io->timer) {
+                htimer_del(io->timer);
+                io->timer = NULL;
+            }
 
             nio_connect(io);
         }
@@ -317,12 +318,10 @@ 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));
@@ -342,11 +341,9 @@ 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
+    io->timer = timer;
     return hio_add(io, hio_handle_events, WRITE_EVENT);
 }
 

+ 7 - 7
event/overlapio.c

@@ -91,7 +91,6 @@ static void on_acceptex_complete(hio_t* io) {
     hoverlapped_t* hovlp = (hoverlapped_t*)io->hovlp;
     int listenfd = io->fd;
     int connfd = hovlp->fd;
-    /*
     LPFN_GETACCEPTEXSOCKADDRS GetAcceptExSockaddrs = NULL;
     GUID guidGetAcceptExSockaddrs = WSAID_GETACCEPTEXSOCKADDRS;
     DWORD dwbytes = 0;
@@ -109,19 +108,20 @@ static void on_acceptex_complete(hio_t* io) {
         &plocaladdr, &localaddrlen, &ppeeraddr, &peeraddrlen);
     memcpy(io->localaddr, plocaladdr, localaddrlen);
     memcpy(io->peeraddr, ppeeraddr, peeraddrlen);
-    */
     if (io->accept_cb) {
+        setsockopt(connfd, SOL_SOCKET, SO_UPDATE_ACCEPT_CONTEXT, (const char*)&listenfd, sizeof(int));
+        hio_t* connio = hio_get(io->loop, connfd);
+        connio->userdata = io->userdata;
+        memcpy(connio->localaddr, io->localaddr, localaddrlen);
+        memcpy(connio->peeraddr, io->peeraddr, peeraddrlen);
         /*
         char localaddrstr[SOCKADDR_STRLEN] = {0};
         char peeraddrstr[SOCKADDR_STRLEN] = {0};
         printd("accept listenfd=%d connfd=%d [%s] <= [%s]\n", listenfd, connfd,
-                SOCKADDR_STR(io->localaddr, localaddrstr),
-                SOCKADDR_STR(io->peeraddr, peeraddrstr));
+                SOCKADDR_STR(connio->localaddr, localaddrstr),
+                SOCKADDR_STR(connio->peeraddr, peeraddrstr));
         */
-        setsockopt(connfd, SOL_SOCKET, SO_UPDATE_ACCEPT_CONTEXT, (const char*)&listenfd, sizeof(int));
         //printd("accept_cb------\n");
-        hio_t* connio = hio_get(io->loop, connfd);
-        connio->userdata = io->userdata;
         io->accept_cb(connio);
         //printd("accept_cb======\n");
     }