| 1 |
/* |
| 2 |
* |
| 3 |
* @build make examples |
| 4 |
* @server bin/one-acceptor-multi-workers 1234 |
| 5 |
* @client bin/nc 127.0.0.1 1234 |
| 6 |
* nc 127.0.0.1 1234 |
| 7 |
* telnet 127.0.0.1 1234 |
| 8 |
*/ |
| 9 |
|
| 10 |
#include "hloop.h" |
| 11 |
#include "hsocket.h" |
| 12 |
#include "hthread.h" |
| 13 |
|
| 14 |
static const char* host = "0.0.0.0"; |
| 15 |
static int port = 1234; |
| 16 |
static int thread_num = 4; |
| 17 |
static hloop_t* accept_loop = NULL; |
| 18 |
static hloop_t** worker_loops = NULL; |
| 19 |
|
| 20 |
static hloop_t* get_next_loop() { |
| 21 |
static int s_cur_index = 0; |
| 22 |
if (++s_cur_index >= thread_num) { |
| 23 |
s_cur_index = 0; |
| 24 |
} |
| 25 |
return worker_loops[s_cur_index % thread_num]; |
| 26 |
} |
| 27 |
|
| 28 |
static void on_close(hio_t* io) { |
| 29 |
printf("on_close fd=%d error=%d\n", hio_fd(io), hio_error(io)); |
| 30 |
} |
| 31 |
|
| 32 |
static void on_recv(hio_t* io, void* buf, int readbytes) { |
| 33 |
// echo |
| 34 |
hio_write(io, buf, readbytes); |
| 35 |
} |
| 36 |
|
| 37 |
static void new_conn_event(hevent_t* ev) { |
| 38 |
hloop_t* loop = ev->loop; |
| 39 |
hio_t* io = (hio_t*)hevent_userdata(ev); |
| 40 |
hio_attach(loop, io); |
| 41 |
|
| 42 |
char localaddrstr[SOCKADDR_STRLEN] = {0}; |
| 43 |
char peeraddrstr[SOCKADDR_STRLEN] = {0}; |
| 44 |
printf("tid=%ld connfd=%d [%s] <= [%s]\n", |
| 45 |
(long)hv_gettid(), |
| 46 |
(int)hio_fd(io), |
| 47 |
SOCKADDR_STR(hio_localaddr(io), localaddrstr), |
| 48 |
SOCKADDR_STR(hio_peeraddr(io), peeraddrstr)); |
| 49 |
|
| 50 |
hio_setcb_close(io, on_close); |
| 51 |
hio_setcb_read(io, on_recv); |
| 52 |
hio_read(io); |
| 53 |
} |
| 54 |
|
| 55 |
static void on_accept(hio_t* io) { |
| 56 |
hio_detach(io); |
| 57 |
|
| 58 |
hloop_t* worker_loop = get_next_loop(); |
| 59 |
hevent_t ev; |
| 60 |
memset(&ev, 0, sizeof(ev)); |
| 61 |
ev.loop = worker_loop; |
| 62 |
ev.cb = new_conn_event; |
| 63 |
ev.userdata = io; |
| 64 |
hloop_post_event(worker_loop, &ev); |
| 65 |
} |
| 66 |
|
| 67 |
static HTHREAD_ROUTINE(worker_thread) { |
| 68 |
hloop_t* loop = (hloop_t*)userdata; |
| 69 |
hloop_run(loop); |
| 70 |
return 0; |
| 71 |
} |
| 72 |
|
| 73 |
static HTHREAD_ROUTINE(accept_thread) { |
| 74 |
hloop_t* loop = (hloop_t*)userdata; |
| 75 |
hio_t* listenio = hloop_create_tcp_server(loop, host, port, on_accept); |
| 76 |
if (listenio == NULL) { |
| 77 |
exit(1); |
| 78 |
} |
| 79 |
hloop_run(loop); |
| 80 |
return 0; |
| 81 |
} |
| 82 |
|
| 83 |
int main(int argc, char** argv) { |
| 84 |
if (argc < 2) { |
| 85 |
printf("Usage: cmd port\n"); |
| 86 |
return -10; |
| 87 |
} |
| 88 |
port = atoi(argv[1]); |
| 89 |
|
| 90 |
worker_loops = (hloop_t**)malloc(sizeof(hloop_t*) * thread_num); |
| 91 |
for (int i = 0; i < thread_num; ++i) { |
| 92 |
worker_loops[i] = hloop_new(HLOOP_FLAG_AUTO_FREE); |
| 93 |
hthread_create(worker_thread, worker_loops[i]); |
| 94 |
} |
| 95 |
|
| 96 |
accept_loop = hloop_new(HLOOP_FLAG_AUTO_FREE); |
| 97 |
accept_thread(accept_loop); |
| 98 |
|
| 99 |
return 0; |
| 100 |
} |