-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathServer.cpp
69 lines (60 loc) · 2.16 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
/*************************************************************************
> File Name: Server.cpp
> Author: zhangfeng
> Mail: [email protected]
> Created Time: Mon 07 Oct 2019 05:44:27 PM CST
> Target:
************************************************************************/
#include "Server.h"
#include "base/Logging.h"
#include "Util.h"
#include <functional>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.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 nonblocking failed");
abort();
}
}
void Server::start() {
eventLoopThreadPool_->start();
acceptChannel_->setEvents(EPOLLIN | EPOLLET);
acceptChannel_->setReadHandler(bind(&Server::handNewConn, this));
acceptChannel_->setConnHandler(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 << "Connect: " << 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 Blocking failed";
return;
}
setSocketNodelay(accept_fd);
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);
}