|
@@ -12,6 +12,8 @@
|
|
|
#include "hthread.h"
|
|
#include "hthread.h"
|
|
|
#include "hproc.h"
|
|
#include "hproc.h"
|
|
|
|
|
|
|
|
|
|
+static char protocol = 't';
|
|
|
|
|
+static const char* protocolname = "tcp";
|
|
|
static const char* host = "0.0.0.0";
|
|
static const char* host = "0.0.0.0";
|
|
|
static int port = 1234;
|
|
static int port = 1234;
|
|
|
static int process_num = 4;
|
|
static int process_num = 4;
|
|
@@ -40,9 +42,17 @@ static void on_accept(hio_t* io) {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
static void loop_proc(void* userdata) {
|
|
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);
|
|
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);
|
|
hloop_run(loop);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -51,17 +61,33 @@ int main(int argc, char** argv) {
|
|
|
printf("Usage: cmd port\n");
|
|
printf("Usage: cmd port\n");
|
|
|
return -10;
|
|
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);
|
|
exit(1);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
proc_ctx_t ctx;
|
|
proc_ctx_t ctx;
|
|
|
memset(&ctx, 0, sizeof(ctx));
|
|
memset(&ctx, 0, sizeof(ctx));
|
|
|
ctx.proc = loop_proc;
|
|
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) {
|
|
for (int i = 0; i < process_num; ++i) {
|
|
|
hproc_spawn(&ctx);
|
|
hproc_spawn(&ctx);
|
|
|
}
|
|
}
|