-
Notifications
You must be signed in to change notification settings - Fork 3
/
POSIX.cpp
145 lines (119 loc) · 3.58 KB
/
POSIX.cpp
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
#include "POSIX.hpp"
#include <glog/logging.h>
#include <fcntl.h>
#include <sys/select.h>
using std::chrono::duration_cast;
using std::chrono::milliseconds;
using std::chrono::seconds;
using std::chrono::system_clock;
using std::chrono::time_point;
void POSIX::set_nonblocking(int fd)
{
int flags;
PCHECK((flags = fcntl(fd, F_GETFL, 0)) != -1);
if (0 == (flags & O_NONBLOCK)) {
PCHECK(fcntl(fd, F_SETFL, flags | O_NONBLOCK) != -1);
}
}
bool POSIX::input_ready(int fd_in, milliseconds wait)
{
auto fds{fd_set{}};
FD_ZERO(&fds);
FD_SET(fd_in, &fds);
auto tv{timeval{}};
tv.tv_sec = duration_cast<seconds>(wait).count();
tv.tv_usec = (wait.count() % 1000) * 1000;
int puts;
PCHECK((puts = select(fd_in + 1, &fds, nullptr, nullptr, &tv)) != -1);
return 0 != puts;
}
bool POSIX::output_ready(int fd_out, milliseconds wait)
{
auto fds{fd_set{}};
FD_ZERO(&fds);
FD_SET(fd_out, &fds);
auto tv{timeval{}};
tv.tv_sec = duration_cast<seconds>(wait).count();
tv.tv_usec = (wait.count() % 1000) * 1000;
int puts;
PCHECK((puts = select(fd_out + 1, nullptr, &fds, nullptr, &tv)) != -1);
return 0 != puts;
}
std::streamsize POSIX::read(int fd,
char* s,
std::streamsize n,
std::function<void(void)> read_hook,
std::chrono::milliseconds timeout,
bool& t_o)
{
auto const start = system_clock::now();
auto const end_time = start + timeout;
for (;;) {
auto const n_ret = ::read(fd, static_cast<void*>(s), n);
if (n_ret == -1) {
switch (errno) {
case EINTR: break; // try read again
case ECONNRESET:
LOG(WARNING) << "read(2) raised ECONNRESET";
return -1;
default:
PCHECK((errno == EWOULDBLOCK) || (errno == EAGAIN))
<< "error from read(2)";
}
}
else if (n_ret >= 0) {
return n_ret;
}
auto const now = system_clock::now();
if (now < end_time) {
auto const time_left = duration_cast<milliseconds>(end_time - now);
read_hook();
if (input_ready(fd, time_left))
continue; // try read again
}
t_o = true;
LOG(WARNING) << "read(2) timed out";
return -1;
}
}
std::streamsize POSIX::write(int fd,
const char* s,
std::streamsize n,
std::chrono::milliseconds timeout,
bool& t_o)
{
auto const start = system_clock::now();
auto const end_time = start + timeout;
auto written = std::streamsize{};
for (;;) {
auto const n_ret = ::write(fd, static_cast<const void*>(s), n - written);
if (n_ret == -1) {
switch (errno) {
case EINTR: break; // try write again
case ECONNRESET:
LOG(WARNING) << "write(2) raised ECONNRESET";
return -1;
default:
PCHECK((errno == EWOULDBLOCK) || (errno == EAGAIN))
<< "error from write(2)";
}
}
else if (n_ret == 0) {
LOG(WARNING) << "zero write";
} else {
s += n_ret;
written += n_ret;
}
if (written == n)
return n;
auto const now = system_clock::now();
if (now < end_time) {
auto const time_left = duration_cast<milliseconds>(end_time - now);
if (output_ready(fd, time_left))
continue; // write some more
}
t_o = true;
LOG(WARNING) << "write(2) time out";
return -1;
}
}