|
|
@ -2,6 +2,7 @@ package core
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"fmt"
|
|
|
|
"git.noahlan.cn/noahlan/nnet/config"
|
|
|
|
"git.noahlan.cn/noahlan/nnet/config"
|
|
|
|
conn2 "git.noahlan.cn/noahlan/nnet/conn"
|
|
|
|
conn2 "git.noahlan.cn/noahlan/nnet/conn"
|
|
|
|
"git.noahlan.cn/noahlan/nnet/entity"
|
|
|
|
"git.noahlan.cn/noahlan/nnet/entity"
|
|
|
@ -15,10 +16,7 @@ import (
|
|
|
|
"log"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"os/signal"
|
|
|
|
|
|
|
|
"strings"
|
|
|
|
"strings"
|
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"time"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
@ -34,9 +32,11 @@ func NotFoundHandler() Handler {
|
|
|
|
type (
|
|
|
|
type (
|
|
|
|
// engine TCP-engine
|
|
|
|
// engine TCP-engine
|
|
|
|
engine struct {
|
|
|
|
engine struct {
|
|
|
|
conf config.EngineConf // conf 配置
|
|
|
|
conf config.EngineConf // conf 配置
|
|
|
|
middlewares []Middleware // 中间件
|
|
|
|
taskTimerPrecision time.Duration
|
|
|
|
routes []Route // 路由
|
|
|
|
|
|
|
|
|
|
|
|
middlewares []Middleware // 中间件
|
|
|
|
|
|
|
|
routes []Route // 路由
|
|
|
|
// handler 消息处理器
|
|
|
|
// handler 消息处理器
|
|
|
|
handler Handler
|
|
|
|
handler Handler
|
|
|
|
// dieChan 应用程序退出信号
|
|
|
|
// dieChan 应用程序退出信号
|
|
|
@ -49,8 +49,7 @@ type (
|
|
|
|
|
|
|
|
|
|
|
|
wsOpt wsOptions // websocket
|
|
|
|
wsOpt wsOptions // websocket
|
|
|
|
|
|
|
|
|
|
|
|
taskTimerPrecision time.Duration
|
|
|
|
connManager *conn2.Manager
|
|
|
|
connManager *conn2.Manager
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
wsOptions struct {
|
|
|
|
wsOptions struct {
|
|
|
@ -72,12 +71,19 @@ func newEngine(conf config.EngineConf) *engine {
|
|
|
|
taskTimerPrecision: conf.TaskTimerPrecision,
|
|
|
|
taskTimerPrecision: conf.TaskTimerPrecision,
|
|
|
|
connManager: conn2.NewManager(),
|
|
|
|
connManager: conn2.NewManager(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pool.InitPool(conf.Pool)
|
|
|
|
pool.InitPool(conf.Pool)
|
|
|
|
|
|
|
|
|
|
|
|
return s
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (ng *engine) shallLogDebug() bool {
|
|
|
|
|
|
|
|
return config.ShallLogDebug(ng.conf.Mode)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (ng *engine) logPrefix() string {
|
|
|
|
|
|
|
|
return fmt.Sprintf("[NNet-%s]", ng.conf.Name)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (ng *engine) use(middleware ...Middleware) {
|
|
|
|
func (ng *engine) use(middleware ...Middleware) {
|
|
|
|
ng.middlewares = append(ng.middlewares, middleware...)
|
|
|
|
ng.middlewares = append(ng.middlewares, middleware...)
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -132,7 +138,9 @@ func (ng *engine) dial(addr string, router Router) (entity.NetworkEntity, error)
|
|
|
|
nlog.Must(err)
|
|
|
|
nlog.Must(err)
|
|
|
|
|
|
|
|
|
|
|
|
// 连接成功,客户端已启动
|
|
|
|
// 连接成功,客户端已启动
|
|
|
|
nlog.Infof("now connect to %s.", addr)
|
|
|
|
if ng.shallLogDebug() {
|
|
|
|
|
|
|
|
nlog.Debugf("now connect to %s.", addr)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return c, nil
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -145,7 +153,7 @@ func (ng *engine) serve(router Router) error {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
go scheduler.Schedule(ng.taskTimerPrecision)
|
|
|
|
go scheduler.Schedule(ng.taskTimerPrecision)
|
|
|
|
defer func() {
|
|
|
|
defer func() {
|
|
|
|
nlog.Info("NNet is stopping...")
|
|
|
|
nlog.Infof("%s is stopping...", ng.logPrefix())
|
|
|
|
|
|
|
|
|
|
|
|
ng.shutdown()
|
|
|
|
ng.shutdown()
|
|
|
|
scheduler.Close()
|
|
|
|
scheduler.Close()
|
|
|
@ -161,16 +169,6 @@ func (ng *engine) serve(router Router) error {
|
|
|
|
ng.listenAndServe()
|
|
|
|
ng.listenAndServe()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sg := make(chan os.Signal)
|
|
|
|
|
|
|
|
signal.Notify(sg, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGKILL, syscall.SIGTERM)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
select {
|
|
|
|
|
|
|
|
case <-ng.dieChan:
|
|
|
|
|
|
|
|
nlog.Info("NNet will shutdown in a few seconds")
|
|
|
|
|
|
|
|
case s := <-sg:
|
|
|
|
|
|
|
|
nlog.Infof("NNet got signal: %s", s)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -186,7 +184,9 @@ func (ng *engine) listenAndServe() {
|
|
|
|
nlog.Must(err)
|
|
|
|
nlog.Must(err)
|
|
|
|
|
|
|
|
|
|
|
|
// 监听成功,服务已启动
|
|
|
|
// 监听成功,服务已启动
|
|
|
|
nlog.Infof("now listening %s on %s.", ng.conf.Protocol, ng.conf.Addr)
|
|
|
|
if ng.shallLogDebug() {
|
|
|
|
|
|
|
|
nlog.Debugf("%s now listening %s at %s.", ng.logPrefix(), ng.conf.Protocol, ng.conf.Addr)
|
|
|
|
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
defer func() {
|
|
|
|
_ = listener.Close()
|
|
|
|
_ = listener.Close()
|
|
|
|
ng.close()
|
|
|
|
ng.close()
|
|
|
@ -196,10 +196,10 @@ func (ng *engine) listenAndServe() {
|
|
|
|
conn, err := listener.Accept()
|
|
|
|
conn, err := listener.Accept()
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, net.ErrClosed) {
|
|
|
|
if errors.Is(err, net.ErrClosed) {
|
|
|
|
nlog.Errorf("服务器网络错误 %+v", err)
|
|
|
|
nlog.Errorf("%s 服务器网络错误 %+v", ng.logPrefix(), err)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nlog.Errorf("监听错误 %v", err)
|
|
|
|
nlog.Errorf("%s 监听错误 %v", ng.logPrefix(), err)
|
|
|
|
continue
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -207,7 +207,7 @@ func (ng *engine) listenAndServe() {
|
|
|
|
ng.handle(conn)
|
|
|
|
ng.handle(conn)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
nlog.Errorf("submit conn pool err: %ng", err.Error())
|
|
|
|
nlog.Errorf("%s submit conn pool err: %ng", ng.logPrefix(), err.Error())
|
|
|
|
continue
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -215,6 +215,9 @@ func (ng *engine) listenAndServe() {
|
|
|
|
|
|
|
|
|
|
|
|
func (ng *engine) listenAndServeWS() {
|
|
|
|
func (ng *engine) listenAndServeWS() {
|
|
|
|
ng.setupWS()
|
|
|
|
ng.setupWS()
|
|
|
|
|
|
|
|
if ng.shallLogDebug() {
|
|
|
|
|
|
|
|
nlog.Debugf("%s now listening websocket at %s.", ng.logPrefix(), ng.conf.Addr)
|
|
|
|
|
|
|
|
}
|
|
|
|
if err := http.ListenAndServe(ng.conf.Addr, nil); err != nil {
|
|
|
|
if err := http.ListenAndServe(ng.conf.Addr, nil); err != nil {
|
|
|
|
log.Fatal(err.Error())
|
|
|
|
log.Fatal(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -222,6 +225,9 @@ func (ng *engine) listenAndServeWS() {
|
|
|
|
|
|
|
|
|
|
|
|
func (ng *engine) listenAndServeWSTLS() {
|
|
|
|
func (ng *engine) listenAndServeWSTLS() {
|
|
|
|
ng.setupWS()
|
|
|
|
ng.setupWS()
|
|
|
|
|
|
|
|
if ng.shallLogDebug() {
|
|
|
|
|
|
|
|
nlog.Debugf("%s now listening websocket with tls at %s.", ng.logPrefix(), ng.conf.Addr)
|
|
|
|
|
|
|
|
}
|
|
|
|
if err := http.ListenAndServeTLS(ng.conf.Addr, ng.wsOpt.TLSCertificate, ng.wsOpt.TLSKey, nil); err != nil {
|
|
|
|
if err := http.ListenAndServeTLS(ng.conf.Addr, ng.wsOpt.TLSCertificate, ng.wsOpt.TLSKey, nil); err != nil {
|
|
|
|
log.Fatal(err.Error())
|
|
|
|
log.Fatal(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -236,14 +242,14 @@ func (ng *engine) setupWS() {
|
|
|
|
http.HandleFunc("/"+strings.TrimPrefix(ng.wsOpt.WebsocketPath, "/"), func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
http.HandleFunc("/"+strings.TrimPrefix(ng.wsOpt.WebsocketPath, "/"), func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
conn, err := upgrade.Upgrade(w, r, nil)
|
|
|
|
conn, err := upgrade.Upgrade(w, r, nil)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
nlog.Errorf("Upgrade failure, URI=%ng, Error=%ng", r.RequestURI, err.Error())
|
|
|
|
nlog.Errorf("%s Upgrade failure, URI=%ng, Error=%ng", ng.logPrefix(), r.RequestURI, err.Error())
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err = pool.Submit(func() {
|
|
|
|
err = pool.Submit(func() {
|
|
|
|
ng.handleWS(conn)
|
|
|
|
ng.handleWS(conn)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("submit conn pool err: %ng", err.Error())
|
|
|
|
log.Fatalf("%s submit conn pool err: %v", ng.logPrefix(), err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|