Browse Source

optimize: hio_create_socket

ithewei 4 years ago
parent
commit
900aa2dc53
5 changed files with 96 additions and 58 deletions
  1. 1 1
      docs/API.md
  2. 3 3
      event/hevent.c
  3. 48 31
      event/hloop.c
  4. 43 22
      event/hloop.h
  5. 1 1
      examples/jsonrpc/jsonrpc_client.c

+ 1 - 1
docs/API.md

@@ -445,7 +445,7 @@
 - hio_setup_tcp_upstream
 - hio_setup_tcp_upstream
 - hio_setup_ssl_upstream
 - hio_setup_ssl_upstream
 - hio_setup_udp_upstream
 - hio_setup_udp_upstream
-- hio_create
+- hio_create_socket
 - hio_context
 - hio_context
 - hio_set_context
 - hio_set_context
 - htimer_add
 - htimer_add

+ 3 - 3
event/hevent.c

@@ -56,7 +56,7 @@ static void hio_socket_init(hio_t* io) {
     // tcp_server peeraddr set by accept
     // tcp_server peeraddr set by accept
     // udp_server peeraddr set by recvfrom
     // udp_server peeraddr set by recvfrom
     // tcp_client/udp_client peeraddr set by hio_setpeeraddr
     // tcp_client/udp_client peeraddr set by hio_setpeeraddr
-    if (io->io_type == HIO_TYPE_TCP || io->io_type == HIO_TYPE_SSL) {
+    if (io->io_type & HIO_TYPE_SOCK_STREAM) {
         // tcp acceptfd
         // tcp acceptfd
         addrlen = sizeof(sockaddr_u);
         addrlen = sizeof(sockaddr_u);
         ret = getpeername(io->fd, io->peeraddr, &addrlen);
         ret = getpeername(io->fd, io->peeraddr, &addrlen);
@@ -581,7 +581,7 @@ hio_t* hio_get_upstream(hio_t* io) {
 }
 }
 
 
 hio_t* hio_setup_tcp_upstream(hio_t* io, const char* host, int port, int ssl) {
 hio_t* hio_setup_tcp_upstream(hio_t* io, const char* host, int port, int ssl) {
-    hio_t* upstream_io = hio_create(io->loop, host, port, SOCK_STREAM);
+    hio_t* upstream_io = hio_create_socket(io->loop, host, port, HIO_TYPE_TCP, HIO_CLIENT_SIDE);
     if (upstream_io == NULL) return NULL;
     if (upstream_io == NULL) return NULL;
     if (ssl) hio_enable_ssl(upstream_io);
     if (ssl) hio_enable_ssl(upstream_io);
     hio_setup_upstream(io, upstream_io);
     hio_setup_upstream(io, upstream_io);
@@ -592,7 +592,7 @@ hio_t* hio_setup_tcp_upstream(hio_t* io, const char* host, int port, int ssl) {
 }
 }
 
 
 hio_t* hio_setup_udp_upstream(hio_t* io, const char* host, int port) {
 hio_t* hio_setup_udp_upstream(hio_t* io, const char* host, int port) {
-    hio_t* upstream_io = hio_create(io->loop, host, port, SOCK_DGRAM);
+    hio_t* upstream_io = hio_create_socket(io->loop, host, port, HIO_TYPE_UDP, HIO_CLIENT_SIDE);
     if (upstream_io == NULL) return NULL;
     if (upstream_io == NULL) return NULL;
     hio_setup_upstream(io, upstream_io);
     hio_setup_upstream(io, upstream_io);
     hio_read_upstream(io);
     hio_read_upstream(io);

+ 48 - 31
event/hloop.c

@@ -804,68 +804,85 @@ hio_t* hsendto (hloop_t* loop, int sockfd, const void* buf, size_t len, hwrite_c
 }
 }
 
 
 //-----------------top-level apis---------------------------------------------
 //-----------------top-level apis---------------------------------------------
-hio_t* hio_create(hloop_t* loop, const char* host, int port, int type) {
-    sockaddr_u peeraddr;
-    memset(&peeraddr, 0, sizeof(peeraddr));
-    int ret = sockaddr_set_ipport(&peeraddr, host, port);
+hio_t* hio_create_socket(hloop_t* loop, const char* host, int port, hio_type_e type, hio_side_e side) {
+    int sock_type = type & HIO_TYPE_SOCK_STREAM ? SOCK_STREAM :
+                    type & HIO_TYPE_SOCK_DGRAM  ? SOCK_DGRAM :
+                    type & HIO_TYPE_SOCK_RAW    ? SOCK_RAW : -1;
+    if (sock_type == -1) return NULL;
+    sockaddr_u addr;
+    memset(&addr, 0, sizeof(addr));
+    int ret = sockaddr_set_ipport(&addr, host, port);
     if (ret != 0) {
     if (ret != 0) {
-        //printf("unknown host: %s\n", host);
+        // fprintf(stderr, "unknown host: %s\n", host);
         return NULL;
         return NULL;
     }
     }
-    int connfd = socket(peeraddr.sa.sa_family, type, 0);
-    if (connfd < 0) {
+    int sockfd = socket(addr.sa.sa_family, sock_type, 0);
+    if (sockfd < 0) {
         perror("socket");
         perror("socket");
         return NULL;
         return NULL;
     }
     }
-
-    hio_t* io = hio_get(loop, connfd);
+    hio_t* io = NULL;
+    if (side == HIO_SERVER_SIDE) {
+        if (bind(sockfd, &addr.sa, sockaddr_len(&addr)) < 0) {
+            perror("bind");
+            closesocket(sockfd);
+            return NULL;
+        }
+        if (sock_type == SOCK_STREAM) {
+            if (listen(sockfd, SOMAXCONN) < 0) {
+                perror("listen");
+                closesocket(sockfd);
+                return NULL;
+            }
+        }
+    }
+    io = hio_get(loop, sockfd);
     assert(io != NULL);
     assert(io != NULL);
-    hio_set_peeraddr(io, &peeraddr.sa, sockaddr_len(&peeraddr));
+    io->io_type = type;
+    if (side == HIO_SERVER_SIDE) {
+        hio_set_localaddr(io, &addr.sa, sockaddr_len(&addr));
+    } else {
+        hio_set_peeraddr(io, &addr.sa, sockaddr_len(&addr));
+    }
     return io;
     return io;
 }
 }
 
 
 hio_t* hloop_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;
-    }
-    hio_t* io = haccept(loop, listenfd, accept_cb);
-    if (io == NULL) {
-        closesocket(listenfd);
-    }
+    hio_t* io = hio_create_socket(loop, host, port, HIO_TYPE_TCP, HIO_SERVER_SIDE);
+    if (io == NULL) return NULL;
+    hio_setcb_accept(io, accept_cb);
+    hio_accept(io);
     return io;
     return io;
 }
 }
 
 
 hio_t* hloop_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) {
-    hio_t* io = hio_create(loop, host, port, SOCK_STREAM);
+    hio_t* io = hio_create_socket(loop, host, port, HIO_TYPE_TCP, HIO_CLIENT_SIDE);
     if (io == NULL) return NULL;
     if (io == NULL) return NULL;
-    hconnect(loop, io->fd, connect_cb);
+    hio_setcb_connect(io, connect_cb);
+    hio_connect(io);
     return io;
     return io;
 }
 }
 
 
 hio_t* hloop_create_ssl_server (hloop_t* loop, const char* host, int port, haccept_cb accept_cb) {
 hio_t* hloop_create_ssl_server (hloop_t* loop, const char* host, int port, haccept_cb accept_cb) {
-    hio_t* io = hloop_create_tcp_server(loop, host, port, accept_cb);
+    hio_t* io = hio_create_socket(loop, host, port, HIO_TYPE_SSL, HIO_SERVER_SIDE);
     if (io == NULL) return NULL;
     if (io == NULL) return NULL;
-    hio_enable_ssl(io);
+    hio_setcb_accept(io, accept_cb);
+    hio_accept(io);
     return io;
     return io;
 }
 }
 
 
 hio_t* hloop_create_ssl_client (hloop_t* loop, const char* host, int port, hconnect_cb connect_cb) {
 hio_t* hloop_create_ssl_client (hloop_t* loop, const char* host, int port, hconnect_cb connect_cb) {
-    hio_t* io = hio_create(loop, host, port, SOCK_STREAM);
+    hio_t* io = hio_create_socket(loop, host, port, HIO_TYPE_SSL, HIO_CLIENT_SIDE);
     if (io == NULL) return NULL;
     if (io == NULL) return NULL;
-    hio_enable_ssl(io);
-    hconnect(loop, io->fd, connect_cb);
+    hio_setcb_connect(io, connect_cb);
+    hio_connect(io);
     return io;
     return io;
 }
 }
 
 
 hio_t* hloop_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;
-    }
-    return hio_get(loop, bindfd);
+    return hio_create_socket(loop, host, port, HIO_TYPE_UDP, HIO_SERVER_SIDE);
 }
 }
 
 
 hio_t* hloop_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) {
-    return hio_create(loop, host, port, SOCK_DGRAM);
+    return hio_create_socket(loop, host, port, HIO_TYPE_UDP, HIO_CLIENT_SIDE);
 }
 }

+ 43 - 22
event/hloop.h

@@ -82,21 +82,34 @@ struct hevent_s {
 #define hevent_userdata(ev)     (((hevent_t*)(ev))->userdata)
 #define hevent_userdata(ev)     (((hevent_t*)(ev))->userdata)
 
 
 typedef enum {
 typedef enum {
-    HIO_TYPE_UNKNOWN = 0,
-    HIO_TYPE_STDIN   = 0x00000001,
-    HIO_TYPE_STDOUT  = 0x00000002,
-    HIO_TYPE_STDERR  = 0x00000004,
-    HIO_TYPE_STDIO   = 0x0000000F,
-
-    HIO_TYPE_FILE    = 0x00000010,
-
-    HIO_TYPE_IP      = 0x00000100,
-    HIO_TYPE_UDP     = 0x00001000,
-    HIO_TYPE_TCP     = 0x00010000,
-    HIO_TYPE_SSL     = 0x00020000,
-    HIO_TYPE_SOCKET  = 0x00FFFF00,
+    HIO_TYPE_UNKNOWN    = 0,
+    HIO_TYPE_STDIN      = 0x00000001,
+    HIO_TYPE_STDOUT     = 0x00000002,
+    HIO_TYPE_STDERR     = 0x00000004,
+    HIO_TYPE_STDIO      = 0x0000000F,
+
+    HIO_TYPE_FILE       = 0x00000010,
+
+    HIO_TYPE_IP         = 0x00000100,
+    HIO_TYPE_SOCK_RAW   = 0x00000F00,
+
+    HIO_TYPE_UDP        = 0x00001000,
+    HIO_TYPE_DTLS       = 0x00010000,
+    HIO_TYPE_SOCK_DGRAM = 0x000FF000,
+
+    HIO_TYPE_TCP        = 0x00100000,
+    HIO_TYPE_SSL        = 0x01000000,
+    HIO_TYPE_TLS        = HIO_TYPE_SSL,
+    HIO_TYPE_SOCK_STREAM= 0x0FF00000,
+
+    HIO_TYPE_SOCKET     = 0x0FFFFF00,
 } hio_type_e;
 } hio_type_e;
 
 
+typedef enum {
+    HIO_SERVER_SIDE  = 0,
+    HIO_CLIENT_SIDE  = 1,
+} hio_side_e;
+
 #define HIO_DEFAULT_CONNECT_TIMEOUT     5000    // ms
 #define HIO_DEFAULT_CONNECT_TIMEOUT     5000    // ms
 #define HIO_DEFAULT_CLOSE_TIMEOUT       60000   // ms
 #define HIO_DEFAULT_CLOSE_TIMEOUT       60000   // ms
 #define HIO_DEFAULT_KEEPALIVE_TIMEOUT   75000   // ms
 #define HIO_DEFAULT_KEEPALIVE_TIMEOUT   75000   // ms
@@ -332,27 +345,35 @@ HV_EXPORT hio_t* hrecvfrom (hloop_t* loop, int sockfd, void* buf, size_t len, hr
 HV_EXPORT hio_t* hsendto   (hloop_t* loop, int sockfd, const void* buf, size_t len, hwrite_cb write_cb DEFAULT(NULL));
 HV_EXPORT hio_t* hsendto   (hloop_t* loop, int sockfd, const void* buf, size_t len, hwrite_cb write_cb DEFAULT(NULL));
 
 
 //-----------------top-level apis---------------------------------------------
 //-----------------top-level apis---------------------------------------------
-// Resolver -> socket -> hio_get
-HV_EXPORT hio_t* hio_create(hloop_t* loop, const char* host, int port, int type DEFAULT(SOCK_STREAM));
-
-// @tcp_server: socket -> bind -> listen -> haccept
+// @hio_create_socket: socket -> bind -> listen
+// sockaddr_set_ipport -> socket -> hio_get(loop, sockfd) ->
+// side == HIO_SERVER_SIDE ? bind ->
+// type & HIO_TYPE_SOCK_STREAM ? listen ->
+HV_EXPORT hio_t* hio_create_socket(hloop_t* loop, const char* host, int port,
+                            hio_type_e type DEFAULT(HIO_TYPE_TCP),
+                            hio_side_e side DEFAULT(HIO_SERVER_SIDE));
+
+// @tcp_server: hio_create_socket(loop, host, port, HIO_TYPE_TCP, HIO_SERVER_SIDE) -> hio_setcb_accept -> hio_accept
 // @see examples/tcp_echo_server.c
 // @see examples/tcp_echo_server.c
 HV_EXPORT hio_t* hloop_create_tcp_server (hloop_t* loop, const char* host, int port, haccept_cb accept_cb);
 HV_EXPORT hio_t* hloop_create_tcp_server (hloop_t* loop, const char* host, int port, haccept_cb accept_cb);
-// @tcp_client: hio_create(loop, host, port, SOCK_STREAM) -> hconnect
+
+// @tcp_client: hio_create_socket(loop, host, port, HIO_TYPE_TCP, HIO_CLIENT_SIDE) -> hio_setcb_connect -> hio_connect
 // @see examples/nc.c
 // @see examples/nc.c
 HV_EXPORT hio_t* hloop_create_tcp_client (hloop_t* loop, const char* host, int port, hconnect_cb connect_cb);
 HV_EXPORT hio_t* hloop_create_tcp_client (hloop_t* loop, const char* host, int port, hconnect_cb connect_cb);
 
 
-// @ssl_server: hloop_create_tcp_server -> hio_enable_ssl
+// @ssl_server: hio_create_socket(loop, host, port, HIO_TYPE_SSL, HIO_SERVER_SIDE) -> hio_setcb_accept -> hio_accept
 // @see examples/tcp_echo_server.c => #define TEST_SSL 1
 // @see examples/tcp_echo_server.c => #define TEST_SSL 1
 HV_EXPORT hio_t* hloop_create_ssl_server (hloop_t* loop, const char* host, int port, haccept_cb accept_cb);
 HV_EXPORT hio_t* hloop_create_ssl_server (hloop_t* loop, const char* host, int port, haccept_cb accept_cb);
-// @ssl_client: hio_create(loop, host, port, SOCK_STREAM) -> hio_enable_ssl -> hconnect
+
+// @ssl_client: hio_create_socket(loop, host, port, HIO_TYPE_SSL, HIO_CLIENT_SIDE) -> hio_setcb_connect -> hio_connect
 // @see examples/nc.c => #define TEST_SSL 1
 // @see examples/nc.c => #define TEST_SSL 1
 HV_EXPORT hio_t* hloop_create_ssl_client (hloop_t* loop, const char* host, int port, hconnect_cb connect_cb);
 HV_EXPORT hio_t* hloop_create_ssl_client (hloop_t* loop, const char* host, int port, hconnect_cb connect_cb);
 
 
-// @udp_server: socket -> bind -> hio_get
+// @udp_server: hio_create_socket(loop, host, port, HIO_TYPE_UDP, HIO_SERVER_SIDE)
 // @see examples/udp_echo_server.c
 // @see examples/udp_echo_server.c
 HV_EXPORT hio_t* hloop_create_udp_server (hloop_t* loop, const char* host, int port);
 HV_EXPORT hio_t* hloop_create_udp_server (hloop_t* loop, const char* host, int port);
-// @udp_client: hio_create(loop, host, port, SOCK_DGRAM)
+
+// @udp_server: hio_create_socket(loop, host, port, HIO_TYPE_UDP, HIO_CLIENT_SIDE)
 // @see examples/nc.c
 // @see examples/nc.c
 HV_EXPORT hio_t* hloop_create_udp_client (hloop_t* loop, const char* host, int port);
 HV_EXPORT hio_t* hloop_create_udp_client (hloop_t* loop, const char* host, int port);
 
 

+ 1 - 1
examples/jsonrpc/jsonrpc_client.c

@@ -102,7 +102,7 @@ static void on_connect(hio_t* io) {
 }
 }
 
 
 static int jsonrpc_call(hloop_t* loop, const char* host, int port, const char* method, const char* param1, const char* param2) {
 static int jsonrpc_call(hloop_t* loop, const char* host, int port, const char* method, const char* param1, const char* param2) {
-    hio_t* connio = hio_create(loop, host, port, SOCK_STREAM);
+    hio_t* connio = hio_create_socket(loop, host, port, HIO_TYPE_TCP, HIO_CLIENT_SIDE);
     if (connio == NULL) {
     if (connio == NULL) {
         return -1;
         return -1;
     }
     }