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
|
// -*- mode: c++; c-basic-offset: 2; -*-
#include "common.hh"
#include <condition_variable>
#include <cstring>
#include <fcntl.h>
#include <mutex>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <thread>
#include <vector>
#include "io.hh"
#include "looper.hh"
#include "resolver.hh"
namespace {
size_t const WORKERS = 4;
class ResolverImpl : public Resolver {
public:
ResolverImpl(Looper* looper)
: looper_(looper), request_(nullptr), buf_(new char[sizeof(Request*)]),
fill_(0), quit_(false) {
while (threads_.size() < WORKERS) {
threads_.emplace_back(std::bind(&ResolverImpl::worker, this));
}
if (pipe_.open() && fcntl(pipe_.read(), F_SETFL, O_NONBLOCK) == 0) {
looper_->add(pipe_.read(),
Looper::EVENT_READ,
std::bind(&ResolverImpl::event, this,
std::placeholders::_1, std::placeholders::_2));
} else {
assert(false);
}
}
~ResolverImpl() override {
quit_ = true;
cond_.notify_all();
for (auto& thread : threads_) {
thread.join();
}
}
void* request(std::string const& host, uint16_t port,
Callback const& callback) override {
auto req = new Request();
req->host = host;
req->port = port;
req->callback = callback;
req->canceled = false;
std::unique_lock<std::mutex> lock(mutex_);
req->next = request_;
request_ = req;
cond_.notify_one();
return req;
}
void cancel(void* ptr) override {
auto req = reinterpret_cast<Request*>(ptr);
req->canceled = true;
std::unique_lock<std::mutex> lock(mutex_);
if (request_ == req) {
request_ = req->next;
delete req;
} else {
for (auto r = request_; r->next; r = r->next) {
if (r->next == req) {
r->next = req->next;
delete req;
return;
}
}
}
}
protected:
struct Request {
Request* next;
std::string host;
uint16_t port;
Callback callback;
bool canceled;
io::auto_fd fd;
bool connected;
std::string error;
};
void event(int fd, uint8_t event) {
assert(fd == pipe_.read());
if (event & Looper::EVENT_READ) {
while (true) {
auto ret = io::read(fd, buf_.get() + fill_, sizeof(Request*) - fill_);
if (ret == -1) {
if (errno == EAGAIN || errno == EWOULDBLOCK) return;
assert(false);
return;
} else if (ret == 0) {
assert(false);
return;
}
fill_ += ret;
if (fill_ == sizeof(Request*)) {
fill_ = 0;
auto req = *reinterpret_cast<Request**>(buf_.get());
if (!req->canceled) {
auto err = req->fd ? nullptr : req->error.c_str();
req->callback(req->fd.release(), req->connected, err);
}
delete req;
} else {
break;
}
}
} else {
assert(false);
}
}
void report(Request* req, int fd, bool connected, char const* errmsg) {
req->fd.reset(fd);
req->connected = connected;
if (errmsg) req->error = errmsg;
io::write_all(pipe_.write(), &req, sizeof(Request*));
}
void worker() {
struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_V4MAPPED | AI_ADDRCONFIG | AI_NUMERICSERV;
char tmp[10];
while (true) {
Request* req;
{
std::unique_lock<std::mutex> lock(mutex_);
while (!quit_ && !request_) {
cond_.wait(lock);
}
if (quit_) return;
auto pr = &request_;
while ((*pr)->next) {
pr = &((*pr)->next);
}
req = *pr;
*pr = nullptr;
}
snprintf(tmp, sizeof(tmp), "%u", static_cast<unsigned int>(req->port));
struct addrinfo* result;
auto ret = getaddrinfo(req->host.c_str(), tmp, &hints, &result);
if (ret != 0) {
report(req, -1, false, gai_strerror(ret));
continue;
}
auto rp = result;
for (; rp; rp = rp->ai_next) {
io::auto_fd fd(socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol));
if (!fd) continue;
fcntl(fd.get(), F_SETFL, O_NONBLOCK);
while (true) {
ret = connect(fd.get(), rp->ai_addr, rp->ai_addrlen);
if (ret == 0 || errno != EINTR) break;
}
if (ret == 0) {
report(req, fd.release(), true, nullptr);
break;
}
if (errno == EINPROGRESS) {
report(req, fd.release(), false, nullptr);
break;
}
}
if (!rp) {
freeaddrinfo(result);
report(req, -1, false, strerror(errno));
continue;
}
freeaddrinfo(result);
}
}
Looper* const looper_;
Request* request_;
io::auto_pipe pipe_;
std::mutex mutex_;
std::condition_variable cond_;
std::unique_ptr<char[]> buf_;
size_t fill_;
bool quit_;
std::vector<std::thread> threads_;
};
} // namespace
// static
Resolver* Resolver::create(Looper* looper) {
return new ResolverImpl(looper);
}
|