2024-12-11 10:22:14 +08:00
|
|
|
#include "net_base.h"
|
|
|
|
|
2024-12-11 23:23:48 +08:00
|
|
|
CTcpClient::CTcpClient(asio::io_context& io_context, const std::shared_ptr<spdlog::logger>& logger)
|
2024-12-11 22:51:43 +08:00
|
|
|
: logger_(logger), io_context_(io_context), socket_(io_context_)
|
2024-12-11 10:22:14 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-12-11 23:23:48 +08:00
|
|
|
CTcpClient::~CTcpClient()
|
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);
|
|
|
|
logger_->info("Connected to {}:{}", host, port);
|
|
|
|
return true;
|
|
|
|
} catch (const std::exception& ex) {
|
|
|
|
logger_->error("Connection failed: {}", ex.what());
|
|
|
|
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 {
|
|
|
|
socket_.close();
|
|
|
|
logger_->info("Disconnected.");
|
|
|
|
} catch (const std::exception& ex) {
|
|
|
|
logger_->error("Error during disconnection: {}", ex.what());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-11 23:23:48 +08:00
|
|
|
bool CTcpClient::send(const char* data, int len)
|
2024-12-11 10:22:14 +08:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
auto send_size = asio::write(socket_, asio::buffer(data, len));
|
|
|
|
logger_->info("Need Send len: {} Real Send len: {}", len, send_size);
|
|
|
|
return static_cast<int>(send_size) == len;
|
|
|
|
} catch (const std::exception& ex) {
|
|
|
|
logger_->error("Send failed: {}", ex.what());
|
|
|
|
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-12 23:11:55 +08:00
|
|
|
// logger_->debug("{} {}", __FUNCTION__, ec.message());
|
2024-12-13 12:35:08 +08:00
|
|
|
if (ec) {
|
|
|
|
if (ec == asio::error::eof) {
|
|
|
|
logger_->error("Remote Server Closed.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
async_recv();
|
|
|
|
} else {
|
2024-12-11 22:51:43 +08:00
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
|
|
buffer_.push(tmp_buf_.data(), length);
|
|
|
|
auto* frame = CTransProtocal::parse(buffer_);
|
|
|
|
if (frame) {
|
|
|
|
if (fun_) {
|
|
|
|
fun_(frame);
|
|
|
|
}
|
|
|
|
delete frame;
|
|
|
|
}
|
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
|
|
|
}
|