diff --git a/utils/TcpModule/src/TcpClientImpl.cpp b/utils/TcpModule/src/TcpClientImpl.cpp index 346ed616..fa3b5a3e 100644 --- a/utils/TcpModule/src/TcpClientImpl.cpp +++ b/utils/TcpModule/src/TcpClientImpl.cpp @@ -23,12 +23,24 @@ #include #include #include +/** + * @brief Called when data is received on a TCP connection. + * + * @param io + * @param buf + * @param len + */ static void on_message(hio_t *io, void *buf, int len) { LogInfo("onmessage: %.*s\n", len, (char *)buf); TcpClientImpl *tcpClient = (TcpClientImpl *)hevent_userdata(io); tcpClient->Readed(buf, len); } +/** + * @brief Called when TCP connection is established. + * + * @param io + */ static void on_connect(hio_t *io) { LogInfo("onconnect: connfd=%d\n", hio_fd(io)); @@ -44,10 +56,14 @@ static void on_close(hio_t *io) TcpClientImpl::TcpClientImpl(const TcpClientParam ¶m, const void *object) : mParam(param), mObjectThis(object) { } +/** + * @brief Initialize TCP clients, create event loops, I/O objects, and set callback functions for connection and closure. + * + */ void TcpClientImpl::Init(void) { constexpr int NO_FALGS = 0; - mLoop = hloop_new(NO_FALGS); + mLoop = hloop_new(NO_FALGS); ///Initialize event loop if (nullptr == mLoop) { LogError("TcpClientImpl::Init hloop_new failed.\n"); return; @@ -68,6 +84,10 @@ void TcpClientImpl::Init(void) }; mTcpClientThread = std::thread(recvThread, impl); } +/** + * @brief De-initialize the TCP client, close the I/O object and wait for the receiving thread to end. + * + */ void TcpClientImpl::UnInit(void) { if (nullptr != mIo) { @@ -108,6 +128,10 @@ void TcpClientImpl::Closed(void) hloop_stop(mLoop); } } +/** + * @brief Run event loop + * + */ void TcpClientImpl::Loop(void) { if (nullptr == mLoop) { @@ -120,7 +144,7 @@ void TcpClientImpl::Loop(void) } std::shared_ptr *NewTcpClient(const TcpClientParam ¶m) { - LogInfo("Create tcp server object.\n"); + LogInfo("Create tcp client object.\n"); TcpClient *impl = (TcpClient *)malloc(sizeof(TcpClient)); if (nullptr == impl) { LogError("NewTcpServer::malloc failed.\n"); @@ -129,8 +153,12 @@ std::shared_ptr *NewTcpClient(const TcpClientParam ¶m) TcpClient tmp; memcpy((void *)impl, (void *)&tmp, sizeof(TcpClient)); impl->mHeader.mCheckName = GetTcpClientModuleName(); + /** + * @brief ObjectThis points to the first address of the impl and offsets the address by ITcpClientHeader bytes, + * that is, skips the mHeader part of the impl. + */ std::shared_ptr *objectThis = (std::shared_ptr *)(((char *)impl) + sizeof(ITcpClientHeader)); impl->mTcpClient = std::make_shared(param, objectThis); - return objectThis; + return objectThis; ///ObjectThis is used to verify whether the client is legal. } \ No newline at end of file diff --git a/utils/TcpModule/src/TcpClientImpl.h b/utils/TcpModule/src/TcpClientImpl.h index 4e0a3379..eefe9883 100644 --- a/utils/TcpModule/src/TcpClientImpl.h +++ b/utils/TcpModule/src/TcpClientImpl.h @@ -35,12 +35,12 @@ public: void Loop(void); private: - std::mutex mMutex; - hloop_t *mLoop; - hio_t *mIo; - const TcpClientParam mParam; + std::mutex mMutex; ///A mutex lock used to synchronize access to shared resources. + hloop_t *mLoop; + hio_t *mIo; ///Socket handle + const TcpClientParam mParam; ///Basic information of the client, including port, ip, reading and closing. std::thread mTcpClientThread; - const void *mObjectThis; + const void *mObjectThis; ///ObjectThis is used to verify whether the client is legal. }; std::shared_ptr *NewTcpClient(const TcpClientParam ¶m); #endif \ No newline at end of file diff --git a/utils/TcpModule/src/TcpModule.cpp b/utils/TcpModule/src/TcpModule.cpp index 8f38357f..8cb81dc2 100644 --- a/utils/TcpModule/src/TcpModule.cpp +++ b/utils/TcpModule/src/TcpModule.cpp @@ -45,7 +45,7 @@ static bool TcpServerObjectCheck(void *object) * @brief Verify that object is a legitimate (existing) client. * * @param object Save the address of the character pointer variable. - * If the value of the character pointer pointed by *object is "tcp_client", return turn + * The correct object is returned by the function NewTcpClient in Itcpclientlmpl.cpp file. * @return true Indicates that the client exists. * @return false Indicates that the client does not exist */