From c975f2c505da048340fc3f4fa80a42c605bf52fe Mon Sep 17 00:00:00 2001 From: LiuShaowen Date: Mon, 9 Aug 2021 14:08:50 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0hook,=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=E4=B8=8A=E4=B8=8B=E7=BA=BF=E5=B9=BF=E6=92=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- demo/Server.go | 16 +++++++++++++++- iface/IServer.go | 8 ++++++++ net/Connection.go | 3 +++ net/Server.go | 27 +++++++++++++++++++++++++++ 4 files changed, 53 insertions(+), 1 deletion(-) diff --git a/demo/Server.go b/demo/Server.go index 0f0f5c0..5a10225 100644 --- a/demo/Server.go +++ b/demo/Server.go @@ -26,7 +26,6 @@ type HelloRouter struct { net.BaseRouter } - func (hr *HelloRouter) Handle(request iface.IRequest) { fmt.Println("Call HelloRouter Handle ....") @@ -39,10 +38,25 @@ func (hr *HelloRouter) Handle(request iface.IRequest) { } } +func DoConnectionBegin(conn iface.IConnection) { + fmt.Println("DoConnectionBegin is Called ...") + if err := conn.SendMsg(202, []byte("DoConnection Begin")); err != nil { + fmt.Println(err) + } +} + +func DoConnectionLost(conn iface.IConnection) { + fmt.Println("DoConnectionLast is Called ...") + fmt.Println("conn id = ", conn.GetConnID(), "is lost") +} + func main() { s := net.NewServer() + s.SetOnConnStart(DoConnectionBegin) + s.SetOnConnStop(DoConnectionLost) + s.AddRouter(0, &PingRouter{}) s.AddRouter(1, &HelloRouter{}) diff --git a/iface/IServer.go b/iface/IServer.go index 965f5b1..76c93e8 100644 --- a/iface/IServer.go +++ b/iface/IServer.go @@ -7,4 +7,12 @@ type IServer interface { AddRouter(msgId uint32, router IRouter) GetConnManager() IConnManager + + SetOnConnStart(hookFunc func(connection IConnection)) + + SetOnConnStop(hookFunc func(connection IConnection)) + + CallOnConnStart(connection IConnection) + + CallOnConnStop(connection IConnection) } diff --git a/net/Connection.go b/net/Connection.go index ad988bf..1fc3ab4 100644 --- a/net/Connection.go +++ b/net/Connection.go @@ -108,6 +108,7 @@ func (c *Connection) Start() { go c.StartWriter() + c.TcpServer.CallOnConnStart(c) } func (c *Connection) Stop() { @@ -118,6 +119,8 @@ func (c *Connection) Stop() { } c.isClosed = true + c.TcpServer.CallOnConnStop(c) + _ = c.Conn.Close() c.ExitChan <- true diff --git a/net/Server.go b/net/Server.go index 28ed8de..9e7e802 100644 --- a/net/Server.go +++ b/net/Server.go @@ -14,6 +14,8 @@ type Server struct { Port int MsgHandler iface.IMsgHandler ConnManager iface.IConnManager + OnConnStart func(conn iface.IConnection) + OnConnStop func(conn iface.IConnection) } func (s *Server) Start() { @@ -98,3 +100,28 @@ func NewServer() iface.IServer { ConnManager: NewConnManager(), } } + +func (s *Server) SetOnConnStart(hookFunc func(connection iface.IConnection)) { + s.OnConnStart = hookFunc +} + +func (s *Server) SetOnConnStop(hookFunc func(connection iface.IConnection)) { + s.OnConnStop = hookFunc +} + +func (s *Server) CallOnConnStart(connection iface.IConnection) { + if s.OnConnStart != nil{ + fmt.Println("Call OnConnStart()") + s.OnConnStart(connection) + } +} + +func (s *Server) CallOnConnStop(connection iface.IConnection) { + if s.OnConnStop != nil{ + fmt.Println("Call OnConnStart()") + s.OnConnStop(connection) + } +} + + +