-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTcpServer.cc
More file actions
126 lines (109 loc) · 3.9 KB
/
TcpServer.cc
File metadata and controls
126 lines (109 loc) · 3.9 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
#include "TcpServer.h"
#include "logger.h"
#include "Util.h"
#include "TcpConnection.h"
#include <cstring>
TcpServer::TcpServer(EventLoop *loop,
const InetAddress & listenAddr,
const std::string & nameArg,
Option option)
: loop_(CheckLoopNotNull(loop))
, ipPort_(listenAddr.toIpPort())
, name_(nameArg)
, acceptor_(new Acceptor(loop,listenAddr,option == kReusePort))
, threadPool_(new EventLoopThreadPool(loop,name_))
, connectionCallback_()
, messageCallback_()
, nextConnId_(1)
, started_(0)
{
// 当有新用户链接时 会执行 TcpServer::newConnectionCallback
acceptor_->setNewConnectonCallback(std::bind(&TcpServer::newConnection,this,
std::placeholders::_1,std::placeholders::_2));
}
TcpServer::~TcpServer()
{
for(auto & item : connects_)
{
// 这个局部的share 的 ptr 的只能指针 出右括号 可以自动释放
TcpConnectionPtr conn(item.second);
item.second.reset();
conn->getLoop()->runInLoop(
std::bind(&TcpConnection::connectDestroyed,conn)
);
}
}
// 设置底层subloop 的个数
void TcpServer::setThreadNum (int numThread)
{
threadPool_->setThreadNum(numThread);
}
// 开启服务器 监听
void TcpServer::start()
{
if(started_ ++ == 0)
{
LogDEBUG("TcpServer::start() started_ = %d =============> start OK \n",(int)started_);
threadPool_->start(threadInitCallback_); // 启动底层的线程池
LogDEBUG("底层的线程池 threadPool===========> start OK!");
loop_->runInLoop(std::bind(&Acceptor::listen,acceptor_.get()));
LogDEBUG("TcpServer runInLoop out =================> OK !");
}
}
void TcpServer::newConnection(int sockfd,const InetAddress & peerAddress)
{
// 轮询算法 选择 subloop 来管理 channel
EventLoop * ioloop = threadPool_->getNextloop();
char buffer[64] = {0};
snprintf(buffer, sizeof buffer, "-%s#%d",ipPort_.c_str(),nextConnId_);
++nextConnId_;
std::string connName = name_+buffer;
LogDEBUG("一个新连接 来了 ");
LogINFO("TcpServer::newConnection[%s] - new connection [%s] from %s \n",\
name_.c_str(),connName.c_str(),peerAddress.toIpPort().c_str()
);
// 通过 socketfd 获取其 绑定的本机的ip地址和 端口信息
sockaddr_in local;
::bzero(& local,sizeof local);
socklen_t addrlen = sizeof local;
if(::getsockname(sockfd,(sockaddr*)&local,&addrlen) < 0)
{
LogERROR("Socket::getLocalAddr \n");
}
InetAddress localAddr(local);
TcpConnectionPtr conn(new TcpConnection(
ioloop,
connName,
sockfd, // Socket Channel
localAddr,
peerAddress));
connects_[connName] = conn;
// 下面的回调都是用户 设置的
conn->setConnectionCallback(connectionCallback_);
conn->setMessageCallback(messageCallback_);
conn->setWriteCompleteCallback(writeComplateCallback_);
// 设置如何关闭连接的 回调
conn->setCloseCallback(
std::bind(&TcpServer::removeConnection,this,std::placeholders::_1)
);
ioloop->runInLoop(std::bind(&TcpConnection::connectEstablelished,conn));
}
void TcpServer::removeConnection(const TcpConnectionPtr & conn)
{
loop_->runInLoop(
std::bind(&TcpServer::removeConnectionInLoop,this,conn)
);
}
void TcpServer::removeConnectionInLoop(const TcpConnectionPtr & conn)
{
LogINFO(
"TcpServer::removeConnectionInLoop[%s] - connection %s \n",
name_.c_str(),conn->name().c_str()
);
connects_.erase(conn->name());
EventLoop * ioloop = conn->getLoop();
ioloop->queenInLoop(
std::bind(&TcpConnection::connectDestroyed,conn)
);
LogDEBUG("TcpServer::removeConnection remove connection ================> OK");
}