-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.cpp
250 lines (209 loc) · 5.79 KB
/
server.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
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
#include "weblegends.h"
#include "helpers.h"
#include <cerrno>
#include <cstring>
#include <iostream>
static std::function<void(std::ostream &, df::history_event *)> weblegends_describe_event_v0 = [](std::ostream & out, df::history_event *e)
{
int32_t last_year = -1;
int32_t last_seconds = -1;
event(out, event_context(), e, last_year, last_seconds);
};
static std::function<std::unique_ptr<weblegends_layout_v1>()> weblegends_allocate_layout_v1 = []() -> std::unique_ptr<weblegends_layout_v1>
{
return dts::make_unique<Layout>();
};
command_result WebLegends::init(color_ostream & out)
{
Core::getInstance().RegisterData(&weblegends_describe_event_v0, WEBLEGENDS_DESCRIBE_EVENT_V0);
Core::getInstance().RegisterData(&weblegends_allocate_layout_v1, WEBLEGENDS_ALLOCATE_LAYOUT_V1);
for (uint16 port = 5080; port < 5090; port++)
{
if (!sock.Initialize() || !sock.Listen("0.0.0.0", port))
{
if (sock.GetSocketError() == CPassiveSocket::SocketAddressInUse)
{
continue;
}
break;
}
out << "weblegends listening on http://localhost:" << port << "/" << std::endl;
thread = new tthread::thread(WebLegends::run, this);
return CR_OK;
}
out << "weblegends startup failed: " << sock.GetSocketError() << std::endl;
out << sock.DescribeError() << std::endl;
return CR_FAILURE;
}
command_result WebLegends::shutdown(color_ostream & out)
{
if (Core::getInstance().isSuspended())
{
return CR_FAILURE;
}
Core::getInstance().RegisterData(nullptr, WEBLEGENDS_DESCRIBE_EVENT_V0);
Core::getInstance().RegisterData(nullptr, WEBLEGENDS_ALLOCATE_LAYOUT_V1);
out << "weblegends cleaning up..." << std::endl;
std::cerr << "weblegends sending shutdown request" << std::endl;
{
tthread::lock_guard<tthread::mutex> l(to_delete_lock);
do_shutdown = true;
}
thread->join();
delete thread;
std::cerr << "weblegends forcibly closing " << clients.size() << " clients" << std::endl;
for (auto it = clients.begin(); it != clients.end(); it++)
{
(*it)->kill();
}
for (auto it = clients.begin(); it != clients.end(); it++)
{
(*it)->join();
}
tthread::lock_guard<tthread::mutex> l(to_delete_lock);
for (auto it = to_delete.begin(); it != to_delete.end(); it++)
{
delete *it;
}
to_delete.clear();
clients.clear();
out << "done." << std::endl;
return CR_OK;
}
void WebLegends::run(void *wl)
{
reinterpret_cast<WebLegends *>(wl)->run();
}
void WebLegends::run()
{
while (true)
{
if (cleanup())
{
std::cerr << "weblegends got shutdown request" << std::endl;
sock.Close();
return;
}
if (!sock.Select(1, 0))
{
continue;
}
CActiveSocket *client = sock.Accept();
if (!client)
{
std::cerr << "weblegends accept: " << sock.GetSocketError() << std::endl;
std::cerr << sock.DescribeError() << std::endl;
continue;
}
Client *c = new Client(this, client);
if (c->start())
{
clients.push_back(c);
}
else
{
int err = errno;
std::cerr << "weblegends failed to start connection handler for " << (const char *)client->GetClientAddr() << ":" << uint16_t(client->GetClientPort()) << ": error " << err << ": " << strerror(err) << std::endl;
client->Close();
delete c;
}
}
}
bool WebLegends::cleanup()
{
tthread::lock_guard<tthread::mutex> l(to_delete_lock);
for (auto it = to_delete.begin(); it != to_delete.end(); it++)
{
clients.erase(std::find(clients.begin(), clients.end(), *it));
(*it)->join();
delete *it;
}
to_delete.clear();
return do_shutdown;
}
void WebLegends::mark(Client *c)
{
tthread::lock_guard<tthread::mutex> l(to_delete_lock);
to_delete.push_back(c);
}
Client::Client(WebLegends *weblegends, CActiveSocket *sock) :
weblegends(weblegends),
sock(sock),
thread(nullptr)
{
}
Client::~Client()
{
delete thread;
delete sock;
}
void Client::run(void *c)
{
reinterpret_cast<Client *>(c)->run();
}
void Client::run()
{
std::string request;
while (true)
{
if (!sock->IsSocketValid())
{
weblegends->mark(this);
return;
}
if (!sock->Select(1, 0))
{
continue;
}
int32_t len = sock->Receive(8192);
if (len == 0)
{
// closed on other end
sock->Close();
continue;
}
if (len == -1)
{
std::cerr << "weblegends recv: " << sock->GetSocketError() << std::endl;
std::cerr << sock->DescribeError() << std::endl;
sock->Close();
continue;
}
request.append((const char *)sock->GetData(), size_t(len));
try
{
while (weblegends->http(sock, request));
}
catch (std::exception & ex)
{
WEBLEGENDS_DEBUG_LOG << "weblegends ex: " << ex.what() << std::endl;
sock->Close();
continue;
}
catch (...)
{
std::cerr << "weblegends unknown error" << std::endl;
sock->Close();
continue;
}
if (request.size() >= 8192)
{
sock->Close();
continue;
}
}
}
bool Client::start()
{
tthread::lock_guard<tthread::mutex> l(weblegends->to_delete_lock);
thread = new tthread::thread(Client::run, this);
return thread->joinable();
}
void Client::kill()
{
sock->Shutdown(CSimpleSocket::Both);
}
void Client::join()
{
thread->join();
}