-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver_libev.c
152 lines (115 loc) · 2.97 KB
/
server_libev.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h> //inet_addr
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <ev.h>
#define PORT_NO 5000
#define BUFFER_SIZE 10240
#define MAX_BACKLOG 1024
int total_clients = 0;
void on_accept(struct ev_loop *loop, struct ev_io *watcher, int revents);
void on_read(struct ev_loop *loop, struct ev_io *watcher, int revents);
int new_tcp_server(const char *host, short port);
int set_nonblock(int fd);
int set_reuse_socket(int sock);
int set_address (const char *host, short port, struct sockaddr_in *addr);
int main(int argc , char *argv[])
{
struct ev_loop *loop = ev_default_loop(0);
int port = PORT_NO;
if(argc==2){
port = atoi(argv[1]);
}else if(argc > 2){
fprintf(stderr, "usage: server_libev [port]\n");
return 1;
}
int fd = new_tcp_server("0.0.0.0", port);
printf("Starting tcp server on :%d\n", port);
ev_io listen_watcher;
ev_io_init(&listen_watcher, on_accept, fd, EV_READ);
ev_io_start(loop, &listen_watcher);
ev_loop(loop, 0);
return 0;
}
int new_tcp_server(const char *host, short port) {
int fd;
struct sockaddr_in address;
fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd == -1) {
perror("socket");
return -1;
}
set_reuse_socket(fd);
set_nonblock(fd);
set_address(host, port, &address);
if (bind(fd, (struct sockaddr*)&address, sizeof(address)) < 0) {
perror("bind");
close(fd);
return -1;
}
if (listen(fd, MAX_BACKLOG) <0){
perror("listen");
close(fd);
return -1;
};
return fd;
}
int set_address (const char *host, short port, struct sockaddr_in *addr)
{
memset(addr, 0, sizeof(*addr));
addr->sin_family = AF_INET;
addr->sin_addr.s_addr = inet_addr(host);
addr->sin_port = htons(port );
return 0;
}
int set_nonblock(int fd) {
return fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
}
int set_reuse_socket(int fd) {
int ok = 1;
return setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &ok, sizeof(ok));
}
void on_accept(struct ev_loop *loop, struct ev_io *watcher, int revents)
{
struct sockaddr_in client_addr;
socklen_t client_len = sizeof(client_addr);
struct ev_io *client_watcher = (struct ev_io*)malloc(sizeof(struct ev_io));
if(EV_ERROR & revents) {
perror("invalid event");
return;
}
int client_fd = accept(watcher->fd, (struct sockaddr*)&client_addr, &client_len);
if (client_fd < 0) {
perror("accept");
return;
}
set_nonblock(client_fd);
total_clients++;
ev_io_init(client_watcher, on_read, client_fd, EV_READ);
ev_io_start(loop, client_watcher);
}
void on_read(struct ev_loop *loop, struct ev_io *watcher, int revents)
{
char buffer[BUFFER_SIZE];
ssize_t len;
if(EV_ERROR & revents) {
return;
}
len = recv(watcher->fd, buffer, BUFFER_SIZE, 0);
if (len < 0) {
return;
} else if (len == 0) {
// disconnected
ev_io_stop(loop, watcher);
close(watcher->fd);
free(watcher);
total_clients--;
return;
}
send(watcher->fd, buffer, len, 0);
memset(buffer, 0, BUFFER_SIZE);
}