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
862 B
Go

package plain
import (
"git.noahlan.cn/noahlan/nnet/core"
"git.noahlan.cn/noahlan/nnet/entity"
"git.noahlan.cn/noahlan/nnet/packet"
"git.noahlan.cn/noahlan/ntools-go/core/nlog"
)
type Router struct {
plainHandler core.Handler
notFound core.Handler
}
func NewRouter() core.Router {
return &Router{}
}
func (r *Router) Handle(entity entity.NetworkEntity, 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(entity, p)
return
}
r.plainHandler.Handle(entity, p)
}
func (r *Router) Register(_ interface{}, handler core.Handler) error {
r.plainHandler = handler
return nil
}
func (r *Router) SetNotFoundHandler(handler core.Handler) {
r.notFound = handler
}