-
Notifications
You must be signed in to change notification settings - Fork 0
/
myServer.cc
693 lines (649 loc) · 13.5 KB
/
myServer.cc
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
#include "myServer.h"
using namespace std;
myServer::myServer(int port)
{
listen_st = socket_create(port);
if (listen_st == 0)
exit(-1);
epfd = 0;
memset(&events, 0, sizeof(struct epoll_event));
}
myServer::~myServer()
{
if (listen_st)
close(listen_st);
}
int myServer::setnonblocking(int st) //将socket设置为非阻塞
{
int opts = fcntl(st, F_GETFL);
if (opts < 0)
{
printf("fcntl failed %s\n", strerror(errno));
return 0;
}
opts = opts | O_NONBLOCK;
if (fcntl(st, F_SETFL, opts) < 0)
{
printf("fcntl failed %s\n", strerror(errno));
return 0;
}
return 1;
}
int myServer::socket_create(int port)//创建参数port指定端口号的server端socket
{
int st = socket(AF_INET, SOCK_STREAM, 0);//创建TCP Socket
int on = 1;
if (setsockopt(st, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1)
{
printf("setsockopt failed %s\n", strerror(errno));
return -1;
}
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(st, (struct sockaddr *) &addr, sizeof(addr)) == -1)
{
printf("bind port %d failed %s\n", port, strerror(errno));
return -1;
}
if (listen(st, 300) == -1)
{
printf("listen failed %s\n", strerror(errno));
return -1;
}
return st;//返回listen的socket描述符
}
void myServer::loginMsg(int st, char *body)
{
Mysql my;
msg m;
memset(&m, 0, sizeof(m));
char *p = body;
char name[512], password[512], userInfo[1024];
memset(name, 0, sizeof(name));
memset(password, 0, sizeof(password));
memset(userInfo, 0, sizeof(userInfo));
for (; *p; p++)
{
if (*p == ':')
break;
}
int name_len = p - body;
int password_len = strlen(body) - name_len - 1;
strncpy(name, body, name_len);
strncpy(password, p+1, password_len);
cout << name << " " << password << endl;
int flag = my.select_user(name, userInfo);
for (p = userInfo + strlen(userInfo) - 1; *p; p--)
{
if (*p == ':')
break;
}
cout <<"password:" << p+1 << endl;
if (flag == 0 && (strcmp(password, p+1) == 0))
{
userName.insert({st, name});
cout << userName[st] << endl;
m.head[0] = 1;
my.selectUserPrivilege(name, m.body);
if (strlen(m.body) == 0)
strcpy(m.body, "This user's role is not exit!");
}
else
{
m.head[0] = -1;
}
Send(st, (const void *)&m, sizeof(msg), 0);
return;
}
int myServer::Send(int st, const void *str, size_t len, int flag)
{
if (send(st, str, len, flag) != -1)
{
cout << "send success!" << endl;
return 0;
}
else
{
cout << "send error " << strerror(errno);
return -1;
}
}
int myServer::socket_accept()
{
struct sockaddr_in client_addr;
socklen_t len = sizeof(client_addr);
memset(&client_addr, 0, sizeof(client_addr));
int client_st = accept(listen_st, (struct sockaddr *) &client_addr, &len);//接收到来自client的socket连接
if (client_st < 0)
printf("accept failed %s\n", strerror(errno));
else
printf("accept by %s\n", inet_ntoa(client_addr.sin_addr));
return client_st;//返回来自client端的socket描述符
}
void myServer::addUserMsg(int st, char *body)
{
Mysql my;
msg m;
memset(&m, 0, sizeof(m));
m.head[0] = 2;
char *p = body;
char *start = body;
char userMsg[8][512];
memset(userMsg, 0, sizeof(userMsg));
for (int i = 0; ; )
{
if (*p == ':' || *p == 0)
{
strncpy(userMsg[i], start, p-start);
userMsg[i][p-start] = 0;
if (*p == 0)
break;
i++;
p++;
start = p;
}
else
++p;
}
for (int i = 0; i < 8 ; ++i)
cout << userMsg[i] << endl;
int exist = my.select_user((char *)userMsg[0]);
if (exist == -1)
{
m.head[1] = -1;
sprintf(m.body, "Add user failed! The databases error!");
Send(st, (const void *)&m, sizeof(msg), 0);
}
if (exist == 0)
{
m.head[1] = -1;
sprintf(m.body, "Add user failed! The user you want to add is already exist!");
Send(st, (const void *)&m, sizeof(msg), 0);
}
else if(exist == -2)
{
cout << "userMsg[0]" << userMsg[0] << endl;
int flag = my.add_user(userMsg);
cout << flag << endl;
if (flag == 0)
{
m.head[1] = 1;
Send(st, (const void *)&m, sizeof(msg), 0);
}
else
{
m.head[1] = -1;
sprintf(m.body, "Add user failed! The operation you want to do is failed!");
Send(st, (const void *)&m, sizeof(msg), 0);
}
}
}
void myServer::getUserName(int st)
{
msg m;
m.head[0] = 2;
m.head[1] = 4;
char *names = NULL;
Mysql my;
int ret = my.select_users_name(&names);
if (ret == 0)
{
char *p = names;
int length;
m.head[2] = 1;
while (*p)
{
length = strlen(p) < sizeof(m.body) ? strlen(p) : sizeof(m.body);
strncpy(m.body, p, length);
Send(st, (const void *)&m, sizeof(m), 0);
memset(m.body, 0, sizeof(m.body));
p += length;
}
}
else
{
m.head[2] = -1;
strcpy(m.body, "Get users names failed! The databases error!");
Send(st, (const void *)&m, sizeof(m), 0);
}
delete names;
return;
}
void myServer::selUserMsg(int st, char *body)
{
Mysql my;
msg m;
memset(&m, 0, sizeof(m));
m.head[0] = 2;
m.head[1] = 4;
char str[1024];
memset(str, 0, sizeof(str));
int flag = my.select_user(body, str);
cout << str << endl;
cout << flag << endl;
if (flag == 0)
{
m.head[2] = 2;
strcpy(m.body, str);
}
else
{
m.head[2] = -2;
sprintf(m.body, "select user failed! The user you want to select is not exist!");
}
Send(st, (const void *)&m, sizeof(m),0);
}
void myServer::delUserMsg(int st, char *body)
{
Mysql my;
msg m;
memset(&m, 0, sizeof(m));
m.head[0] = 2;
m.head[1] = 2;
int flag = my.select_user(body, NULL);
if (flag == 0)
{
int ret = my.delete_user(body);
if (ret == 0)
{
m.head[1] = 2;
}
else
{
m.head[1] = -2;
sprintf(m.body, "delete user failed! The operation you want to do is failed!");
}
}
else
{
m.head[1] = -2;
sprintf(m.body, "delete user failed! The user you want to delete is not exist");
}
Send(st, (const void *)&m, sizeof(m), 0);
}
void myServer::updUserMsg(int st, char *body)
{
Mysql my;
char *p = body;
char *start = body;
char userMsg[8][512];
msg m;
m.head[0] = 2;
bzero(&m, sizeof(m));
memset(userMsg, 0, sizeof(userMsg));
for (int i = 0; ; )
{
if (*p == ':' || *p == 0)
{
strncpy(userMsg[i], start, p-start);
userMsg[i][p-start] = 0;
if (*p == 0)
break;
i++;
p++;
start = p;
}
else
p++;
}
for (int i = 0; i < 8 ; ++i)
cout << userMsg[i] << endl;
int ret = my.select_user(userMsg[0]);
cout << ret << endl;
if (ret != 0)
{
m.head[1] = -3;
sprintf(m.body, "select user failed! The user you want to select is not exist!");
}
else
{
int flag = my.update_user(userMsg);
if (flag == 0)
{
m.head[1] = 3;
}
else
{
m.head[1] = -3;
sprintf(m.body, "update user failed! The operation you want to do is failed!");
}
}
Send(st, (const void *)&m, sizeof(m), 0);
}
int myServer::updPrivilegeMsg(int st, char *body)
{
msg m;
memset(&m, 0, sizeof(m));
m.head[0] = 4;
Mysql my;
if (strlen(body) != 10)
{
cout << "PrivilegeMsg error" << endl;
return -1;
}
char *p = body;
if (my.update_privilege(p))
{
m.head[1] = -1;
strcpy(m.body, "update privilege failed! The databases error!");
}
else
{
m.head[1] = 1;
my.selectUserPrivilege(userName[st].c_str(), m.body);
if (strlen(m.body) == 0)
strcpy(m.body, "This user's role is not exit!");
}
Send(st, (const void *)&m, sizeof(m), 0);
return 0;
}
int myServer::selPrivilegeMsg(int st)
{
Mysql my;
msg m;
m.head[0] = 4;
char pri[20];
memset(pri, 0, sizeof(pri));
if (my.select_privilege(pri))
{
m.head[1] = -2;
strcpy(m.body, "select privilege failed! The databases error!");
return -1;
}
m.head[1] = 2;
strcpy(m.body, pri);
Send(st, (const void *)&m, sizeof(m), 0);
return 0;
}
void myServer::addRoleMsg(int st, char *body)
{
Mysql my;
msg m;
m.head[0] = 3;
char *p = body;
char *start = p;
char name[10] = {0};
char prilivege[15] = {0};
while (*p != ':')
p++;
strncpy(name, start, p-start);
strcpy(prilivege, p+1);
if (!my.select_role((char *)name))
{
m.head[1] = -1;
strcpy(m.body, "Add role failed! The role you want to add is exist!");
}
else if (my.add_role(name, prilivege))
{
m.head[1] = -1;
strcpy(m.body, "Add role failed! Databases error!");
}
else
{
m.head[1] = 1;
}
Send(st, (const void *)&m, sizeof(m), 0);
}
void myServer::selRoleMsg(int st, char *body)
{
Mysql my;
msg m;
m.head[0] = 3;
m.head[1] = 4;
char *p = body;
char privilege[15] = {0};
if (my.select_role(p, privilege))
{
m.head[2] = -2;
strcpy(m.body, "Select role failed! Databases error!");
}
else
{
m.head[2] = 2;
strcpy(m.body, privilege);
}
Send(st, (const void *)&m, sizeof(m), 0);
}
void myServer::delRoleMsg(int st, char *body)
{
Mysql my;
msg m;
m.head[0] = 3;
char *p = body;
if (my.select_role(p))
{
m.head[1] = -2;
strcpy(m.body, "Delect role failed! The role you want to del is not exist!");
}
else if (my.delete_role(p))
{
m.head[1] = -2;
strcpy(m.body, "Select role failed! Databases error!");
}
else
{
m.head[1] = 2;
}
Send(st, (const void *)&m, sizeof(m), 0);
}
void myServer::updRoleMsg(int st, char *body)
{
Mysql my;
msg m;
m.head[0] = 3;
char *p = body;
char *start = p;
char name[10] = {0};
char privilege[15] = {0};
while (*p != ':')
p++;
strncpy(name, start, p-start);
strcpy(privilege, p+1);
if (my.select_role(name))
{
m.head[1] = -3;
strcpy(m.body, "Delect role failed! The role you want to add is not exist!");
}
else if (my.update_role(name, privilege))
{
m.head[1] = -3;
strcpy(m.body, "Select role failed! Databases error!");
}
else
{
m.head[1] = 3;
my.selectUserPrivilege(userName[st].data(), m.body);
if (strlen(m.body) == 0)
strcpy(m.body, "This user's role is not exit!");
}
Send(st, (const void *)&m, sizeof(m), 0);
}
void myServer::getRolesName(int st)
{
Mysql my;
msg m;
m.head[0] = 3;
char *p = NULL;
int ret = my.select_roles_name(&p);
if (ret == 0)
{
char *tmp = p;
int length;
m.head[2] = 1;
while (*tmp)
{
length = strlen(tmp) < sizeof(m.body) ? strlen(tmp) : sizeof(m.body);
strncpy(m.body, tmp, length);
Send(st, (const void *)&m, sizeof(m), 0);
memset(m.body, 0, sizeof(m.body));
tmp += length;
}
}
else
{
m.head[2] = -1;
strcpy(m.body, "Get users names failed! The databases error!");
Send(st, (const void *)&m, sizeof(m), 0);
}
delete p;
return;
}
void myServer::logoutMsg(int st)
{
userName.erase(st);
close(st);
epoll_ctl(epfd, EPOLL_CTL_DEL, st, NULL);
}
int myServer::socket_recv(int st)
{
struct msg_t msg;
memset(&msg, 0, sizeof(msg));
ssize_t rc = recv(st, (char *) &msg, sizeof(msg), 0);//接收来自client socket发送来的消息
if (rc <= 0)//接收失败
{
if (rc < 0)
printf("recv failed %s\n", strerror(errno));
} else//接收成功
{
printf("recvice msg:%d %d %s\n", msg.head[0],msg.head[1], msg.body);
switch (msg.head[0])
{
case 0:
logoutMsg(st);
break;
case 1: //login消息
loginMsg(st, msg.body);
break;
case 2: //消息
switch (msg.head[1])
{
case 1://add usr
addUserMsg(st, msg.body);
break;
case 2://del usr
delUserMsg(st, msg.body);
break;
case 3://update usr
updUserMsg(st, msg.body);
break;
case 4://select usr
if (msg.head[2] == 1)
{
getUserName(st);
}
if (msg.head[2] == 2)
{
selUserMsg(st, msg.body);
}
break;
}
break;
case 3:
switch (msg.head[1])
{
case 1://add
addRoleMsg(st, msg.body);
break;
case 2://del
delRoleMsg(st, msg.body);
break;
case 3://update
updRoleMsg(st, msg.body);
break;
case 4://select
if (msg.head[2] == 1)
{
getRolesName(st);
}
if (msg.head[2] == 2)
{
selRoleMsg(st, msg.body);
}
break;
}
break;
case 4:
switch (msg.head[1])
{
case 1://update privilege
updPrivilegeMsg(st, msg.body);
break;
case 2://select
selPrivilegeMsg(st);
break;
}
break;
default: //无法识别的消息
printf("login fail, it's not login message,%s\n",
(const char *) &msg);
msg.head[0] = 2; //系统消息
msg.head[1] = 0; //无法识别的消息
msg.head[2] = 0; //暂时保留,填0
msg.head[3] = 0; //暂时保留,填0
send(st, (const char *) &msg, sizeof(msg.head), 0);//给client端socket下发系统消息
return 0;
}
}
return rc;
}
void myServer::run()
{
setnonblocking(listen_st);
epfd = epoll_create(CLIENTCOUNT);
ev.data.fd = listen_st;
ev.events = EPOLLIN | EPOLLERR | EPOLLHUP;
epoll_ctl(epfd, EPOLL_CTL_ADD, listen_st, &ev);
int st = 0;
int nfds = 0;
while (1)
{
nfds = epoll_wait(epfd, events, CLIENTCOUNT, -1);
if (nfds == -1)
{
cout << "epoll_wait failed " << strerror(errno) << endl;
break;
}
for (int i = 0; i < nfds; i++)
{
if (events[i].data.fd < 0)
continue;
if (events[i].data.fd == listen_st)
{
st = socket_accept();
if (st >= 0)
{
setnonblocking(st);
ev.data.fd = st;
ev.events = EPOLLIN | EPOLLERR | EPOLLHUP; //设置要处理的事件类型
epoll_ctl(epfd, EPOLL_CTL_ADD, st, &ev);//将来自client端的socket描述符加入epoll
continue;
}
}
if (events[i].events & EPOLLIN) //socket收到数据
{
st = events[i].data.fd;
if (socket_recv(st) <= 0)
{
// user_logout(st);
events[i].data.fd = -1;
}
}
if (events[i].events & EPOLLERR) //socket错误。
{
st = events[i].data.fd;
// user_logout(st);
events[i].data.fd = -1;
}
if (events[i].events & EPOLLHUP) //socket被挂断。
{
st = events[i].data.fd;
// user_logout(st);
events[i].data.fd = -1;
}
}
}
close(epfd);
}