This commit is contained in:
aozhiwei 2024-12-22 09:21:50 +08:00
parent b684e835d7
commit f658c698a0
4 changed files with 37 additions and 8 deletions

View File

@ -71,7 +71,7 @@ namespace f8
return connected_;
}
void AsioTcpClient::SendBuff(const char* buff, unsigned int bufflen)
void AsioTcpClient::SendBuff(const char* buff, int bufflen)
{
//a8::XPrintf("SendBuff bufflen:%d\n", {bufflen});
if (bufflen > 0) {
@ -149,8 +149,8 @@ namespace f8
[this](std::error_code ec, std::size_t bytes_transferred)
{
if (!ec) {
if (on_socketread) {
on_socketread(this, buffer_.data(), bytes_transferred);
if (on_read) {
on_read(this, buffer_.data(), bytes_transferred);
}
DoRead();
} else {
@ -214,4 +214,5 @@ asio::buffer(buf, buf_len),
}
}
}

View File

@ -15,7 +15,7 @@ namespace f8
std::function<void (f8::internal::AsioTcpClient*, int)> on_error;
std::function<void (f8::internal::AsioTcpClient*)> on_connect;
std::function<void (f8::internal::AsioTcpClient*, int)> on_disconnect;
std::function<void (f8::internal::AsioTcpClient*, char*, unsigned int)> on_socketread;
std::function<void (f8::internal::AsioTcpClient*, char*, int)> on_read;
AsioTcpClient(std::shared_ptr<asio::io_context> io_context,
const std::string& remote_ip,
@ -29,7 +29,7 @@ namespace f8
void Close();
bool IsActive();
bool Connected();
void SendBuff(const char* buff, unsigned int bufflen);
void SendBuff(const char* buff, int bufflen);
private:
void Init(std::shared_ptr<asio::io_context> io_context,

View File

@ -9,6 +9,34 @@ namespace f8
TcpClient::TcpClient(const std::string& remote_ip, int remote_port)
{
impl_ = std::make_shared<TcpClientImpl>(remote_ip, remote_port);
impl_->on_error =
[this] (f8::internal::AsioTcpClient*, int err_code)
{
if (on_error) {
on_error(this, err_code);
}
};
impl_->on_connect =
[this] (f8::internal::AsioTcpClient*)
{
if (on_connect) {
on_connect(this);
}
};
impl_->on_disconnect =
[this] (f8::internal::AsioTcpClient*, int err_code)
{
if (on_disconnect) {
on_disconnect(this, err_code);
}
};
impl_->on_read =
[this] (f8::internal::AsioTcpClient*, char* buff, int buff_len)
{
if (on_read) {
on_read(this, buff, buff_len);
}
};
}
TcpClient::~TcpClient()
@ -31,7 +59,7 @@ namespace f8
return impl_->Connected();
}
void TcpClient::SendBuff(const char* buff, unsigned int bufflen)
void TcpClient::SendBuff(const char* buff, int bufflen)
{
impl_->SendBuff(buff, bufflen);
}

View File

@ -19,7 +19,7 @@ namespace f8
std::function<void (f8::TcpClient*, int)> on_error;
std::function<void (f8::TcpClient*)> on_connect;
std::function<void (f8::TcpClient*, int)> on_disconnect;
std::function<void (f8::TcpClient*, char*, unsigned int)> on_socketread;
std::function<void (f8::TcpClient*, char*, int)> on_read;
TcpClient(const std::string& remote_ip, int remote_port);
virtual ~TcpClient();
@ -29,7 +29,7 @@ namespace f8
void Open();
void Close();
bool Connected();
void SendBuff(const char* buff, unsigned int bufflen);
void SendBuff(const char* buff, int bufflen);
private:
std::shared_ptr<TcpClientImpl> impl_;