SpiecsEngine
 
Loading...
Searching...
No Matches
TcpServer.cpp
Go to the documentation of this file.
1/**
2* @file TcpServer.cpp.
3* @brief The TcpServer Class Implementation.
4* @author Spices & Muduo.
5*/
6
7#include "Pchheader.h"
8#include "TcpServer.h"
11
12namespace Spices {
13
14namespace Net {
15
17 const InetAddress& listenAddress ,
18 Option option
19 )
21 {
23
24 m_IpPort = listenAddress.ToIPPort();
25
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));
28
29 m_ThreadPool = std::make_shared<EventLoopThreadPool>(listenAddress);
30 m_ThreadPool->SetMode(PoolMode::MODE_FIXED);
31 }
32
34 {
36
37 for (auto& item : m_Connections)
38 {
39 const TcpConnectionPtr connection = item.second;
40 item.second.reset();
41
42 EventLoopThreadWrapper::GetInst()->RunInLoop([=]() { connection->ConnectDestroyed(); });
43 }
44 }
45
46 void TcpServer::Start(int threadSize) const
47 {
49
50 if(!m_ThreadPool->IsPoolRunning())
51 {
52 EventLoopThreadWrapper::GetInst()->RunInLoop([=]() { m_Acceptor->Listen(); });
53 m_ThreadPool->Start(threadSize, m_ThreadInitCallback);
54 }
55 }
56
57 void TcpServer::NewConnection(SOCKET socketFd, const InetAddress& peerAddress)
58 {
60
61 EventLoop* ioLoop = m_ThreadPool->GetNextLoop();
62 char buf[64] = {};
63 snprintf(buf, sizeof(buf), "-%s#%d", m_IpPort.c_str(), m_NextConnectedId);
65 std::string connName = buf;
66
67 sockaddr_in local = {};
68 socklen_t addrLen = sizeof(local);
69 if (::getsockname(socketFd, (sockaddr*)&local, &addrLen) < 0)
70 {
71 std::stringstream ss;
72 ss << "TcpServer::NewConnection::getsockname error, fd: " << socketFd << ", Error: " << WSAGetLastError();
73
74 SPICES_CORE_ERROR(ss.str())
75 }
76 InetAddress localAddress(local);
77
78 const TcpConnectionPtr connectionPtr = std::make_shared<TcpConnection>(ioLoop, connName, socketFd, localAddress, peerAddress);
79 m_Connections[connName] = connectionPtr;
80
81 connectionPtr->SetConnectionCallback(m_ConnectionCallback);
82 connectionPtr->SetMessageCallback(m_MessageCallback);
83 connectionPtr->SetWriteCompleteCallback(m_WriteCompleteCallback);
84
85 DelegateCloseCallback closeCallback;
86 closeCallback.Bind([=](const TcpConnectionPtr& connection) { RemoveConnection(connection); });
87 connectionPtr->SetCloseCallback(closeCallback);
88
89 ioLoop->RunInLoop([=]() { connectionPtr->ConnectEstablished(); });
90 }
91
92 void TcpServer::RemoveConnection(const TcpConnectionPtr& connection)
93 {
94 EventLoopThreadWrapper::GetInst()->RunInLoop([=]() { RemoveConnectionInLoop(connection); });
95 }
96
97 void TcpServer::RemoveConnectionInLoop(const TcpConnectionPtr& connection)
98 {
99 SPICES_CORE_INFO("TcpServer::RemoveConnectionInLoop")
100
101 m_Connections.erase(connection->GetName());
102 EventLoop* ioLoop = connection->GetLoop();
103 ioLoop->QueueInLoop([=]() { connection->ConnectDestroyed(); });
104 }
105}
106
107}
#define SPICES_PROFILE_ZONE
static EventLoop *& GetInst(InetAddress *address=nullptr)
Get EventLoop Instance. @reutrn Returns EventLoop Instance.
Wrapper of Instance/Delete ThreadCache in thread.
Definition EventLoop.h:180
Wrapper of Poller and wakeup socket to acceptor(SubLoop).
Definition EventLoop.h:28
std::string ToIPPort() const
Get IP and Port from Socket Address.
This class is Wrapper of current socket address.
Definition InetAddress.h:22
void Start(int threadSize) const
Start ThreadPool and call listen on acceptor.
Definition TcpServer.cpp:46
void NewConnection(SOCKET socketFd, const InetAddress &peerAddress)
Handle New Connection to Acceptor.
Definition TcpServer.cpp:57
void RemoveConnectionInLoop(const TcpConnectionPtr &connection)
Remove a TcpConnection InLoop.
Definition TcpServer.cpp:97
void RemoveConnection(const TcpConnectionPtr &connection)
Remove a TcpConnection.
Definition TcpServer.cpp:92
TcpServer(const InetAddress &listenAddress, Option option=Option::NoReusePort)
Constructor Function.
Definition TcpServer.cpp:16
std::string m_IpPort
TcpServer Ip and Port.
Definition TcpServer.h:135
virtual ~TcpServer()
Destructor Function.
Definition TcpServer.cpp:33
int m_NextConnectedId
Next Connection id.
Definition TcpServer.h:170
Sample of a TcpServer.
Definition TcpServer.h:29