|
|
|
|
package nnet
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"git.noahlan.cn/noahlan/nnet/conn"
|
|
|
|
|
"git.noahlan.cn/noahlan/nnet/event"
|
|
|
|
|
"git.noahlan.cn/noahlan/nnet/packet"
|
|
|
|
|
rt "git.noahlan.cn/noahlan/nnet/router"
|
|
|
|
|
"git.noahlan.cn/noahlan/ntool/ndef"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type (
|
|
|
|
|
// RunOption defines the method to customize an Engine.
|
|
|
|
|
RunOption func(ngin *Engine)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// EventManager returns EventManager.
|
|
|
|
|
func (ngin *Engine) EventManager() event.EventManager {
|
|
|
|
|
return ngin.evtMgr
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ConnManager returns connection manager
|
|
|
|
|
func (ngin *Engine) ConnManager() *conn.ConnManager {
|
|
|
|
|
return ngin.connMgr
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//////////////////////// Options
|
|
|
|
|
|
|
|
|
|
func WithSendSize(sendSize int) RunOption {
|
|
|
|
|
return func(ngin *Engine) {
|
|
|
|
|
ngin.SendChannelSize = sendSize
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func WithWriteSize(size int) RunOption {
|
|
|
|
|
return func(ngin *Engine) {
|
|
|
|
|
ngin.WriteChannelSize = size
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func WithMiddleware(middleware ...rt.Middleware) RunOption {
|
|
|
|
|
return func(ngin *Engine) {
|
|
|
|
|
ngin.Use(middleware...)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WithRouter 设置消息路由
|
|
|
|
|
func WithRouter(router rt.Router) RunOption {
|
|
|
|
|
return func(ngin *Engine) {
|
|
|
|
|
ngin.router = router
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WithNotFoundHandler returns a RunOption with not found handler set to given handler.
|
|
|
|
|
func WithNotFoundHandler(handler rt.Handler) RunOption {
|
|
|
|
|
return func(ngin *Engine) {
|
|
|
|
|
ngin.router.SetNotFoundHandler(rt.NotFoundHandler(handler))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WithTimerPrecision 设置Timer精度,需在 Start 或 Dial 之前执行
|
|
|
|
|
// 注:精度需大于1ms, 并且不能在运行时更改
|
|
|
|
|
// 默认精度是 time.Second
|
|
|
|
|
func WithTimerPrecision(precision time.Duration) RunOption {
|
|
|
|
|
if precision < time.Millisecond {
|
|
|
|
|
panic("time precision can not less than a Millisecond")
|
|
|
|
|
}
|
|
|
|
|
return func(ngin *Engine) {
|
|
|
|
|
ngin.TaskTimerPrecision = precision
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WithPackerBuilder 设置 消息的封包/解包构造器
|
|
|
|
|
func WithPackerBuilder(fn packet.PackerBuilder) RunOption {
|
|
|
|
|
return func(ngin *Engine) {
|
|
|
|
|
ngin.packerBuilder = fn
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WithSerializer 设置消息的 序列化/反序列化 方式
|
|
|
|
|
func WithSerializer(s ndef.Serializer) RunOption {
|
|
|
|
|
return func(ngin *Engine) {
|
|
|
|
|
ngin.serializer = s
|
|
|
|
|
}
|
|
|
|
|
}
|