forked from wshgithub/redis-net
/
server.c
210 lines (169 loc) · 4.52 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
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <signal.h>
#include "ae.h"
#include "anet.h"
#define MAX_LEN 1024
#define MAX_REQUEST_SIZE 5120
#define PORT 12345
#define IP_ADDR_LEN 40
//10s
#define TIMER_LOOP_CYCLE 10000
// 读数据状态
#define OK 0
#define ERR -1
#define NO_READY 1
// 全局事件循环体
aeEventLoop *g_event_loop = NULL;
// 存放错误信息的字符串
char g_err_string[MAX_LEN];
// 客户端结构体
typedef struct client {
int fd; // 文件描述符
char ipaddr[IP_ADDR_LEN]; // client IP 地址
int port; // 端口
char request[MAX_LEN]; // 客户端请求字符串
char response[MAX_LEN]; // 响应字符串
int len; // 缓冲长度
int rsplen;
}client_t;
client_t* createClient() {
client_t* c = malloc(sizeof(client_t));
if(c == NULL) {
fprintf(stderr, "alloc mem failure.");
exit(1);
}
c->fd = -1;
c->len = 0;
c->rsplen = 0;
return c;
}
// 客户退出处理函数
void ClientClose(aeEventLoop *el, client_t* c, int err)
{
//如果err为0,则说明是正常退出,否则就是异常退出
if( 0 == err )
printf("Client quit: %d\n", c->fd);
else if( -1 == err )
fprintf(stderr, "Client Error: %s\n", strerror(errno));
//删除结点,关闭文件
aeDeleteFileEvent(el, c->fd, AE_READABLE);
close(c->fd);
free(c);
}
// 响应客户
void sendReplyToClient(aeEventLoop *el, int fd, void *privdata, int mask) {
client_t* c = privdata;
int nwritten = c->rsplen;
int res, sentlen = 0;
printf("Request From %s:%d : %s\n", c->ipaddr, c->port, c->response);
while(nwritten) {
res = write(fd, c->response + sentlen, c->rsplen - sentlen);
// 写入出错
if (res == -1) {
if (errno == EAGAIN) {
continue;
} else {
fprintf(stderr, "send response to client failure.\n");
ClientClose(el, c, res);
}
}
nwritten -= res;
sentlen += res;
if (sentlen == c->rsplen) {
c->rsplen = 0;
}
}
aeDeleteFileEvent(el,c->fd,AE_WRITABLE);
}
int processBuffer(aeEventLoop *el, client_t* c, int res) {
char *newline = strstr(c->request,"\r\n");
int reqlen;
if(newline == NULL) {
if(c->len > MAX_REQUEST_SIZE) {
fprintf(stderr,"Protocol error: too big request");
ClientClose(el, c, res);
return ERR;
}
return NO_READY;
}
reqlen = newline - c->request + 2;
memcpy(c->response, c->request, reqlen);
c->rsplen = reqlen;
c->len -= reqlen;
if(c->len)
memmove(c->request, c->request+reqlen, c->len);
return OK;
}
// 读取客户端数据
void ReadFromClient(aeEventLoop *el, int fd, void *privdata, int mask)
{
int res;
client_t* c = privdata;
res = read(fd, c->request + c->len, MAX_LEN - c->len);
if( res <= 0 )
{
ClientClose(el, c, res);
return;
}
c->len += res;
if(processBuffer(el, c, res) == OK) {
if(aeCreateFileEvent(el, fd, AE_WRITABLE, sendReplyToClient, c) == AE_ERR) {
fprintf(stderr, "Can't Register File Writeable Event.\n");
ClientClose(el, c, res);
}
}
}
// 定时器
int PrintTimer(struct aeEventLoop *eventLoop, long long id, void *clientData)
{
static int i = 0;
printf("I am a Timer: %d\n", i++);
// TIMER_LOOP_CYCLE/1000 秒后再次执行该函数
return TIMER_LOOP_CYCLE;
}
//接受新连接
void AcceptTcpHandler(aeEventLoop *el, int fd, void *privdata, int mask)
{
client_t* c = createClient();
c->fd = anetTcpAccept(g_err_string, fd, c->ipaddr, &c->port);
if(c->fd == ANET_ERR) {
fprintf(stderr, "Accepting client connection: %s", g_err_string);
free(c);
return;
}
printf("Connected from %s:%d\n", c->ipaddr, c->port);
if( aeCreateFileEvent(el, c->fd, AE_READABLE, ReadFromClient, c) == AE_ERR )
{
fprintf(stderr, "Create File Event fail: fd(%d)\n", c->fd);
close(c->fd);
free(c);
}
}
//停止事件循环
void StopServer()
{
aeStop(g_event_loop);
}
int main()
{
printf("Start\n");
signal(SIGINT, StopServer);
//初始化网络事件循环
g_event_loop = aeCreateEventLoop(1024*10);
//设置监听事件
int fd = anetTcpServer(g_err_string, PORT, NULL);
if( ANET_ERR == fd )
fprintf(stderr, "Open port %d error: %s\n", PORT, g_err_string);
if( aeCreateFileEvent(g_event_loop, fd, AE_READABLE, AcceptTcpHandler, NULL) == AE_ERR )
fprintf(stderr, "Unrecoverable error creating server.ipfd file event.");
//设置定时事件
aeCreateTimeEvent(g_event_loop, 1, PrintTimer, NULL, NULL);
//开启事件循环
aeMain(g_event_loop);
//删除事件循环
aeDeleteEventLoop(g_event_loop);
printf("End\n");
return 0;
}