Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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}
Expand Down
7 changes: 7 additions & 0 deletions include/tcpcat/base/TcpClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ class TcpClient
/// @return Returns the number of bytes sent.
size_t Send(const std::vector<unsigned char> &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<unsigned char> &data);
Expand Down
9 changes: 9 additions & 0 deletions src/TcpClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,15 @@ size_t TcpClient::Send(const std::vector<unsigned char> &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<unsigned char> &data)
{
session_->SendAsync(data, 0, data.size());
Expand Down
5 changes: 5 additions & 0 deletions test/CommonTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<tcpcat::TcpSession> 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<tcpcat::TcpSession> session) override
{
std::cout << "Disconnected from server: " << session->RemoteEndpoint().address().to_string() << " : "
Expand Down
21 changes: 15 additions & 6 deletions test/TcpClient/TEST_TcpClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,14 @@ class Test003Handler : public tcpcat::EventHandler
void OnConnected(std::shared_ptr<tcpcat::TcpSession> 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<tcpcat::TcpSession> 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<tcpcat::TcpSession> session, const asio::error_code &err) override
Expand All @@ -84,8 +82,19 @@ TEST(Client, TEST_004)
tcpcat::TcpClient client("tcpbin.com", 4242, std::make_shared<ClientHandler>());
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<unsigned char>(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<ClientHandler>());
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));
}