-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnet_client.c
233 lines (205 loc) · 5.79 KB
/
net_client.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
#include "net_client.h"
/* reader and writer */
/* writer buf */
/* read buf */
rwer* new_rwer() {
rwer * rw = smalloc(sizeof(rwer));
rw->buf_size = CLIENT_BUF_SIZE;
rw->read_pos = 0;
rw->write_pos = 0;
rw->buf = smalloc(CLIENT_BUF_SIZE);
return rw;
}
/* reduce boundary */
void reduce_rwbuf(rwer *rw) {
char *new_buf = smalloc(rw->buf_size - rw->write_pos);
sfree(rw->buf);
rw->buf_size = rw->buf_size - rw->write_pos;
rw->read_pos = rw->read_pos - rw->write_pos;
rw->write_pos = 0;
rw->buf = new_buf;
}
void append_rw_reader(rwer *rw, const char *string , int size) {
AGENT_SLOG(SLOG_DEBUG, "[client] append rwriter string %s, size:%d", string, size);
if (size + rw->read_pos < rw->buf_size) {
memcpy(rw->buf + rw->read_pos, string , size);
rw->read_pos += size;
} else {
/* todo more simple add */
/* memory align */
int new_size = rw->buf_size + (((rw->read_pos + size) - rw->buf_size)/CLIENT_BUF_SIZE + 1) * CLIENT_BUF_SIZE;
rw->buf = srealloc(rw->buf, new_size);
rw->buf_size = new_size;
memcpy(rw->buf + rw->read_pos, string, size);
rw->read_pos += size;
}
}
/* rewind read write pos to start */
int rwer_rewind(rwer *rw) {
if (rw->write_pos == rw->read_pos) {
rw->write_pos = 0;
rw->read_pos = 0;
bzero(rw->buf, rw->buf_size);
}
}
/* free reader writer */
void free_rwer(rwer *rw) {
sfree(rw->buf);
sfree(rw);
}
/* for reader/writer append */
static void appender(char **buf, int *cap, int *len, const char *string , int size) {
AGENT_SLOG(SLOG_DEBUG, "[client] append buf string %s, size:%d", string, size);
if (size + *len < *cap) {
memcpy(*buf + *len, string ,size);
*len += size;
} else {
int new_size = *cap + (((*len + size) - *cap)/CLIENT_BUF_SIZE + 1) * CLIENT_BUF_SIZE;
*buf = (char *)srealloc(*buf, new_size);
*cap = new_size;
memcpy(*buf + *len, string, size);
*len += size;
}
}
/* reader */
reader* new_reader() {
reader *r = smalloc(sizeof(reader));
r->cap = CLIENT_BUF_SIZE;
r->read_pos = 0;
r->len = 0;
r->read_buf = smalloc(r->cap);
return r;
}
/* read size */
int reader_read_size(reader *r) {
return r->len - r->read_pos;
}
char *reader_read_start(reader *r) {
return r->read_buf + r->read_pos;
}
/* reduce reader */
void reduce_reader(reader *r) {
/* here memory is overlap , use memmove */
if (r->rewind_count > 50) {
memmove(r->read_buf, r->read_buf + r->read_pos, (r->len - r->read_pos));
r->read_pos = 0;
r->len = r->len - r->read_pos;
r->rewind_count = 0;
}
}
/* if read reach len we will rewind */
/* but if the read cannot , we should reduce */
/* if use ring buffer, it not simple dilatation */
void rewind_reader(reader *r) {
if (r->read_pos == r->len) {
if (r->read_pos == 0) {
return;
}
r->read_pos = r->len = 0;
memset(r->read_buf, 0x00, r->cap);
r->rewind_count = 0;
} else {
r->rewind_count++;
reduce_reader(r);
}
}
/* offset reader pos */
/* return 0 success -1 fail */
int forward_reader_pos(reader *r, int offset) {
if (r->read_pos + offset > r->len) {
return -1;
}
r->read_pos += offset;
rewind_reader(r);
return 0;
}
/* appen size to reader */
void append_reader(reader *r, const char *string, int size) {
appender(&r->read_buf, &r->cap, &r->len, string , size);
}
void free_reader(reader *r) {
sfree(r->read_buf);
sfree(r);
}
/* new writer */
writer* new_writer() {
writer *w = smalloc(sizeof(writer));
w->cap = CLIENT_BUF_SIZE;
w->write_pos = 0;
w->len = 0;
w->write_buf = smalloc(w->cap);
return w;
}
void free_writer(writer *w) {
sfree(w->write_buf);
sfree(w);
}
/* reduce writer */
void reduce_writer(writer *w) {
/* here memory is overlap , use memmove */
if (w->rewind_count > 50) {
memmove(w->write_buf, w->write_buf + w->write_pos, (w->len - w->write_pos));
w->write_pos = 0;
w->len = w->len - w->write_pos;
w->rewind_count = 0;
}
}
/* rewind writer */
void rewind_writer(writer *w) {
if (w->write_pos == w->len) {
w->write_pos = w->len = 0;
memset(w->write_buf, 0x00, w->cap);
w->rewind_count = 0;
} else {
w->rewind_count++;
}
reduce_writer(w);
}
void append_writer(writer *w, const char *string, int size) {
appender(&w->write_buf, &w->cap, &w->len, string , size);
}
/* writer */
/* leve triger no need to write until EWOULDBLOCK */
/* use in single thread, no condition race */
int writer_write(writer *w, int fd) {
char *start_pos;
int need_write, nwrite = 0;
do {
w->write_pos += nwrite;
start_pos = w->write_buf + w->write_pos;
need_write = w->len - w->write_pos;
if (need_write <= 0) {
break;
}
nwrite = write(fd, start_pos, need_write);
}while (nwrite >= 0);
/* success and need_write == 0 */
if (nwrite >= 0 && need_write == 0) {
rewind_writer(w);
return RW_WRITE_FINISH;
} else if (nwrite =-1) {
/* other error is not forbident */
if (errno == EAGAIN || errno == EINTR) {
return RW_WRITE_CONTINUE;
} else {
return RW_WRITE_ERROR;
}
}
}
/* net client */
net_client *new_client(int fd, char *client_ip) {
net_client *nc = smalloc(sizeof(net_client));
nc->fd = fd;
nc->client_ip = sstring_init(client_ip, strlen(client_ip));
nc->create_time = time(NULL);
nc->r = new_reader();
nc->w = new_writer();
return nc;
}
/* free client */
void free_client(net_client *nc) {
free_reader(nc->r);
free_writer(nc->w);
sstring_free(nc->client_ip);
sfree(nc);
}