package plain import ( "git.noahlan.cn/noahlan/nnet/connection" "git.noahlan.cn/noahlan/nnet/packet" "git.noahlan.cn/noahlan/nnet/router" "git.noahlan.cn/noahlan/ntool/nlog" ) type Router struct { plainHandler router.Handler notFound router.Handler } func NewRouter() router.Router { return &Router{} } func (r *Router) Handle(conn *connection.Connection, pkg packet.IPacket) { p, ok := pkg.(*Packet) if !ok { nlog.Error(packet.ErrWrongPacketType) return } if r.plainHandler == nil { if r.notFound == nil { nlog.Error("message handler not found") return } r.notFound.Handle(conn, p) return } r.plainHandler.Handle(conn, p) } func (r *Router) Register(_ interface{}, handler router.Handler) error { r.plainHandler = handler return nil } func (r *Router) SetNotFoundHandler(handler router.Handler) { r.notFound = handler }