#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); /*! * \brief async_connect Asynchronously connects to next_host:port and calls on_connect * \param next_host The host to connect to * \param next_port The port to connect to * \param on_connect The callback to call on a succes. */ void async_connect(std::string next_host, std::string next_port, std::function on_connect); /*! * \brief send sends the string prefixed with it's length over the socket. * \param message The string to be sent. */ 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); /*! * \brief close Closes the underlying socket. */ void close(); /*! * \brief on_done sets the done callback. * \param f The new done callback function. */ void on_done(OnDoneFT f); };