-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.cc
More file actions
93 lines (75 loc) · 1.55 KB
/
client.cc
File metadata and controls
93 lines (75 loc) · 1.55 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
#define __MAKEMORE_CLIENT_CC__ 1
#include <assert.h>
#include <string.h>
#include "client.hh"
#include "youtil.hh"
namespace makemore {
bool Client::read(uint8_t *buf, unsigned int len) {
if (inpbufk < len)
return false;
if (buf)
memcpy(buf, inpbuf, len);
memmove(inpbuf, inpbuf + len, inpbufk - len);
inpbufk -= len;
return true;
}
bool Client::slurp() {
int ret = ::read(s, inpbuf + inpbufk, inpbufn - inpbufk);
info(fmt("slurped ret=%d", ret));
if (ret == 0)
return false;
if (ret < 0) {
if (ret != EAGAIN)
return false;
ret = 0;
}
inpbufk += ret;
return true;
}
bool Client::flush() {
if (outbufk == 0)
return true;
int ret = ::write(s, outbuf, outbufk);
info(fmt("flushed ret=%d", ret));
if (ret == 0)
return false;
if (ret < 0) {
if (ret != EAGAIN)
return false;
ret = 0;
}
if (ret == outbufk) {
outbufk = 0;
return true;
}
assert(ret < outbufk);
memmove(outbuf, outbuf + ret, outbufk - ret);
outbufk -= ret;
return true;
}
bool Client::write(const uint8_t *buf, unsigned int len) {
if (outbufk > 0) {
if (outbufk + len > outbufn)
return false;
memcpy(outbuf + outbufk, buf, len);
outbufk += len;
return true;
}
int ret = ::write(s, buf, len);
if (ret < 0) {
if (errno != EAGAIN)
return false;
ret = 0;
}
if (ret == len)
return true;
assert(ret < len);
buf += ret;
len -= ret;
if (outbufk + len > outbufn)
return false;
memcpy(outbuf + outbufk, buf, len);
outbufk += len;
return true;
}
}