From 73f7afa1164676315a5e666d6ac126c4c0125da7 Mon Sep 17 00:00:00 2001 From: Paul Hariel Date: Thu, 28 Sep 2023 13:18:46 +0200 Subject: [PATCH] Implement client class --- client/include/client.h | 65 ++++++++++++++++++++++++++++++ client/include/client_connection.h | 4 ++ client/src/client.cpp | 60 +++++++++++++++++++++++++++ client/src/client_connection.cpp | 4 +- 4 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 client/include/client.h create mode 100644 client/src/client.cpp diff --git a/client/include/client.h b/client/include/client.h new file mode 100644 index 0000000..068853b --- /dev/null +++ b/client/include/client.h @@ -0,0 +1,65 @@ +#pragma once + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "client_connection.h" + +namespace pine +{ + /// @brief A client that can connect to a single server. + class client + { + friend class client_connection; + public: + /// @brief Initialize a client. + /// @param username: The username of the client. + client(std::string username); + + /// @brief Connect to a server. + /// @param host: Hostname or ip address of the server. + /// @param port: TCP port of the server. + /// @return True if the connection was successful, false otherwise. + bool connect(std::string const& host = "localhost", uint16_t const& port = 80); + + /// @brief Disconnect from the server. + void disconnect(); + + /// @brief Send a message to the server. + async_task message_server(std::shared_ptr const& message) const; + + /// @brief Register a callback for when a message is received. + /// @return A reference to the client. + client& on_message(std::function + )> const& on_message); + + /// @brief Get the user data of the client. + [[nodiscard]] + constexpr user const& get_user() const; + + private: + asio::ip::tcp::socket socket; + std::jthread connection_thread; + + std::vector< + std::function + )> + > on_message_callbacks; + + asio::io_context io_context; + std::unique_ptr connection; + user user_data; + }; +} diff --git a/client/include/client_connection.h b/client/include/client_connection.h index dfa32d2..ae323cc 100644 --- a/client/include/client_connection.h +++ b/client/include/client_connection.h @@ -18,6 +18,10 @@ namespace pine bool connect(std::string const& host, uint16_t const& port = 80); + void disconnect(); + + async_task listen(); + asio::error_code ec; private: diff --git a/client/src/client.cpp b/client/src/client.cpp new file mode 100644 index 0000000..2fecaae --- /dev/null +++ b/client/src/client.cpp @@ -0,0 +1,60 @@ +#include +#include +#include +#include +#include + +#include +#include +#include + +#include "client.h" +#include "client_connection.h" + +namespace pine +{ + client::client(std::string username) + : connection{ std::make_unique(socket) }, + user_data{ username }, + io_context{}, + socket{ io_context } + {} + + bool client::connect(std::string const& host, uint16_t const& port) + { + bool result = connection->connect(host, port); + + if (!result) + return false; + + connection_thread = std::jthread{ [this]() { + connection->listen(); + } }; + + return true; + } + + void client::disconnect() + { + connection->disconnect(); + } + + async_task client::message_server(std::shared_ptr const& message) const + { + co_await this->connection->send_message(message); + } + + client& client::on_message(std::function + )> const& on_message) + { + on_message_callbacks.push_back(on_message); + + return *this; + } + + constexpr user const& client::get_user() const + { + return user_data; + } +} diff --git a/client/src/client_connection.cpp b/client/src/client_connection.cpp index 33f60d4..1251ad4 100644 --- a/client/src/client_connection.cpp +++ b/client/src/client_connection.cpp @@ -1,6 +1,8 @@ +#include +#include #include -#include +#include #include "client_connection.h"