|
|
|
@ -47,8 +47,10 @@ type (
|
|
|
|
|
packerFn packet.NewPackerFunc // 封包、拆包器
|
|
|
|
|
serializer serialize.Serializer // 消息 序列化/反序列化
|
|
|
|
|
|
|
|
|
|
retryInterval time.Duration // 消息重试间隔时长
|
|
|
|
|
wsOpt wsOptions // websocket
|
|
|
|
|
wsOpt wsOptions // websocket
|
|
|
|
|
|
|
|
|
|
taskTimerPrecision time.Duration
|
|
|
|
|
connManager *conn2.Manager
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wsOptions struct {
|
|
|
|
@ -62,11 +64,13 @@ type (
|
|
|
|
|
|
|
|
|
|
func newEngine(conf config.EngineConf) *engine {
|
|
|
|
|
s := &engine{
|
|
|
|
|
conf: conf,
|
|
|
|
|
dieChan: make(chan struct{}),
|
|
|
|
|
pipeline: pipeline.New(),
|
|
|
|
|
middlewares: make([]Middleware, 0),
|
|
|
|
|
routes: make([]Route, 0),
|
|
|
|
|
conf: conf,
|
|
|
|
|
dieChan: make(chan struct{}),
|
|
|
|
|
pipeline: pipeline.New(),
|
|
|
|
|
middlewares: make([]Middleware, 0),
|
|
|
|
|
routes: make([]Route, 0),
|
|
|
|
|
taskTimerPrecision: conf.TaskTimerPrecision,
|
|
|
|
|
connManager: conn2.NewManager(),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pool.InitPool(conf.Pool)
|
|
|
|
@ -113,7 +117,7 @@ func (ng *engine) dial(addr string, router Router) (entity.NetworkEntity, error)
|
|
|
|
|
if err := ng.bindRoutes(router); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
go scheduler.Schedule()
|
|
|
|
|
go scheduler.Schedule(ng.taskTimerPrecision)
|
|
|
|
|
|
|
|
|
|
// connection
|
|
|
|
|
conn, err := net.Dial("tcp", addr)
|
|
|
|
@ -123,6 +127,9 @@ func (ng *engine) dial(addr string, router Router) (entity.NetworkEntity, error)
|
|
|
|
|
c.serve()
|
|
|
|
|
// hook
|
|
|
|
|
Lifetime.Open(c)
|
|
|
|
|
// connection manager
|
|
|
|
|
err = ng.connManager.Store(conn2.DefaultGroupName, c)
|
|
|
|
|
nlog.Must(err)
|
|
|
|
|
|
|
|
|
|
// 连接成功,客户端已启动
|
|
|
|
|
nlog.Infof("now connect to %s.", addr)
|
|
|
|
@ -136,7 +143,7 @@ func (ng *engine) serve(router Router) error {
|
|
|
|
|
if err := ng.bindRoutes(router); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
go scheduler.Schedule()
|
|
|
|
|
go scheduler.Schedule(ng.taskTimerPrecision)
|
|
|
|
|
defer func() {
|
|
|
|
|
nlog.Info("NNet is stopping...")
|
|
|
|
|
|
|
|
|
@ -248,7 +255,8 @@ func (ng *engine) handleWS(conn *websocket.Conn) {
|
|
|
|
|
|
|
|
|
|
func (ng *engine) handle(conn net.Conn) {
|
|
|
|
|
c := newConnection(ng, conn)
|
|
|
|
|
conn2.ConnManager.Store(conn2.DefaultGroupName, c)
|
|
|
|
|
err := ng.connManager.Store(conn2.DefaultGroupName, c)
|
|
|
|
|
nlog.Must(err)
|
|
|
|
|
|
|
|
|
|
c.serve()
|
|
|
|
|
// hook
|
|
|
|
|