55 lines
1.1 KiB
C++
55 lines
1.1 KiB
C++
/*
|
||
* server.cpp
|
||
*
|
||
* Created on: 20 сент. 2022 г.
|
||
* Author: alexander
|
||
*/
|
||
|
||
#include <server/tcp_server.hpp>
|
||
#include <connection/connection.hpp>
|
||
#include <iostream>
|
||
|
||
namespace azh
|
||
{
|
||
using boost::asio::ip::tcp;
|
||
|
||
TCPServer::TCPServer(IPV ipv, int port) : _ipVersion(ipv), _port(port), _acceptor(_ioContext,
|
||
tcp::endpoint(_ipVersion == IPV::V4 ? tcp::v4() : tcp::v6(), _port))
|
||
{
|
||
}
|
||
|
||
TCPServer::~TCPServer()
|
||
{
|
||
|
||
}
|
||
|
||
int TCPServer::run()
|
||
{
|
||
try
|
||
{
|
||
startAccept();
|
||
_ioContext.run();
|
||
}
|
||
catch (std::exception &e)
|
||
{
|
||
std::cerr << e.what() << std::endl;
|
||
return -1;
|
||
}
|
||
return 0;
|
||
}
|
||
|
||
void TCPServer::startAccept()
|
||
{
|
||
auto connection = Connection::create(_ioContext);
|
||
_acceptor.async_accept(connection->socket(), [connection, this](const boost::system::error_code &error)
|
||
{
|
||
if (!error)
|
||
{
|
||
connection->start();
|
||
}
|
||
|
||
startAccept();
|
||
});
|
||
}
|
||
}
|