diff --git a/Makefile b/Makefile index a357338e..a8e3622c 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,10 @@ CUR_DIR=$(shell pwd) +.PHONY: build +# build +build: + mkdir -p bin/ && CGO_ENABLED=0 go build -ldflags "-X main.Version=$(VERSION)" -o ./bin/ ./cmd/... + .PHONY: dev_tool # 安装工具 dev_tool: @@ -11,7 +16,3 @@ dev_tool: gen: cd protocol/proto && make gen -.PHONY: build -# build -build: - mkdir -p bin/ && CGO_ENABLED=0 go build -ldflags "-X main.Version=$(VERSION)" -o ./bin/ ./cmd/... diff --git a/cmd/dispatch/main.go b/cmd/dispatch/main.go index b0ea0edf..d7adc347 100644 --- a/cmd/dispatch/main.go +++ b/cmd/dispatch/main.go @@ -1,43 +1,25 @@ package main import ( + "context" + "flag" + "fmt" _ "net/http/pprof" "os" - "os/signal" - "syscall" - "time" - "hk4e/common/config" - "hk4e/dispatch/controller" - "hk4e/dispatch/dao" - "hk4e/pkg/logger" + "hk4e/dispatch/app" +) + +var ( + config = flag.String("config", "application.toml", "config file") ) func main() { - filePath := "./application.toml" - config.InitConfig(filePath) - - logger.InitLogger("dispatch", config.CONF.Logger) - logger.LOG.Info("dispatch start") - - db := dao.NewDao() - - _ = controller.NewController(db) - - c := make(chan os.Signal, 1) - signal.Notify(c, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT) - for { - s := <-c - logger.LOG.Info("get a signal %s", s.String()) - switch s { - case syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT: - logger.LOG.Info("dispatch exit") - db.CloseDao() - time.Sleep(time.Second) - return - case syscall.SIGHUP: - default: - return - } + flag.Parse() + //go statsviz_serve.Serve("0.0.0.0:2345") + err := app.Run(context.TODO(), *config) + if err != nil { + fmt.Println(err) + os.Exit(1) } } diff --git a/cmd/gate/main.go b/cmd/gate/main.go index fa7f6c5e..093cbf69 100644 --- a/cmd/gate/main.go +++ b/cmd/gate/main.go @@ -1,72 +1,26 @@ package main import ( - "net/http" + "context" + "flag" + "fmt" _ "net/http/pprof" "os" - "os/signal" - "syscall" - "time" - "hk4e/common/config" - "hk4e/gate/forward" - "hk4e/gate/mq" - "hk4e/gate/net" - "hk4e/pkg/logger" - "hk4e/protocol/cmd" + "hk4e/gate/app" + "hk4e/pkg/statsviz_serve" +) - "github.com/arl/statsviz" +var ( + config = flag.String("config", "application.toml", "config file") ) func main() { - filePath := "./application.toml" - config.InitConfig(filePath) - - logger.InitLogger("gate", config.CONF.Logger) - logger.LOG.Info("gate start") - - go func() { - // 性能检测 - err := statsviz.RegisterDefault() - if err != nil { - logger.LOG.Error("statsviz init error: %v", err) - } - err = http.ListenAndServe("0.0.0.0:2345", nil) - if err != nil { - logger.LOG.Error("perf debug http start error: %v", err) - } - }() - - kcpEventInput := make(chan *net.KcpEvent) - kcpEventOutput := make(chan *net.KcpEvent) - protoMsgInput := make(chan *net.ProtoMsg, 10000) - protoMsgOutput := make(chan *net.ProtoMsg, 10000) - netMsgInput := make(chan *cmd.NetMsg, 10000) - netMsgOutput := make(chan *cmd.NetMsg, 10000) - - connectManager := net.NewKcpConnectManager(protoMsgInput, protoMsgOutput, kcpEventInput, kcpEventOutput) - connectManager.Start() - - forwardManager := forward.NewForwardManager(protoMsgInput, protoMsgOutput, kcpEventInput, kcpEventOutput, netMsgInput, netMsgOutput) - forwardManager.Start() - - messageQueue := mq.NewMessageQueue(netMsgInput, netMsgOutput) - messageQueue.Start() - - c := make(chan os.Signal, 1) - signal.Notify(c, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT) - for { - s := <-c - logger.LOG.Info("get a signal %s", s.String()) - switch s { - case syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT: - logger.LOG.Info("gate exit") - messageQueue.Close() - time.Sleep(time.Second) - return - case syscall.SIGHUP: - default: - return - } + flag.Parse() + go statsviz_serve.Serve("0.0.0.0:2345") + err := app.Run(context.TODO(), *config) + if err != nil { + fmt.Println(err) + os.Exit(1) } } diff --git a/cmd/gm/main.go b/cmd/gm/main.go index 36273a4b..198a1e84 100644 --- a/cmd/gm/main.go +++ b/cmd/gm/main.go @@ -1,38 +1,23 @@ package main import ( + "context" + "flag" + "fmt" "os" - "os/signal" - "syscall" - "time" - "hk4e/common/config" - "hk4e/gm/controller" - "hk4e/pkg/logger" + "hk4e/gm/app" +) + +var ( + config = flag.String("config", "application.toml", "config file") ) func main() { - filePath := "./application.toml" - config.InitConfig(filePath) - - logger.InitLogger("gm", config.CONF.Logger) - logger.LOG.Info("gm start") - - _ = controller.NewController() - - c := make(chan os.Signal, 1) - signal.Notify(c, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT) - for { - s := <-c - logger.LOG.Info("get a signal %s", s.String()) - switch s { - case syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT: - logger.LOG.Info("gm exit") - time.Sleep(time.Second) - return - case syscall.SIGHUP: - default: - return - } + flag.Parse() + err := app.Run(context.TODO(), *config) + if err != nil { + fmt.Println(err) + os.Exit(1) } } diff --git a/cmd/gs/main.go b/cmd/gs/main.go index 548efce5..c671e18e 100644 --- a/cmd/gs/main.go +++ b/cmd/gs/main.go @@ -1,75 +1,26 @@ package main import ( - "net/http" + "context" + "flag" + "fmt" _ "net/http/pprof" "os" - "os/signal" - "syscall" - "time" - "hk4e/common/config" - gdc "hk4e/gs/config" - "hk4e/gs/constant" - "hk4e/gs/dao" - "hk4e/gs/game" - "hk4e/gs/mq" - "hk4e/pkg/logger" - "hk4e/protocol/cmd" + "hk4e/gs/app" + "hk4e/pkg/statsviz_serve" +) - "github.com/arl/statsviz" +var ( + config = flag.String("config", "application.toml", "config file") ) func main() { - filePath := "./application.toml" - config.InitConfig(filePath) - - logger.InitLogger("gs", config.CONF.Logger) - logger.LOG.Info("gs start") - - go func() { - // 性能检测 - err := statsviz.RegisterDefault() - if err != nil { - logger.LOG.Error("statsviz init error: %v", err) - } - err = http.ListenAndServe("0.0.0.0:3456", nil) - if err != nil { - logger.LOG.Error("perf debug http start error: %v", err) - } - }() - - constant.InitConstant() - - gdc.InitGameDataConfig() - - db := dao.NewDao() - - netMsgInput := make(chan *cmd.NetMsg, 10000) - netMsgOutput := make(chan *cmd.NetMsg, 10000) - - messageQueue := mq.NewMessageQueue(netMsgInput, netMsgOutput) - messageQueue.Start() - - gameManager := game.NewGameManager(db, netMsgInput, netMsgOutput) - gameManager.Start() - - c := make(chan os.Signal, 1) - signal.Notify(c, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT) - for { - s := <-c - logger.LOG.Info("get a signal %s", s.String()) - switch s { - case syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT: - logger.LOG.Info("gs exit") - gameManager.Stop() - db.CloseDao() - messageQueue.Close() - time.Sleep(time.Second) - return - case syscall.SIGHUP: - default: - return - } + flag.Parse() + go statsviz_serve.Serve("0.0.0.0:3456") + err := app.Run(context.TODO(), *config) + if err != nil { + fmt.Println(err) + os.Exit(1) } } diff --git a/cmd/hk4e/dispatch.go b/cmd/hk4e/dispatch.go new file mode 100644 index 00000000..a58ded68 --- /dev/null +++ b/cmd/hk4e/dispatch.go @@ -0,0 +1,23 @@ +package main + +import ( + "context" + + "hk4e/dispatch/app" + + "github.com/spf13/cobra" +) + +// DispatchCmd +func DispatchCmd() *cobra.Command { + var cfg string + c := &cobra.Command{ + Use: "dispatch", + Short: "dispatch server", + RunE: func(cmd *cobra.Command, args []string) error { + return app.Run(context.Background(), cfg) + }, + } + c.Flags().StringVar(&cfg, "config", "application.toml", "config file") + return c +} diff --git a/cmd/hk4e/gate.go b/cmd/hk4e/gate.go new file mode 100644 index 00000000..cd66d69a --- /dev/null +++ b/cmd/hk4e/gate.go @@ -0,0 +1,23 @@ +package main + +import ( + "context" + + "hk4e/gate/app" + + "github.com/spf13/cobra" +) + +// GateCmd 检查配表命令 +func GateCmd() *cobra.Command { + var cfg string + c := &cobra.Command{ + Use: "gate", + Short: "gate server", + RunE: func(cmd *cobra.Command, args []string) error { + return app.Run(context.Background(), cfg) + }, + } + c.Flags().StringVar(&cfg, "config", "application.toml", "config file") + return c +} diff --git a/cmd/hk4e/gm.go b/cmd/hk4e/gm.go new file mode 100644 index 00000000..55ba1662 --- /dev/null +++ b/cmd/hk4e/gm.go @@ -0,0 +1,23 @@ +package main + +import ( + "context" + + "hk4e/gm/app" + + "github.com/spf13/cobra" +) + +// GMCmd +func GMCmd() *cobra.Command { + var cfg string + c := &cobra.Command{ + Use: "gm", + Short: "gm server", + RunE: func(cmd *cobra.Command, args []string) error { + return app.Run(context.Background(), cfg) + }, + } + c.Flags().StringVar(&cfg, "config", "application.toml", "config file") + return c +} diff --git a/cmd/hk4e/gs.go b/cmd/hk4e/gs.go new file mode 100644 index 00000000..0afaf6c7 --- /dev/null +++ b/cmd/hk4e/gs.go @@ -0,0 +1,23 @@ +package main + +import ( + "context" + + "hk4e/gs/app" + + "github.com/spf13/cobra" +) + +// GSCmd +func GSCmd() *cobra.Command { + var cfg string + c := &cobra.Command{ + Use: "gs", + Short: "game server", + RunE: func(cmd *cobra.Command, args []string) error { + return app.Run(context.Background(), cfg) + }, + } + c.Flags().StringVar(&cfg, "config", "application.toml", "config file") + return c +} diff --git a/cmd/hk4e/main.go b/cmd/hk4e/main.go new file mode 100644 index 00000000..c7685e68 --- /dev/null +++ b/cmd/hk4e/main.go @@ -0,0 +1,33 @@ +package main + +import ( + "flag" + "fmt" + _ "net/http/pprof" + "os" + + "github.com/spf13/cobra" +) + +var ( + config = flag.String("config", "application.toml", "config file") +) + +func main() { + rootCmd := &cobra.Command{ + Use: "hk4e", + Short: "hk4e server", + SilenceUsage: true, + } + rootCmd.AddCommand( + GSCmd(), + GMCmd(), + DispatchCmd(), + GateCmd(), + ) + + if err := rootCmd.Execute(); err != nil { + fmt.Println(err) + os.Exit(1) + } +} diff --git a/dispatch/app/app.go b/dispatch/app/app.go new file mode 100644 index 00000000..de6b4ad0 --- /dev/null +++ b/dispatch/app/app.go @@ -0,0 +1,48 @@ +package app + +import ( + "context" + _ "net/http/pprof" + "os" + "os/signal" + "syscall" + "time" + + "hk4e/common/config" + "hk4e/dispatch/controller" + "hk4e/dispatch/dao" + "hk4e/pkg/logger" +) + +func Run(ctx context.Context, configFile string) error { + config.InitConfig(configFile) + + logger.InitLogger("dispatch", config.CONF.Logger) + logger.LOG.Info("dispatch start") + + db := dao.NewDao() + defer db.CloseDao() + + _ = controller.NewController(db) + + c := make(chan os.Signal, 1) + signal.Notify(c, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT) + for { + select { + case <-ctx.Done(): + return nil + case s := <-c: + logger.LOG.Info("get a signal %s", s.String()) + switch s { + case syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT: + logger.LOG.Info("dispatch exit") + + time.Sleep(time.Second) + return nil + case syscall.SIGHUP: + default: + return nil + } + } + } +} diff --git a/gate/app/app.go b/gate/app/app.go new file mode 100644 index 00000000..24e132d4 --- /dev/null +++ b/gate/app/app.go @@ -0,0 +1,62 @@ +package app + +import ( + "context" + _ "net/http/pprof" + "os" + "os/signal" + "syscall" + "time" + + "hk4e/common/config" + "hk4e/gate/forward" + "hk4e/gate/mq" + "hk4e/gate/net" + "hk4e/pkg/logger" + "hk4e/protocol/cmd" +) + +func Run(ctx context.Context, configFile string) error { + config.InitConfig(configFile) + + logger.InitLogger("gate", config.CONF.Logger) + logger.LOG.Info("gate start") + + kcpEventInput := make(chan *net.KcpEvent) + kcpEventOutput := make(chan *net.KcpEvent) + protoMsgInput := make(chan *net.ProtoMsg, 10000) + protoMsgOutput := make(chan *net.ProtoMsg, 10000) + netMsgInput := make(chan *cmd.NetMsg, 10000) + netMsgOutput := make(chan *cmd.NetMsg, 10000) + + connectManager := net.NewKcpConnectManager(protoMsgInput, protoMsgOutput, kcpEventInput, kcpEventOutput) + connectManager.Start() + + forwardManager := forward.NewForwardManager(protoMsgInput, protoMsgOutput, kcpEventInput, kcpEventOutput, netMsgInput, netMsgOutput) + forwardManager.Start() + + messageQueue := mq.NewMessageQueue(netMsgInput, netMsgOutput) + messageQueue.Start() + defer messageQueue.Close() + + c := make(chan os.Signal, 1) + signal.Notify(c, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT) + for { + select { + case <-ctx.Done(): + return nil + case s := <-c: + logger.LOG.Info("get a signal %s", s.String()) + switch s { + case syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT: + logger.LOG.Info("gate exit") + time.Sleep(time.Second) + return nil + case syscall.SIGHUP: + default: + return nil + } + } + + } +} diff --git a/gm/app/app.go b/gm/app/app.go new file mode 100644 index 00000000..f9e65c3e --- /dev/null +++ b/gm/app/app.go @@ -0,0 +1,42 @@ +package app + +import ( + "context" + "os" + "os/signal" + "syscall" + "time" + + "hk4e/common/config" + "hk4e/gm/controller" + "hk4e/pkg/logger" +) + +func Run(ctx context.Context, configFile string) error { + config.InitConfig(configFile) + + logger.InitLogger("gm", config.CONF.Logger) + logger.LOG.Info("gm start") + + _ = controller.NewController() + + c := make(chan os.Signal, 1) + signal.Notify(c, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT) + for { + select { + case <-ctx.Done(): + return nil + case s := <-c: + logger.LOG.Info("get a signal %s", s.String()) + switch s { + case syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT: + logger.LOG.Info("gm exit") + time.Sleep(time.Second) + return nil + case syscall.SIGHUP: + default: + return nil + } + } + } +} diff --git a/go.mod b/go.mod index def87e3f..4a0b1a94 100644 --- a/go.mod +++ b/go.mod @@ -51,6 +51,7 @@ require ( github.com/golang/protobuf v1.5.0 // indirect github.com/golang/snappy v0.0.1 // indirect github.com/gorilla/websocket v1.4.2 // indirect + github.com/inconshreveable/mousetrap v1.0.1 // indirect github.com/json-iterator/go v1.1.9 // indirect github.com/klauspost/compress v1.15.11 // indirect github.com/klauspost/cpuid/v2 v2.0.6 // indirect @@ -62,6 +63,8 @@ require ( github.com/nats-io/nkeys v0.3.0 // indirect github.com/nats-io/nuid v1.0.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/spf13/cobra v1.6.1 // indirect + github.com/spf13/pflag v1.0.5 // indirect github.com/templexxx/cpu v0.0.1 // indirect github.com/ugorji/go/codec v1.1.7 // indirect github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect @@ -73,5 +76,5 @@ require ( golang.org/x/sys v0.0.0-20220928140112-f11e5e49a4ec // indirect golang.org/x/text v0.3.6 // indirect gopkg.in/yaml.v2 v2.2.8 // indirect - gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 395b7a3b..695c2878 100644 --- a/go.sum +++ b/go.sum @@ -6,6 +6,7 @@ github.com/arl/statsviz v0.5.1/go.mod h1:zDnjgRblGm1Dyd7J5YlbH7gM1/+HRC+SfkhZhQb github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= +github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -53,6 +54,8 @@ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc= +github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns= github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= github.com/jszwec/csvutil v1.7.1 h1:btxPxFwms8lHMgl0OIgOQ4Tayfqo0xid0hGkq1kM510= @@ -93,6 +96,11 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/spf13/cobra v1.6.1 h1:o94oiPyS4KD1mPy2fmcYYHHfCxLqYjJOhGsCHFZtEzA= +github.com/spf13/cobra v1.6.1/go.mod h1:IOw/AERYS7UzyrGinqmz6HLUo219MORXGxhbaJUqzrY= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= @@ -203,5 +211,6 @@ gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/gs/app/app.go b/gs/app/app.go new file mode 100644 index 00000000..5f63b9d2 --- /dev/null +++ b/gs/app/app.go @@ -0,0 +1,68 @@ +package app + +import ( + "context" + _ "net/http/pprof" + "os" + "os/signal" + "syscall" + "time" + + "hk4e/common/config" + gdc "hk4e/gs/config" + "hk4e/gs/constant" + "hk4e/gs/dao" + "hk4e/gs/game" + "hk4e/gs/mq" + "hk4e/pkg/logger" + "hk4e/protocol/cmd" +) + +func Run(ctx context.Context, configFile string) error { + config.InitConfig(configFile) + + logger.InitLogger("gs", config.CONF.Logger) + logger.LOG.Info("gs start") + + constant.InitConstant() + + gdc.InitGameDataConfig() + + db, err := dao.NewDao() + if err != nil { + panic(err) + } + defer db.CloseDao() + + netMsgInput := make(chan *cmd.NetMsg, 10000) + netMsgOutput := make(chan *cmd.NetMsg, 10000) + + messageQueue := mq.NewMessageQueue(netMsgInput, netMsgOutput) + messageQueue.Start() + defer messageQueue.Close() + + gameManager := game.NewGameManager(db, netMsgInput, netMsgOutput) + gameManager.Start() + defer gameManager.Stop() + + c := make(chan os.Signal, 1) + signal.Notify(c, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT) + + for { + select { + case <-ctx.Done(): + return nil + case s := <-c: + logger.LOG.Info("get a signal %s", s.String()) + switch s { + case syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT: + logger.LOG.Info("gs exit") + time.Sleep(time.Second) + return nil + case syscall.SIGHUP: + default: + return nil + } + } + } +} diff --git a/gs/dao/dao.go b/gs/dao/dao.go index 0b587ded..2883cc04 100644 --- a/gs/dao/dao.go +++ b/gs/dao/dao.go @@ -15,17 +15,17 @@ type Dao struct { db *mongo.Database } -func NewDao() (r *Dao) { +func NewDao() (r *Dao, err error) { r = new(Dao) clientOptions := options.Client().ApplyURI(config.CONF.Database.Url) client, err := mongo.Connect(context.TODO(), clientOptions) if err != nil { logger.LOG.Error("mongo connect error: %v", err) - return nil + return nil, err } r.client = client r.db = client.Database("game_hk4e") - return r + return r, nil } func (d *Dao) CloseDao() { diff --git a/pkg/statsviz_serve/statsviz_serve.go b/pkg/statsviz_serve/statsviz_serve.go new file mode 100644 index 00000000..0dc34cb0 --- /dev/null +++ b/pkg/statsviz_serve/statsviz_serve.go @@ -0,0 +1,24 @@ +package statsviz_serve + +import ( + "net/http" + + "hk4e/pkg/logger" + + "github.com/arl/statsviz" +) + +func Serve(addr string) error { + // 性能检测 + err := statsviz.RegisterDefault() + if err != nil { + logger.LOG.Error("statsviz init error: %v", err) + return err + } + err = http.ListenAndServe(addr, nil) + if err != nil { + logger.LOG.Error("perf debug http start error: %v", err) + return err + } + return nil +}