-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTcpServer.cpp
32 lines (27 loc) · 1010 Bytes
/
TcpServer.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
#include "TcpServer.h"
// Constructor
CTcpServer::CTcpServer(boost::asio::io_service& aIoService, int aPort)
: mIoService(aIoService)
, mAcceptor(aIoService, tcp::endpoint(tcp::v4(), aPort))
{
StartAccept();
}
// Start or restart listen on server socket
void CTcpServer::StartAccept()
{
CTcpConnection::pointer NewConnection = CTcpConnection::pointer(new CTcpConnection (mIoService, mEmailAnalyser));
mAcceptor.async_accept(NewConnection->socket(),
[this, NewConnection] (auto error) {this->HandleAccept(NewConnection, error);}
);
std::cout << "Listening " << mAcceptor.local_endpoint().address().to_string() << ":" << mAcceptor.local_endpoint().port() << std::endl;
}
// Callback on new client connection
void CTcpServer::HandleAccept(CTcpConnection::pointer aNewConnection, const boost::system::error_code& error)
{
if (!error)
{
aNewConnection->Init();
std::cout << "Connection of client: " << aNewConnection->ToString() << std::endl;
}
StartAccept(); // Accept next client
}