34 lines
1 KiB
C++
34 lines
1 KiB
C++
#include "NetworkService.h"
|
|
#include "Logger.h"
|
|
#include "Session.h"
|
|
|
|
NetworkService::NetworkService(boost::asio::io_context &io_context,
|
|
uint16_t port, int threadCount)
|
|
: io_context_(io_context),
|
|
acceptor_(io_context_, tcp::endpoint(tcp::v4(), port)),
|
|
work_guard_(boost::asio::make_work_guard(io_context_)) {
|
|
threads_.reserve(threadCount);
|
|
for (int i = 0; i < threadCount; ++i) {
|
|
threads_.emplace_back([this]() { io_context_.run(); });
|
|
}
|
|
}
|
|
|
|
NetworkService::~NetworkService() { Stop(); }
|
|
|
|
void NetworkService::Run() {
|
|
Logger::Log("서버가 포트 ", acceptor_.local_endpoint().port(),
|
|
"에서 리스닝을 시작했습니다.");
|
|
DoAccept();
|
|
}
|
|
|
|
void NetworkService::Stop() { io_context_.stop(); }
|
|
|
|
void NetworkService::DoAccept() {
|
|
acceptor_.async_accept(
|
|
[this](boost::system::error_code ec, tcp::socket socket) {
|
|
if (!ec) {
|
|
std::make_shared<Session>(std::move(socket))->Start();
|
|
}
|
|
DoAccept();
|
|
});
|
|
}
|