This commit is contained in:
binbinnomoney 2024-07-26 01:23:27 +08:00
commit 500c338d10

View File

@ -26,8 +26,8 @@
/** /**
* @brief Called when data is received on a TCP connection. * @brief Called when data is received on a TCP connection.
* *
* @param io * @param io Socket handle
* @param buf * @param buf The transmitted data content
* @param len * @param len
*/ */
static void on_message(hio_t *io, void *buf, int len) static void on_message(hio_t *io, void *buf, int len)
@ -38,8 +38,7 @@ static void on_message(hio_t *io, void *buf, int len)
} }
/** /**
* @brief Called when TCP connection is established. * @brief Called when TCP connection is established.
* * @param io Socket handle
* @param io
*/ */
static void on_connect(hio_t *io) static void on_connect(hio_t *io)
{ {
@ -47,6 +46,10 @@ static void on_connect(hio_t *io)
hio_setcb_read(io, on_message); hio_setcb_read(io, on_message);
hio_read(io); hio_read(io);
} }
/**
* @brief Called when tcp connection is disconnected.
* @param io Socket handle
*/
static void on_close(hio_t *io) static void on_close(hio_t *io)
{ {
LogInfo("onclose: connfd=%d error=%d\n", hio_fd(io), hio_error(io)); LogInfo("onclose: connfd=%d error=%d\n", hio_fd(io), hio_error(io));
@ -74,9 +77,9 @@ void TcpClientImpl::Init(void)
return; return;
} }
hevent_set_userdata(io, this); hevent_set_userdata(io, this);
hio_setcb_connect(io, on_connect); hio_setcb_connect(io, on_connect);///Set the callback function of successful connection, and there is no connection operation.
hio_setcb_close(io, on_close); hio_setcb_close(io, on_close);
hio_connect(io); hio_connect(io); ///Connection operation
mIo = io; mIo = io;
std::shared_ptr<TcpClientImpl> impl = std::dynamic_pointer_cast<TcpClientImpl>(shared_from_this()); std::shared_ptr<TcpClientImpl> impl = std::dynamic_pointer_cast<TcpClientImpl>(shared_from_this());
auto recvThread = [](std::shared_ptr<TcpClientImpl> tcpClient) { auto recvThread = [](std::shared_ptr<TcpClientImpl> tcpClient) {
@ -114,7 +117,7 @@ ssize_t TcpClientImpl::Write(const void *buf, const size_t bufLenght)
LogError("mIo is nullptr.\n"); LogError("mIo is nullptr.\n");
return TCP_MODULE_WRITE_ERROR; return TCP_MODULE_WRITE_ERROR;
} }
return hio_write(mIo, buf, bufLenght); return hio_write(mIo, buf, bufLenght); ///Returns the byte length of a packet.If it fails, an error code is returned.
} }
void TcpClientImpl::Closed(void) void TcpClientImpl::Closed(void)
{ {