-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.cpp
More file actions
378 lines (339 loc) · 7.51 KB
/
Copy pathclient.cpp
File metadata and controls
378 lines (339 loc) · 7.51 KB
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
#include <iostream>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <assert.h>
#include <fcntl.h>
#include <vector>
enum
{
SER_NIL = 0,
SER_ERR = 1,
SER_STR = 2,
SER_INT = 3,
SER_ARR = 4,
};
const size_t k_max_msg = 4096;
static void msg(const char *msg)
{
std::cerr << msg << std::endl;
}
static void die(const char *msg)
{
int err = errno;
std::cerr << msg << std::endl;
abort();
}
static void fd_set_nb(int fd)
{
errno = 0;
int flags = fcntl(fd, F_GETFL, 0);
if (errno)
{
die("fcntl error");
return;
}
flags |= O_NONBLOCK;
errno = 0;
(void)fcntl(fd, F_SETFL, flags);
if (errno)
{
die("fcntl error");
}
}
static int32_t read_full(int fd, char *buf, size_t n)
{
while (n > 0)
{
ssize_t rv = read(fd, buf, n); // Read exactly n bytes into buf
if (rv <= 0)
{
return -1;
}
assert((size_t)rv <= n);
n -= (size_t)rv;
buf += rv;
}
return 0;
}
static int32_t write_all(int fd, char *buf, size_t n)
{
while (n > 0)
{
ssize_t rv = write(fd, buf, n); // Write exactly n bytes into buf
if (rv <= 0)
{
return -1;
}
assert((size_t)rv <= n);
n -= (size_t)rv;
buf += rv;
}
return 0;
}
// static int32_t send_req(int fd, const char *text)
// {
// uint32_t len = (uint32_t)strlen(text);
// if (len > k_max_msg)
// {
// return -1;
// }
// char wbuf[4 + k_max_msg];
// memcpy(wbuf, &len, 4); // assume little endian
// memcpy(wbuf + 4, text, len);
// if (int32_t err = write_all(fd, wbuf, 4 + len))
// {
// return err;
// }
// return 0;
// }
static int32_t send_req(int fd, std::vector<std::string> &cmd)
{
uint32_t len = 4;
for (const std::string &s : cmd)
{
len += 4 + s.size();
}
if (len > k_max_msg)
{
return -1;
}
char wbuf[4 + k_max_msg];
memcpy(&wbuf[0], &len, 4);
uint32_t n = cmd.size();
memcpy(&wbuf[4], &n, 4);
size_t cur = 8;
for (const std::string &s : cmd)
{
uint32_t p = (uint32_t)s.size();
memcpy(&wbuf[cur], &p, 4);
memcpy(&wbuf[cur + 4], s.data(), s.size());
cur += 4 + s.size();
}
return write_all(fd, wbuf, 4 + len);
}
// static int32_t read_res(int fd, const char *text)
// {
// // 4 bytes header
// char rbuf[4 + k_max_msg + 1];
// errno = 0;
// int32_t err = read_full(fd, rbuf, 4);
// if (err)
// {
// if (errno == 0)
// {
// msg("EOF");
// }
// else
// {
// msg("read() error");
// }
// return err;
// }
// uint32_t len = 0;
// memcpy(&len, rbuf, 4);
// if (len > k_max_msg)
// {
// msg("too long");
// return -1;
// }
// // reply body
// err = read_full(fd, rbuf + 4, len);
// if (err)
// {
// msg("read() error");
// return err;
// }
// // do something
// rbuf[4 + len] = '\0';
// std::cout << "server says: " << rbuf + 4 << std::endl;
// return 0;
// }
static int32_t on_response(const uint8_t *data, size_t size)
{
if (size < 1)
{
msg("bad response");
return -1;
}
switch (data[0])
{
case SER_NIL:
std::cout << "(nil)" << std::endl;
return 1;
case SER_ERR:
if (size < 1 + 8)
{
msg("bad response");
return -1;
}
else
{
int32_t code = 0;
uint32_t len = 0;
memcpy(&code, &data[1], 4);
memcpy(&len, &data[1 + 4], 4);
if (size < 1 + 8 + len)
{
msg("bad response");
return -1;
}
std::cout << "(err) ";
std::cout.write((char *)&data[1 + 8], len);
std::cout << std::endl;
return 1 + 8 + len;
}
case SER_STR:
if (size < 1 + 4)
{
msg("bad response");
return -1;
}
else
{
uint32_t len = 0;
memcpy(&len, &data[1], 4);
if (size < 1 + 4 + len)
{
msg("bad response");
return -1;
}
std::cout << "(str) ";
std::cout.write((char *)&data[1 + 4], len);
std::cout << std::endl;
return 1 + 4 + len;
}
case SER_INT:
if (size < 1 + 8)
{
msg("bad response");
return -1;
}
else
{
int64_t val = 0;
memcpy(&val, &data[1], 8);
std::cout << "(int) " << val << std::endl;
return 1 + 8;
}
case SER_ARR:
if (size < 1 + 4)
{
msg("bad response");
return -1;
}
else
{
uint32_t len = 0;
memcpy(&len, &data[1], 4);
std::cout << "(arr) len=" << len << std::endl;
size_t arr_bytes = 1 + 4;
for (u_int32_t i = 0; i < len; i++)
{
int32_t rv = on_response(&data[arr_bytes], size - arr_bytes);
if (rv < 0)
{
return rv;
}
arr_bytes += (size_t)rv;
}
std::cout << "(arr) end" << std::endl;
return (int32_t)arr_bytes;
}
default:
msg("bad response");
return -1;
}
}
static int32_t read_res(int fd, std::vector<std::string> &cmd)
{
char rbuf[4 + k_max_msg + 1];
errno = 0;
int32_t err = read_full(fd, rbuf, 4);
if (err)
{
if (errno == 0)
{
msg("EOF");
}
else
{
msg("read() error");
}
return err;
}
uint32_t len = 0;
memcpy(&len, rbuf, 4); // assume little endian
if (len > k_max_msg)
{
msg("too long");
return -1;
}
// reply body
err = read_full(fd, &rbuf[4], len);
if (err)
{
msg("read() error");
return err;
}
// print the result
// uint32_t rescode = 0;
// if (len < 4)
// {
// msg("bad response");
// return -1;
// }
// memcpy(&rescode, &rbuf[4], 4);
// // std::cout << ("server says: [%u] %.*s\n", rescode, len - 4, &rbuf[8]);
// std::cout << "Server says: [" << rescode << "] ";
// fprintf(stdout, "%.*s", len - 4, &rbuf[8]);
// std::cout << std::endl;
// return 0;
// print the result
int32_t rv = on_response((uint8_t *)&rbuf[4], len);
if (rv > 0 && (uint32_t)rv != len)
{
msg("bad response");
rv = -1;
}
return rv;
}
int main(int argc, char **argv)
{
int fd;
fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0)
{
die("socket()");
}
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = ntohs(1234);
addr.sin_addr.s_addr = ntohl(INADDR_LOOPBACK);
int rv = connect(fd, (const sockaddr *)&addr, sizeof(addr));
if (rv)
{
die("connect()");
}
std::vector<std::string> cmd;
for (int i = 1; i < argc; i++)
{
cmd.push_back(argv[i]);
}
int32_t err = send_req(fd, cmd);
if (err)
{
goto L_DONE;
}
err = read_res(fd, cmd);
if (err)
{
goto L_DONE;
}
L_DONE:
close(fd);
return 0;
}