-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
275 lines (220 loc) · 4.5 KB
/
server.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#include <sys/un.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/epoll.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include "common.h"
#include "server.h"
#include "db.h"
#define READ_BUFFER_SIZE 5
#define QUERY_BUFFER_SIZE 100
#define MAX_CLIENT 100
const char *SOCK_NAME = "/tmp/mysock";
int makeNonBlocking(int fd)
{
int flags, r;
flags = fcntl (fd, F_GETFL, 0);
if (flags == -1){
puts("fcntl");
return -1;
}
flags |= O_NONBLOCK;
r = fcntl(fd, F_SETFL, flags);
if (r == -1){
puts("F_SETFL");
return -1;
}
return 0;
}
int createUnixSocket(){
int sfd;
struct sockaddr_un addr;
sfd = socket(AF_UNIX, SOCK_STREAM, 0);
if(sfd == -1){
puts("Socket Error\n");
exit(-1);
}
memset(&addr, 0, sizeof(struct sockaddr_un));
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, SOCK_NAME, sizeof(addr.sun_path) - 1);
if(remove(SOCK_NAME) == -1 && errno != ENOENT){
puts("File Error\n");
exit(-1);
}
if (bind(sfd, (struct sockaddr *) &addr, sizeof(struct sockaddr_un)) == -1){
puts("Bind Error\n");
exit(-1);
}
return sfd;
}
int createTCPSocket(){
int sfd;
struct sockaddr_in addr;
sfd = socket(AF_INET, SOCK_STREAM, 0);
if(sfd == -1){
puts("Socket Error\n");
exit(-1);
}
memset(&addr, 0, sizeof(struct sockaddr_in));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = htons(50005);
if (bind(sfd, (struct sockaddr *) &addr, sizeof(struct sockaddr_in)) == -1){
puts("Bind Error\n");
exit(-1);
}
return sfd;
}
Client *createClient(int fd)
{
Client *c = (Client *)malloc(sizeof(Client));
if (c == NULL)
return NULL;
c->fd = fd;
c->buf = (char*)malloc(QUERY_BUFFER_SIZE + 1);
c->numRead = 0;
c->ready = 0;
c->argc = 0;
c->free = 0;
c->pos = 0;
c->len = 0;
c->buf_size = QUERY_BUFFER_SIZE + 1;
return c;
}
void resetClient(Client *c)
{
c->ready = 0;
c->len = 0;
c->current = 0;
}
void freeClient()
{
}
int acceptClient(Client **clients, int sfd, int epfd){
int cfd;
struct Client *c=NULL;
struct epoll_event ev;
for(;;){
puts("Accept\n");
cfd = accept(sfd, NULL, NULL);
puts("Accepted\n");
if(cfd == -1){
break;
}
makeNonBlocking(cfd);
c = createClient(cfd);
if(c == NULL)
return -1;
clients[cfd] = c;
ev.events = EPOLLIN;
ev.data.fd = cfd;
if(epoll_ctl(epfd, EPOLL_CTL_ADD, cfd, &ev) == -1){
puts("epoll_ctl Error\n");
return -1;
}
}
return 0;
}
int readClient(Client **clients, int cfd)
{
int r=0;
ssize_t numRead = 0;
Client *c = clients[cfd];
if (c == NULL)
return -1;
if ((c->numRead + READ_BUFFER_SIZE) >= c->buf_size){
printf("Buffer Max Client %d, %d\n", c->fd, c->numRead);
c->buf = (char*)realloc(c->buf, c->numRead + QUERY_BUFFER_SIZE + 1);
c->buf_size = c->numRead + QUERY_BUFFER_SIZE + 1;
}
numRead = read(cfd, c->buf+c->len, READ_BUFFER_SIZE);
printf("numRead %d\n", numRead);
if(numRead == -1){
puts("numRead -1");
if (errno == EAGAIN) {
puts("EAGAIN");
numRead = 0;
}
else
return -1;
}
else if(numRead == 0){
close(cfd);
}
if(numRead){
c->len += numRead;
c->buf[c->len] = '\0';
if(parseQuery(c) == 0){
puts("Parsed");
c->ready = 1;
}
}
return 0;
}
void initServer()
{
int fd, sfd, cfd, epfd;
int lgfd;
int i, num=0;
struct Client *clients[MAX_CLIENT] = { NULL };
struct Client *c=NULL;
struct epoll_event *evlist;
sfd = createTCPSocket();
makeNonBlocking(sfd);
if(listen(sfd, 10) == -1){
puts("Listen Error\n");
exit(-1);
}
epfd = epoll_create1(0);
if(epfd == -1){
puts("epoll Error\n");
exit(-1);
}
struct epoll_event ev;
ev.events = EPOLLIN;
ev.data.fd = sfd;
if(epoll_ctl(epfd, EPOLL_CTL_ADD, sfd, &ev) == -1){
puts("epoll_ctl Error\n");
exit(-1);
}
evlist = calloc(10, sizeof(struct epoll_event));
for(;;){
for(i=0; i < MAX_CLIENT; i++){
c = clients[i];
if (c == NULL)
continue;
if (c->ready){
printf("Client %d ready, %d\n", c->fd, c->len);
if (write(STDOUT_FILENO, c->buf, c->len) != c->len){
puts("write Error\n");
return;
}
resetClient(c);
}
}
puts("epoll_wait");
num = epoll_wait(epfd, evlist, 1, -1);
if(num == -1){
if(errno == EINTR){
puts("EINTR");
continue;
}
}
for(i=0; i < num; i++){
if(sfd == evlist[i].data.fd) {
if(evlist[i].events & EPOLLIN){
acceptClient(clients, sfd, epfd);
}
continue;
}
else if (evlist[i].events & EPOLLIN){
puts("readClient");
cfd = evlist[i].data.fd;
readClient(clients, cfd);
}
}
}
}