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;