rot13 服务端程序:

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
/* For sockaddr_in */
#include <netinet/in.h>
/* For socket functions */
#include <sys/socket.h>
/* For fcntl */
#include <fcntl.h>

#include <event2/event.h>

#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define MAX_LINE 16384

void do_read(evutil_socket_t fd, short events, void *arg);
void do_write(evutil_socket_t fd, short events, void *arg);

char rot13_char(char c)
{
/* We don't want to use isalpha here; setting the locale would change
* which characters are considered alphabetical. */
if ((c >= 'a' && c <= 'm') || (c >= 'A' && c <= 'M'))
return c + 13;
else if ((c >= 'n' && c <= 'z') || (c >= 'N' && c <= 'Z'))
return c - 13;
else
return c;
}

struct fd_state
{
char buffer[MAX_LINE];
size_t buffer_used;

size_t n_written;
size_t write_upto; // 标记缓存到的位置

struct event *read_event;
struct event *write_event;
};

struct fd_state *alloc_fd_state(struct event_base *base, evutil_socket_t fd)
{
struct fd_state *state = malloc(sizeof(struct fd_state));
if (!state)
{
fprintf(stderr, "failed to alloc fs state\n");
return NULL;
}

state->read_event = event_new(base, fd, EV_READ | EV_PERSIST, do_read, state);
if (!state->read_event)
{
fprintf(stderr, "failed to new read event\n");
free(state);
return NULL;
}

state->write_event =
event_new(base, fd, EV_WRITE | EV_PERSIST, do_write, state);
if (!state->write_event)
{
fprintf(stderr, "failed to new write event\n");
event_free(state->read_event);
free(state);
return NULL;
}

state->buffer_used = state->n_written = state->write_upto = 0;

return state;
}

void free_fd_state(struct fd_state *state)
{
event_free(state->read_event);
event_free(state->write_event);
free(state);
}

void do_read(evutil_socket_t fd, short events, void *arg)
{
struct fd_state *state = (struct fd_state *)arg;
char buf[1024];
int i;
ssize_t result;

while (1)
{
assert(state->write_event);
result = recv(fd, buf, sizeof(buf), 0);
if (result <= 0)
break;
for (i = 0; i < result; ++i)
{
// 缓存还有空闲空间
if (state->buffer_used < sizeof(state->buffer))
state->buffer[state->buffer_used++] = rot13_char(buf[i]);
// 服务端读完一行后,加入可写事件,可向客户端写入数据
if (buf[i] == '\n')
{
{
state->buffer[state->buffer_used - 1] = '\0';
printf("receive a line data: %s\n",
(char *)state->buffer + state->write_upto);
state->buffer[state->buffer_used - 1] = '\n';
}
assert(state->write_event);
event_add(state->write_event, NULL);
state->write_upto = state->buffer_used;
}
}
}

if (result == 0)
{ // 客户端关闭
printf("warning, peer closed\n");
free_fd_state(state);
}
else if (result < 0)
{
if (errno == EAGAIN || errno == EWOULDBLOCK)
return;
perror("recv");
free_fd_state(state);
}
}

void do_write(evutil_socket_t fd, short events, void *arg)
{
struct fd_state *state = arg;

// 一直写到标记处
while (state->n_written < state->write_upto)
{
ssize_t result = send(fd, state->buffer + state->n_written,
state->write_upto - state->n_written, 0);
if (result < 0)
{
if (errno == EAGAIN || errno == EWOULDBLOCK)
return;
perror("send");
free_fd_state(state);
return;
}
assert(result != 0);
// printf("send %ld bytes data\n", result);

state->n_written += result;
}

// 巧妙的缓存复用
if (state->n_written == state->buffer_used)
state->n_written = state->write_upto = state->buffer_used = 0;

// 不再可写,等待服务端读取一行客户端数据后,再将其加入可写
event_del(state->write_event);
}

void do_accept(evutil_socket_t listener, short event, void *arg)
{
struct event_base *base = arg;
struct sockaddr_storage ss;
socklen_t slen = sizeof(ss);

/* 阻塞等待客户端的连接 */
int cfd = accept(listener, (struct sockaddr *)&ss, &slen);
if (cfd < 0)
{ // XXXX eagain??
perror("accept");
}
else if (cfd > FD_SETSIZE)
{
close(cfd); // XXX replace all closes with EVUTIL_CLOSESOCKET */
}
else
{
printf("accept a client connection, fd=%d\n", cfd);
struct fd_state *state;
evutil_make_socket_nonblocking(cfd);
state = alloc_fd_state(base, cfd);
assert(state);
assert(state->write_event);
// 接受客户端连接后,监听客户端可读事件(等监听到可读事件后会触发可写事件)
event_add(state->read_event, NULL);
}
}

void run(void)
{
evutil_socket_t listener;
struct sockaddr_in sin;
struct event_base *base;
struct event *listener_event;

base = event_base_new();
if (!base)
{
fprintf(stderr, "failed to new event base\n");
return;
}

sin.sin_family = AF_INET;
sin.sin_addr.s_addr = 0;
sin.sin_port = htons(40713);

listener = socket(AF_INET, SOCK_STREAM, 0);
evutil_make_socket_nonblocking(listener);

int one = 1;
setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));

if (bind(listener, (struct sockaddr *)&sin, sizeof(sin)) < 0)
{
perror("bind");
return;
}

if (listen(listener, 16) < 0)
{
perror("listen");
return;
}

listener_event =
event_new(base, listener, EV_READ | EV_PERSIST, do_accept, (void *)base);
event_add(listener_event, NULL);
event_base_dispatch(base);
}

int main(int argc, char **argv)
{
/* is exactly equivalent to setlinebuf(stdout) */
setvbuf(stdout, NULL, _IONBF, 0);
run();
return 0;
}

rot13 客户端:

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
/* For sockaddr_in */
#include <netinet/in.h>
/* For socket functions */
#include <sys/socket.h>
/* For fcntl */
#include <fcntl.h>

#include <event2/event.h>

#include <arpa/inet.h>
#include <assert.h>
#include <errno.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>

struct user_event
{
struct event_base *base;
struct event *read_event;
struct event *write_event;
};

int read_cnt = 0;
struct user_event my_event;

void do_read(evutil_socket_t fd, short events, void *arg)
{
struct user_event *my_event = (struct user_event *)arg;
char buf[1024];
ssize_t recv_bytes = 0;
ssize_t res;

do
{
res = recv(fd, buf + recv_bytes, sizeof(buf) - recv_bytes - 1, 0);
if (res < 0)
{
if (errno == EAGAIN || errno == EWOULDBLOCK)
{
break;
}
else
{
fprintf(stderr, "failed to recv, error=%s\n", strerror(errno));
return;
}
}
else if (res == 0)
{
printf("warning, peer closed\n");
return;
}
recv_bytes += res;
} while (res > 0 && recv_bytes < 1023);

buf[recv_bytes] = '\0';
printf("receive %dth rot13: %s", read_cnt + 1, buf);
event_add(my_event->write_event, NULL);

return;
}

void do_write(evutil_socket_t fd, short events, void *arg)
{
struct user_event *my_event = (struct user_event *)arg;
const char *send_data = "abcdtxyz\n";

if (++read_cnt < 10)
{
send(fd, send_data, strlen(send_data), 0);
// 不再可写,等待再次读取到数据时,再将其加入可写
event_del(my_event->write_event);
printf("send data: %s", send_data);
}
else
{
// 有限次发送后退出
event_base_loopbreak(my_event->base);
event_free(my_event->write_event);
event_free(my_event->read_event);
event_base_free(my_event->base);
close(fd);
}

return;
}

void run(void)
{
evutil_socket_t cfd;
struct sockaddr_in saddr;

my_event.base = event_base_new();
if (!my_event.base)
{
fprintf(stderr, "failed to new event base\n");
return;
}

cfd = socket(AF_INET, SOCK_STREAM, 0);

int one = 1;
setsockopt(cfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));

memset(&saddr, 0, sizeof(saddr));
saddr.sin_family = AF_INET;
saddr.sin_addr.s_addr = inet_addr("127.0.0.1");
saddr.sin_port = htons(40713);

if (connect(cfd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0)
{
perror("connect");
return;
}

/* 在连接成功后再设置为非阻塞模式,否则需要监听连接事件的结果 */
evutil_make_socket_nonblocking(cfd);

/* 写一些数据,可触发服务端的读事件就绪 */
const char *send_data = "ABCxyz\n";
send(cfd, send_data, strlen(send_data), 0);
printf("send data: %s", send_data);

/* 创建客户端的监听事件,用于读取服务端的数据 */
my_event.read_event = event_new(my_event.base, cfd, EV_READ | EV_PERSIST,
do_read, (void *)&my_event);
/* 创建客户端的监听事件,用于像服务端写数据 */
my_event.write_event = event_new(my_event.base, cfd, EV_WRITE | EV_PERSIST,
do_write, (void *)&my_event);
event_add(my_event.read_event, NULL);
event_base_dispatch(my_event.base);
printf("client exit\n");
}

int main(int argc, char **argv)
{
/* is exactly equivalent to setlinebuf(stdout) */
setvbuf(stdout, NULL, _IONBF, 0);
run();
return 0;
}