From 2859e7a9f0529a937e1b2fbb761bf07bdbfd15c5 Mon Sep 17 00:00:00 2001 From: ydcpp Date: Sun, 9 Mar 2025 00:13:41 +0300 Subject: [PATCH] added Send function overload to pass pointer to data --- CMakeLists.txt | 2 +- include/tcpcat/base/TcpClient.h | 7 +++++++ src/TcpClient.cpp | 9 +++++++++ test/CommonTypes.h | 5 +++++ test/TcpClient/TEST_TcpClient.cpp | 21 +++++++++++++++------ 5 files changed, 37 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index eba39c9..37d0d66 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.15) -set(PROJECT_VER 1.0.4) +set(PROJECT_VER 1.0.5) set(PROJECT_NAME "ydcpp-tcpcat") project(${PROJECT_NAME} LANGUAGES CXX VERSION ${PROJECT_VER} diff --git a/include/tcpcat/base/TcpClient.h b/include/tcpcat/base/TcpClient.h index a167c11..d771322 100644 --- a/include/tcpcat/base/TcpClient.h +++ b/include/tcpcat/base/TcpClient.h @@ -78,6 +78,13 @@ class TcpClient /// @return Returns the number of bytes sent. size_t Send(const std::vector &data, size_t offset, size_t size); + /// @brief Sends data to host. EventHandler->OnSent will be called on success. + /// @param data Buffer that holds the data to be sent. + /// @param offset Offset value from the beginning of `data`. + /// @param size Count of bytes to be sent starting from `offset`. + /// @return Returns the number of bytes sent. + size_t Send(const unsigned char *data, size_t offset, size_t size); + /// @brief Sends data to host with non-blocking operation. EventHandler->OnSent will be called on success. /// @param data Buffer that holds the data to be sent. void SendAsync(const std::vector &data); diff --git a/src/TcpClient.cpp b/src/TcpClient.cpp index e5215c5..18a7761 100644 --- a/src/TcpClient.cpp +++ b/src/TcpClient.cpp @@ -104,6 +104,15 @@ size_t TcpClient::Send(const std::vector &data, size_t offset, si return session_->Send(data, offset, size); } +size_t TcpClient::Send(const unsigned char *data, size_t offset, size_t size) +{ + if (!session_->IsConnected() || !data) { + return 0; + } + + return session_->Send(data, offset, size); +} + void TcpClient::SendAsync(const std::vector &data) { session_->SendAsync(data, 0, data.size()); diff --git a/test/CommonTypes.h b/test/CommonTypes.h index 97c8789..60e4980 100644 --- a/test/CommonTypes.h +++ b/test/CommonTypes.h @@ -74,6 +74,11 @@ class ClientHandler : public tcpcat::EventHandler std::cout << "Message sent to server: " << std::string(buf.begin(), buf.begin() + bytes) << '\n'; } + void OnSent(std::shared_ptr session, const unsigned char *buf, size_t bytes) override + { + std::cout << "Message sent to server: " << std::string((const char *)buf) << '\n'; + } + void OnDisconnected(std::shared_ptr session) override { std::cout << "Disconnected from server: " << session->RemoteEndpoint().address().to_string() << " : " diff --git a/test/TcpClient/TEST_TcpClient.cpp b/test/TcpClient/TEST_TcpClient.cpp index dcaecd6..2a3c1ec 100644 --- a/test/TcpClient/TEST_TcpClient.cpp +++ b/test/TcpClient/TEST_TcpClient.cpp @@ -54,16 +54,14 @@ class Test003Handler : public tcpcat::EventHandler void OnConnected(std::shared_ptr session) override { std::cout << "Test003Handler connected.\n"; - std::cout << "Server: " << session->RemoteEndpoint().address().to_string() << " : " << session->RemoteEndpoint().port() - << '\n'; + std::cout << "Server: " << session->RemoteEndpoint().address().to_string() << " : " << session->RemoteEndpoint().port() << '\n'; session->Close(); } void OnDisconnected(std::shared_ptr session) override { std::cout << "Test003Handler disconnected.\n"; - std::cout << "Server: " << session->RemoteEndpoint().address().to_string() << " : " - << session->RemoteEndpoint().port() << '\n'; + std::cout << "Server: " << session->RemoteEndpoint().address().to_string() << " : " << session->RemoteEndpoint().port() << '\n'; } void OnError(std::shared_ptr session, const asio::error_code &err) override @@ -84,8 +82,19 @@ TEST(Client, TEST_004) tcpcat::TcpClient client("tcpbin.com", 4242, std::make_shared()); ASSERT_NO_THROW(client.Connect()); if (client.IsConnected()) { - const std::string msg = "hello tcpcat"; + const std::string msg = "hello tcpcat TEST_004"; client.Send(std::vector(msg.begin(), msg.end())); } - std::this_thread::sleep_for(std::chrono::seconds(2)); + std::this_thread::sleep_for(std::chrono::seconds(1)); +} + +TEST(Client, TEST_005) +{ + tcpcat::TcpClient client("tcpbin.com", 4242, std::make_shared()); + ASSERT_NO_THROW(client.Connect()); + if (client.IsConnected()) { + const char *data = "hello tcpcat TEST_005"; + client.Send((const unsigned char *)data, 0, strlen(data)); + } + std::this_thread::sleep_for(std::chrono::seconds(1)); }