-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.c
95 lines (81 loc) · 2.09 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "server.h"
//Global info server
serverInfo server;
//test the main datasturct sortmap curd function.
//it can be see in eyes;
void test_curd() {
for(int i = 0; i < 100000; i++) {
char string[20];
itoa(i,string);
set(string,string,server.sm);
}
set("9999","ljoejgieg",server.sm);
del("9998",server.sm);
/* char* tmp = scan(server.sm);
if(!tmp)
printf("tmp is null\n");
printf("%s\n",tmp);
freestr(tmp);*/
/* for(int i = 0; i < 9000; i++) {
char string[20];
itoa(i,string);
del(string,server.sm);
}
//delete null
// del("10001",server.sm);
*/
}
client* initClient(const char* ip, int port,int fd) {
client* newclient = zmalloc(sizeof(* newclient));
if(!newclient) {
return NULL;
}
newclient->inbuf = emptystr();
newclient->outbuf = emptystr();
newclient->client_ip = newstr(ip);
newclient->port = port;
newclient->fd = fd;
newclient->inlen = 0;
newclient->outlen = 0;
}
void destroyClient(client* client) {
aeDeleteFileEvent(server.el,client->fd,AE_READABLE | AE_WRITABLE);
server.client_channel--;
freestr(client->client_ip);
freestr(client->inbuf);
freestr(client->outbuf);
zfree(client);
}
void* reportPoolInfo(void* val) {
time_t now = time(NULL);
if(now - server.pool->lastReport >= 10) {
printf("pool monitor run count %d idle %d core %d max %d\n",
server.pool->count,server.pool->idle,server.pool->coresize,server.pool->maxsize);
server.pool->lastReport = now;
}
return NULL;
}
int main() {
server.sm = initSortMap();
// str_test();
// sortmap_test();
// threadpool_test();
test_curd();
server.el = aeCreateEventLoop(1024);
server.pool = initThreadPool(8,12);
server.el->reportPool = reportPoolInfo;
int fd = anetCreateSocket();
anetTcpServer(fd, 9876, "127.0.0.1",1024);
server.fd = fd;
server.client_channel = 0;
if (aeCreateFileEvent(server.el, server.fd, AE_READABLE,acceptTcpHandler,NULL) == AE_ERR) {
printf("Unrecoverable error creating server.ipfd file event.\n");
}
aeMain(server.el);
destroySortMap(server.sm);
destroyThreadPool(server.pool);
aeDeleteEventLoop(server.el);
}