Skip to content

Commit

Permalink
Implement client class
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacquwes committed Sep 28, 2023
1 parent 1b39c57 commit 73f7afa
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 1 deletion.
65 changes: 65 additions & 0 deletions client/include/client.h
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;
};
}
4 changes: 4 additions & 0 deletions client/include/client_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
60 changes: 60 additions & 0 deletions client/src/client.cpp
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;
}
}
4 changes: 3 additions & 1 deletion client/src/client_connection.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <cstdint>
#include <string>

#include <asio.hpp>
#include <coroutine.h>
#include <connection.h>

#include "client_connection.h"

Expand Down

0 comments on commit 73f7afa

Please sign in to comment.