-
Notifications
You must be signed in to change notification settings - Fork 4
/
dispatcher.c
166 lines (143 loc) · 3.88 KB
/
dispatcher.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include "define.h"
#include "dispatcher.h"
#include "worker.h"
global_t global = {NULL, NULL};
void dispatcher_timer(int fd, short event, void *arg)
{
dispatcher_t *d = (dispatcher_t *)arg;
if (d->stop) {
printf("dispatcher stoped\n");
event_base_loopexit(d->base, NULL);
}
}
dispatcher_t *dispatcher_create()
{
dispatcher_t *d = malloc(sizeof(dispatcher_t));
if (NULL == d) {
err_quit("malloc");
}
memset(d, 0, sizeof(dispatcher_t));
d->base = event_base_new();
if (NULL == d->base) {
err_quit("event_base_new()");
}
return d;
}
void dispatcher_destroy(dispatcher_t *d)
{
if (d) {
free(d);
}
}
// hooks
void dispatcher_worker_readcb(struct bufferevent *bev, void *arg)
{
#ifdef TRACE
printf("%s\n", __FUNCTION__);
#endif
}
void dispatcher_worker_writecb(struct bufferevent *bev, void *arg)
{
#ifdef TRACE
printf("%s\n", __FUNCTION__);
#endif
}
void dispatcher_worker_eventcb(struct bufferevent *bev, short error, void *arg)
{
#ifdef TRACE
printf("%s\n", __FUNCTION__);
#endif
}
int round_robin_dispatch(dispatcher_t *d, int fd)
{
static worker_t *cur = NULL;
cmd_t cmd = {0};
if (!cur) {
cur = global.workers;
}
cmd.cmd_no = CMD_ADD_CLIENT;
cmd.data = (void *)(uint64_t)fd;
if (evbuffer_add(bufferevent_get_output(cur->bev_dispatcher[1]), &cmd, sizeof cmd) != 0) {
err_quit("evbuffer_add");
}
cur = cur->next;
return 0;
}
void do_accept(int listener, short event, void *arg)
{
dispatcher_t *d = (dispatcher_t *)arg;
struct sockaddr_storage ss;
socklen_t slen = sizeof(ss);
int fd = accept(listener, (struct sockaddr*)&ss, &slen);
#ifdef TRACE
printf("%s\n", __FUNCTION__);
#endif
if (fd < 0) {
err_quit("accept");
//FD_SETSIZE
} else {
evutil_make_socket_nonblocking(fd);
round_robin_dispatch(d, fd);
}
}
void *dispatcher_loop(void *arg)
{
dispatcher_t *d = (dispatcher_t *)arg;
event_base_dispatch(d->base);
return (void *)0;
}
int dispatcher_start(dispatcher_t *d)
{
struct event *timer_event, *listener_event;
struct timeval timeout = {1, 0};
evutil_socket_t listener;
struct sockaddr_in sin;
worker_t *w;
int ret;
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = 0;
#ifdef ECHO_SERVER
sin.sin_port = htons(ECHO_SERVER_PORT);
#else
sin.sin_port = htons(DISPATCHER_PORT);
#endif
listener = socket(AF_INET, SOCK_STREAM, 0);
evutil_make_listen_socket_reuseable(listener);
evutil_make_socket_nonblocking(listener);
if (bind(listener, (struct sockaddr*)&sin, sizeof(sin)) < 0) {
err_quit("bind");
}
if (listen(listener, 128) < 0) {
err_quit("listen");
}
listener_event = event_new(d->base, listener, EV_READ|EV_PERSIST, do_accept, d);
if (NULL == listener_event) {
err_quit("event_new");
}
event_add(listener_event, NULL);
timer_event = event_new(d->base, -1, EV_PERSIST, dispatcher_timer, d);
if (NULL == timer_event) {
err_quit("event_new");
}
event_add(timer_event, &timeout);
// create bev to workers
for (w = global.workers; w != NULL; w = w->next) {
w->bev_dispatcher[1] = bufferevent_socket_new(d->base, w->sockpair_dispatcher[1], BEV_OPT_CLOSE_ON_FREE);
if (!w->bev_dispatcher[1]) {
err_quit("bufferevent_socket_new");
}
bufferevent_setcb(w->bev_dispatcher[1], dispatcher_worker_readcb, dispatcher_worker_writecb, dispatcher_worker_eventcb, w);
bufferevent_setwatermark(w->bev_dispatcher[1], EV_READ, sizeof(cmd_t), 0);
bufferevent_enable(w->bev_dispatcher[1], EV_READ | EV_WRITE);
}
ret = pthread_create(&(d->thread_id), NULL, dispatcher_loop, d);
if (ret != 0) {
err_quit("pthread_create");
}
return 0;
}
int dispatcher_stop(dispatcher_t *d)
{
d->stop = 1;
return 0;
}