This commit is contained in:
binbinnomoney 2024-07-25 15:16:50 +08:00
commit 363172042a
3 changed files with 37 additions and 9 deletions

View File

@ -23,12 +23,24 @@
#include <mutex>
#include <stdlib.h>
#include <sys/types.h>
/**
* @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 &param, 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<ITcpClient> *NewTcpClient(const TcpClientParam &param)
{
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<ITcpClient> *NewTcpClient(const TcpClientParam &param)
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<ITcpClient> *objectThis =
(std::shared_ptr<ITcpClient> *)(((char *)impl) + sizeof(ITcpClientHeader));
impl->mTcpClient = std::make_shared<TcpClientImpl>(param, objectThis);
return objectThis;
return objectThis; ///ObjectThis is used to verify whether the client is legal.
}

View File

@ -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<ITcpClient> *NewTcpClient(const TcpClientParam &param);
#endif

View File

@ -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
*/