поддержка соединения

This commit is contained in:
Alexander Zhirov 2022-09-20 17:10:09 +03:00
parent 28e33863e4
commit e8c1ace236
3 changed files with 18 additions and 4 deletions

View File

@ -27,10 +27,20 @@ namespace azh
[strongThis](const boost::system::error_code &error, size_t bytesTransferred)
{
if (error)
std::cerr << "Failed to send message!" << std::endl;
std::cerr << "Failed to send message!" << std::endl;
else
std::cout << "Sent " << bytesTransferred << " bytes" << std::endl;
std::cout << "Sent " << bytesTransferred << " bytes" << std::endl;
});
boost::asio::streambuf buffer;
_socket.async_receive(buffer.prepare(512), [this](const boost::system::error_code &error, size_t bytesTransferred)
{
if (error == boost::asio::error::eof)
std::cout << "Client disconnected properly!" << std::endl;
else if (error)
std::cerr << "Client disconnected in bad way!" << std::endl;
});
}
Connection::pointer Connection::create(boost::asio::io_context &ioContext)

View File

@ -41,12 +41,13 @@ namespace azh
void TCPServer::startAccept()
{
auto connection = Connection::create(_ioContext);
_connections.push_back(connection);
_acceptor.async_accept(connection->socket(), [connection, this](const boost::system::error_code &error)
{
if (!error)
{
connection->start();
}
startAccept();
});

View File

@ -8,6 +8,8 @@
#pragma once
#include <boost/asio.hpp>
#include <connection/connection.hpp>
#include <vector>
namespace azh
{
@ -27,6 +29,7 @@ namespace azh
int _port;
boost::asio::io_context _ioContext;
boost::asio::ip::tcp::acceptor _acceptor;
std::vector<Connection::pointer> _connections;
private:
void startAccept();
};