diff --git a/lesson_04/connection/connection.cpp b/lesson_04/connection/connection.cpp index defabb3..f2c1a59 100644 --- a/lesson_04/connection/connection.cpp +++ b/lesson_04/connection/connection.cpp @@ -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) diff --git a/lesson_04/server/tcp_server.cpp b/lesson_04/server/tcp_server.cpp index 850fc2a..0b26b9a 100644 --- a/lesson_04/server/tcp_server.cpp +++ b/lesson_04/server/tcp_server.cpp @@ -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(); }); diff --git a/lesson_04/server/tcp_server.hpp b/lesson_04/server/tcp_server.hpp index c5f34d8..aa400a1 100644 --- a/lesson_04/server/tcp_server.hpp +++ b/lesson_04/server/tcp_server.hpp @@ -8,6 +8,8 @@ #pragma once #include +#include +#include namespace azh { @@ -27,6 +29,7 @@ namespace azh int _port; boost::asio::io_context _ioContext; boost::asio::ip::tcp::acceptor _acceptor; + std::vector _connections; private: void startAccept(); };