From 56f576e60d471f0390d12172cf2aa499c0772e93 Mon Sep 17 00:00:00 2001 From: azw Date: Thu, 28 Sep 2023 01:08:51 +0000 Subject: [PATCH] 1 --- a8/tcplistener.cc | 4 ++-- a8/tcplistener.h | 12 +++++++++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/a8/tcplistener.cc b/a8/tcplistener.cc index e32b7b1..40ceda8 100644 --- a/a8/tcplistener.cc +++ b/a8/tcplistener.cc @@ -190,8 +190,8 @@ namespace a8 clients_mutex.unlock(); if (!session) { - if (master->on_create_client_socket) { - master->on_create_client_socket(&session); + if (master->on_create_client_socket_) { + master->on_create_client_socket_(&session); if (session) { if (!session->AllocRecvBuf()) { delete session; diff --git a/a8/tcplistener.h b/a8/tcplistener.h index 25c54db..10a3697 100644 --- a/a8/tcplistener.h +++ b/a8/tcplistener.h @@ -18,7 +18,6 @@ namespace a8 class TcpListener { public: - std::function on_create_client_socket; std::function on_client_connect; std::function on_error; std::string bind_address; @@ -35,6 +34,15 @@ namespace a8 void Open(); void Close(); bool IsActive(); + template + void RegisterSessionClass() + { + on_create_client_socket_ = + [] (a8::TcpSession** p) + { + *p = new T; + }; + } bool SendClientMsg(unsigned short sockhandle, const char *buff, int buffLen); void BroadcastMsg(const char* buff, int bufflen); @@ -42,6 +50,7 @@ namespace a8 void MarkClient(unsigned short sockhandle, bool is_active); int GetClientSocketCount(); int GetPoolSocketCount(); + private: void LockClients(); @@ -50,6 +59,7 @@ namespace a8 a8::TcpSession* GetClientSession(unsigned short handle); private: + std::function on_create_client_socket_; a8::TcpListenerImpl* impl_ = nullptr; friend class TcpSession;