2
3
4
5
26 m_Acceptor = std::make_unique<Acceptor>(listenAddress, option == Option::ReusePort);
27 m_Acceptor->SetConnectionCallback(std::bind(&TcpServer::NewConnection,
this, std::placeholders::_1, std::placeholders::_2));
29 m_ThreadPool = std::make_shared<EventLoopThreadPool>(listenAddress);
30 m_ThreadPool->SetMode(PoolMode::MODE_FIXED);
37 for (
auto& item : m_Connections)
39 const TcpConnectionPtr connection = item.second;
42 EventLoopThreadWrapper::GetInst()->RunInLoop([=]() { connection->ConnectDestroyed(); });
50 if(!m_ThreadPool->IsPoolRunning())
53 m_ThreadPool->Start(threadSize, m_ThreadInitCallback);
61 EventLoop* ioLoop = m_ThreadPool->GetNextLoop();
65 std::string connName = buf;
67 sockaddr_in local = {};
68 socklen_t addrLen =
sizeof(local);
69 if (::getsockname(socketFd, (sockaddr*)&local, &addrLen) < 0)
72 ss <<
"TcpServer::NewConnection::getsockname error, fd: " << socketFd <<
", Error: " << WSAGetLastError();
74 SPICES_CORE_ERROR(ss.str())
78 const TcpConnectionPtr connectionPtr = std::make_shared<TcpConnection>(ioLoop, connName, socketFd, localAddress, peerAddress);
79 m_Connections[connName] = connectionPtr;
81 connectionPtr->SetConnectionCallback(m_ConnectionCallback);
82 connectionPtr->SetMessageCallback(m_MessageCallback);
83 connectionPtr->SetWriteCompleteCallback(m_WriteCompleteCallback);
85 DelegateCloseCallback closeCallback;
86 closeCallback.Bind([=](
const TcpConnectionPtr& connection) {
RemoveConnection(connection
); });
87 connectionPtr->SetCloseCallback(closeCallback);
89 ioLoop->RunInLoop([=]() { connectionPtr->ConnectEstablished(); });
94 EventLoopThreadWrapper::GetInst()->RunInLoop([=]() { RemoveConnectionInLoop(connection); });
99 SPICES_CORE_INFO(
"TcpServer::RemoveConnectionInLoop")
101 m_Connections.erase(connection->GetName());
102 EventLoop* ioLoop = connection->GetLoop();
103 ioLoop->QueueInLoop([=]() { connection->ConnectDestroyed(); });
#define SPICES_PROFILE_ZONE
static EventLoop *& GetInst(InetAddress *address=nullptr)
Get EventLoop Instance. @reutrn Returns EventLoop Instance.
Wrapper of Instance/Delete ThreadCache in thread.
Wrapper of Poller and wakeup socket to acceptor(SubLoop).
std::string ToIPPort() const
Get IP and Port from Socket Address.
This class is Wrapper of current socket address.
void Start(int threadSize) const
Start ThreadPool and call listen on acceptor.
void NewConnection(SOCKET socketFd, const InetAddress &peerAddress)
Handle New Connection to Acceptor.
void RemoveConnectionInLoop(const TcpConnectionPtr &connection)
Remove a TcpConnection InLoop.
void RemoveConnection(const TcpConnectionPtr &connection)
Remove a TcpConnection.
TcpServer(const InetAddress &listenAddress, Option option=Option::NoReusePort)
Constructor Function.
std::string m_IpPort
TcpServer Ip and Port.
virtual ~TcpServer()
Destructor Function.
int m_NextConnectedId
Next Connection id.