Improve:TcpModule.
This commit is contained in:
parent
a7fc63285c
commit
7ebd2e3aa7
|
@ -25,14 +25,23 @@ namespace TcpModuleTest
|
|||
*/
|
||||
TEST(TcpModuleTest, UNIT_TcpModule_EXAMPLE_AUTO_IllegalObject)
|
||||
{
|
||||
static void *tcpClientAccept = nullptr;
|
||||
TcpServerParam tcpServerparam = {
|
||||
.mIp = "127.0.0.1",
|
||||
.mPort = 9876,
|
||||
.mAcceptClientFunc = [](void *object, const char *ip) -> void {
|
||||
LogInfo("accept client, peer ip: %s", ip);
|
||||
tcpClientAccept = object;
|
||||
return;
|
||||
.mAcceptClientFunc = [](void *object, const char *ip) -> bool {
|
||||
LogInfo("accept client, peer ip: %s\n", ip);
|
||||
if (nullptr != object) {
|
||||
AcceptClientWrite(object, "client accept send data.", strlen("client accept send data."));
|
||||
}
|
||||
return true;
|
||||
},
|
||||
.mClientAcceptParam = {
|
||||
.mReadFunc = [](const void *data, const ssize_t len, const void *object) -> void {
|
||||
LogInfo("client accept read data: %s\n", (char *)data);
|
||||
},
|
||||
.mClosedFunc = [](const void *object) -> void {
|
||||
LogInfo("client accept closed.\n");
|
||||
},
|
||||
},
|
||||
};
|
||||
TcpClientParam param = {
|
||||
|
@ -42,6 +51,9 @@ TEST(TcpModuleTest, UNIT_TcpModule_EXAMPLE_AUTO_IllegalObject)
|
|||
LogInfo("read data: %s", (char *)data);
|
||||
return;
|
||||
},
|
||||
.mClosedFunc = [](const void *object) -> void {
|
||||
LogInfo("tcp client closed.\n");
|
||||
},
|
||||
};
|
||||
CreateLogModule();
|
||||
ILogInit(LOG_INSTANCE_TYPE_END);
|
||||
|
@ -49,11 +61,7 @@ TEST(TcpModuleTest, UNIT_TcpModule_EXAMPLE_AUTO_IllegalObject)
|
|||
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
||||
void *tcpClient = CreateTcpClient(param);
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
TcpClientWrite(tcpClient, "123456789", strlen("123456789"));
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
if (nullptr != tcpClientAccept) {
|
||||
AcceptClientWrite(tcpClientAccept, "9876543210", strlen("9876543210"));
|
||||
}
|
||||
TcpClientWrite(tcpClient, "client send data.", strlen("client send data."));
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
|
||||
if (nullptr != tcpClient) {
|
||||
FreeTcpClient(tcpClient);
|
||||
|
|
|
@ -21,26 +21,27 @@
|
|||
extern "C" {
|
||||
#endif
|
||||
typedef void (*TcpReadFunction)(const void *, const ssize_t, const void *);
|
||||
typedef void (*TcpAcceptClientFunction)(void *, const char *);
|
||||
typedef bool (*TcpAcceptClientFunction)(void *, const char *);
|
||||
typedef void (*SocketClosedFunction)(const void *);
|
||||
typedef struct client_accept_parm
|
||||
{
|
||||
const TcpReadFunction mReadFunc;
|
||||
const SocketClosedFunction mClosedFunc;
|
||||
} ClientAcceptParam;
|
||||
typedef struct tcp_server_parm
|
||||
{
|
||||
const char *mIp;
|
||||
const int mPort;
|
||||
TcpAcceptClientFunction mAcceptClientFunc;
|
||||
const TcpAcceptClientFunction mAcceptClientFunc;
|
||||
const ClientAcceptParam mClientAcceptParam;
|
||||
} TcpServerParam;
|
||||
typedef struct tcp_parm
|
||||
{
|
||||
const char *mIp;
|
||||
const int mPort;
|
||||
TcpReadFunction mReadFunc;
|
||||
SocketClosedFunction mClosedFunc;
|
||||
const TcpReadFunction mReadFunc;
|
||||
const SocketClosedFunction mClosedFunc;
|
||||
} TcpClientParam;
|
||||
typedef struct client_accept_parm
|
||||
{
|
||||
TcpReadFunction mReadFunc;
|
||||
SocketClosedFunction mClosedFunc;
|
||||
} ClientAcceptParam;
|
||||
void *CreateTcpServer(const TcpServerParam param);
|
||||
void FreeTcpServer(void *object);
|
||||
void AcceptClientSetParam(void *object, const ClientAcceptParam param);
|
||||
|
|
|
@ -45,24 +45,21 @@ static void on_accept(hio_t *io)
|
|||
|
||||
hio_setcb_close(io, on_close);
|
||||
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);
|
||||
server->AddClient(io, client);
|
||||
server->AddClient(io);
|
||||
hio_read_start(io);
|
||||
}
|
||||
TcpClientAcceptImpl::TcpClientAcceptImpl(const hio_t *io, const void *object) : mIo(io), mObjectThis(object)
|
||||
TcpClientAcceptImpl::TcpClientAcceptImpl(const hio_t *io, const void *object, const ClientAcceptParam ¶m)
|
||||
: mIo(io), mObjectThis(object), mParam(param)
|
||||
{
|
||||
mParam.mReadFunc = nullptr;
|
||||
mParam.mClosedFunc = nullptr;
|
||||
}
|
||||
void TcpClientAcceptImpl::SetParam(const ClientAcceptParam ¶m)
|
||||
{
|
||||
std::lock_guard<std::mutex> locker(mMutex);
|
||||
mParam = param;
|
||||
// mParam = param;
|
||||
}
|
||||
void TcpClientAcceptImpl::Readed(const void *data, size_t length)
|
||||
{
|
||||
std::lock_guard<std::mutex> locker(mMutex);
|
||||
if (nullptr != mParam.mReadFunc) {
|
||||
mParam.mReadFunc(data, length, mObjectThis);
|
||||
return;
|
||||
|
@ -80,7 +77,6 @@ ssize_t TcpClientAcceptImpl::Write(const void *data, size_t length)
|
|||
}
|
||||
void TcpClientAcceptImpl::Closed(void)
|
||||
{
|
||||
std::lock_guard<std::mutex> locker(mMutex);
|
||||
if (nullptr != mParam.mClosedFunc) {
|
||||
mParam.mClosedFunc(mObjectThis);
|
||||
return;
|
||||
|
@ -138,19 +134,27 @@ void TcpServerImpl::Loop(void)
|
|||
}
|
||||
hloop_run(mLoop);
|
||||
}
|
||||
void TcpServerImpl::AddClient(hio_t *io, std::shared_ptr<ITcpClientAccept> *client)
|
||||
void TcpServerImpl::AddClient(hio_t *io)
|
||||
{
|
||||
std::lock_guard<std::mutex> locker(mMutex);
|
||||
mMutex.lock();
|
||||
char localaddrstr[SOCKADDR_STRLEN] = {0};
|
||||
char peeraddrstr[SOCKADDR_STRLEN] = {0};
|
||||
LogInfo("accept connfd=%d [%s] <= [%s]\n",
|
||||
hio_fd(io),
|
||||
SOCKADDR_STR(hio_localaddr(io), localaddrstr),
|
||||
SOCKADDR_STR(hio_peeraddr(io), peeraddrstr));
|
||||
mClients[hio_fd(io)] = client;
|
||||
std::shared_ptr<ITcpClientAccept> *addClient = NewTcpClientAccept(io, mParam.mClientAcceptParam);
|
||||
mClients[hio_fd(io)] = addClient;
|
||||
if (mParam.mAcceptClientFunc) {
|
||||
mParam.mAcceptClientFunc(client, peeraddrstr);
|
||||
if (mParam.mAcceptClientFunc(addClient, peeraddrstr) == true) {
|
||||
mMutex.unlock();
|
||||
return;
|
||||
}
|
||||
LogWarning("User did not accept client.\n");
|
||||
}
|
||||
mMutex.unlock();
|
||||
hio_close(io);
|
||||
LogWarning("AddClient failed.\n");
|
||||
}
|
||||
std::shared_ptr<ITcpClientAccept> *TcpServerImpl::GetClient(hio_t *io)
|
||||
{
|
||||
|
@ -210,7 +214,7 @@ std::shared_ptr<ITcpServer> *NewTcpServer(const TcpServerParam ¶m)
|
|||
impl->mTcpServer = std::make_shared<TcpServerImpl>(param);
|
||||
return (std::shared_ptr<ITcpServer> *)(((char *)impl) + sizeof(ITcpServerHeader));
|
||||
}
|
||||
std::shared_ptr<ITcpClientAccept> *NewTcpClientAccept(const hio_t *io)
|
||||
std::shared_ptr<ITcpClientAccept> *NewTcpClientAccept(const hio_t *io, const ClientAcceptParam ¶m)
|
||||
{
|
||||
LogInfo("Create tcp server object.\n");
|
||||
TcpClientAccept *impl = (TcpClientAccept *)malloc(sizeof(TcpClientAccept));
|
||||
|
@ -223,6 +227,6 @@ std::shared_ptr<ITcpClientAccept> *NewTcpClientAccept(const hio_t *io)
|
|||
impl->mHeader.mCheckName = GetTcpClientAcceptName();
|
||||
std::shared_ptr<ITcpClientAccept> *objectThis =
|
||||
(std::shared_ptr<ITcpClientAccept> *)(((char *)impl) + sizeof(ITcpServerHeader));
|
||||
impl->mTcpClientAccept = std::make_shared<TcpClientAcceptImpl>(io, objectThis);
|
||||
impl->mTcpClientAccept = std::make_shared<TcpClientAcceptImpl>(io, objectThis, param);
|
||||
return objectThis;
|
||||
}
|
|
@ -25,7 +25,7 @@
|
|||
class TcpClientAcceptImpl : public ITcpClientAccept, public std::enable_shared_from_this<TcpClientAcceptImpl>
|
||||
{
|
||||
public:
|
||||
TcpClientAcceptImpl(const hio_t *io, const void *object);
|
||||
TcpClientAcceptImpl(const hio_t *io, const void *object, const ClientAcceptParam ¶m);
|
||||
virtual ~TcpClientAcceptImpl() = default;
|
||||
void SetParam(const ClientAcceptParam ¶m) override;
|
||||
void Readed(const void *data, size_t length) override;
|
||||
|
@ -33,10 +33,9 @@ public:
|
|||
void Closed(void) override;
|
||||
|
||||
private:
|
||||
std::mutex mMutex;
|
||||
const hio_t *mIo;
|
||||
ClientAcceptParam mParam;
|
||||
const void *mObjectThis;
|
||||
const ClientAcceptParam mParam;
|
||||
};
|
||||
class TcpServerImpl : public ITcpServer, public std::enable_shared_from_this<TcpServerImpl>
|
||||
{
|
||||
|
@ -46,7 +45,7 @@ public:
|
|||
void Init(void) override;
|
||||
void UnInit(void) override;
|
||||
void Loop(void);
|
||||
void AddClient(hio_t *io, std::shared_ptr<ITcpClientAccept> *client);
|
||||
void AddClient(hio_t *io);
|
||||
std::shared_ptr<ITcpClientAccept> *GetClient(hio_t *io);
|
||||
void RemoveClient(hio_t *io);
|
||||
void FreeClients(void);
|
||||
|
@ -60,5 +59,5 @@ private:
|
|||
std::map<int, std::shared_ptr<ITcpClientAccept> *> mClients;
|
||||
};
|
||||
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, const ClientAcceptParam ¶m);
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user