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