-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
132 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#pragma once | ||
|
||
#include <cstdint> | ||
#include <functional> | ||
#include <thread> | ||
#include <memory> | ||
#include <string> | ||
#include <vector> | ||
|
||
#include <asio/io_context.hpp> | ||
#include <asio/ip/tcp.hpp> | ||
#include <coroutine.h> | ||
#include <message.h> | ||
#include <user.h> | ||
|
||
#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<socket_messages::message> const& message) const; | ||
|
||
/// @brief Register a callback for when a message is received. | ||
/// @return A reference to the client. | ||
client& on_message(std::function<async_task( | ||
std::shared_ptr<socket_messages::message> | ||
)> 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<async_task( | ||
std::shared_ptr<socket_messages::message> | ||
)> | ||
> on_message_callbacks; | ||
|
||
asio::io_context io_context; | ||
std::unique_ptr<client_connection> connection; | ||
user user_data; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#include <cstdint> | ||
#include <functional> | ||
#include <memory> | ||
#include <string> | ||
#include <thread> | ||
|
||
#include <coroutine.h> | ||
#include <message.h> | ||
#include <user.h> | ||
|
||
#include "client.h" | ||
#include "client_connection.h" | ||
|
||
namespace pine | ||
{ | ||
client::client(std::string username) | ||
: connection{ std::make_unique<client_connection>(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<socket_messages::message> const& message) const | ||
{ | ||
co_await this->connection->send_message(message); | ||
} | ||
|
||
client& client::on_message(std::function<async_task( | ||
std::shared_ptr<socket_messages::message> | ||
)> const& on_message) | ||
{ | ||
on_message_callbacks.push_back(on_message); | ||
|
||
return *this; | ||
} | ||
|
||
constexpr user const& client::get_user() const | ||
{ | ||
return user_data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters