From e7bf6d761a50d8e665ff014e942934aacd7eaa3c Mon Sep 17 00:00:00 2001 From: chenhaijian <2830005753@qq.com> Date: Mon, 12 Aug 2024 17:13:12 +0800 Subject: [PATCH] modify some annotation --- utils/TcpModule/readme.md | 3 ++- utils/TcpModule/src/TcpServerImpl.cpp | 27 ++++++++++++------- utils/TcpModule/src/TcpServerImpl.h | 39 +++++++++++++-------------- 3 files changed, 38 insertions(+), 31 deletions(-) diff --git a/utils/TcpModule/readme.md b/utils/TcpModule/readme.md index ff2f0267..741ca01c 100644 --- a/utils/TcpModule/readme.md +++ b/utils/TcpModule/readme.md @@ -34,10 +34,11 @@ # 一些易混淆的点 1. 客户端创建的套接字包含指定`目标的IP地址和端口号`(TcpClientImpl类中的init函数),服务器创建的套接字只包含`本机IP地址和监听端口`。 -2. 在网络编程中,服务器通常会在一个监听端口上等待来自客户端的连接请求。当服务器接受到一个连接请求时,它会创建一个新的套接字(或文件描述符)来表示这个新建立的连接,并且通常会为这个新的连接设置一系列的事件回调函数。 +2. 在网络编程中,`服务器`通常会在一个监听端口上等待来自客户端的连接请求。当服务器接受到一个连接请求时,它会创建一个新的套接字(或文件描述符)来表示这个新建立的连接(TcpClientAccept类实例),并且通常会为这个新的连接设置一系列的事件回调函数。 3. 客户端实例(TcpClientImpl类对象)进行init操作时,会创建一个套接字并对目标服务器发出连接请求并启动事件循环(Loop函数),然后客户端实例会一直进行事件循环(监听是否有读写操作)直至连接关闭(Close函数)。 + # 该模块的实现过程 ## 服务器端: 1. 创建服务器端套接字:使用 hloop_create_tcp_server 函数创建一个新的 TCP 服务器端套接字,并指定要监听的 IP 地址和端口号。 diff --git a/utils/TcpModule/src/TcpServerImpl.cpp b/utils/TcpModule/src/TcpServerImpl.cpp index 16a8e5bd..d8d7c48a 100644 --- a/utils/TcpModule/src/TcpServerImpl.cpp +++ b/utils/TcpModule/src/TcpServerImpl.cpp @@ -106,8 +106,8 @@ void TcpClientAcceptImpl::Close(void) /** * @brief The server reads the data received by tcp connection. * - * @param data - * @param length bytes + * @param data Read data content + * @param length Read data byte length */ void TcpClientAcceptImpl::Readed(const void *data, size_t length) { @@ -134,6 +134,10 @@ ssize_t TcpClientAcceptImpl::Write(const void *data, size_t length) LogError("mIo is null\n"); return TCP_MODULE_WRITE_ERROR; } +/** + * @brief Used to perform some cleaning work or notify upper layer logic when a TCP client accepts a connection that is closed + * + */ void TcpClientAcceptImpl::Closed(void) { if (nullptr != mParam.mClosedFunc) { @@ -147,6 +151,10 @@ TcpServerImpl::TcpServerImpl(const TcpServerParam param) : mParam(param) mLoop = nullptr; mIo = nullptr; } +/** + * @brief Create an event loop to listen for connection requests from the server socket, set up relevant callback functions, and start a separate thread to run the event loop + * + */ void TcpServerImpl::Init(void) { constexpr int NO_FALGS = 0; @@ -208,9 +216,9 @@ void TcpServerImpl::Loop(void) mLoop = nullptr; } /** - * @brief + * @brief Add a newly connected client to the server connection management list. * - * @param io Add the socket of the newly connected client. + * @param io Socket associated with server and client connections */ void TcpServerImpl::AddClient(hio_t *io) { @@ -223,9 +231,7 @@ void TcpServerImpl::AddClient(hio_t *io) SOCKADDR_STR(hio_peeraddr(io), peeraddrstr)); std::shared_ptr *addClient = NewTcpClientAccept(io, mParam.mClientAcceptParam); mClients[hio_fd(io)] = addClient; - /** - * @brief Check whether the server side accepts the connection of the client side. - */ + /// Check whether the server side accepts the connection of the client side. if (mParam.mAcceptClientFunc) { if (mParam.mAcceptClientFunc(addClient, peeraddrstr) == true) { mMutex.unlock(); @@ -238,9 +244,9 @@ void TcpServerImpl::AddClient(hio_t *io) LogWarning("AddClient failed.\n"); } /** - * @brief + * @brief Retrieve the corresponding client acceptance object based on the socket handle. * - * @param io socket + * @param io Handle pointing to socket * @return std::shared_ptr* */ std::shared_ptr *TcpServerImpl::GetClient(hio_t *io) @@ -322,7 +328,7 @@ std::shared_ptr *NewTcpServer(const TcpServerParam ¶m) } std::shared_ptr *NewTcpClientAccept(const hio_t *io, const ClientAcceptParam ¶m) { - LogInfo("Create tcp server object.\n"); + LogInfo("Create tcp client accept object.\n"); TcpClientAccept *impl = (TcpClientAccept *)malloc(sizeof(TcpClientAccept)); if (nullptr == impl) { LogError("NewTcpServer::malloc failed.\n"); @@ -331,6 +337,7 @@ std::shared_ptr *NewTcpClientAccept(const hio_t *io, const Cli TcpClientAccept tmp; memcpy((void *)impl, (void *)&tmp, sizeof(TcpClientAccept)); impl->mHeader.mCheckName = GetTcpClientAcceptName(); + ///ObjectThis actually refers to mTcpClientAccept std::shared_ptr *objectThis = (std::shared_ptr *)(((char *)impl) + sizeof(ITcpServerHeader)); impl->mTcpClientAccept = std::make_shared(io, objectThis, param); diff --git a/utils/TcpModule/src/TcpServerImpl.h b/utils/TcpModule/src/TcpServerImpl.h index 74a7a93b..ea2cc5d0 100644 --- a/utils/TcpModule/src/TcpServerImpl.h +++ b/utils/TcpModule/src/TcpServerImpl.h @@ -23,8 +23,7 @@ #include #include /** - * @brief The server manages the connection life cycle and data interaction of each connected client through this kind - * of instance. + * @brief The TcpClient Accept class instance is used to associate with the socket after connecting to the server and client, responsible for a series of operations (read/write, etc.) after the connection * */ class TcpClientAcceptImpl : public ITcpClientAccept, public std::enable_shared_from_this @@ -32,14 +31,14 @@ class TcpClientAcceptImpl : public ITcpClientAccept, public std::enable_shared_f public: TcpClientAcceptImpl(const hio_t *io, const void *object, const ClientAcceptParam ¶m); virtual ~TcpClientAcceptImpl() = default; - void Close(void) override; - void Readed(const void *data, size_t length) override; - ssize_t Write(const void *data, size_t length) override; - void Closed(void) override; + void Close(void) override; /// *GetClient(hio_t *io); - void RemoveClient(hio_t *io); /// Remove the data element in the map, that is, the connected client. - void FreeClients(void); /// Clear all connected clients. - void Closed(void); /// Stop listening to the event loop(mLoop) + void UnInit(void) override; + void Loop(void); ///< Run an event loop and release resources after completion or error. + void AddClient(hio_t *io); ///< Add a newly connected client to the server connection management list. + std::shared_ptr *GetClient(hio_t *io);///< Retrieve the corresponding client acceptance object based on the socket handle. + void RemoveClient(hio_t *io); ///< Remove the data element in the map, that is, the connected client. + void FreeClients(void); ///< Clear all connected clients. + void Closed(void); ///< Stop listening to the event loop(mLoop) private: - std::mutex mMutex; /// A mutex lock used to synchronize access to shared resources. - hloop_t *mLoop; /// Event loop, listening for all io objects - hio_t *mIo; /// A server socket to listen whether a new client sends a connection request. + std::mutex mMutex; ///< A mutex lock used to synchronize access to shared resources. + hloop_t *mLoop; ///< Event loop, listening for all io objects + hio_t *mIo; ///< A server socket to listen whether a new client sends a connection request. const TcpServerParam mParam; - std::thread mTcpServerThread; - std::map *> mClients; + std::thread mTcpServerThread; ///< A separate thread used to run event loops + std::map *> mClients;///< Container for storing clients connected to servers }; std::shared_ptr *NewTcpServer(const TcpServerParam ¶m); std::shared_ptr *NewTcpClientAccept(const hio_t *io, const ClientAcceptParam ¶m);