mirror of
https://gitee.com/jiuyilian/embedded-framework.git
synced 2025-01-06 10:16:51 -05:00
Improve the readme file
This commit is contained in:
parent
e7bf6d761a
commit
63147a9928
|
@ -25,15 +25,15 @@ typedef bool (*TcpAcceptClientFunction)(void *, const char *);
|
||||||
typedef void (*SocketClosedFunction)(const void *);
|
typedef void (*SocketClosedFunction)(const void *);
|
||||||
typedef struct client_accept_parm
|
typedef struct client_accept_parm
|
||||||
{
|
{
|
||||||
const TcpReadFunction mReadFunc; /// 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.
|
const SocketClosedFunction mClosedFunc; ///< This function is defined in the test file.
|
||||||
} ClientAcceptParam;
|
} ClientAcceptParam;
|
||||||
typedef struct tcp_server_parm
|
typedef struct tcp_server_parm
|
||||||
{
|
{
|
||||||
const char *mIp; /// Server ip
|
const char *mIp; ///< Server ip
|
||||||
const int mPort; /// Server port
|
const int mPort; ///< Server port
|
||||||
const TcpAcceptClientFunction mAcceptClientFunc; /// This function is defined in the test file.
|
const TcpAcceptClientFunction mAcceptClientFunc; ///< This function is defined in the test file.
|
||||||
const ClientAcceptParam mClientAcceptParam; /// This function is defined in the test file.
|
const ClientAcceptParam mClientAcceptParam; ///< This function is defined in the test file.
|
||||||
} TcpServerParam;
|
} TcpServerParam;
|
||||||
typedef struct tcp_parm
|
typedef struct tcp_parm
|
||||||
{
|
{
|
||||||
|
|
|
@ -38,23 +38,27 @@
|
||||||
|
|
||||||
3. 客户端实例(TcpClientImpl类对象)进行init操作时,会创建一个套接字并对目标服务器发出连接请求并启动事件循环(Loop函数),然后客户端实例会一直进行事件循环(监听是否有读写操作)直至连接关闭(Close函数)。
|
3. 客户端实例(TcpClientImpl类对象)进行init操作时,会创建一个套接字并对目标服务器发出连接请求并启动事件循环(Loop函数),然后客户端实例会一直进行事件循环(监听是否有读写操作)直至连接关闭(Close函数)。
|
||||||
|
|
||||||
|
4. 服务器`监听连接的套接字`和`连接后新建立的套接字`要分别设置回调函数,这是两个东西,不要混淆。
|
||||||
|
|
||||||
|
|
||||||
# 该模块的实现过程
|
# 该模块的实现过程
|
||||||
## 服务器端:
|
## 服务器端:
|
||||||
1. 创建服务器端套接字:使用 hloop_create_tcp_server 函数创建一个新的 TCP 服务器端套接字,并指定要监听的 IP 地址和端口号。
|
1. 创建服务器端套接字:使用 hloop_create_tcp_server 函数创建一个新的 TCP 服务器端套接字,并指定要监听的 IP 地址和端口号。
|
||||||
2. 设置回调函数:为服务器端套接字设置回调函数,包括:
|
2. 设置回调函数:为服务器端套接字设置回调函数,包括:
|
||||||
on_accept:当有新的客户端尝试连接时调用。
|
on_accept:当有新的客户端尝试连接时调用。
|
||||||
on_close:当服务器端套接字关闭时调用。
|
on_close:当服务器端套接字关闭时调用。
|
||||||
3. 启动事件循环:使用 hloop_run 启动服务器的事件循环,等待客户端的连接请求。
|
3. 启动事件循环:使用 hloop_run 启动服务器的事件循环,等待客户端的连接请求。
|
||||||
4. 接受连接:在 on_accept 回调函数中,接受客户端的连接请求,并创建用于该连接的新套接字。
|
4. 接受连接:在 on_accept 回调函数中,接受客户端的连接请求,并创建用于该连接的新套接字。
|
||||||
5. 创建客户端接受对象:为新的客户端连接创建一个 TcpClientAcceptImpl 对象,并设置相应的回调函数。
|
5. 创建客户端接受对象:为新的客户端连接创建一个 TcpClientAcceptImpl 对象,并设置相应的回调函数。
|
||||||
管理客户端连接:将新创建的客户端接受对象添加到管理容器中,以便跟踪和管理。
|
on_close:当客户端连接关闭时调用。
|
||||||
|
on_recv:当客户端发送数据时调用。
|
||||||
|
6. 管理客户端连接:将新创建的客户端接受对象添加到管理容器中,以便跟踪和管理。
|
||||||
## 客户端:
|
## 客户端:
|
||||||
1. 创建客户端套接字:使用 hio_create_socket 函数创建一个新的 TCP 客户端套接字。
|
1. 创建客户端套接字:使用 hio_create_socket 函数创建一个新的 TCP 客户端套接字。
|
||||||
2. 设置回调函数:为客户端套接字设置回调函数,包括:
|
2. 设置回调函数:为客户端套接字设置回调函数,包括:
|
||||||
on_connect:当连接成功建立时调用。
|
on_connect:当连接成功建立时调用。
|
||||||
on_close:当连接关闭时调用。
|
on_close:当连接关闭时调用。
|
||||||
on_message:当接收到服务器发送的数据时调用。
|
on_message:当接收到服务器发送的数据时调用。
|
||||||
3. 连接到服务器:使用 hio_connect 函数向服务器发起连接请求。
|
3. 连接到服务器:使用 hio_connect 函数向服务器发起连接请求。
|
||||||
4. 启动事件循环:使用 hloop_run 启动客户端的事件循环,等待连接结果和数据传输。
|
4. 启动事件循环:使用 hloop_run 启动客户端的事件循环,等待连接结果和数据传输。
|
||||||
5. 处理连接结果:在 on_connect 回调函数中处理连接结果,如果连接成功,可以开始发送和接收数据。
|
5. 处理连接结果:在 on_connect 回调函数中处理连接结果,如果连接成功,可以开始发送和接收数据。
|
|
@ -27,8 +27,8 @@ class TcpClientImpl : public ITcpClient, public std::enable_shared_from_this<Tcp
|
||||||
public:
|
public:
|
||||||
TcpClientImpl(const TcpClientParam ¶m, const void *object);
|
TcpClientImpl(const TcpClientParam ¶m, const void *object);
|
||||||
virtual ~TcpClientImpl() = default;
|
virtual ~TcpClientImpl() = default;
|
||||||
void Init(void) override; ///< Create a socket and connect it, and start an event loop to listen to io object
|
void Init(void) override; ///< Create a socket and connect it, and start an event loop to listen to io object
|
||||||
///< operations (read and write, etc.)
|
///< operations (read and write, etc.)
|
||||||
void UnInit(void) override; ///< close the client't socket and wait for the event cycle to end.
|
void UnInit(void) override; ///< close the client't socket and wait for the event cycle to end.
|
||||||
void Readed(const void *data, size_t length) override; ///< Read the data sent by the server in tcp connection.
|
void Readed(const void *data, size_t length) override; ///< Read the data sent by the server in tcp connection.
|
||||||
ssize_t Write(const void *buf, const size_t bufLenght) override; ///< client writes data to the server
|
ssize_t Write(const void *buf, const size_t bufLenght) override; ///< client writes data to the server
|
||||||
|
|
|
@ -19,6 +19,10 @@
|
||||||
#include "StatusCode.h"
|
#include "StatusCode.h"
|
||||||
#include "TcpModule.h"
|
#include "TcpModule.h"
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
/**
|
||||||
|
* @brief A factory class that indirectly creates server-side and client-side instances through this class
|
||||||
|
*
|
||||||
|
*/
|
||||||
class TcpModuleMakePtr
|
class TcpModuleMakePtr
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
|
@ -135,7 +135,8 @@ ssize_t TcpClientAcceptImpl::Write(const void *data, size_t length)
|
||||||
return TCP_MODULE_WRITE_ERROR;
|
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)
|
void TcpClientAcceptImpl::Closed(void)
|
||||||
|
@ -152,7 +153,8 @@ TcpServerImpl::TcpServerImpl(const TcpServerParam param) : mParam(param)
|
||||||
mIo = 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
|
* @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)
|
void TcpServerImpl::Init(void)
|
||||||
|
@ -337,7 +339,7 @@ std::shared_ptr<ITcpClientAccept> *NewTcpClientAccept(const hio_t *io, const Cli
|
||||||
TcpClientAccept tmp;
|
TcpClientAccept tmp;
|
||||||
memcpy((void *)impl, (void *)&tmp, sizeof(TcpClientAccept));
|
memcpy((void *)impl, (void *)&tmp, sizeof(TcpClientAccept));
|
||||||
impl->mHeader.mCheckName = GetTcpClientAcceptName();
|
impl->mHeader.mCheckName = GetTcpClientAcceptName();
|
||||||
///ObjectThis actually refers to mTcpClientAccept
|
/// ObjectThis actually refers to mTcpClientAccept
|
||||||
std::shared_ptr<ITcpClientAccept> *objectThis =
|
std::shared_ptr<ITcpClientAccept> *objectThis =
|
||||||
(std::shared_ptr<ITcpClientAccept> *)(((char *)impl) + sizeof(ITcpServerHeader));
|
(std::shared_ptr<ITcpClientAccept> *)(((char *)impl) + sizeof(ITcpServerHeader));
|
||||||
impl->mTcpClientAccept = std::make_shared<TcpClientAcceptImpl>(io, objectThis, param);
|
impl->mTcpClientAccept = std::make_shared<TcpClientAcceptImpl>(io, objectThis, param);
|
||||||
|
|
|
@ -23,7 +23,8 @@
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <thread>
|
#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>
|
class TcpClientAcceptImpl : public ITcpClientAccept, public std::enable_shared_from_this<TcpClientAcceptImpl>
|
||||||
|
@ -31,14 +32,15 @@ class TcpClientAcceptImpl : public ITcpClientAccept, public std::enable_shared_f
|
||||||
public:
|
public:
|
||||||
TcpClientAcceptImpl(const hio_t *io, const void *object, const ClientAcceptParam ¶m);
|
TcpClientAcceptImpl(const hio_t *io, const void *object, const ClientAcceptParam ¶m);
|
||||||
virtual ~TcpClientAcceptImpl() = default;
|
virtual ~TcpClientAcceptImpl() = default;
|
||||||
void Close(void) override; ///<Disconnect the client
|
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.
|
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.
|
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:
|
private:
|
||||||
const hio_t *mIo; ///< Connected client socket
|
const hio_t *mIo; ///< Connected client socket
|
||||||
const void *mObjectThis;///<Used to verify whether this type of instance is legal
|
const void *mObjectThis; ///< Used to verify whether this type of instance is legal
|
||||||
const ClientAcceptParam mParam;
|
const ClientAcceptParam mParam;
|
||||||
};
|
};
|
||||||
/**
|
/**
|
||||||
|
@ -53,9 +55,10 @@ public:
|
||||||
virtual ~TcpServerImpl() = default;
|
virtual ~TcpServerImpl() = default;
|
||||||
void Init(void) override;
|
void Init(void) override;
|
||||||
void UnInit(void) override;
|
void UnInit(void) override;
|
||||||
void Loop(void); ///< Run an event loop and release resources after completion or error.
|
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.
|
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 RemoveClient(hio_t *io); ///< Remove the data element in the map, that is, the connected client.
|
||||||
void FreeClients(void); ///< Clear all connected clients.
|
void FreeClients(void); ///< Clear all connected clients.
|
||||||
void Closed(void); ///< Stop listening to the event loop(mLoop)
|
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
|
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.
|
hio_t *mIo; ///< A server socket to listen whether a new client sends a connection request.
|
||||||
const TcpServerParam mParam;
|
const TcpServerParam mParam;
|
||||||
std::thread mTcpServerThread; ///< A separate thread used to run event loops
|
std::thread mTcpServerThread; ///< A separate thread used to run event loops
|
||||||
std::map<int, std::shared_ptr<ITcpClientAccept> *> mClients;///< Container for storing clients connected to servers
|
std::map<int, std::shared_ptr<ITcpClientAccept> *> mClients; ///< Container for storing clients connected to servers
|
||||||
};
|
};
|
||||||
std::shared_ptr<ITcpServer> *NewTcpServer(const TcpServerParam ¶m);
|
std::shared_ptr<ITcpServer> *NewTcpServer(const TcpServerParam ¶m);
|
||||||
std::shared_ptr<ITcpClientAccept> *NewTcpClientAccept(const hio_t *io, const ClientAcceptParam ¶m);
|
std::shared_ptr<ITcpClientAccept> *NewTcpClientAccept(const hio_t *io, const ClientAcceptParam ¶m);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user