diff options
Diffstat (limited to 'libcmix-network')
| -rw-r--r-- | libcmix-network/acceptor.hpp | 32 | ||||
| -rw-r--r-- | libcmix-network/client.hpp | 48 | ||||
| -rw-r--r-- | libcmix-network/connect.cpp | 4 | ||||
| -rw-r--r-- | libcmix-network/connect.hpp | 21 | ||||
| -rw-r--r-- | libcmix-network/nodeclient.hpp | 29 | ||||
| -rw-r--r-- | libcmix-network/server.hpp | 38 | ||||
| -rw-r--r-- | libcmix-network/uriparser.hpp | 39 |
7 files changed, 194 insertions, 17 deletions
diff --git a/libcmix-network/acceptor.hpp b/libcmix-network/acceptor.hpp index 7d8e0a2..06b709d 100644 --- a/libcmix-network/acceptor.hpp +++ b/libcmix-network/acceptor.hpp @@ -4,15 +4,47 @@ #include <functional> +/*! + * \file + */ + +/*! + * \brief The Acceptor class + * + * Encapsulates an endpoint and an acceptor. + * Otherwise they would be side to side in the Server class. + */ class Acceptor{ boost::asio::ip::tcp::acceptor acceptor; boost::asio::ip::tcp::endpoint endpoint; public: + /*! + * \brief Acceptor + * \param io_service The io_service to associate with + * \param address The listen address + * \param port The listen port + */ Acceptor(boost::asio::io_service& io_service, boost::asio::ip::address address, uint16_t port); + /*! + * \brief get_address + * \return The listening address + */ boost::asio::ip::address get_address() { return endpoint.address(); } + /*! + * \brief bind_v6_and_v4_any + * \param accept_handler Function to call when accepting an incoming connection. + * + * If you want to accept on both ipv6 and ipv any you have to do some special setup. + * This function handles that and starts listening. + */ void bind_v6_and_v4_any(std::function<void(boost::asio::ip::tcp::socket&&)> accept_handler); + + /*! + * \brief setup_listen_socket + * \param accept_handler Function to call when accepting an incoming connection. + */ void setup_listen_socket(std::function<void(boost::asio::ip::tcp::socket&&)> accept_handler); }; diff --git a/libcmix-network/client.hpp b/libcmix-network/client.hpp index 46b989d..981283b 100644 --- a/libcmix-network/client.hpp +++ b/libcmix-network/client.hpp @@ -5,22 +5,66 @@ #include <array> +/*! + * \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<void(void)> OnDoneFT; + + /*! + * \brief MessageHandler Function type of the function handling incoming messages. + */ typedef std::function<void(std::vector<uint8_t>)> MessageHandler; protected: + /*! + * \brief socket The socket connection this instance handles. + */ boost::asio::ip::tcp::socket socket; private: + /*! + * \brief buffer Internal private buffer used to receive messages. + */ boost::asio::streambuf buffer; + + /*! + * \brief done The done function being called when this is done operating. + */ OnDoneFT done; + /*! + * \brief handle_receive + * \param message_handler The function to call when a message has been received. + * \param ec A possible error that occured during receiving. + * \param read_bytes The number of bytes read. + */ + void handle_receive(MessageHandler message_handler, boost::system::error_code const& ec, size_t read_bytes); + 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 handle_receive(MessageHandler message_handler, boost::system::error_code const& ec, size_t read_bytes); + + /*! + * \brief receive + * \param message_handler The function to call when a message has been received. + */ void receive(MessageHandler message_handler); + + /*! + * \brief on_done sets the done callback. + * \param f The new done callback function. + */ void on_done(OnDoneFT f); }; diff --git a/libcmix-network/connect.cpp b/libcmix-network/connect.cpp index bf200d5..f25ae24 100644 --- a/libcmix-network/connect.cpp +++ b/libcmix-network/connect.cpp @@ -8,11 +8,11 @@ using namespace boost::asio::ip; using boost::asio::io_service; -boost::asio::ip::tcp::socket connect(std::string host, io_service& io_service) { +boost::asio::ip::tcp::socket connect(std::string host, std::string port, io_service& io_service) { boost::asio::ip::basic_resolver<tcp> resolver(io_service); - boost::asio::ip::basic_resolver_query<tcp> query(host, ""); + boost::asio::ip::basic_resolver_query<tcp> query(host, port); for(auto it = resolver.resolve(query); it != boost::asio::ip::tcp::resolver::iterator(); ++it) { diff --git a/libcmix-network/connect.hpp b/libcmix-network/connect.hpp index 071ed9b..0a58115 100644 --- a/libcmix-network/connect.hpp +++ b/libcmix-network/connect.hpp @@ -3,5 +3,24 @@ #include <boost/asio/ip/tcp.hpp> #include <boost/asio/io_service.hpp> -boost::asio::ip::tcp::socket connect(std::string host, boost::asio::io_service& io_service); +/*! + * \file + */ + +/*! + * \brief connect Connects to the supplied endpoint and returns the socket + * \param host The host + * \param port The port + * \param io_service The io_service used for the socket being returned. + * \return + */ +boost::asio::ip::tcp::socket connect(std::string host, std::string port, boost::asio::io_service& io_service); + +/*! + * \brief async_connect Asynchronously connects to the supplied endpoint. + * \param socket The socket on which to invoke the asynchronous connect. + * \param host The host + * \param next_port The port + * \param on_connect The function to call when the connect has succeeded. + */ void async_connect(boost::asio::ip::tcp::socket& socket, std::string host, std::string next_port, std::function<void()> on_connect); diff --git a/libcmix-network/nodeclient.hpp b/libcmix-network/nodeclient.hpp index 9a3799a..ca1ee67 100644 --- a/libcmix-network/nodeclient.hpp +++ b/libcmix-network/nodeclient.hpp @@ -6,15 +6,44 @@ #include <vector> +/*! + * \file + */ + +/*! + * \brief The NodeClient class + */ class NodeClient { + /*! + * \brief client + */ Client client; + + /*! + * \brief handle_message + * \param message + */ void handle_message(std::vector<uint8_t> message); public: + + /*! + * \brief NodeClient + * \param socket + */ NodeClient(boost::asio::ip::tcp::socket&& socket); + virtual ~NodeClient() = default; + /*! + * \brief receive + */ void receive(); + + /*! + * \brief on_done + * \param done + */ void on_done(Client::OnDoneFT done); }; diff --git a/libcmix-network/server.hpp b/libcmix-network/server.hpp index 942cb14..2e5b272 100644 --- a/libcmix-network/server.hpp +++ b/libcmix-network/server.hpp @@ -4,26 +4,54 @@ #include "acceptor.hpp" +/*! + * \file + */ + +/*! + * \brief The ListenSettings struct + */ struct ListenSettings { - bool enable_ipv4; - std::string ipv4_inaddr; - bool enable_ipv6; - std::string ipv6_inaddr; - uint16_t port; + bool enable_ipv4; ///< Should we listen on ipv4 + std::string ipv4_inaddr; ///< listen on this ipv4 address + bool enable_ipv6; ///< Should we listen on ipv6 + std::string ipv6_inaddr; ///< listen on this ipv6 address + uint16_t port; ///< listen on this port. }; +/*! + * \brief The Server class + */ class Server { public: + /*! + * \brief AcceptHandler + */ typedef std::function<void(boost::asio::ip::tcp::socket&& socket)> AcceptHandler; private: + /*! + * \brief listen_settings + */ ListenSettings const& listen_settings; + /*! + * \brief v4_acceptor + */ Acceptor v4_acceptor; + /*! + * \brief v6_acceptor + */ Acceptor v6_acceptor; public: + /*! + * \brief Server + * \param io_service The io_service this server will use for all its connections. + * \param listen_settings The parameters used for determining on what endpoints to listen. + * \param accept_handler The function to call when connections are established. + */ Server(boost::asio::io_service& io_service, ListenSettings const& listen_settings, AcceptHandler accept_handler); }; diff --git a/libcmix-network/uriparser.hpp b/libcmix-network/uriparser.hpp index c46b70a..dd439fb 100644 --- a/libcmix-network/uriparser.hpp +++ b/libcmix-network/uriparser.hpp @@ -4,16 +4,31 @@ #include <regex> #include <iostream> +/*! + * \file + */ + +/*! + * \brief The Uri struct + */ + struct Uri { - std::string scheme; - std::string username; - std::string password; - std::string host; - std::string port; - std::string query; - std::string hash; + std::string scheme; ///< The URI scheme + std::string username; ///< The URI username + std::string password; ///< The URI password + std::string host; ///< The URI host + std::string port; ///< The URI port + std::string query; ///< The URI query + std::string hash; ///< The URI hash }; +/*! + * \brief parse_uri + * \param str The string that contains a URI. + * It must be a valid URI, this function will to do no validation. + * There may be no leading or trailing whitespace + * \return The corresponding URI struct. + */ Uri parse_uri(std::string str) { std::string scheme = "(?:(.+?)://)?"; std::string uname_pass = "(?:(.*?)?(?::(.*?))@)?"; @@ -37,6 +52,11 @@ Uri parse_uri(std::string str) { }; } +/*! + * \brief debug_uri Debug function to see the contents of the URI struct. + * \param uri + * \return A Debug string representation of an URI + */ std::string debug_uri(Uri uri) { std::stringstream ss; ss << "scheme: " << uri.scheme << std::endl @@ -49,6 +69,11 @@ std::string debug_uri(Uri uri) { return ss.str(); } +/*! + * \brief uri_to_string + * \param uri + * \return returns the string representation of the URI stored in uri. + */ std::string uri_to_string(Uri uri) { return (!uri.scheme.empty() ? uri.scheme + "://" : "") + |
