/* * tcp_connection.hpp * * Created on: 20 сент. 2022 г. * Author: alexander */ #pragma once #include #include #include namespace azh { using boost::asio::ip::tcp; namespace io = boost::asio; using MessageHandler = std::function; using ErrorHandler = std::function; class TCPConnection : public std::enable_shared_from_this { public: using Pointer = std::shared_ptr; static Pointer create(io::ip::tcp::socket&& socket); inline const std::string& getUsername() const { return _username; } tcp::socket& socket(); void start(MessageHandler&& messageHandler, ErrorHandler&& errorHandler); void post(const std::string& message); private: tcp::socket _socket; std::string _username; std::queue _outgoingMessages; io::streambuf _streamBuf {65536}; MessageHandler _messageHandler; ErrorHandler _errorHandler; private: explicit TCPConnection(io::ip::tcp::socket&& socket); void asyncRead(); void onRead(boost::system::error_code &ec, size_t bytesTransferred); void asyncWrite(); void onWrite(boost::system::error_code &ec, size_t bytesTransferred); }; }