2024-12-11 10:22:14 +08:00
|
|
|
#include "net_base.h"
|
|
|
|
|
2025-03-01 03:49:40 +08:00
|
|
|
CTcpClient::CTcpClient(asio::io_context& io_context) : io_context_(io_context), socket_(io_context_)
|
2024-12-11 10:22:14 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2025-02-06 09:37:24 +08:00
|
|
|
CTcpClient::~CTcpClient() = default;
|
2024-12-11 10:22:14 +08:00
|
|
|
|
2024-12-11 23:23:48 +08:00
|
|
|
bool CTcpClient::connect(const std::string& host, const std::string& port)
|
2024-12-11 10:22:14 +08:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
asio::ip::tcp::resolver resolver(io_context_);
|
|
|
|
asio::ip::tcp::resolver::results_type endpoints = resolver.resolve(host, port);
|
|
|
|
asio::connect(socket_, endpoints);
|
2025-03-01 03:49:40 +08:00
|
|
|
TLOGI("Connected to {}:{}", host, port);
|
2024-12-19 15:54:42 +08:00
|
|
|
is_normal_ = true;
|
2024-12-11 10:22:14 +08:00
|
|
|
return true;
|
|
|
|
} catch (const std::exception& ex) {
|
2025-03-01 03:49:40 +08:00
|
|
|
TLOGE("Connection failed: {}", ex.what());
|
2024-12-11 10:22:14 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-11 23:23:48 +08:00
|
|
|
void CTcpClient::disconnect()
|
2024-12-11 10:22:14 +08:00
|
|
|
{
|
|
|
|
if (socket_.is_open()) {
|
|
|
|
try {
|
2024-12-19 15:54:42 +08:00
|
|
|
socket_.shutdown(asio::ip::tcp::socket::shutdown_both);
|
2024-12-11 10:22:14 +08:00
|
|
|
socket_.close();
|
2025-03-01 03:49:40 +08:00
|
|
|
TLOGI("Disconnected.");
|
2024-12-11 10:22:14 +08:00
|
|
|
} catch (const std::exception& ex) {
|
2025-03-01 03:49:40 +08:00
|
|
|
TLOGE("Error during disconnection: {}", ex.what());
|
2024-12-11 10:22:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-11 23:23:48 +08:00
|
|
|
bool CTcpClient::send(const char* data, int len)
|
2024-12-11 10:22:14 +08:00
|
|
|
{
|
2024-12-19 15:54:42 +08:00
|
|
|
if (!is_normal_) {
|
2025-03-01 03:49:40 +08:00
|
|
|
TLOGE("abnormal state, will not send.");
|
2024-12-19 15:54:42 +08:00
|
|
|
return false;
|
|
|
|
}
|
2024-12-11 10:22:14 +08:00
|
|
|
try {
|
|
|
|
auto send_size = asio::write(socket_, asio::buffer(data, len));
|
2025-03-01 03:49:40 +08:00
|
|
|
// TLOGI("Need Send len: {} Real Send len: {}", len, send_size);
|
2024-12-11 10:22:14 +08:00
|
|
|
return static_cast<int>(send_size) == len;
|
|
|
|
} catch (const std::exception& ex) {
|
2025-03-01 03:49:40 +08:00
|
|
|
TLOGE("Send failed: {}", ex.what());
|
2024-12-11 10:22:14 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-12 22:43:24 +08:00
|
|
|
void CTcpClient::register_func(ExFun_t&& f)
|
2024-12-11 10:22:14 +08:00
|
|
|
{
|
2024-12-11 22:51:43 +08:00
|
|
|
fun_ = f;
|
|
|
|
}
|
|
|
|
|
2024-12-11 23:23:48 +08:00
|
|
|
void CTcpClient::async_recv()
|
2024-12-11 22:51:43 +08:00
|
|
|
{
|
|
|
|
auto self(shared_from_this());
|
|
|
|
socket_.async_read_some(asio::buffer(tmp_buf_), [this, self](std::error_code ec, std::size_t length) {
|
2024-12-13 12:35:08 +08:00
|
|
|
if (ec) {
|
2024-12-19 15:54:42 +08:00
|
|
|
is_normal_ = false;
|
|
|
|
if (ec.value() == 995) {
|
|
|
|
// 正常中止退出
|
|
|
|
return;
|
2024-12-13 12:35:08 +08:00
|
|
|
}
|
2025-01-06 13:41:15 +08:00
|
|
|
if (ec.value() == 125) {
|
2025-03-01 03:49:40 +08:00
|
|
|
TLOGI("{} exit.", __FUNCTION__);
|
2025-01-06 13:41:15 +08:00
|
|
|
return;
|
|
|
|
}
|
2025-03-01 03:49:40 +08:00
|
|
|
TLOGE("{} {} error => {}", __FUNCTION__, ec.value(), ec.message());
|
2024-12-13 12:35:08 +08:00
|
|
|
} else {
|
2024-12-11 22:51:43 +08:00
|
|
|
buffer_.push(tmp_buf_.data(), length);
|
2024-12-18 12:55:00 +08:00
|
|
|
while (true) {
|
2024-12-15 23:19:56 +08:00
|
|
|
auto* frame = CTransProtocal::parse(buffer_);
|
|
|
|
if (frame) {
|
|
|
|
if (fun_) {
|
|
|
|
fun_(frame);
|
|
|
|
}
|
|
|
|
delete frame;
|
2024-12-18 12:55:00 +08:00
|
|
|
continue;
|
2024-12-11 22:51:43 +08:00
|
|
|
}
|
2024-12-18 12:55:00 +08:00
|
|
|
break;
|
2024-12-11 22:51:43 +08:00
|
|
|
}
|
2024-12-11 23:23:48 +08:00
|
|
|
async_recv();
|
2024-12-11 22:51:43 +08:00
|
|
|
}
|
|
|
|
});
|
2024-12-11 10:22:14 +08:00
|
|
|
}
|
2025-01-13 16:24:08 +08:00
|
|
|
|
2025-02-06 09:37:24 +08:00
|
|
|
bool CTcpClient::is_normal() const
|
2025-01-13 16:24:08 +08:00
|
|
|
{
|
|
|
|
return is_normal_;
|
|
|
|
}
|