Browse Source

hio_set_close_timeout

ithewei 5 years ago
parent
commit
4c85b7b6c3
14 changed files with 182 additions and 98 deletions
  1. 1 1
      README.md
  2. 4 4
      docs/apis.md
  3. 1 1
      echo-servers/libhv_echo.c
  4. 4 0
      event/hevent.c
  5. 3 0
      event/hevent.h
  6. 49 12
      event/hloop.c
  7. 17 6
      event/hloop.h
  8. 81 53
      event/nio.c
  9. 1 1
      event/nlog.c
  10. 16 15
      event/overlapio.c
  11. 2 2
      examples/nc.c
  12. 1 1
      examples/tcp.c
  13. 1 1
      examples/udp.c
  14. 1 1
      readme_cn.md

+ 1 - 1
README.md

@@ -146,7 +146,7 @@ int main(int argc, char** argv) {
     int port = atoi(argv[1]);
 
     hloop_t* loop = hloop_new(0);
-    hio_t* listenio = create_tcp_server(loop, "0.0.0.0", port, on_accept);
+    hio_t* listenio = hloop_create_tcp_server(loop, "0.0.0.0", port, on_accept);
     if (listenio == NULL) {
         return -20;
     }

+ 4 - 4
docs/apis.md

@@ -324,10 +324,10 @@
 ## event
 
 ### hloop.h
-- create_tcp_client
-- create_tcp_server
-- create_udp_client
-- create_udp_server
+- hloop_create_tcp_client
+- hloop_create_tcp_server
+- hloop_create_udp_client
+- hloop_create_udp_server
 - hloop_new
 - hloop_free
 - hloop_run

+ 1 - 1
echo-servers/libhv_echo.c

@@ -21,7 +21,7 @@ int main(int argc, char** argv) {
     int port = atoi(argv[1]);
 
     hloop_t* loop = hloop_new(0);
-    hio_t* listenio = create_tcp_server(loop, "0.0.0.0", port, on_accept);
+    hio_t* listenio = hloop_create_tcp_server(loop, "0.0.0.0", port, on_accept);
     if (listenio == NULL) {
         return -20;
     }

+ 4 - 0
event/hevent.c

@@ -83,6 +83,10 @@ void hio_set_connect_timeout(hio_t* io, int timeout_ms) {
     io->connect_timeout = timeout_ms;
 }
 
+void hio_set_close_timeout(hio_t* io, int timeout_ms) {
+    io->close_timeout = timeout_ms;
+}
+
 void hio_set_keepalive_timeout(hio_t* io, int timeout_ms) {
     io->keepalive_timeout = timeout_ms;
 }

+ 3 - 0
event/hevent.h

@@ -99,6 +99,7 @@ struct hio_s {
     unsigned    send        :1;
     unsigned    recvfrom    :1;
     unsigned    sendto      :1;
+    unsigned    close       :1;
 // public:
     int         fd;
     hio_type_e  io_type;
@@ -118,6 +119,8 @@ struct hio_s {
     // timers
     int         connect_timeout;    // ms
     htimer_t*   connect_timer;
+    int         close_timeout;      // ms
+    htimer_t*   close_timer;
     int         keepalive_timeout;  // ms
     htimer_t*   keepalive_timer;
     int         heartbeat_interval; // ms

+ 49 - 12
event/hloop.c

@@ -14,6 +14,13 @@
 
 #define IO_ARRAY_INIT_SIZE              1024
 #define CUSTOM_EVENT_QUEUE_INIT_SIZE    16
+
+/*
+ * hio lifeline:
+ * hio_get => HV_ALLOC_SIZEOF(io) => hio_init =>
+ * hio_ready => hio_add => hio_del => hio_done =>
+ * hio_free => HV_FREE(io)
+ */
 static void hio_init(hio_t* io);
 static void hio_ready(hio_t* io);
 static void hio_done(hio_t* io);
@@ -221,9 +228,6 @@ static void hloop_cleanup(hloop_t* loop) {
     for (int i = 0; i < loop->ios.maxsize; ++i) {
         hio_t* io = loop->ios.ptr[i];
         if (io) {
-            if ((!(io->io_type&HIO_TYPE_STDIO)) && io->active) {
-                hio_close(io);
-            }
             hio_free(io);
         }
     }
@@ -503,6 +507,16 @@ static void hio_socket_init(hio_t* io) {
 }
 
 void hio_init(hio_t* io) {
+    // alloc localaddr,peeraddr when hio_socket_init
+    /*
+    if (io->localaddr == NULL) {
+        HV_ALLOC(io->localaddr, sizeof(sockaddr_u));
+    }
+    if (io->peeraddr == NULL) {
+        HV_ALLOC(io->peeraddr, sizeof(sockaddr_u));
+    }
+    */
+
     // write_queue init when hwrite try_write failed
     // write_queue_init(&io->write_queue, 4);
 }
@@ -515,6 +529,7 @@ void hio_ready(hio_t* io) {
     io->accept = io->connect = io->connectex = 0;
     io->recv = io->send = 0;
     io->recvfrom = io->sendto = 0;
+    io->close = 0;
     // public:
     io->io_type = HIO_TYPE_UNKNOWN;
     io->error = 0;
@@ -526,7 +541,15 @@ void hio_ready(hio_t* io) {
     io->accept_cb = 0;
     io->connect_cb = 0;
     // timers
+    io->connect_timeout = 0;
     io->connect_timer = NULL;
+    io->close_timeout = 0;
+    io->close_timer = NULL;
+    io->keepalive_timeout = 0;
+    io->keepalive_timer = NULL;
+    io->heartbeat_interval = 0;
+    io->heartbeat_fn = NULL;
+    io->heartbeat_timer = NULL;
     // private:
     io->event_index[0] = io->event_index[1] = -1;
     io->hovlp = NULL;
@@ -540,7 +563,9 @@ void hio_ready(hio_t* io) {
 }
 
 void hio_done(hio_t* io) {
+    if (!io->ready) return;
     io->ready = 0;
+
     offset_buf_t* pbuf = NULL;
     while (!write_queue_empty(&io->write_queue)) {
         pbuf = write_queue_front(&io->write_queue);
@@ -552,7 +577,9 @@ void hio_done(hio_t* io) {
 
 void hio_free(hio_t* io) {
     if (io == NULL) return;
+    // NOTE: call hio_done to cleanup write_queue
     hio_done(io);
+    hio_close(io);
     HV_FREE(io->localaddr);
     HV_FREE(io->peeraddr);
     HV_FREE(io);
@@ -592,15 +619,15 @@ int hio_add(hio_t* io, hio_cb cb, int events) {
     if (io->fd < 3) return 0;
 #endif
     hloop_t* loop = io->loop;
-    if (!io->ready) {
-        hio_ready(io);
-    }
-
     if (!io->active) {
         EVENT_ADD(loop, io, cb);
         loop->nios++;
     }
 
+    if (!io->ready) {
+        hio_ready(io);
+    }
+
     if (cb) {
         io->cb = (hevent_cb)cb;
     }
@@ -612,7 +639,11 @@ int hio_add(hio_t* io, hio_cb cb, int events) {
 
 int hio_del(hio_t* io, int events) {
     printd("hio_del fd=%d io->events=%d events=%d\n", io->fd, io->events, events);
-    if (!io->active) return 0;
+#ifdef OS_WIN
+    // Windows iowatcher not work on stdio
+    if (io->fd < 3) return 0;
+#endif
+    if (!io->active || !io->ready) return 0;
     iowatcher_del_event(io->loop, io->fd, events);
     io->events &= ~events;
     if (io->events == 0) {
@@ -668,6 +699,12 @@ hio_t* hconnect (hloop_t* loop, int connfd, hconnect_cb connect_cb) {
     return io;
 }
 
+void hclose (hloop_t* loop, int fd) {
+    hio_t* io = hio_get(loop, fd);
+    if (io == NULL) return;
+    hio_close(io);
+}
+
 hio_t* hrecv (hloop_t* loop, int connfd, void* buf, size_t len, hread_cb read_cb) {
     //hio_t* io = hio_get(loop, connfd);
     //if (io == NULL) return NULL;
@@ -704,7 +741,7 @@ hio_t* hsendto (hloop_t* loop, int sockfd, const void* buf, size_t len, hwrite_c
     return hwrite(loop, sockfd, buf, len, write_cb);
 }
 
-hio_t* create_tcp_server (hloop_t* loop, const char* host, int port, haccept_cb accept_cb) {
+hio_t* hloop_create_tcp_server (hloop_t* loop, const char* host, int port, haccept_cb accept_cb) {
     int listenfd = Listen(port, host);
     if (listenfd < 0) {
         return NULL;
@@ -716,7 +753,7 @@ hio_t* create_tcp_server (hloop_t* loop, const char* host, int port, haccept_cb
     return io;
 }
 
-hio_t* create_tcp_client (hloop_t* loop, const char* host, int port, hconnect_cb connect_cb) {
+hio_t* hloop_create_tcp_client (hloop_t* loop, const char* host, int port, hconnect_cb connect_cb) {
     sockaddr_u peeraddr;
     memset(&peeraddr, 0, sizeof(peeraddr));
     int ret = sockaddr_set_ipport(&peeraddr, host, port);
@@ -738,7 +775,7 @@ hio_t* create_tcp_client (hloop_t* loop, const char* host, int port, hconnect_cb
 }
 
 // @server: socket -> bind -> hrecvfrom
-hio_t* create_udp_server(hloop_t* loop, const char* host, int port) {
+hio_t* hloop_create_udp_server(hloop_t* loop, const char* host, int port) {
     int bindfd = Bind(port, host, SOCK_DGRAM);
     if (bindfd < 0) {
         return NULL;
@@ -747,7 +784,7 @@ hio_t* create_udp_server(hloop_t* loop, const char* host, int port) {
 }
 
 // @client: Resolver -> socket -> hio_get -> hio_set_peeraddr
-hio_t* create_udp_client(hloop_t* loop, const char* host, int port) {
+hio_t* hloop_create_udp_client(hloop_t* loop, const char* host, int port) {
     sockaddr_u peeraddr;
     memset(&peeraddr, 0, sizeof(peeraddr));
     int ret = sockaddr_set_ipport(&peeraddr, host, port);

+ 17 - 6
event/hloop.h

@@ -89,6 +89,7 @@ typedef enum {
 } hio_type_e;
 
 #define HIO_DEFAULT_CONNECT_TIMEOUT     5000    // ms
+#define HIO_DEFAULT_CLOSE_TIMEOUT       60000   // ms
 #define HIO_DEFAULT_KEEPALIVE_TIMEOUT   75000   // ms
 #define HIO_DEFAULT_HEARTBEAT_INTERVAL  3000    // ms
 
@@ -206,6 +207,8 @@ HV_EXPORT int  hio_enable_ssl(hio_t* io);
 HV_EXPORT void hio_set_readbuf(hio_t* io, void* buf, size_t len);
 // connect timeout => hclose_cb
 HV_EXPORT void hio_set_connect_timeout(hio_t* io, int timeout_ms DEFAULT(HIO_DEFAULT_CONNECT_TIMEOUT));
+// close timeout => hclose_cb
+HV_EXPORT void hio_set_close_timeout(hio_t* io, int timeout_ms DEFAULT(HIO_DEFAULT_CLOSE_TIMEOUT));
 // keepalive timeout => hclose_cb
 HV_EXPORT void hio_set_keepalive_timeout(hio_t* io, int timeout_ms DEFAULT(HIO_DEFAULT_KEEPALIVE_TIMEOUT));
 /*
@@ -220,10 +223,15 @@ typedef void (*hio_send_heartbeat_fn)(hio_t* io);
 HV_EXPORT void hio_set_heartbeat(hio_t* io, int interval_ms, hio_send_heartbeat_fn fn);
 
 // Nonblocking, poll IO events in the loop to call corresponding callback.
+// hio_add(io, HV_READ) => accept => haccept_cb
 HV_EXPORT int hio_accept (hio_t* io);
+// connect => hio_add(io, HV_WRITE) => hconnect_cb
 HV_EXPORT int hio_connect(hio_t* io);
+// hio_add(io, HV_READ) => read => hread_cb
 HV_EXPORT int hio_read   (hio_t* io);
+// hio_try_write => hio_add(io, HV_WRITE) => write => hwrite_cb
 HV_EXPORT int hio_write  (hio_t* io, const void* buf, size_t len);
+// hio_del(io, HV_RDWR) => close => hclose_cb
 HV_EXPORT int hio_close  (hio_t* io);
 
 //------------------high-level apis-------------------------------------------
@@ -244,8 +252,7 @@ HV_EXPORT hio_t* hrecv    (hloop_t* loop, int connfd, void* buf, size_t len, hre
 // hio_get -> hio_setcb_write -> hio_write
 HV_EXPORT hio_t* hsend    (hloop_t* loop, int connfd, const void* buf, size_t len, hwrite_cb write_cb DEFAULT(NULL));
 
-// udp/ip
-// for HIO_TYPE_IP
+// udp
 HV_EXPORT void hio_set_type(hio_t* io, hio_type_e type);
 HV_EXPORT void hio_set_localaddr(hio_t* io, struct sockaddr* addr, int addrlen);
 HV_EXPORT void hio_set_peeraddr (hio_t* io, struct sockaddr* addr, int addrlen);
@@ -257,14 +264,18 @@ HV_EXPORT hio_t* hsendto   (hloop_t* loop, int sockfd, const void* buf, size_t l
 
 //----------------- top-level apis---------------------------------------------
 // @tcp_server: socket -> bind -> listen -> haccept
-HV_EXPORT hio_t* create_tcp_server (hloop_t* loop, const char* host, int port, haccept_cb accept_cb);
+// @see examples/tcp.c
+HV_EXPORT hio_t* hloop_create_tcp_server (hloop_t* loop, const char* host, int port, haccept_cb accept_cb);
 // @tcp_client: resolver -> socket -> hio_get -> hio_set_peeraddr -> hconnect
-HV_EXPORT hio_t* create_tcp_client (hloop_t* loop, const char* host, int port, hconnect_cb connect_cb);
+// @see examples/nc.c
+HV_EXPORT hio_t* hloop_create_tcp_client (hloop_t* loop, const char* host, int port, hconnect_cb connect_cb);
 
 // @udp_server: socket -> bind -> hio_get
-HV_EXPORT hio_t* create_udp_server (hloop_t* loop, const char* host, int port);
+// @see examples/udp.c
+HV_EXPORT hio_t* hloop_create_udp_server (hloop_t* loop, const char* host, int port);
 // @udp_client: resolver -> socket -> hio_get -> hio_set_peeraddr
-HV_EXPORT hio_t* create_udp_client (hloop_t* loop, const char* host, int port);
+// @see examples/nc.c
+HV_EXPORT hio_t* hloop_create_udp_client (hloop_t* loop, const char* host, int port);
 
 END_EXTERN_C
 

+ 81 - 53
event/nio.c

@@ -12,6 +12,20 @@ static void __connect_timeout_cb(htimer_t* timer) {
         hlogw("connect timeout [%s] <=> [%s]",
                 SOCKADDR_STR(io->localaddr, localaddrstr),
                 SOCKADDR_STR(io->peeraddr, peeraddrstr));
+        io->error = ETIMEDOUT;
+        hio_close(io);
+    }
+}
+
+static void __close_timeout_cb(htimer_t* timer) {
+    hio_t* io = (hio_t*)timer->privdata;
+    if (io) {
+        char localaddrstr[SOCKADDR_STRLEN] = {0};
+        char peeraddrstr[SOCKADDR_STRLEN] = {0};
+        hlogw("close timeout [%s] <=> [%s]",
+                SOCKADDR_STR(io->localaddr, localaddrstr),
+                SOCKADDR_STR(io->peeraddr, peeraddrstr));
+        io->error = ETIMEDOUT;
         hio_close(io);
     }
 }
@@ -24,6 +38,7 @@ static void __keepalive_timeout_cb(htimer_t* timer) {
         hlogw("keepalive timeout [%s] <=> [%s]",
                 SOCKADDR_STR(io->localaddr, localaddrstr),
                 SOCKADDR_STR(io->peeraddr, peeraddrstr));
+        io->error = ETIMEDOUT;
         hio_close(io);
     }
 }
@@ -126,6 +141,12 @@ static void __close_cb(hio_t* io) {
         io->connect_timeout = 0;
     }
 
+    if (io->close_timer) {
+        htimer_del(io->close_timer);
+        io->close_timer = NULL;
+        io->close_timeout = 0;
+    }
+
     if (io->keepalive_timer) {
         htimer_del(io->keepalive_timer);
         io->keepalive_timer = NULL;
@@ -273,15 +294,8 @@ connect_failed:
     hio_close(io);
 }
 
-static void nio_read(hio_t* io) {
-    //printd("nio_read fd=%d\n", io->fd);
-    int nread;
-    if (io->readbuf.base == NULL || io->readbuf.len == 0) {
-        hio_set_readbuf(io, io->loop->readbuf.base, io->loop->readbuf.len);
-    }
-    void* buf = io->readbuf.base;
-    int   len = io->readbuf.len;
-read:
+static int __nio_read(hio_t* io, void* buf, int len) {
+    int nread = 0;
     switch (io->io_type) {
 #ifdef WITH_OPENSSL
     case HIO_TYPE_SSL:
@@ -306,6 +320,45 @@ read:
         nread = read(io->fd, buf, len);
         break;
     }
+    return nread;
+}
+
+static int __nio_write(hio_t* io, const void* buf, int len) {
+    int nwrite = 0;
+    switch (io->io_type) {
+#ifdef WITH_OPENSSL
+    case HIO_TYPE_SSL:
+        nwrite = SSL_write((SSL*)io->ssl, buf, len);
+        break;
+#endif
+    case HIO_TYPE_TCP:
+#ifdef OS_UNIX
+        nwrite = write(io->fd, buf, len);
+#else
+        nwrite = send(io->fd, buf, len, 0);
+#endif
+        break;
+    case HIO_TYPE_UDP:
+    case HIO_TYPE_IP:
+        nwrite = sendto(io->fd, buf, len, 0, io->peeraddr, sizeof(sockaddr_u));
+        break;
+    default:
+        nwrite = write(io->fd, buf, len);
+        break;
+    }
+    return nwrite;
+}
+
+static void nio_read(hio_t* io) {
+    //printd("nio_read fd=%d\n", io->fd);
+    if (io->readbuf.base == NULL || io->readbuf.len == 0) {
+        hio_set_readbuf(io, io->loop->readbuf.base, io->loop->readbuf.len);
+    }
+    void* buf = io->readbuf.base;
+    int   len = io->readbuf.len;
+    int   nread = 0;
+read:
+    nread = __nio_read(io, buf, len);
     //printd("read retval=%d\n", nread);
     if (nread < 0) {
         if (socket_errno() == EAGAIN) {
@@ -336,32 +389,16 @@ static void nio_write(hio_t* io) {
     int nwrite = 0;
 write:
     if (write_queue_empty(&io->write_queue)) {
+        if (io->close) {
+            io->close = 0;
+            hio_close(io);
+        }
         return;
     }
     offset_buf_t* pbuf = write_queue_front(&io->write_queue);
     char* buf = pbuf->base + pbuf->offset;
     int len = pbuf->len - pbuf->offset;
-    switch (io->io_type) {
-#ifdef WITH_OPENSSL
-    case HIO_TYPE_SSL:
-        nwrite = SSL_write((SSL*)io->ssl, buf, len);
-        break;
-#endif
-    case HIO_TYPE_TCP:
-#ifdef OS_UNIX
-        nwrite = write(io->fd, buf, len);
-#else
-        nwrite = send(io->fd, buf, len, 0);
-#endif
-        break;
-    case HIO_TYPE_UDP:
-    case HIO_TYPE_IP:
-        nwrite = sendto(io->fd, buf, len, 0, io->peeraddr, sizeof(sockaddr_u));
-        break;
-    default:
-        nwrite = write(io->fd, buf, len);
-        break;
-    }
+    nwrite = __nio_write(io, buf, len);
     //printd("write retval=%d\n", nwrite);
     if (nwrite < 0) {
         if (socket_errno() == EAGAIN) {
@@ -457,27 +494,7 @@ int hio_write (hio_t* io, const void* buf, size_t len) {
     int nwrite = 0;
     if (write_queue_empty(&io->write_queue)) {
 try_write:
-        switch (io->io_type) {
-#ifdef WITH_OPENSSL
-        case HIO_TYPE_SSL:
-            nwrite = SSL_write((SSL*)io->ssl, buf, len);
-            break;
-#endif
-        case HIO_TYPE_TCP:
-#ifdef OS_UNIX
-            nwrite = write(io->fd, buf, len);
-#else
-            nwrite = send(io->fd, buf, len, 0);
-#endif
-            break;
-        case HIO_TYPE_UDP:
-        case HIO_TYPE_IP:
-            nwrite = sendto(io->fd, buf, len, 0, io->peeraddr, SOCKADDR_LEN(io->peeraddr));
-            break;
-        default:
-            nwrite = write(io->fd, buf, len);
-            break;
-        }
+        nwrite = __nio_write(io, buf, len);
         //printd("write retval=%d\n", nwrite);
         if (nwrite < 0) {
             if (socket_errno() == EAGAIN) {
@@ -523,13 +540,24 @@ disconnect:
 
 int hio_close (hio_t* io) {
     if (io->closed) return 0;
+    if (!write_queue_empty(&io->write_queue) && io->error == 0 && io->close == 0) {
+        io->close = 1;
+        hlogw("write_queue not empty, close later.");
+        int timeout_ms = io->close_timeout ? io->close_timeout : HIO_DEFAULT_CLOSE_TIMEOUT;
+        io->close_timer = htimer_add(io->loop, __close_timeout_cb, timeout_ms, 1);
+        io->close_timer->privdata = io;
+        return 0;
+    }
+
     io->closed = 1;
     hio_del(io, HV_RDWR);
+    if (io->io_type & HIO_TYPE_SOCKET) {
 #ifdef OS_UNIX
-    close(io->fd);
+        close(io->fd);
 #else
-    closesocket(io->fd);
+        closesocket(io->fd);
 #endif
+    }
 #ifdef WITH_OPENSSL
     if (io->ssl) {
         SSL_free((SSL*)io->ssl);

+ 1 - 1
event/nlog.c

@@ -83,7 +83,7 @@ void network_logger(int loglevel, const char* buf, int len) {
 hio_t* nlog_listen(hloop_t* loop, int port) {
     list_init(&s_logger.clients);
     s_logger.loop = loop;
-    s_logger.listenio = create_tcp_server(loop, "0.0.0.0", port, on_accept);
+    s_logger.listenio = hloop_create_tcp_server(loop, "0.0.0.0", port, on_accept);
     hmutex_init(&s_mutex);
     return s_logger.listenio;
 }

+ 16 - 15
event/overlapio.c

@@ -377,28 +377,29 @@ disconnect:
 }
 
 int hio_close (hio_t* io) {
-    printd("close fd=%d\n", io->fd);
     if (io->closed) return 0;
     io->closed = 1;
     hio_del(io, HV_RDWR);
+    if (io->io_type & HIO_TYPE_SOCKET) {
 #ifdef USE_DISCONNECTEX
-    // DisconnectEx reuse socket
-    if (io->connectex) {
-        io->connectex = 0;
-        LPFN_DISCONNECTEX DisconnectEx = NULL;
-        GUID guidDisconnectEx = WSAID_DISCONNECTEX;
-        DWORD dwbytes;
-        if (WSAIoctl(io->fd, SIO_GET_EXTENSION_FUNCTION_POINTER,
-            &guidDisconnectEx, sizeof(guidDisconnectEx),
-            &DisconnectEx, sizeof(DisconnectEx),
-            &dwbytes, NULL, NULL) != 0) {
-            return;
+        // DisconnectEx reuse socket
+        if (io->connectex) {
+            io->connectex = 0;
+            LPFN_DISCONNECTEX DisconnectEx = NULL;
+            GUID guidDisconnectEx = WSAID_DISCONNECTEX;
+            DWORD dwbytes;
+            if (WSAIoctl(io->fd, SIO_GET_EXTENSION_FUNCTION_POINTER,
+                &guidDisconnectEx, sizeof(guidDisconnectEx),
+                &DisconnectEx, sizeof(DisconnectEx),
+                &dwbytes, NULL, NULL) != 0) {
+                return;
+            }
+            DisconnectEx(io->fd, NULL, 0, 0);
         }
-        DisconnectEx(io->fd, NULL, 0, 0);
-    }
 #else
-    closesocket(io->fd);
+        closesocket(io->fd);
 #endif
+    }
     if (io->hovlp) {
         hoverlapped_t* hovlp = (hoverlapped_t*)io->hovlp;
         // NOTE: hread buf provided by caller

+ 2 - 2
examples/nc.c

@@ -115,11 +115,11 @@ Examples: nc 127.0.0.1 80\n\
     // socket
     if (protocol == 1) {
         // tcp
-        sockio = create_tcp_client(loop, host, port, on_connect);
+        sockio = hloop_create_tcp_client(loop, host, port, on_connect);
     }
     else if (protocol == 2) {
         // udp
-        sockio = create_udp_client(loop, host, port);
+        sockio = hloop_create_udp_client(loop, host, port);
         hio_read(sockio);
     }
     if (sockio == NULL) {

+ 1 - 1
examples/tcp.c

@@ -39,7 +39,7 @@ int main(int argc, char** argv) {
     int port = atoi(argv[1]);
 
     hloop_t* loop = hloop_new(0);
-    hio_t* listenio = create_tcp_server(loop, "0.0.0.0", port, on_accept);
+    hio_t* listenio = hloop_create_tcp_server(loop, "0.0.0.0", port, on_accept);
     if (listenio == NULL) {
         return -20;
     }

+ 1 - 1
examples/udp.c

@@ -26,7 +26,7 @@ int main(int argc, char** argv) {
     int port = atoi(argv[1]);
 
     hloop_t* loop = hloop_new(0);
-    hio_t* io = create_udp_server(loop, "0.0.0.0", port);
+    hio_t* io = hloop_create_udp_server(loop, "0.0.0.0", port);
     if (io == NULL) {
         return -20;
     }

+ 1 - 1
readme_cn.md

@@ -145,7 +145,7 @@ int main(int argc, char** argv) {
     int port = atoi(argv[1]);
 
     hloop_t* loop = hloop_new(0);
-    hio_t* listenio = create_tcp_server(loop, "0.0.0.0", port, on_accept);
+    hio_t* listenio = hloop_create_tcp_server(loop, "0.0.0.0", port, on_accept);
     if (listenio == NULL) {
         return -20;
     }