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
|
#include "server.hh"
#include <algorithm>
#include <cassert>
#include <cerrno>
#include <chrono>
#include <cstring>
#include <signal.h>
#include <unistd.h>
#include "buffer.hh"
#include "logger.hh"
#include "looper.hh"
#include "net.hh"
namespace {
constexpr auto CLIENT_TIMEOUT = std::chrono::seconds(30);
constexpr size_t BUFFER_SIZE = 1 * 1024 * 1024;
int g_signal_pipe[2];
void signal_handler(int) {
write(g_signal_pipe[1], "1", 1);
}
class ServerImpl : public Server {
public:
ServerImpl()
: looper_(Looper::create()) {}
~ServerImpl() override {
for (auto& fd : listen_) {
looper_->remove(fd.get());
}
}
bool setup(std::shared_ptr<Logger> logger, std::string const& host,
uint16_t port) override {
logger_ = logger;
listen_ = net::bind_and_listen(logger.get(), host, port);
if (listen_.empty()) {
logger->err("Unable to listen");
return false;
}
for (auto& fd : listen_) {
looper_->add(fd.get(), Looper::EVENT_READ,
std::bind(&ServerImpl::listen_read, this, fd.get(),
std::placeholders::_1));
}
return true;
}
bool run(std::shared_ptr<Logger> logger) override {
logger_ = logger;
bool close_pipe;
if (pipe(g_signal_pipe) == 0) {
close_pipe = true;
struct sigaction sa;
sa.sa_handler = signal_handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
sigaction(SIGINT, &sa, nullptr);
sigaction(SIGTERM, &sa, nullptr);
sigaction(SIGQUIT, &sa, nullptr);
looper_->add(g_signal_pipe[0], Looper::EVENT_READ,
std::bind(&ServerImpl::signal, this, std::placeholders::_1));
} else {
logger->warn("Unable to create a pipe: %s", strerror(errno));
close_pipe = false;
}
bool ret = looper_->run(logger.get());
if (close_pipe) {
looper_->remove(g_signal_pipe[0]);
close(g_signal_pipe[0]);
close(g_signal_pipe[1]);
}
return ret;
}
private:
void signal(uint8_t) {
looper_->quit();
}
struct Client {
unique_fd fd_;
uint8_t events_{Looper::EVENT_WRITE};
std::unique_ptr<Buffer> in_;
std::unique_ptr<Buffer> out_;
uint32_t watchdog_{0};
std::chrono::steady_clock::time_point last_event_;
explicit Client(unique_fd fd)
: fd_(std::move(fd)) {}
};
void listen_read(int fd, uint8_t events) {
if (events & Looper::EVENT_ERROR) {
auto it = std::find_if(listen_.begin(), listen_.end(),
[fd](auto& listen) {
return listen.get() == fd;
});
if (it != listen_.end()) {
logger_->warn("Listening socket error");
listen_.erase(it);
if (listen_.empty()) {
logger_->err("No listening sockets left");
looper_->quit();
return;
}
} else {
assert(false);
}
return;
}
unique_fd client_fd = net::accept(logger_.get(), fd);
if (client_fd) {
logger_->dbg("New client: %d", client_fd.get());
auto client = new Client(std::move(client_fd));
client->in_ = Buffer::fixed(BUFFER_SIZE);
client->out_ = Buffer::fixed(BUFFER_SIZE);
clients_.push_back(client);
looper_->add(client->fd_.get(), client->events_,
std::bind(&ServerImpl::client_event,
this, client,
std::placeholders::_1));
client->watchdog_ = looper_->schedule(
CLIENT_TIMEOUT.count(),
std::bind(&ServerImpl::client_check_timeout,
this, client,
std::placeholders::_1));
}
}
void client_event(Client* client, uint8_t events) {
if (events & Looper::EVENT_ERROR) {
logger_->warn("%d: Unexpected client error", client->fd_.get());
remove_client(client);
return;
}
client->last_event_ = std::chrono::steady_clock::now();
if (events & Looper::EVENT_READ && client->in_) {
while (true) {
size_t avail;
auto* ptr = client->in_->wbuf(65535, avail);
if (avail == 0)
break;
auto ret = read(client->fd_.get(), ptr, avail);
if (ret < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK)
break;
if (errno == EINTR)
continue;
logger_->warn("%d: Read error: %s", client->fd_.get(),
strerror(errno));
break;
} else if (ret == 0) {
logger_->dbg("%d: Connection closed", client->fd_.get());
if (client->out_->empty()) {
remove_client(client);
return;
} else {
// Might be only read side that is shutdown, try writing the
// rest before we give up.
client->in_.reset();
break;
}
} else {
client->in_->wcommit(ret);
if (static_cast<size_t>(ret) < avail)
break;
}
}
}
// Always try to write, usually we write as a result of a read above
// so saves having to call back to the loop just to answer.
// And if there wasn't a read event then we already know there was an write
// event.
if (/* events & Looper::EVENT_WRITE */ true) {
while (true) {
size_t avail;
auto* ptr = client->out_->rbuf(65535, avail);
if (avail == 0)
break;
auto ret = write(client->fd_.get(), ptr, avail);
if (ret < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK)
break;
if (errno == EINTR)
continue;
logger_->warn("%d: Write error: %s", client->fd_.get(),
strerror(errno));
break;
} else {
client->out_->rcommit(ret);
if (static_cast<size_t>(ret) < avail)
break;
}
}
}
auto const old_events = client->events_;
client->events_ = 0;
if (client->in_) {
if (!client->in_->full())
client->events_ |= Looper::EVENT_READ;
if (!client->out_->empty())
client->events_ |= Looper::EVENT_WRITE;
} else {
if (client->out_->empty()) {
remove_client(client);
return;
} else {
client->events_ |= Looper::EVENT_WRITE;
}
}
if (client->events_ != old_events) {
looper_->update(client->fd_.get(), client->events_);
}
}
void client_check_timeout(Client* client, uint32_t) {
auto since_last = std::chrono::steady_clock::now() - client->last_event_;
if (since_last >= CLIENT_TIMEOUT) {
client->watchdog_ = 0;
remove_client(client);
return;
}
auto delay = CLIENT_TIMEOUT - since_last;
client->watchdog_ = looper_->schedule(
std::chrono::duration_cast<std::chrono::seconds>(delay).count(),
std::bind(&ServerImpl::client_check_timeout,
this, client,
std::placeholders::_1));
}
void remove_client(Client* client) {
if (client->fd_)
looper_->remove(client->fd_.get());
if (client->watchdog_)
looper_->cancel(client->watchdog_);
auto it = std::find(clients_.begin(), clients_.end(), client);
if (it != clients_.end()) {
clients_.erase(it);
} else {
assert(false);
}
delete client;
}
std::shared_ptr<Looper> looper_;
std::shared_ptr<Logger> logger_;
std::vector<unique_fd> listen_;
std::vector<Client*> clients_;
};
} // namespace
// static
std::unique_ptr<Server> Server::create() {
return std::make_unique<ServerImpl>();
}
|