#pragma once #include #include #include /*! * \file */ /*! * \brief The Client class */ class Client { public: /*! * \brief OnDoneFT Function type of the function being called when this instance is done operating. */ typedef std::function OnDoneFT; /*! * \brief MessageHandler Function type of the function handling incoming messages. */ typedef std::function)> MessageHandler; protected: /*! * \brief socket The socket connection this instance handles. */ boost::asio::ip::tcp::socket socket; private: boost::asio::streambuf buffer; OnDoneFT done; std::vector received_bytes_to_vector(size_t read_bytes); void handle_receive_size(MessageHandler message_handler, boost::system::error_code const& ec, size_t read_bytes); void handle_receive_message(MessageHandler message_handler, boost::system::error_code const& ec, size_t read_bytes); std::array prepare_length_prefix(uint32_t length); public: /*! * \brief Client * \param socket An rvalue reference to a socket it will now own and receive from. */ Client(boost::asio::ip::tcp::socket&& socket); void async_connect(std::string next_host, std::string next_port, std::function on_connect); void send(std::string message); /*! * \brief receive * \param message_handler The function to call when a message has been received. */ void receive(MessageHandler message_handler); void close(); /*! * \brief on_done sets the done callback. * \param f The new done callback function. */ void on_done(OnDoneFT f); };