You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nnet/protocol/plain/router_plain.go

44 lines
865 B
Go

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
}