-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.cpp
58 lines (52 loc) · 1.96 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
#include "Server.h"
#include "Util.h"
#include <netinet/in.h>
#include <string.h>
Server::Server(EventLoop* loop, int threadNum, int port)
: loop_(loop),
threadNum_(threadNum),
eventLoopThreadPool_(new EventLoopThreadPool(loop_, threadNum)),
started_(false),
acceptChannel_(new Channel(loop_)),
port_(port),
listenFd_(socket_bind_listen(port_)) {
acceptChannel_->setFd(listenFd_);
handle_for_sigpipe();
if (setSocketNonBlocking(listenFd_) < 0) {
perror("set socket non block failed");
abort();
}
}
void Server::start() {
eventLoopThreadPool_->start();
acceptChannel_->setEvents(EPOLLIN | EPOLLET);
acceptChannel_->setReadHandler(std::bind(&Server::handNewConn, this));
acceptChannel_->setConnHandler(std::bind(&Server::handThisConn, this));
loop_->addToPoller(acceptChannel_, 0);
started_ = true;
}
void Server::handNewConn() {
struct sockaddr_in client_addr;
memset(&client_addr, 0, sizeof(struct sockaddr_in));
socklen_t client_addr_len = sizeof(client_addr);
int accept_fd = 0;
while ((accept_fd = accept(listenFd_, (struct sockaddr*)&client_addr, &client_addr_len)) > 0) {
EventLoop* loop = eventLoopThreadPool_->getNextLoop();
// LOG << "New connection from " << inet_ntoa(client_addr.sin_addr) << ":" << ntohs(client_addr.sin_port);
// 限制服务器的最大并发连接数
if (accept_fd >= MAXFDS) {
close(accept_fd);
continue;
}
// 设为非阻塞模式
if (setSocketNonBlocking(accept_fd) < 0) {
// LOG << "Set non block failed";
return;
}
setSocketNodelay(accept_fd);
std::shared_ptr<HttpData> req_info(new HttpData(loop, accept_fd));
req_info->getChannel()->setHolder(req_info);
loop->queueInLoop(std::bind(&HttpData::newEvent, req_info));
}
acceptChannel_->setEvents(EPOLLIN | EPOLLET);
}