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

39 lines
757 B
Go

package plain
import (
"git.noahlan.cn/noahlan/nnet/conn"
"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(nc *conn.Connection, pkg packet.IPacket) {
if r.plainHandler == nil {
if r.notFound == nil {
nlog.Error("message handler not found")
return
}
r.notFound.Handle(nc, pkg)
return
}
r.plainHandler.Handle(nc, pkg)
}
func (r *Router) Register(_ any, handler router.Handler) error {
r.plainHandler = handler
return nil
}
func (r *Router) SetNotFoundHandler(handler router.Handler) {
r.notFound = handler
}