-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.cpp
More file actions
376 lines (326 loc) · 14.1 KB
/
Copy pathserver.cpp
File metadata and controls
376 lines (326 loc) · 14.1 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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#include <atomic>
#include <cerrno>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <limits>
#include <fcntl.h>
#include <iostream>
#include <memory>
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>
#include <vector>
#include "Channel.h"
#include "EventLoop.h"
#include "EventLoopThread.h"
#include "HttpRequest.h"
#include "HttpResponse.h"
namespace {
struct ServerConfig {
int port = 8080;
int workers = 1;
int64_t idleTimeoutMs = 5000;
size_t maxHeaderSize = 8 * 1024;
};
void printUsage(const char* program) {
std::cout
<< "Usage: " << program << " [options]\n"
<< " -p, --port PORT Listen port (default: 8080)\n"
<< " -w, --workers N Worker loop count (default: 1)\n"
<< " --idle-timeout-ms MS Idle connection timeout (default: 5000)\n"
<< " --max-header-size BYTES Request header limit (default: 8192)\n"
<< " -h, --help Show this help\n";
}
bool parseInt(const char* value, int& result) {
try {
size_t parsed = 0;
const int parsedValue = std::stoi(value, &parsed);
if (parsed != std::string(value).size()) return false;
result = parsedValue;
return true;
} catch (...) {
return false;
}
}
bool parseArguments(int argc, char* argv[], ServerConfig& config) {
for (int i = 1; i < argc; ++i) {
const std::string argument = argv[i];
auto requireValue = [&](const std::string& name) -> const char* {
if (i + 1 >= argc) {
std::cerr << "Missing value for " << name << '\n';
return nullptr;
}
return argv[++i];
};
if (argument == "-p" || argument == "--port") {
const char* value = requireValue(argument);
if (!value || !parseInt(value, config.port) ||
config.port < 1 || config.port > 65535) {
std::cerr << "Invalid port: " << (value ? value : "") << '\n';
return false;
}
} else if (argument == "-w" || argument == "--workers") {
const char* value = requireValue(argument);
if (!value || !parseInt(value, config.workers) ||
config.workers < 1 || config.workers > 256) {
std::cerr << "Invalid worker count: " << (value ? value : "") << '\n';
return false;
}
} else if (argument == "--idle-timeout-ms") {
const char* value = requireValue(argument);
int timeout = 0;
if (!value || !parseInt(value, timeout) || timeout < 1) {
std::cerr << "Invalid idle timeout: " << (value ? value : "") << '\n';
return false;
}
config.idleTimeoutMs = timeout;
} else if (argument == "--max-header-size") {
const char* value = requireValue(argument);
int size = 0;
if (!value || !parseInt(value, size) || size < 128) {
std::cerr << "Invalid max header size: " << (value ? value : "") << '\n';
return false;
}
config.maxHeaderSize = static_cast<size_t>(size);
} else if (argument == "-h" || argument == "--help") {
printUsage(argv[0]);
std::exit(0);
} else {
std::cerr << "Unknown argument: " << argument << '\n';
printUsage(argv[0]);
return false;
}
}
return true;
}
bool setNonBlocking(int fd) {
int flags = fcntl(fd, F_GETFL, 0);
if (flags < 0) {
std::cerr << "fcntl(F_GETFL) failed: " << strerror(errno) << '\n';
return false;
}
if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0) {
std::cerr << "fcntl(F_SETFL) failed: " << strerror(errno) << '\n';
return false;
}
return true;
}
void refreshIdleTimer(EventLoop* loop,
const std::weak_ptr<Channel>& weakChannel,
const ServerConfig& config) {
auto channel = weakChannel.lock();
if (!channel) return;
loop->addTimer(channel->fd(), EventLoop::nowMs() + config.idleTimeoutMs,
[weakChannel]() {
auto channel = weakChannel.lock();
if (!channel) return;
std::cout << "[TIMER] idle connection timeout fd="
<< channel->fd() << '\n';
channel->handleClose();
});
}
HttpResponse makeTextResponse(int status,
const std::string& message,
const std::string& body) {
HttpResponse response;
response.setStatus(status, message);
response.addHeader("Content-Type", "text/html; charset=utf-8");
response.setBody(body);
return response;
}
bool canReuseConnection(const HttpRequest& request) {
if (!request.shouldKeepAlive()) return false;
if (request.method() != "GET") return false;
const std::string& contentLength = request.getHeader("Content-Length");
return contentLength.empty() || contentLength == "0";
}
int createListenSocket(const ServerConfig& config) {
const int listenFd = socket(AF_INET, SOCK_STREAM, 0);
if (listenFd < 0) {
std::cerr << "socket() failed: " << strerror(errno) << '\n';
return -1;
}
if (!setNonBlocking(listenFd)) {
close(listenFd);
return -1;
}
int reuseAddress = 1;
if (setsockopt(listenFd, SOL_SOCKET, SO_REUSEADDR,
&reuseAddress, sizeof(reuseAddress)) < 0) {
std::cerr << "setsockopt(SO_REUSEADDR) failed: "
<< strerror(errno) << '\n';
close(listenFd);
return -1;
}
sockaddr_in address{};
address.sin_family = AF_INET;
address.sin_port = htons(static_cast<uint16_t>(config.port));
address.sin_addr.s_addr = INADDR_ANY;
if (bind(listenFd, reinterpret_cast<sockaddr*>(&address),
sizeof(address)) < 0) {
std::cerr << "bind() failed on port " << config.port
<< ": " << strerror(errno) << '\n';
close(listenFd);
return -1;
}
if (listen(listenFd, SOMAXCONN) < 0) {
std::cerr << "listen() failed: " << strerror(errno) << '\n';
close(listenFd);
return -1;
}
return listenFd;
}
} // namespace
int main(int argc, char* argv[]) {
ServerConfig config;
if (!parseArguments(argc, argv, config)) {
return 1;
}
const int listenFd = createListenSocket(config);
if (listenFd < 0) {
return 1;
}
std::cout << "Server started on port " << config.port
<< " with " << config.workers << " worker(s)" << std::endl;
EventLoop mainLoop;
const int numWorkers = config.workers;
std::vector<std::unique_ptr<EventLoopThread>> threads;
std::vector<EventLoop*> workerLoops;
for (int i = 0; i < numWorkers; ++i) {
auto t = std::make_unique<EventLoopThread>();
workerLoops.push_back(t->startLoop());
threads.push_back(std::move(t));
}
std::atomic<int> nextWorker{0};
Channel listenChannel(listenFd, &mainLoop);
listenChannel.setReadCallback([&]() {
// 非阻塞 accept:一次 EPOLLIN 可能积压多个连接,循环取直到 EAGAIN
while (true) {
struct sockaddr_in cliaddr;
socklen_t clilen = sizeof(cliaddr);
int clientFd = accept(listenFd, (struct sockaddr*)&cliaddr, &clilen);
if (clientFd < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) break; // 本次连接已取完
if (errno == EINTR) continue; // 被信号打断,重试
std::cerr << "[ACCEPT] error: " << strerror(errno) << std::endl;
break;
}
if (!setNonBlocking(clientFd)) {
close(clientFd);
continue;
}
int idx = nextWorker.fetch_add(1) % numWorkers;
EventLoop *workerLoop = workerLoops[idx];
std::cout << "New connection fd=" << clientFd << " -> worker" << idx << std::endl;
workerLoop->runInLoop([workerLoop, clientFd, config]() {
auto clientChannel = std::make_shared<Channel>(clientFd, workerLoop);
workerLoop->holdChannel(clientChannel);
// Channel 生命周期保护:回调链触发关闭时,保证本次事件处理完整结束
clientChannel->tie(clientChannel);
std::weak_ptr<Channel> weakChannel = clientChannel;
clientChannel->setReadCallback([weakChannel, workerLoop, config]() {
auto channel = weakChannel.lock();
if (!channel) return;
const ssize_t n =
channel->inputBuffer().readFromFd(channel->fd());
if (n > 0) {
refreshIdleTimer(workerLoop, weakChannel, config);
if (channel->inputBuffer().size() >
config.maxHeaderSize) {
// 请求头总量超过上限:无论是否完整,都拒绝并断连
std::cerr << "[GUARD] header too large fd="
<< channel->fd() << " size="
<< channel->inputBuffer().size() << '\n';
HttpResponse response = makeTextResponse(
431, "Request Header Fields Too Large",
"<html><body><h1>431</h1></body></html>");
channel->reject(response.toString());
return;
}
while (true) {
const char* end =
strstr(channel->inputBuffer().data(), "\r\n\r\n");
if (!end) break;
size_t requestLen =
end - channel->inputBuffer().data() + 4;
std::string rawRequest =
channel->inputBuffer().retrieveAsString(requestLen);
HttpRequest request;
if (!request.parse(rawRequest)) {
std::cerr << "[PARSE] bad request fd="
<< channel->fd() << '\n';
HttpResponse response = makeTextResponse(
400, "Bad Request",
"<html><body><h1>400</h1></body></html>");
channel->reject(response.toString());
break;
}
const bool keepAlive = canReuseConnection(request);
std::cout << "Request: " << request.method() << " "
<< request.path() << std::endl;
HttpResponse response;
response.setConnection(
keepAlive ? "keep-alive" : "close");
if (request.method() != "GET") {
response.setStatus(405, "Method Not Allowed");
response.addHeader("Allow", "GET");
response.setBody(
"<html><body><h1>405</h1></body></html>");
} else if (request.path() == "/") {
response.setStatus(200, "OK");
response.addHeader(
"Content-Type", "text/html; charset=utf-8");
response.setBody(
"<html><body><h1>Hello!</h1></body></html>");
} else {
response.setStatus(404, "Not Found");
response.addHeader(
"Content-Type", "text/html; charset=utf-8");
response.setBody(
"<html><body><h1>404</h1></body></html>");
}
channel->setCloseAfterFlush(!keepAlive);
channel->sendData(response.toString());
if (!keepAlive && channel->outputBuffer().empty()) {
channel->handleClose();
break;
}
}
} else if (n == 0) {
channel->handleClose();
} else {
// n < 0:EAGAIN 不是错误;真实错误才断连
if (errno == EAGAIN || errno == EWOULDBLOCK) {
// LT 模式下一般不会走到这里,防御性保留连接
} else {
std::cerr << "[READ] error fd=" << channel->fd()
<< " errno=" << strerror(errno) << std::endl;
channel->handleClose();
}
}
});
clientChannel->setWriteCallback([weakChannel]() {
auto channel = weakChannel.lock();
if (!channel) return;
channel->flushOutput();
});
clientChannel->setCloseCallback([workerLoop, weakChannel]() {
workerLoop->runInLoop([workerLoop, weakChannel]() {
auto channel = weakChannel.lock();
if (!channel) return;
std::cout << "[CLOSE] remove fd="
<< channel->fd() << '\n';
workerLoop->removeChannel(channel.get());
});
});
refreshIdleTimer(workerLoop, weakChannel, config);
clientChannel->enableReading();
});
}
});
listenChannel.enableReading();
mainLoop.loop();
close(listenFd);
return 0;
}