-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.c
67 lines (56 loc) · 1.72 KB
/
main.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
#include <stdlib.h>
#include "udp_echo.h"
#include "tcp_echo.h"
#define UDP_SERVER_PORT 8888
#define TCP_SERVER_PORT 9999
static uv_udp_t* udp_socket = NULL;
static uv_tcp_t* tcp_socket = NULL;
static void on_walk_cleanup(uv_handle_t* handle, void* data)
{
(void)data;
uv_close(handle, NULL);
}
static void on_server_exit()
{
printf("on_server_exit\n");
// clean all stuff
uv_stop(uv_default_loop());
uv_run(uv_default_loop(), UV_RUN_DEFAULT);
uv_walk(uv_default_loop(), on_walk_cleanup, NULL);
uv_run(uv_default_loop(), UV_RUN_DEFAULT);
uv_loop_close(uv_default_loop());
free(udp_socket);
free(tcp_socket);
exit(0);
}
static void on_signal(uv_signal_t* signal, int signum)
{
(void)signum;
printf("on_signal\n");
uv_signal_stop(signal);
if(tcp_socket)
uv_close((uv_handle_t *) tcp_socket, on_server_exit);
else if(udp_socket)
uv_close((uv_handle_t *) udp_socket, on_server_exit);
else
on_server_exit();
}
static inline void init_signal(uv_loop_t *loop, uv_signal_t* signal, int signum)
{
uv_signal_init(loop, signal);
uv_signal_start(signal, on_signal, signum);
}
int main(int argc, const char** argv)
{
(void)argc; (void) argv;
uv_loop_t *loop = uv_default_loop();
uv_signal_t sigkill, sigterm, sigint;
init_signal(loop, &sigkill, SIGKILL);
init_signal(loop, &sigterm, SIGTERM);
init_signal(loop, &sigint, SIGINT);
udp_socket = init_echo_udp_server(loop, "0.0.0.0", UDP_SERVER_PORT);
tcp_socket = init_echo_tcp_server(loop, "0.0.0.0", TCP_SERVER_PORT);
printf("Server listening on: UDP port: %d, TCP port: %d\n", UDP_SERVER_PORT, TCP_SERVER_PORT);
fflush(stdout);
return uv_run(loop, UV_RUN_DEFAULT);
}