Skip to content

Commit

Permalink
增加hook,实现上下线广播
Browse files Browse the repository at this point in the history
  • Loading branch information
liuligames committed Aug 9, 2021
1 parent 2f6e296 commit c975f2c
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
16 changes: 15 additions & 1 deletion demo/Server.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ type HelloRouter struct {
net.BaseRouter
}


func (hr *HelloRouter) Handle(request iface.IRequest) {
fmt.Println("Call HelloRouter Handle ....")

Expand All @@ -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{})

Expand Down
8 changes: 8 additions & 0 deletions iface/IServer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
3 changes: 3 additions & 0 deletions net/Connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ func (c *Connection) Start() {

go c.StartWriter()

c.TcpServer.CallOnConnStart(c)
}

func (c *Connection) Stop() {
Expand All @@ -118,6 +119,8 @@ func (c *Connection) Stop() {
}
c.isClosed = true

c.TcpServer.CallOnConnStop(c)

_ = c.Conn.Close()

c.ExitChan <- true
Expand Down
27 changes: 27 additions & 0 deletions net/Server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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)
}
}



0 comments on commit c975f2c

Please sign in to comment.