diff --git a/utils/TcpModule/include/TcpModule.h b/utils/TcpModule/include/TcpModule.h index a47a928c..c8c35edd 100644 --- a/utils/TcpModule/include/TcpModule.h +++ b/utils/TcpModule/include/TcpModule.h @@ -25,15 +25,15 @@ typedef bool (*TcpAcceptClientFunction)(void *, const char *); typedef void (*SocketClosedFunction)(const void *); typedef struct client_accept_parm { - const TcpReadFunction mReadFunc; /// This function is defined in the test file - const SocketClosedFunction mClosedFunc; /// This function is defined in the test file. + const TcpReadFunction mReadFunc; ///< This function is defined in the test file + const SocketClosedFunction mClosedFunc; ///< This function is defined in the test file. } ClientAcceptParam; typedef struct tcp_server_parm { - const char *mIp; /// Server ip - const int mPort; /// Server port - const TcpAcceptClientFunction mAcceptClientFunc; /// This function is defined in the test file. - const ClientAcceptParam mClientAcceptParam; /// This function is defined in the test file. + const char *mIp; ///< Server ip + const int mPort; ///< Server port + const TcpAcceptClientFunction mAcceptClientFunc; ///< This function is defined in the test file. + const ClientAcceptParam mClientAcceptParam; ///< This function is defined in the test file. } TcpServerParam; typedef struct tcp_parm { diff --git a/utils/TcpModule/readme.md b/utils/TcpModule/readme.md index 741ca01c..9f95174a 100644 --- a/utils/TcpModule/readme.md +++ b/utils/TcpModule/readme.md @@ -38,23 +38,27 @@ 3. 客户端实例(TcpClientImpl类对象)进行init操作时,会创建一个套接字并对目标服务器发出连接请求并启动事件循环(Loop函数),然后客户端实例会一直进行事件循环(监听是否有读写操作)直至连接关闭(Close函数)。 +4. 服务器`监听连接的套接字`和`连接后新建立的套接字`要分别设置回调函数,这是两个东西,不要混淆。 + # 该模块的实现过程 ## 服务器端: 1. 创建服务器端套接字:使用 hloop_create_tcp_server 函数创建一个新的 TCP 服务器端套接字,并指定要监听的 IP 地址和端口号。 2. 设置回调函数:为服务器端套接字设置回调函数,包括: -on_accept:当有新的客户端尝试连接时调用。 -on_close:当服务器端套接字关闭时调用。 + on_accept:当有新的客户端尝试连接时调用。 + on_close:当服务器端套接字关闭时调用。 3. 启动事件循环:使用 hloop_run 启动服务器的事件循环,等待客户端的连接请求。 4. 接受连接:在 on_accept 回调函数中,接受客户端的连接请求,并创建用于该连接的新套接字。 5. 创建客户端接受对象:为新的客户端连接创建一个 TcpClientAcceptImpl 对象,并设置相应的回调函数。 -管理客户端连接:将新创建的客户端接受对象添加到管理容器中,以便跟踪和管理。 + on_close:当客户端连接关闭时调用。 + on_recv:当客户端发送数据时调用。 +6. 管理客户端连接:将新创建的客户端接受对象添加到管理容器中,以便跟踪和管理。 ## 客户端: 1. 创建客户端套接字:使用 hio_create_socket 函数创建一个新的 TCP 客户端套接字。 2. 设置回调函数:为客户端套接字设置回调函数,包括: -on_connect:当连接成功建立时调用。 -on_close:当连接关闭时调用。 -on_message:当接收到服务器发送的数据时调用。 + on_connect:当连接成功建立时调用。 + on_close:当连接关闭时调用。 + on_message:当接收到服务器发送的数据时调用。 3. 连接到服务器:使用 hio_connect 函数向服务器发起连接请求。 4. 启动事件循环:使用 hloop_run 启动客户端的事件循环,等待连接结果和数据传输。 5. 处理连接结果:在 on_connect 回调函数中处理连接结果,如果连接成功,可以开始发送和接收数据。 \ No newline at end of file diff --git a/utils/TcpModule/src/TcpClientImpl.h b/utils/TcpModule/src/TcpClientImpl.h index b2023168..20036652 100644 --- a/utils/TcpModule/src/TcpClientImpl.h +++ b/utils/TcpModule/src/TcpClientImpl.h @@ -27,8 +27,8 @@ class TcpClientImpl : public ITcpClient, public std::enable_shared_from_this +/** + * @brief A factory class that indirectly creates server-side and client-side instances through this class + * + */ class TcpModuleMakePtr { public: diff --git a/utils/TcpModule/src/TcpServerImpl.cpp b/utils/TcpModule/src/TcpServerImpl.cpp index d8d7c48a..9d053c38 100644 --- a/utils/TcpModule/src/TcpServerImpl.cpp +++ b/utils/TcpModule/src/TcpServerImpl.cpp @@ -135,8 +135,9 @@ ssize_t TcpClientAcceptImpl::Write(const void *data, size_t length) 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 - * + * @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) { @@ -152,8 +153,9 @@ TcpServerImpl::TcpServerImpl(const TcpServerParam param) : mParam(param) 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 - * + * @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) { @@ -337,7 +339,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 + /// 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 ea2cc5d0..35b5eecb 100644 --- a/utils/TcpModule/src/TcpServerImpl.h +++ b/utils/TcpModule/src/TcpServerImpl.h @@ -23,7 +23,8 @@ #include #include /** - * @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 + * @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 @@ -31,14 +32,15 @@ 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; /// *GetClient(hio_t *io);///< Retrieve the corresponding client acceptance object based on the socket handle. + 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) @@ -65,8 +68,8 @@ private: 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; ///< A separate thread used to run event loops - std::map *> mClients;///< Container for storing clients connected to servers + 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);