Improve the readme file

This commit is contained in:
chenhaijian 2024-08-13 15:48:29 +08:00
parent e7bf6d761a
commit 63147a9928
6 changed files with 45 additions and 32 deletions

View File

@ -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
{

View File

@ -38,6 +38,8 @@
3. 客户端实例TcpClientImpl类对象进行init操作时会创建一个套接字并对目标服务器发出连接请求并启动事件循环Loop函数然后客户端实例会一直进行事件循环监听是否有读写操作直至连接关闭Close函数
4. 服务器`监听连接的套接字`和`连接后新建立的套接字`要分别设置回调函数,这是两个东西,不要混淆。
# 该模块的实现过程
## 服务器端:
@ -48,7 +50,9 @@ on_close当服务器端套接字关闭时调用。
3. 启动事件循环:使用 hloop_run 启动服务器的事件循环,等待客户端的连接请求。
4. 接受连接:在 on_accept 回调函数中,接受客户端的连接请求,并创建用于该连接的新套接字。
5. 创建客户端接受对象:为新的客户端连接创建一个 TcpClientAcceptImpl 对象,并设置相应的回调函数。
管理客户端连接:将新创建的客户端接受对象添加到管理容器中,以便跟踪和管理。
on_close当客户端连接关闭时调用。
on_recv当客户端发送数据时调用。
6. 管理客户端连接:将新创建的客户端接受对象添加到管理容器中,以便跟踪和管理。
## 客户端:
1. 创建客户端套接字:使用 hio_create_socket 函数创建一个新的 TCP 客户端套接字。
2. 设置回调函数:为客户端套接字设置回调函数,包括:

View File

@ -19,6 +19,10 @@
#include "StatusCode.h"
#include "TcpModule.h"
#include <memory>
/**
* @brief A factory class that indirectly creates server-side and client-side instances through this class
*
*/
class TcpModuleMakePtr
{
public:

View File

@ -135,7 +135,8 @@ 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,7 +153,8 @@ 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)

View File

@ -23,7 +23,8 @@
#include <mutex>
#include <thread>
/**
* @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<TcpClientAcceptImpl>
@ -34,7 +35,8 @@ public:
void Close(void) override; ///< Disconnect the client
void Readed(const void *data, size_t length) override; ///< The server reads the data received by tcp connection.
ssize_t Write(const void *data, size_t length) override; ///< The server writes data to the tcp connection.
void Closed(void) override; ///<Used to perform some cleaning work or notify upper layer logic when a TCP client accepts a connection that is closed
void Closed(void) override; ///< Used to perform some cleaning work or notify upper layer logic when a TCP client
///< accepts a connection that is closed
private:
const hio_t *mIo; ///< Connected client socket
@ -55,7 +57,8 @@ public:
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<ITcpClientAccept> *GetClient(hio_t *io);///< Retrieve the corresponding client acceptance object based on the socket handle.
std::shared_ptr<ITcpClientAccept> *
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)