From 042ab95dda5e6013d6e03ddf22cb39c6984c917e Mon Sep 17 00:00:00 2001 From: Kieran Kihn <114803508+kierankihn@users.noreply.github.com> Date: Fri, 28 Nov 2025 22:22:03 +0800 Subject: [PATCH] feat(network): add `NetworkClient` for TCP communication - Introduced `NetworkClient` class for client-side TCP communication. - Implemented `connect`, `send`, and `read` methods for message handling. - Updated `CMakeLists.txt` to include `NetworkClient.cpp` in the build configuration. --- CMakeLists.txt | 1 + src/network/NetworkClient.cpp | 39 +++++++++++++++++++++++++++++++++++ src/network/NetworkClient.h | 39 +++++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 src/network/NetworkClient.cpp create mode 100644 src/network/NetworkClient.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 308d79f..f6e535f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,6 +16,7 @@ add_library(uno-game-lib src/network/Message.cpp src/network/MessageSerializer.cpp src/network/NetworkServer.cpp + src/network/NetworkClient.cpp ) target_link_libraries(uno-game-lib PRIVATE ftxui::screen diff --git a/src/network/NetworkClient.cpp b/src/network/NetworkClient.cpp new file mode 100644 index 0000000..9a11692 --- /dev/null +++ b/src/network/NetworkClient.cpp @@ -0,0 +1,39 @@ +/** + * @file NetworkClient.cpp + * + * @author Yuzhe Guo + * @date 2025.11.28 + */ +#include "NetworkClient.h" + +#include +#include +#include +#include + +namespace UNO::NETWORK { + NetworkClient::NetworkClient(std::function callback) : socket_(io_context_), callback_(std::move(callback)) {} + + void NetworkClient::connect(const std::string &host, uint16_t port) + { + asio::ip::tcp::resolver resolver(io_context_); + auto endpoints = resolver.resolve(host, std::to_string(port)); + asio::connect(socket_, endpoints.begin(), endpoints.end()); + } + + void NetworkClient::send(const std::string &message) + { + size_t length = message.size(); + asio::write(this->socket_, asio::buffer(&length, sizeof(length))); + asio::write(this->socket_, asio::buffer(message)); + } + + std::string NetworkClient::read() + { + size_t length; + asio::read(this->socket_, asio::buffer(&length, sizeof(length))); + std::vector buffer(length); + asio::read(this->socket_, asio::buffer(buffer)); + return {buffer.begin(), buffer.end()}; + } +} // namespace UNO::NETWORK diff --git a/src/network/NetworkClient.h b/src/network/NetworkClient.h new file mode 100644 index 0000000..0cc9357 --- /dev/null +++ b/src/network/NetworkClient.h @@ -0,0 +1,39 @@ +/** + * @file NetworkClient.h + * + * @author Yuzhe Guo + * @date 2025.11.28 + */ +#pragma once +#include +#include + +namespace UNO::NETWORK { + + class NetworkClient { + private: + asio::io_context io_context_; + asio::ip::tcp::socket socket_; + std::function callback_; + + public: + explicit NetworkClient(std::function callback); + + /** + * 连接到服务端 + * @param host 服务端地址 + * @param port 服务端端口 + */ + void connect(const std::string &host, uint16_t port); + + /** + * 向服务端发送消息 + * @param message 要发送的消息 + */ + void send(const std::string &message); + + [[nodiscard]] std::string read(); + }; + +} // namespace UNO::NETWORK +