13 #include <event2/event_struct.h> |
12 #include <event2/event_struct.h> |
14 #include <event2/event_compat.h> |
13 #include <event2/event_compat.h> |
15 #include <event2/bufferevent.h> |
14 #include <event2/bufferevent.h> |
16 |
15 |
17 #include "common.h" |
16 #include "common.h" |
|
17 #include "socket.h" |
18 #include "render.h" |
18 #include "render.h" |
19 #include "render_struct.h" |
19 #include "render_struct.h" |
20 #include "render_net.h" |
20 #include "render_net.h" |
21 #include "render_thread.h" |
21 #include "render_thread.h" |
22 #include "render_thread_struct.h" |
22 #include "render_thread_struct.h" |
240 close(socket); |
240 close(socket); |
241 } |
241 } |
242 |
242 |
243 int main (int argc, char** argv) { |
243 int main (int argc, char** argv) { |
244 struct event_base *ev_base; |
244 struct event_base *ev_base; |
|
245 struct config_endpoint endpoint; |
245 int ssock; |
246 int ssock; |
246 struct sockaddr_in addr; |
247 |
247 |
|
248 // parse arguments |
248 // parse arguments |
249 int opt; |
249 int opt; |
250 const char *port_name = NULL; |
250 const char *listen_spec = NULL; |
251 int enable_debug = 0; |
251 int enable_debug = 0; |
252 unsigned short port; |
|
253 |
252 |
254 while ((opt = getopt(argc, argv, "l:")) != -1) { |
253 while ((opt = getopt(argc, argv, "l:")) != -1) { |
255 switch (opt) { |
254 switch (opt) { |
256 case 'l': |
255 case 'l': |
257 if (port_name) |
256 if (listen_spec) |
258 ERROR("only specify -l once"); |
257 ERROR("only specify -l once"); |
259 |
258 |
260 port_name = optarg; |
259 listen_spec = optarg; |
261 break; |
260 break; |
262 |
261 |
263 case 'd': |
262 case 'd': |
264 // enable libevent debugging |
263 // enable libevent debugging |
265 enable_debug = 1; |
264 enable_debug = 1; |
266 break; |
265 break; |
267 |
266 |
268 default: |
267 default: |
269 err_exit("Usage: %s [-l port] [-d]", argv[0]); |
268 err_exit("Usage: %s [-l addr_spec] [-d]", argv[0]); |
270 } |
269 } |
271 } |
270 } |
272 |
271 |
273 // init libevent |
272 // init libevent |
274 if (!(ev_base = event_init())) |
273 if (!(ev_base = event_init())) |
275 FATAL("event_init"); |
274 FATAL("event_init"); |
276 |
275 |
277 // post-process arguments |
|
278 if (!port_name) |
|
279 port_name = RENDER_PORT_NAME; |
|
280 |
|
281 if (!(port = atoi(port_name))) |
|
282 ERROR("invalid port: %s", port_name); |
|
283 |
|
284 // per default it is enabled |
276 // per default it is enabled |
285 if (!enable_debug) |
277 if (!enable_debug) |
286 event_set_log_callback(&log_null); |
278 event_set_log_callback(&log_null); |
287 |
279 |
288 // create the socket |
280 // create the socket |
289 if ((ssock = socket(PF_INET, SOCK_STREAM, 0)) == -1) |
281 endpoint_init(&endpoint, RENDER_PORT); |
290 PERROR("socket"); |
282 |
291 |
283 if (endpoint_parse(&endpoint, listen_spec)) |
292 addr.sin_family = AF_INET; |
284 goto error; |
293 addr.sin_port = htons(port); |
285 |
294 addr.sin_addr.s_addr = INADDR_ANY; |
286 if ((ssock = socket_listen(&endpoint, SOCK_STREAM)) == -1) |
295 |
287 goto error; |
296 if (bind(ssock, (struct sockaddr *) &addr, sizeof(struct sockaddr_in)) == -1) |
288 |
297 PERROR("bind"); |
|
298 |
|
299 if (listen(ssock, 1) == -1) |
|
300 PERROR("listen"); |
|
301 |
|
302 // create the listen event |
289 // create the listen event |
303 struct event listen_ev; |
290 struct event listen_ev; |
304 |
291 |
305 event_set(&listen_ev, ssock, EV_READ | EV_PERSIST, &handle_accept, NULL); |
292 event_set(&listen_ev, ssock, EV_READ | EV_PERSIST, &handle_accept, NULL); |
306 |
293 |