Backup:TcpModule.
This commit is contained in:
parent
6f6e8eb7cd
commit
a7fc63285c
|
@ -79,7 +79,7 @@ const StatusCode McuDevice::UnInit(void)
|
||||||
DeleteAllAsk();
|
DeleteAllAsk();
|
||||||
return CreateStatusCode(STATUS_CODE_OK);
|
return CreateStatusCode(STATUS_CODE_OK);
|
||||||
}
|
}
|
||||||
size_t McuDevice::WriteData(const void *buff, const size_t buffLength, std::shared_ptr<VProtocolContext> &context,
|
ssize_t McuDevice::WriteData(const void *buff, const size_t buffLength, std::shared_ptr<VProtocolContext> &context,
|
||||||
const unsigned int &serialNumber)
|
const unsigned int &serialNumber)
|
||||||
{
|
{
|
||||||
constexpr size_t WRITE_ERROR = -1;
|
constexpr size_t WRITE_ERROR = -1;
|
||||||
|
|
|
@ -29,7 +29,7 @@ public:
|
||||||
const StatusCode UnInit(void) override;
|
const StatusCode UnInit(void) override;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
size_t WriteData(const void *buff, const size_t buffLength, std::shared_ptr<VProtocolContext> &context,
|
ssize_t WriteData(const void *buff, const size_t buffLength, std::shared_ptr<VProtocolContext> &context,
|
||||||
const unsigned int &serialNumber) override;
|
const unsigned int &serialNumber) override;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
|
@ -19,33 +19,42 @@
|
||||||
#include <thread>
|
#include <thread>
|
||||||
namespace TcpModuleTest
|
namespace TcpModuleTest
|
||||||
{
|
{
|
||||||
// ../output_files/test/bin/TcpModuleTest --gtest_filter=TcpModuleTest.UNIT_TcpModule_AUTO_IllegalObject
|
// ../output_files/test/bin/TcpModuleTest --gtest_filter=TcpModuleTest.UNIT_TcpModule_EXAMPLE_AUTO_IllegalObject
|
||||||
/**
|
/**
|
||||||
* TcpModule module api will not crash when object is illegal.
|
* TcpModule module api will not crash when object is illegal.
|
||||||
*/
|
*/
|
||||||
TEST(TcpModuleTest, UNIT_TcpModule_AUTO_IllegalObject)
|
TEST(TcpModuleTest, UNIT_TcpModule_EXAMPLE_AUTO_IllegalObject)
|
||||||
{
|
{
|
||||||
static void *tcpClientAccept = nullptr;
|
static void *tcpClientAccept = nullptr;
|
||||||
TcpParam param = {
|
TcpServerParam tcpServerparam = {
|
||||||
.mIp = "127.0.0.1",
|
.mIp = "127.0.0.1",
|
||||||
.mPort = 9876,
|
.mPort = 9876,
|
||||||
.mReadFunc = [](const void *p, const size_t len, void *context) -> void {
|
|
||||||
return;
|
|
||||||
},
|
|
||||||
.mAcceptClientFunc = [](void *object, const char *ip) -> void {
|
.mAcceptClientFunc = [](void *object, const char *ip) -> void {
|
||||||
LogInfo("accept client, peer ip: %s", ip);
|
LogInfo("accept client, peer ip: %s", ip);
|
||||||
tcpClientAccept = object;
|
tcpClientAccept = object;
|
||||||
return;
|
return;
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
TcpClientParam param = {
|
||||||
|
.mIp = "127.0.0.1",
|
||||||
|
.mPort = 9876,
|
||||||
|
.mReadFunc = [](const void *data, const ssize_t len, const void *context) -> void {
|
||||||
|
LogInfo("read data: %s", (char *)data);
|
||||||
|
return;
|
||||||
|
},
|
||||||
|
};
|
||||||
CreateLogModule();
|
CreateLogModule();
|
||||||
ILogInit(LOG_INSTANCE_TYPE_END);
|
ILogInit(LOG_INSTANCE_TYPE_END);
|
||||||
void *tcpServer = CreateTcpServer(param);
|
void *tcpServer = CreateTcpServer(tcpServerparam);
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
||||||
void *tcpClient = CreateTcpClient(param);
|
void *tcpClient = CreateTcpClient(param);
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||||
TcpClientWrite(tcpClient, "123456789", strlen("123456789"));
|
TcpClientWrite(tcpClient, "123456789", strlen("123456789"));
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(3000));
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||||
|
if (nullptr != tcpClientAccept) {
|
||||||
|
AcceptClientWrite(tcpClientAccept, "9876543210", strlen("9876543210"));
|
||||||
|
}
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
|
||||||
if (nullptr != tcpClient) {
|
if (nullptr != tcpClient) {
|
||||||
FreeTcpClient(tcpClient);
|
FreeTcpClient(tcpClient);
|
||||||
}
|
}
|
||||||
|
|
|
@ -68,7 +68,7 @@ protected:
|
||||||
{
|
{
|
||||||
return std::make_shared<VProtocolBase>();
|
return std::make_shared<VProtocolBase>();
|
||||||
}
|
}
|
||||||
virtual size_t WriteData(const void *buff, const size_t buffLength, std::shared_ptr<VProtocolContext> &context,
|
virtual ssize_t WriteData(const void *buff, const size_t buffLength, std::shared_ptr<VProtocolContext> &context,
|
||||||
const unsigned int &serialNumber)
|
const unsigned int &serialNumber)
|
||||||
{
|
{
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -16,23 +16,36 @@
|
||||||
#define TCP_MODULE_H
|
#define TCP_MODULE_H
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
#define TCP_MODULE_WRITE_ERROR -1;
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
typedef void (*TcpReadFunction)(const void *, const size_t, void *);
|
typedef void (*TcpReadFunction)(const void *, const ssize_t, const void *);
|
||||||
typedef void (*TcpAcceptClientFunction)(void *, const char *);
|
typedef void (*TcpAcceptClientFunction)(void *, const char *);
|
||||||
|
typedef void (*SocketClosedFunction)(const void *);
|
||||||
|
typedef struct tcp_server_parm
|
||||||
|
{
|
||||||
|
const char *mIp;
|
||||||
|
const int mPort;
|
||||||
|
TcpAcceptClientFunction mAcceptClientFunc;
|
||||||
|
} TcpServerParam;
|
||||||
typedef struct tcp_parm
|
typedef struct tcp_parm
|
||||||
{
|
{
|
||||||
const char *mIp;
|
const char *mIp;
|
||||||
const int mPort;
|
const int mPort;
|
||||||
TcpReadFunction mReadFunc; // TODO: delete this memeber
|
TcpReadFunction mReadFunc;
|
||||||
TcpAcceptClientFunction mAcceptClientFunc;
|
SocketClosedFunction mClosedFunc;
|
||||||
} TcpParam;
|
} TcpClientParam;
|
||||||
void *CreateTcpServer(const TcpParam param);
|
typedef struct client_accept_parm
|
||||||
|
{
|
||||||
|
TcpReadFunction mReadFunc;
|
||||||
|
SocketClosedFunction mClosedFunc;
|
||||||
|
} ClientAcceptParam;
|
||||||
|
void *CreateTcpServer(const TcpServerParam param);
|
||||||
void FreeTcpServer(void *object);
|
void FreeTcpServer(void *object);
|
||||||
int AcceptClientSetReadFunc(void *object, TcpReadFunction readFunc);
|
void AcceptClientSetParam(void *object, const ClientAcceptParam param);
|
||||||
int AcceptClientWrite(void *object, void *buf, const size_t bufLenght);
|
ssize_t AcceptClientWrite(void *object, const void *buf, const size_t bufLenght);
|
||||||
void *CreateTcpClient(const TcpParam param);
|
void *CreateTcpClient(const TcpClientParam param);
|
||||||
void FreeTcpClient(void *object);
|
void FreeTcpClient(void *object);
|
||||||
ssize_t TcpClientWrite(void *object, const void *buf, const size_t bufLenght);
|
ssize_t TcpClientWrite(void *object, const void *buf, const size_t bufLenght);
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
|
|
@ -14,6 +14,7 @@
|
||||||
*/
|
*/
|
||||||
#include "ITcpClient.h"
|
#include "ITcpClient.h"
|
||||||
#include "ILog.h"
|
#include "ILog.h"
|
||||||
|
#include "TcpModule.h"
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
void ITcpClient::Init(void)
|
void ITcpClient::Init(void)
|
||||||
{
|
{
|
||||||
|
@ -21,9 +22,15 @@ void ITcpClient::Init(void)
|
||||||
void ITcpClient::UnInit(void)
|
void ITcpClient::UnInit(void)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
void ITcpClient::Readed(const void *data, size_t length)
|
||||||
|
{
|
||||||
|
}
|
||||||
ssize_t ITcpClient::Write(const void *buf, const size_t bufLenght)
|
ssize_t ITcpClient::Write(const void *buf, const size_t bufLenght)
|
||||||
{
|
{
|
||||||
return WRITE_ERROR;
|
return TCP_MODULE_WRITE_ERROR;
|
||||||
|
}
|
||||||
|
void ITcpClient::Closed(void)
|
||||||
|
{
|
||||||
}
|
}
|
||||||
static const char *TCP_CLIENT_NAME = "tcp_client";
|
static const char *TCP_CLIENT_NAME = "tcp_client";
|
||||||
const char *GetTcpClientModuleName(void)
|
const char *GetTcpClientModuleName(void)
|
||||||
|
|
|
@ -16,7 +16,6 @@
|
||||||
#define I_TCP_CLIENT_H
|
#define I_TCP_CLIENT_H
|
||||||
#include "StatusCode.h"
|
#include "StatusCode.h"
|
||||||
#include <memory>
|
#include <memory>
|
||||||
constexpr int WRITE_ERROR = -1;
|
|
||||||
class ITcpClient
|
class ITcpClient
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -24,7 +23,9 @@ public:
|
||||||
virtual ~ITcpClient() = default;
|
virtual ~ITcpClient() = default;
|
||||||
virtual void Init(void);
|
virtual void Init(void);
|
||||||
virtual void UnInit(void);
|
virtual void UnInit(void);
|
||||||
|
virtual void Readed(const void *data, size_t length);
|
||||||
virtual ssize_t Write(const void *buf, const size_t bufLenght);
|
virtual ssize_t Write(const void *buf, const size_t bufLenght);
|
||||||
|
virtual void Closed(void);
|
||||||
};
|
};
|
||||||
typedef struct i_tcp_client_header
|
typedef struct i_tcp_client_header
|
||||||
{
|
{
|
||||||
|
|
|
@ -14,10 +14,21 @@
|
||||||
*/
|
*/
|
||||||
#include "ITcpServer.h"
|
#include "ITcpServer.h"
|
||||||
#include "ILog.h"
|
#include "ILog.h"
|
||||||
|
#include "TcpModule.h"
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
void ITcpClientAccept::SetParam(const ClientAcceptParam ¶m)
|
||||||
|
{
|
||||||
|
}
|
||||||
void ITcpClientAccept::Readed(const void *data, size_t length)
|
void ITcpClientAccept::Readed(const void *data, size_t length)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
ssize_t ITcpClientAccept::Write(const void *data, size_t length)
|
||||||
|
{
|
||||||
|
return TCP_MODULE_WRITE_ERROR;
|
||||||
|
}
|
||||||
|
void ITcpClientAccept::Closed(void)
|
||||||
|
{
|
||||||
|
}
|
||||||
void ITcpServer::Init(void)
|
void ITcpServer::Init(void)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
@ -34,3 +45,15 @@ const char *GetTcpClientAcceptName(void)
|
||||||
{
|
{
|
||||||
return TCP_CLIENT_ACCEPT_NAME;
|
return TCP_CLIENT_ACCEPT_NAME;
|
||||||
}
|
}
|
||||||
|
bool TcpClientAcceptObjectCheck(void *object)
|
||||||
|
{
|
||||||
|
if (nullptr == object) {
|
||||||
|
LogError("nullptr object!\n");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (*((const char **)(((char *)object) - sizeof(ITcpServerHeader))) != GetTcpClientAcceptName()) {
|
||||||
|
LogError("Illegal object!\n");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
|
@ -15,13 +15,17 @@
|
||||||
#ifndef I_TCP_SERVER_H
|
#ifndef I_TCP_SERVER_H
|
||||||
#define I_TCP_SERVER_H
|
#define I_TCP_SERVER_H
|
||||||
#include "StatusCode.h"
|
#include "StatusCode.h"
|
||||||
|
#include "TcpModule.h"
|
||||||
#include <memory>
|
#include <memory>
|
||||||
class ITcpClientAccept
|
class ITcpClientAccept
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
ITcpClientAccept() = default;
|
ITcpClientAccept() = default;
|
||||||
virtual ~ITcpClientAccept() = default;
|
virtual ~ITcpClientAccept() = default;
|
||||||
|
virtual void SetParam(const ClientAcceptParam ¶m);
|
||||||
virtual void Readed(const void *data, size_t length);
|
virtual void Readed(const void *data, size_t length);
|
||||||
|
virtual ssize_t Write(const void *data, size_t length);
|
||||||
|
virtual void Closed(void);
|
||||||
};
|
};
|
||||||
class ITcpServer
|
class ITcpServer
|
||||||
{
|
{
|
||||||
|
@ -47,4 +51,5 @@ typedef struct tcp_client_accept
|
||||||
} TcpClientAccept;
|
} TcpClientAccept;
|
||||||
const char *GetTcpServerModuleName(void);
|
const char *GetTcpServerModuleName(void);
|
||||||
const char *GetTcpClientAcceptName(void);
|
const char *GetTcpClientAcceptName(void);
|
||||||
|
bool TcpClientAcceptObjectCheck(void *object);
|
||||||
#endif
|
#endif
|
|
@ -15,38 +15,26 @@
|
||||||
#include "TcpClientImpl.h"
|
#include "TcpClientImpl.h"
|
||||||
#include "ILog.h"
|
#include "ILog.h"
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
TcpClientImpl::TcpClientImpl(const TcpParam param) : mParam(param)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
static void on_message(hio_t *io, void *buf, int len)
|
static void on_message(hio_t *io, void *buf, int len)
|
||||||
{
|
{
|
||||||
LogInfo("onmessage: %.*s\n", len, (char *)buf);
|
LogInfo("onmessage: %.*s\n", len, (char *)buf);
|
||||||
TcpClientImpl *cli = (TcpClientImpl *)hevent_userdata(io);
|
TcpClientImpl *tcpClient = (TcpClientImpl *)hevent_userdata(io);
|
||||||
// ...
|
tcpClient->Readed(buf, len);
|
||||||
}
|
}
|
||||||
static void on_connect(hio_t *io)
|
static void on_connect(hio_t *io)
|
||||||
{
|
{
|
||||||
LogInfo("onconnect: connfd=%d\n", hio_fd(io));
|
LogInfo("onconnect: connfd=%d\n", hio_fd(io));
|
||||||
TcpClientImpl *cli = (TcpClientImpl *)hevent_userdata(io);
|
|
||||||
// cli->connected = 1;
|
|
||||||
|
|
||||||
// hio_write(io, "hello\r\n", 7);
|
|
||||||
|
|
||||||
hio_setcb_read(io, on_message);
|
hio_setcb_read(io, on_message);
|
||||||
hio_read(io);
|
hio_read(io);
|
||||||
}
|
}
|
||||||
static void on_close(hio_t *io)
|
static void on_close(hio_t *io)
|
||||||
{
|
{
|
||||||
LogInfo("onclose: connfd=%d error=%d\n", hio_fd(io), hio_error(io));
|
LogInfo("onclose: connfd=%d error=%d\n", hio_fd(io), hio_error(io));
|
||||||
TcpClientImpl *cli = (TcpClientImpl *)hevent_userdata(io);
|
TcpClientImpl *tcpClient = (TcpClientImpl *)hevent_userdata(io);
|
||||||
// cli->connected = 0;
|
tcpClient->Closed();
|
||||||
// reconnect
|
}
|
||||||
// if (cli->reconn_setting && reconn_setting_can_retry(cli->reconn_setting)) {
|
TcpClientImpl::TcpClientImpl(const TcpClientParam ¶m, const void *object) : mParam(param), mObjectThis(object)
|
||||||
// uint32_t delay = reconn_setting_calc_delay(cli->reconn_setting);
|
{
|
||||||
// LogInfo("reconnect cnt=%d, delay=%d ...\n", cli->reconn_setting->cur_retry_cnt,
|
|
||||||
// cli->reconn_setting->cur_delay); cli->reconn_timer = htimer_add(cli->mLoop, reconnect_timer_cb, delay, 1);
|
|
||||||
// hevent_set_userdata(cli->reconn_timer, cli);
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
void TcpClientImpl::Init(void)
|
void TcpClientImpl::Init(void)
|
||||||
{
|
{
|
||||||
|
@ -85,14 +73,30 @@ void TcpClientImpl::UnInit(void)
|
||||||
mLoop = nullptr;
|
mLoop = nullptr;
|
||||||
mIo = nullptr;
|
mIo = nullptr;
|
||||||
}
|
}
|
||||||
|
void TcpClientImpl::Readed(const void *data, size_t length)
|
||||||
|
{
|
||||||
|
if (nullptr != mParam.mReadFunc) {
|
||||||
|
mParam.mReadFunc(data, length, nullptr);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
LogError("mParam.mReadFunc is nullptr.\n");
|
||||||
|
}
|
||||||
ssize_t TcpClientImpl::Write(const void *buf, const size_t bufLenght)
|
ssize_t TcpClientImpl::Write(const void *buf, const size_t bufLenght)
|
||||||
{
|
{
|
||||||
if (nullptr == mIo) {
|
if (nullptr == mIo) {
|
||||||
LogError("mIo is nullptr.\n");
|
LogError("mIo is nullptr.\n");
|
||||||
return WRITE_ERROR;
|
return TCP_MODULE_WRITE_ERROR;
|
||||||
}
|
}
|
||||||
return hio_write(mIo, buf, bufLenght);
|
return hio_write(mIo, buf, bufLenght);
|
||||||
}
|
}
|
||||||
|
void TcpClientImpl::Closed(void)
|
||||||
|
{
|
||||||
|
if (nullptr != mParam.mClosedFunc) {
|
||||||
|
mParam.mClosedFunc(mObjectThis);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
LogWarning("mParam.mClosedFunc is null\n");
|
||||||
|
}
|
||||||
void TcpClientImpl::Loop(void)
|
void TcpClientImpl::Loop(void)
|
||||||
{
|
{
|
||||||
if (nullptr == mLoop) {
|
if (nullptr == mLoop) {
|
||||||
|
@ -101,7 +105,7 @@ void TcpClientImpl::Loop(void)
|
||||||
}
|
}
|
||||||
hloop_run(mLoop);
|
hloop_run(mLoop);
|
||||||
}
|
}
|
||||||
std::shared_ptr<ITcpClient> *NewTcpClient(const TcpParam param)
|
std::shared_ptr<ITcpClient> *NewTcpClient(const TcpClientParam ¶m)
|
||||||
{
|
{
|
||||||
LogInfo("Create tcp server object.\n");
|
LogInfo("Create tcp server object.\n");
|
||||||
TcpClient *impl = (TcpClient *)malloc(sizeof(TcpClient));
|
TcpClient *impl = (TcpClient *)malloc(sizeof(TcpClient));
|
||||||
|
@ -112,6 +116,8 @@ std::shared_ptr<ITcpClient> *NewTcpClient(const TcpParam param)
|
||||||
TcpClient tmp;
|
TcpClient tmp;
|
||||||
memcpy((void *)impl, (void *)&tmp, sizeof(TcpClient));
|
memcpy((void *)impl, (void *)&tmp, sizeof(TcpClient));
|
||||||
impl->mHeader.mCheckName = GetTcpClientModuleName();
|
impl->mHeader.mCheckName = GetTcpClientModuleName();
|
||||||
impl->mTcpClient = std::make_shared<TcpClientImpl>(param);
|
std::shared_ptr<ITcpClient> *objectThis =
|
||||||
return (std::shared_ptr<ITcpClient> *)(((char *)impl) + sizeof(ITcpClientHeader));
|
(std::shared_ptr<ITcpClient> *)(((char *)impl) + sizeof(ITcpClientHeader));
|
||||||
|
impl->mTcpClient = std::make_shared<TcpClientImpl>(param, objectThis);
|
||||||
|
return objectThis;
|
||||||
}
|
}
|
|
@ -24,18 +24,21 @@
|
||||||
class TcpClientImpl : public ITcpClient, public std::enable_shared_from_this<TcpClientImpl>
|
class TcpClientImpl : public ITcpClient, public std::enable_shared_from_this<TcpClientImpl>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
TcpClientImpl(const TcpParam param);
|
TcpClientImpl(const TcpClientParam ¶m, const void *object);
|
||||||
virtual ~TcpClientImpl() = default;
|
virtual ~TcpClientImpl() = default;
|
||||||
void Init(void) override;
|
void Init(void) override;
|
||||||
void UnInit(void) override;
|
void UnInit(void) override;
|
||||||
|
void Readed(const void *data, size_t length) override;
|
||||||
ssize_t Write(const void *buf, const size_t bufLenght) override;
|
ssize_t Write(const void *buf, const size_t bufLenght) override;
|
||||||
|
void Closed(void) override;
|
||||||
void Loop(void);
|
void Loop(void);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
hloop_t* mLoop;
|
hloop_t *mLoop;
|
||||||
hio_t *mIo;
|
hio_t *mIo;
|
||||||
const TcpParam mParam;
|
const TcpClientParam mParam;
|
||||||
std::thread mTcpClientThread;
|
std::thread mTcpClientThread;
|
||||||
|
const void *mObjectThis;
|
||||||
};
|
};
|
||||||
std::shared_ptr<ITcpClient> *NewTcpClient(const TcpParam param);
|
std::shared_ptr<ITcpClient> *NewTcpClient(const TcpClientParam ¶m);
|
||||||
#endif
|
#endif
|
|
@ -42,7 +42,7 @@ static bool TcpClientObjectCheck(void *object)
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
void *CreateTcpServer(const TcpParam param)
|
void *CreateTcpServer(const TcpServerParam param)
|
||||||
{
|
{
|
||||||
std::shared_ptr<ITcpServer> *server = TcpModuleMakePtr::GetInstance()->CreateTcpServer(param);
|
std::shared_ptr<ITcpServer> *server = TcpModuleMakePtr::GetInstance()->CreateTcpServer(param);
|
||||||
if (nullptr != *server) {
|
if (nullptr != *server) {
|
||||||
|
@ -58,7 +58,20 @@ void FreeTcpServer(void *object)
|
||||||
free(((char *)object) - sizeof(ITcpServerHeader)); // TODO: bug?
|
free(((char *)object) - sizeof(ITcpServerHeader)); // TODO: bug?
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void *CreateTcpClient(const TcpParam param)
|
void AcceptClientSetParam(void *object, const ClientAcceptParam param)
|
||||||
|
{
|
||||||
|
if (TcpClientAcceptObjectCheck(object) == true) {
|
||||||
|
(*(std::shared_ptr<ITcpClientAccept> *)object)->SetParam(param);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ssize_t AcceptClientWrite(void *object, const void *buf, const size_t bufLenght)
|
||||||
|
{
|
||||||
|
if (TcpClientAcceptObjectCheck(object) == true) {
|
||||||
|
return (*(std::shared_ptr<ITcpClientAccept> *)object)->Write(buf, bufLenght);
|
||||||
|
}
|
||||||
|
return TCP_MODULE_WRITE_ERROR;
|
||||||
|
}
|
||||||
|
void *CreateTcpClient(const TcpClientParam param)
|
||||||
{
|
{
|
||||||
std::shared_ptr<ITcpClient> *client = TcpModuleMakePtr::GetInstance()->CreateTcpClient(param);
|
std::shared_ptr<ITcpClient> *client = TcpModuleMakePtr::GetInstance()->CreateTcpClient(param);
|
||||||
if (nullptr != *client) {
|
if (nullptr != *client) {
|
||||||
|
|
|
@ -20,7 +20,7 @@
|
||||||
// {
|
// {
|
||||||
// return TCP_MODULE_NAME;
|
// return TCP_MODULE_NAME;
|
||||||
// }
|
// }
|
||||||
// void *NewTcpModuleImpl(const TcpParam &tcpParam)
|
// void *NewTcpModuleImpl(const TcpClientParam &tcpParam)
|
||||||
// {
|
// {
|
||||||
// if (nullptr == tcpParam.mIp) {
|
// if (nullptr == tcpParam.mIp) {
|
||||||
// LogError("Parament error, nullptr == tcpParam.mIp\n");
|
// LogError("Parament error, nullptr == tcpParam.mIp\n");
|
||||||
|
@ -34,7 +34,7 @@
|
||||||
// impl->mTcpImpl = std::make_shared<TcpModuleImpl>(tcpParam);
|
// impl->mTcpImpl = std::make_shared<TcpModuleImpl>(tcpParam);
|
||||||
// return (void *)(((char *)impl) + sizeof(TcpModuleHeader));
|
// return (void *)(((char *)impl) + sizeof(TcpModuleHeader));
|
||||||
// }
|
// }
|
||||||
// void *NewTcpServer(const TcpParam &tcpParam)
|
// void *NewTcpServer(const TcpClientParam &tcpParam)
|
||||||
// {
|
// {
|
||||||
// return nullptr;
|
// return nullptr;
|
||||||
// }
|
// }
|
|
@ -25,14 +25,14 @@
|
||||||
// class TcpModuleImpl
|
// class TcpModuleImpl
|
||||||
// {
|
// {
|
||||||
// public:
|
// public:
|
||||||
// TcpModuleImpl(const TcpParam &uatrInfo);
|
// TcpModuleImpl(const TcpClientParam &uatrInfo);
|
||||||
// virtual ~TcpModuleImpl() = default;
|
// virtual ~TcpModuleImpl() = default;
|
||||||
|
|
||||||
// private:
|
// private:
|
||||||
// const StatusCode SetConfig(void);
|
// const StatusCode SetConfig(void);
|
||||||
|
|
||||||
// private:
|
// private:
|
||||||
// const TcpParam mUatrInfo;
|
// const TcpClientParam mUatrInfo;
|
||||||
// int mFd;
|
// int mFd;
|
||||||
// };
|
// };
|
||||||
// TODO: There may be a CPU byte alignment bug.
|
// TODO: There may be a CPU byte alignment bug.
|
||||||
|
@ -42,8 +42,8 @@
|
||||||
// TcpModuleHeader mHeader;
|
// TcpModuleHeader mHeader;
|
||||||
// std::shared_ptr<TcpModuleImpl> mTcpImpl;
|
// std::shared_ptr<TcpModuleImpl> mTcpImpl;
|
||||||
// } TcpServer;
|
// } TcpServer;
|
||||||
// void *NewTcpModuleImpl(const TcpParam &tcpParam);
|
// void *NewTcpModuleImpl(const TcpClientParam &tcpParam);
|
||||||
// void *NewTcpServer(const TcpParam &tcpParam);
|
// void *NewTcpServer(const TcpClientParam &tcpParam);
|
||||||
// static inline TcpServer *TcpModuleImplConvert(void *object)
|
// static inline TcpServer *TcpModuleImplConvert(void *object)
|
||||||
// {
|
// {
|
||||||
// return ((TcpServer *)(((char *)object) - sizeof(TcpModuleHeader)));
|
// return ((TcpServer *)(((char *)object) - sizeof(TcpModuleHeader)));
|
||||||
|
|
|
@ -14,8 +14,8 @@
|
||||||
*/
|
*/
|
||||||
#include "TcpModuleMakePtr.h"
|
#include "TcpModuleMakePtr.h"
|
||||||
#include "ILog.h"
|
#include "ILog.h"
|
||||||
#include "TcpServerImpl.h"
|
|
||||||
#include "TcpClientImpl.h"
|
#include "TcpClientImpl.h"
|
||||||
|
#include "TcpServerImpl.h"
|
||||||
std::shared_ptr<TcpModuleMakePtr> &TcpModuleMakePtr::GetInstance(std::shared_ptr<TcpModuleMakePtr> *impl)
|
std::shared_ptr<TcpModuleMakePtr> &TcpModuleMakePtr::GetInstance(std::shared_ptr<TcpModuleMakePtr> *impl)
|
||||||
{
|
{
|
||||||
static auto instance = std::make_shared<TcpModuleMakePtr>();
|
static auto instance = std::make_shared<TcpModuleMakePtr>();
|
||||||
|
@ -24,11 +24,11 @@ std::shared_ptr<TcpModuleMakePtr> &TcpModuleMakePtr::GetInstance(std::shared_ptr
|
||||||
}
|
}
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
std::shared_ptr<ITcpServer> *TcpModuleMakePtr::CreateTcpServer(const TcpParam param)
|
std::shared_ptr<ITcpServer> *TcpModuleMakePtr::CreateTcpServer(const TcpServerParam ¶m)
|
||||||
{
|
{
|
||||||
return NewTcpServer(param);
|
return NewTcpServer(param);
|
||||||
}
|
}
|
||||||
std::shared_ptr<ITcpClient> *TcpModuleMakePtr::CreateTcpClient(const TcpParam param)
|
std::shared_ptr<ITcpClient> *TcpModuleMakePtr::CreateTcpClient(const TcpClientParam ¶m)
|
||||||
{
|
{
|
||||||
return NewTcpClient(param);
|
return NewTcpClient(param);
|
||||||
}
|
}
|
|
@ -14,10 +14,10 @@
|
||||||
*/
|
*/
|
||||||
#ifndef TCP_MODULE_MAKE_PTR_H
|
#ifndef TCP_MODULE_MAKE_PTR_H
|
||||||
#define TCP_MODULE_MAKE_PTR_H
|
#define TCP_MODULE_MAKE_PTR_H
|
||||||
|
#include "ITcpClient.h"
|
||||||
|
#include "ITcpServer.h"
|
||||||
#include "StatusCode.h"
|
#include "StatusCode.h"
|
||||||
#include "TcpModule.h"
|
#include "TcpModule.h"
|
||||||
#include "ITcpServer.h"
|
|
||||||
#include "ITcpClient.h"
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
class TcpModuleMakePtr
|
class TcpModuleMakePtr
|
||||||
{
|
{
|
||||||
|
@ -25,7 +25,7 @@ public:
|
||||||
TcpModuleMakePtr() = default;
|
TcpModuleMakePtr() = default;
|
||||||
virtual ~TcpModuleMakePtr() = default;
|
virtual ~TcpModuleMakePtr() = default;
|
||||||
static std::shared_ptr<TcpModuleMakePtr> &GetInstance(std::shared_ptr<TcpModuleMakePtr> *impl = nullptr);
|
static std::shared_ptr<TcpModuleMakePtr> &GetInstance(std::shared_ptr<TcpModuleMakePtr> *impl = nullptr);
|
||||||
virtual std::shared_ptr<ITcpServer> *CreateTcpServer(const TcpParam param);
|
virtual std::shared_ptr<ITcpServer> *CreateTcpServer(const TcpServerParam ¶m);
|
||||||
virtual std::shared_ptr<ITcpClient> *CreateTcpClient(const TcpParam param);
|
virtual std::shared_ptr<ITcpClient> *CreateTcpClient(const TcpClientParam ¶m);
|
||||||
};
|
};
|
||||||
#endif // !TCP_MODULE_MAKE_PTR_H
|
#endif // !TCP_MODULE_MAKE_PTR_H
|
|
@ -15,28 +15,11 @@
|
||||||
#include "TcpServerImpl.h"
|
#include "TcpServerImpl.h"
|
||||||
#include "ILog.h"
|
#include "ILog.h"
|
||||||
#include "TcpServerHandle.h"
|
#include "TcpServerHandle.h"
|
||||||
static bool TcpClientAcceptObjectCheck(void *object)
|
|
||||||
{
|
|
||||||
if (nullptr == object) {
|
|
||||||
LogError("nullptr object!\n");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (*((const char **)(((char *)object) - sizeof(ITcpServerHeader))) != GetTcpClientAcceptName()) {
|
|
||||||
LogError("Illegal object!\n");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
static void on_close(hio_t *io)
|
static void on_close(hio_t *io)
|
||||||
{
|
{
|
||||||
LogInfo("on_close fd=%d error=%d\n", hio_fd(io), hio_error(io));
|
LogInfo("on_close fd=%d error=%d\n", hio_fd(io), hio_error(io));
|
||||||
// std::shared_ptr<ITcpServer> server;
|
TcpServerImpl *server = (TcpServerImpl *)hevent_userdata(io);
|
||||||
// if (TcpServerHandle::GetInstance()->GetServer(hio_fd(io), server) == true) {
|
server->RemoveClient(io);
|
||||||
// std::shared_ptr<TcpServerImpl> impl = std::dynamic_pointer_cast<TcpServerImpl>(server);
|
|
||||||
// if (impl != nullptr) {
|
|
||||||
// impl->ClosedEvent();
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
static void on_recv(hio_t *io, void *buf, int readbytes)
|
static void on_recv(hio_t *io, void *buf, int readbytes)
|
||||||
{
|
{
|
||||||
|
@ -46,19 +29,9 @@ static void on_recv(hio_t *io, void *buf, int readbytes)
|
||||||
LogInfo(
|
LogInfo(
|
||||||
"[%s] <=> [%s]\n", SOCKADDR_STR(hio_localaddr(io), localaddrstr), SOCKADDR_STR(hio_peeraddr(io), peeraddrstr));
|
"[%s] <=> [%s]\n", SOCKADDR_STR(hio_localaddr(io), localaddrstr), SOCKADDR_STR(hio_peeraddr(io), peeraddrstr));
|
||||||
LogInfo("< %.*s", readbytes, (char *)buf);
|
LogInfo("< %.*s", readbytes, (char *)buf);
|
||||||
ITcpClientAccept *client = (ITcpClientAccept *)hevent_userdata(io);
|
TcpServerImpl *server = (TcpServerImpl *)hevent_userdata(io);
|
||||||
client->Readed((const char *)buf, readbytes);
|
std::shared_ptr<ITcpClientAccept> *client = server->GetClient(io);
|
||||||
// server->RemoveClient(hio_fd(io));
|
(*client)->Readed((const char *)buf, readbytes);
|
||||||
// std::shared_ptr<ITcpServer> server;
|
|
||||||
// if (TcpServerHandle::GetInstance()->GetServer(hio_fd(io), server) == true) {
|
|
||||||
// std::shared_ptr<TcpServerImpl> impl = std::dynamic_pointer_cast<TcpServerImpl>(server);
|
|
||||||
// if (impl != nullptr) {
|
|
||||||
// impl->TcpServerReaded(buf, readbytes);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// echo
|
|
||||||
// LogInfo("> %.*s", readbytes, (char *)buf);
|
|
||||||
// hio_write(io, buf, readbytes);
|
|
||||||
}
|
}
|
||||||
static void on_accept(hio_t *io)
|
static void on_accept(hio_t *io)
|
||||||
{
|
{
|
||||||
|
@ -74,25 +47,47 @@ static void on_accept(hio_t *io)
|
||||||
hio_setcb_read(io, on_recv);
|
hio_setcb_read(io, on_recv);
|
||||||
std::shared_ptr<ITcpClientAccept> *client = NewTcpClientAccept(io);
|
std::shared_ptr<ITcpClientAccept> *client = NewTcpClientAccept(io);
|
||||||
TcpServerImpl *server = (TcpServerImpl *)hevent_userdata(io);
|
TcpServerImpl *server = (TcpServerImpl *)hevent_userdata(io);
|
||||||
// std::shared_ptr<ITcpServer> server;
|
|
||||||
// if (TcpServerHandle::GetInstance()->GetServer(hio_fd(io), server) == true) {
|
|
||||||
// std::shared_ptr<TcpServerImpl> impl = std::dynamic_pointer_cast<TcpServerImpl>(server);
|
|
||||||
// if (impl != nullptr) {
|
|
||||||
// impl->SetIo(io);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
server->AddClient(io, client);
|
server->AddClient(io, client);
|
||||||
hevent_set_userdata(io, (*client).get());
|
|
||||||
hio_read_start(io);
|
hio_read_start(io);
|
||||||
}
|
}
|
||||||
TcpClientAcceptImpl::TcpClientAcceptImpl(const hio_t *io) : mIo(io)
|
TcpClientAcceptImpl::TcpClientAcceptImpl(const hio_t *io, const void *object) : mIo(io), mObjectThis(object)
|
||||||
{
|
{
|
||||||
|
mParam.mReadFunc = nullptr;
|
||||||
|
mParam.mClosedFunc = nullptr;
|
||||||
|
}
|
||||||
|
void TcpClientAcceptImpl::SetParam(const ClientAcceptParam ¶m)
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> locker(mMutex);
|
||||||
|
mParam = param;
|
||||||
}
|
}
|
||||||
void TcpClientAcceptImpl::Readed(const void *data, size_t length)
|
void TcpClientAcceptImpl::Readed(const void *data, size_t length)
|
||||||
{
|
{
|
||||||
LogInfo("TcpClientAcceptImpl::Readed\n");
|
std::lock_guard<std::mutex> locker(mMutex);
|
||||||
|
if (nullptr != mParam.mReadFunc) {
|
||||||
|
mParam.mReadFunc(data, length, mObjectThis);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
LogWarning("mParam.mClosedFunc is null\n");
|
||||||
}
|
}
|
||||||
TcpServerImpl::TcpServerImpl(const TcpParam param) : mParam(param)
|
ssize_t TcpClientAcceptImpl::Write(const void *data, size_t length)
|
||||||
|
{
|
||||||
|
if (mIo) {
|
||||||
|
hio_t *io = (hio_t *)mIo;
|
||||||
|
return hio_write(io, data, length);
|
||||||
|
}
|
||||||
|
LogError("mIo is null\n");
|
||||||
|
return TCP_MODULE_WRITE_ERROR;
|
||||||
|
}
|
||||||
|
void TcpClientAcceptImpl::Closed(void)
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> locker(mMutex);
|
||||||
|
if (nullptr != mParam.mClosedFunc) {
|
||||||
|
mParam.mClosedFunc(mObjectThis);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
LogWarning("mParam.mClosedFunc is null\n");
|
||||||
|
}
|
||||||
|
TcpServerImpl::TcpServerImpl(const TcpServerParam param) : mParam(param)
|
||||||
{
|
{
|
||||||
mLoop = nullptr;
|
mLoop = nullptr;
|
||||||
mIo = nullptr;
|
mIo = nullptr;
|
||||||
|
@ -114,7 +109,6 @@ void TcpServerImpl::Init(void)
|
||||||
hevent_set_userdata(listenio, this);
|
hevent_set_userdata(listenio, this);
|
||||||
mIo = listenio;
|
mIo = listenio;
|
||||||
std::shared_ptr<ITcpServer> server = shared_from_this();
|
std::shared_ptr<ITcpServer> server = shared_from_this();
|
||||||
// TcpServerHandle::GetInstance()->AddServer(hio_fd(listenio), server);
|
|
||||||
std::shared_ptr<TcpServerImpl> impl = std::dynamic_pointer_cast<TcpServerImpl>(server);
|
std::shared_ptr<TcpServerImpl> impl = std::dynamic_pointer_cast<TcpServerImpl>(server);
|
||||||
auto recvThread = [](std::shared_ptr<TcpServerImpl> tcpServer) {
|
auto recvThread = [](std::shared_ptr<TcpServerImpl> tcpServer) {
|
||||||
tcpServer->Loop();
|
tcpServer->Loop();
|
||||||
|
@ -136,22 +130,6 @@ void TcpServerImpl::UnInit(void)
|
||||||
mLoop = nullptr;
|
mLoop = nullptr;
|
||||||
mIo = nullptr;
|
mIo = nullptr;
|
||||||
}
|
}
|
||||||
void TcpServerImpl::TcpServerReaded(const void *buf, size_t length)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
void TcpServerImpl::TcpServerWrite(const void *buf, size_t length)
|
|
||||||
{
|
|
||||||
if (mIo) {
|
|
||||||
hio_write(mIo, buf, length);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
LogError("mIo is null\n");
|
|
||||||
}
|
|
||||||
void TcpServerImpl::ClosedEvent(void)
|
|
||||||
{
|
|
||||||
mLoop = nullptr;
|
|
||||||
mIo = nullptr;
|
|
||||||
}
|
|
||||||
void TcpServerImpl::Loop(void)
|
void TcpServerImpl::Loop(void)
|
||||||
{
|
{
|
||||||
if (nullptr == mLoop) {
|
if (nullptr == mLoop) {
|
||||||
|
@ -162,6 +140,7 @@ void TcpServerImpl::Loop(void)
|
||||||
}
|
}
|
||||||
void TcpServerImpl::AddClient(hio_t *io, std::shared_ptr<ITcpClientAccept> *client)
|
void TcpServerImpl::AddClient(hio_t *io, std::shared_ptr<ITcpClientAccept> *client)
|
||||||
{
|
{
|
||||||
|
std::lock_guard<std::mutex> locker(mMutex);
|
||||||
char localaddrstr[SOCKADDR_STRLEN] = {0};
|
char localaddrstr[SOCKADDR_STRLEN] = {0};
|
||||||
char peeraddrstr[SOCKADDR_STRLEN] = {0};
|
char peeraddrstr[SOCKADDR_STRLEN] = {0};
|
||||||
LogInfo("accept connfd=%d [%s] <= [%s]\n",
|
LogInfo("accept connfd=%d [%s] <= [%s]\n",
|
||||||
|
@ -173,37 +152,51 @@ void TcpServerImpl::AddClient(hio_t *io, std::shared_ptr<ITcpClientAccept> *clie
|
||||||
mParam.mAcceptClientFunc(client, peeraddrstr);
|
mParam.mAcceptClientFunc(client, peeraddrstr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
std::shared_ptr<ITcpClientAccept> *TcpServerImpl::GetClient(hio_t *io)
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> locker(mMutex);
|
||||||
|
auto it = mClients.find(hio_fd(io));
|
||||||
|
if (it != mClients.end()) {
|
||||||
|
return it->second;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
LogError("GetClient failed, client not exit.\n");
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void TcpServerImpl::RemoveClient(hio_t *io)
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> locker(mMutex);
|
||||||
|
auto it = mClients.find(hio_fd(io));
|
||||||
|
if (it != mClients.end()) {
|
||||||
|
void *object = (void *)it->second;
|
||||||
|
if (TcpClientAcceptObjectCheck(object) == true) {
|
||||||
|
(*(std::shared_ptr<ITcpClientAccept> *)object)->Closed();
|
||||||
|
(*(std::shared_ptr<ITcpClientAccept> *)object).reset();
|
||||||
|
free(((char *)object) - sizeof(ITcpServerHeader));
|
||||||
|
}
|
||||||
|
mClients.erase(it);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
LogError("RemoveClient failed, client not exit.\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
void TcpServerImpl::FreeClients(void)
|
void TcpServerImpl::FreeClients(void)
|
||||||
{
|
{
|
||||||
|
std::lock_guard<std::mutex> locker(mMutex);
|
||||||
for (auto &client : mClients) {
|
for (auto &client : mClients) {
|
||||||
if (nullptr != client.second) {
|
if (nullptr != client.second) {
|
||||||
// delete client.second;
|
|
||||||
// client.second = nullptr;
|
|
||||||
void *object = (void *)client.second;
|
void *object = (void *)client.second;
|
||||||
if (TcpClientAcceptObjectCheck(object) == true) {
|
if (TcpClientAcceptObjectCheck(object) == true) {
|
||||||
// (*(std::shared_ptr<ITcpClientAccept> *)object)->UnInit();
|
(*(std::shared_ptr<ITcpClientAccept> *)object)->Closed();
|
||||||
(*(std::shared_ptr<ITcpClientAccept> *)object).reset();
|
(*(std::shared_ptr<ITcpClientAccept> *)object).reset();
|
||||||
free(((char *)object) - sizeof(ITcpServerHeader)); // TODO: bug?
|
free(((char *)object) - sizeof(ITcpServerHeader));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// for (auto it = mClients.begin(); it != mClients.end();) {
|
mClients.clear();
|
||||||
// // 检查指针是否为空
|
|
||||||
// if (it->second != nullptr) {
|
|
||||||
// // 释放 shared_ptr,这将减少引用计数
|
|
||||||
// it->second->reset();
|
|
||||||
// // 删除指针
|
|
||||||
// delete it->second;
|
|
||||||
// // 从 map 中删除条目
|
|
||||||
// it = mClients.erase(it);
|
|
||||||
// }
|
|
||||||
// else {
|
|
||||||
// // 指针为空,继续迭代
|
|
||||||
// ++it;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
std::shared_ptr<ITcpServer> *NewTcpServer(const TcpParam param)
|
std::shared_ptr<ITcpServer> *NewTcpServer(const TcpServerParam ¶m)
|
||||||
{
|
{
|
||||||
LogInfo("Create tcp server object.\n");
|
LogInfo("Create tcp server object.\n");
|
||||||
TcpServer *impl = (TcpServer *)malloc(sizeof(TcpServer));
|
TcpServer *impl = (TcpServer *)malloc(sizeof(TcpServer));
|
||||||
|
@ -228,6 +221,8 @@ std::shared_ptr<ITcpClientAccept> *NewTcpClientAccept(const hio_t *io)
|
||||||
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();
|
||||||
impl->mTcpClientAccept = std::make_shared<TcpClientAcceptImpl>(io);
|
std::shared_ptr<ITcpClientAccept> *objectThis =
|
||||||
return (std::shared_ptr<ITcpClientAccept> *)(((char *)impl) + sizeof(ITcpServerHeader));
|
(std::shared_ptr<ITcpClientAccept> *)(((char *)impl) + sizeof(ITcpServerHeader));
|
||||||
|
impl->mTcpClientAccept = std::make_shared<TcpClientAcceptImpl>(io, objectThis);
|
||||||
|
return objectThis;
|
||||||
}
|
}
|
|
@ -20,38 +20,45 @@
|
||||||
#include "hsocket.h"
|
#include "hsocket.h"
|
||||||
#include "hssl.h"
|
#include "hssl.h"
|
||||||
#include <map>
|
#include <map>
|
||||||
|
#include <mutex>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
class TcpClientAcceptImpl : public ITcpClientAccept
|
class TcpClientAcceptImpl : public ITcpClientAccept, public std::enable_shared_from_this<TcpClientAcceptImpl>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
TcpClientAcceptImpl(const hio_t *io);
|
TcpClientAcceptImpl(const hio_t *io, const void *object);
|
||||||
virtual ~TcpClientAcceptImpl() = default;
|
virtual ~TcpClientAcceptImpl() = default;
|
||||||
|
void SetParam(const ClientAcceptParam ¶m) override;
|
||||||
void Readed(const void *data, size_t length) override;
|
void Readed(const void *data, size_t length) override;
|
||||||
|
ssize_t Write(const void *data, size_t length) override;
|
||||||
|
void Closed(void) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
std::mutex mMutex;
|
||||||
const hio_t *mIo;
|
const hio_t *mIo;
|
||||||
|
ClientAcceptParam mParam;
|
||||||
|
const void *mObjectThis;
|
||||||
};
|
};
|
||||||
class TcpServerImpl : public ITcpServer, public std::enable_shared_from_this<TcpServerImpl>
|
class TcpServerImpl : public ITcpServer, public std::enable_shared_from_this<TcpServerImpl>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
TcpServerImpl(const TcpParam param);
|
TcpServerImpl(const TcpServerParam param);
|
||||||
virtual ~TcpServerImpl() = default;
|
virtual ~TcpServerImpl() = default;
|
||||||
void Init(void) override;
|
void Init(void) override;
|
||||||
void UnInit(void) override;
|
void UnInit(void) override;
|
||||||
void TcpServerReaded(const void *buf, size_t length);
|
|
||||||
void TcpServerWrite(const void *buf, size_t length);
|
|
||||||
void ClosedEvent(void);
|
|
||||||
void Loop(void);
|
void Loop(void);
|
||||||
void AddClient(hio_t *io, std::shared_ptr<ITcpClientAccept> *client);
|
void AddClient(hio_t *io, std::shared_ptr<ITcpClientAccept> *client);
|
||||||
|
std::shared_ptr<ITcpClientAccept> *GetClient(hio_t *io);
|
||||||
|
void RemoveClient(hio_t *io);
|
||||||
void FreeClients(void);
|
void FreeClients(void);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
std::mutex mMutex;
|
||||||
hloop_t *mLoop;
|
hloop_t *mLoop;
|
||||||
hio_t *mIo;
|
hio_t *mIo;
|
||||||
const TcpParam mParam;
|
const TcpServerParam mParam;
|
||||||
std::thread mTcpServerThread;
|
std::thread mTcpServerThread;
|
||||||
std::map<int, std::shared_ptr<ITcpClientAccept> *> mClients;
|
std::map<int, std::shared_ptr<ITcpClientAccept> *> mClients;
|
||||||
};
|
};
|
||||||
std::shared_ptr<ITcpServer> *NewTcpServer(const TcpParam param);
|
std::shared_ptr<ITcpServer> *NewTcpServer(const TcpServerParam ¶m);
|
||||||
std::shared_ptr<ITcpClientAccept> *NewTcpClientAccept(const hio_t *io);
|
std::shared_ptr<ITcpClientAccept> *NewTcpClientAccept(const hio_t *io);
|
||||||
#endif
|
#endif
|
Loading…
Reference in New Issue
Block a user