Quellcode durchsuchen

show udp multi-threads example for (#433)

ithewei vor 2 Jahren
Ursprung
Commit
3e75b6545e

+ 32 - 6
examples/multi-thread/multi-acceptor-processes.c

@@ -12,6 +12,8 @@
 #include "hthread.h"
 #include "hproc.h"
 
+static char protocol = 't';
+static const char* protocolname = "tcp";
 static const char* host = "0.0.0.0";
 static int port = 1234;
 static int process_num = 4;
@@ -40,9 +42,17 @@ static void on_accept(hio_t* io) {
 }
 
 static void loop_proc(void* userdata) {
-    int listenfd = (int)(intptr_t)(userdata);
+    int sockfd = (int)(intptr_t)(userdata);
     hloop_t* loop = hloop_new(HLOOP_FLAG_AUTO_FREE);
-    haccept(loop, listenfd, on_accept);
+    hio_t* io = hio_get(loop, sockfd);
+    if (protocol == 't') {
+        hio_setcb_accept(io, on_accept);
+        hio_accept(io);
+    }
+    else if (protocol == 'u') {
+        hio_setcb_read(io, on_recv);
+        hio_read(io);
+    }
     hloop_run(loop);
 }
 
@@ -51,17 +61,33 @@ int main(int argc, char** argv) {
         printf("Usage: cmd port\n");
         return -10;
     }
-    port = atoi(argv[1]);
+    int index = 1;
+    if (argv[1][0] == '-') {
+        protocol = argv[1][1];
+        switch(protocol) {
+        case 't': protocolname = "tcp"; break;
+        case 'u': protocolname = "udp"; break;
+        default:  fprintf(stderr, "Unsupported protocol '%c'\n", protocol); exit(1);
+        }
+        ++index;
+    }
+    port = atoi(argv[index++]);
 
-    int listenfd = Listen(port, host);
-    if (listenfd < 0) {
+    int sockfd = -1;
+    if (protocol == 't') {
+        sockfd = Listen(port, host);
+    }
+    else if (protocol == 'u') {
+        sockfd = Bind(port, host, SOCK_DGRAM);
+    }
+    if (sockfd < 0) {
         exit(1);
     }
 
     proc_ctx_t ctx;
     memset(&ctx, 0, sizeof(ctx));
     ctx.proc = loop_proc;
-    ctx.proc_userdata = (void*)(intptr_t)listenfd;
+    ctx.proc_userdata = (void*)(intptr_t)sockfd;
     for (int i = 0; i < process_num; ++i) {
         hproc_spawn(&ctx);
     }

+ 33 - 7
examples/multi-thread/multi-acceptor-threads.c

@@ -11,6 +11,8 @@
 #include "hsocket.h"
 #include "hthread.h"
 
+static char protocol = 't';
+static const char* protocolname = "tcp";
 static const char* host = "0.0.0.0";
 static int port = 1234;
 static int thread_num = 4;
@@ -39,27 +41,51 @@ static void on_accept(hio_t* io) {
 }
 
 static HTHREAD_ROUTINE(loop_thread) {
-    int listenfd = (int)(intptr_t)(userdata);
+    int sockfd = (int)(intptr_t)(userdata);
     hloop_t* loop = hloop_new(HLOOP_FLAG_AUTO_FREE);
-    haccept(loop, listenfd, on_accept);
+    hio_t* io = hio_get(loop, sockfd);
+    if (protocol == 't') {
+        hio_setcb_accept(io, on_accept);
+        hio_accept(io);
+    }
+    else if (protocol == 'u') {
+        hio_setcb_read(io, on_recv);
+        hio_read(io);
+    }
     hloop_run(loop);
     return 0;
 }
 
 int main(int argc, char** argv) {
     if (argc < 2) {
-        printf("Usage: cmd port\n");
+        printf("Usage: cmd [-tu] port\n");
         return -10;
     }
-    port = atoi(argv[1]);
+    int index = 1;
+    if (argv[1][0] == '-') {
+        protocol = argv[1][1];
+        switch(protocol) {
+        case 't': protocolname = "tcp"; break;
+        case 'u': protocolname = "udp"; break;
+        default:  fprintf(stderr, "Unsupported protocol '%c'\n", protocol); exit(1);
+        }
+        ++index;
+    }
+    port = atoi(argv[index++]);
 
-    int listenfd = Listen(port, host);
-    if (listenfd < 0) {
+    int sockfd = -1;
+    if (protocol == 't') {
+        sockfd = Listen(port, host);
+    }
+    else if (protocol == 'u') {
+        sockfd = Bind(port, host, SOCK_DGRAM);
+    }
+    if (sockfd < 0) {
         exit(1);
     }
 
     for (int i = 0; i < thread_num; ++i) {
-        hthread_create(loop_thread, (void*)(intptr_t)listenfd);
+        hthread_create(loop_thread, (void*)(intptr_t)sockfd);
     }
 
     while(1) hv_sleep(1);