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

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

@ -31,6 +31,16 @@ namespace azh
else 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) Connection::pointer Connection::create(boost::asio::io_context &ioContext)

View File

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

View File

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