This commit is contained in:
lu.bai
2022-11-24 23:29:33 +08:00
parent 71d0135d6f
commit 48d5291b33
18 changed files with 445 additions and 191 deletions

View File

@@ -1,5 +1,10 @@
CUR_DIR=$(shell pwd) 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 .PHONY: dev_tool
# 安装工具 # 安装工具
dev_tool: dev_tool:
@@ -11,7 +16,3 @@ dev_tool:
gen: gen:
cd protocol/proto && make 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/...

View File

@@ -1,43 +1,25 @@
package main package main
import ( import (
"context"
"flag"
"fmt"
_ "net/http/pprof" _ "net/http/pprof"
"os" "os"
"os/signal"
"syscall"
"time"
"hk4e/common/config" "hk4e/dispatch/app"
"hk4e/dispatch/controller" )
"hk4e/dispatch/dao"
"hk4e/pkg/logger" var (
config = flag.String("config", "application.toml", "config file")
) )
func main() { func main() {
filePath := "./application.toml" flag.Parse()
config.InitConfig(filePath) //go statsviz_serve.Serve("0.0.0.0:2345")
err := app.Run(context.TODO(), *config)
logger.InitLogger("dispatch", config.CONF.Logger) if err != nil {
logger.LOG.Info("dispatch start") fmt.Println(err)
os.Exit(1)
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
}
} }
} }

View File

@@ -1,72 +1,26 @@
package main package main
import ( import (
"net/http" "context"
"flag"
"fmt"
_ "net/http/pprof" _ "net/http/pprof"
"os" "os"
"os/signal"
"syscall"
"time"
"hk4e/common/config" "hk4e/gate/app"
"hk4e/gate/forward" "hk4e/pkg/statsviz_serve"
"hk4e/gate/mq" )
"hk4e/gate/net"
"hk4e/pkg/logger"
"hk4e/protocol/cmd"
"github.com/arl/statsviz" var (
config = flag.String("config", "application.toml", "config file")
) )
func main() { func main() {
filePath := "./application.toml" flag.Parse()
config.InitConfig(filePath) go statsviz_serve.Serve("0.0.0.0:2345")
err := app.Run(context.TODO(), *config)
logger.InitLogger("gate", config.CONF.Logger) if err != nil {
logger.LOG.Info("gate start") fmt.Println(err)
os.Exit(1)
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
}
} }
} }

View File

@@ -1,38 +1,23 @@
package main package main
import ( import (
"context"
"flag"
"fmt"
"os" "os"
"os/signal"
"syscall"
"time"
"hk4e/common/config" "hk4e/gm/app"
"hk4e/gm/controller" )
"hk4e/pkg/logger"
var (
config = flag.String("config", "application.toml", "config file")
) )
func main() { func main() {
filePath := "./application.toml" flag.Parse()
config.InitConfig(filePath) err := app.Run(context.TODO(), *config)
if err != nil {
logger.InitLogger("gm", config.CONF.Logger) fmt.Println(err)
logger.LOG.Info("gm start") os.Exit(1)
_ = 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
}
} }
} }

View File

@@ -1,75 +1,26 @@
package main package main
import ( import (
"net/http" "context"
"flag"
"fmt"
_ "net/http/pprof" _ "net/http/pprof"
"os" "os"
"os/signal"
"syscall"
"time"
"hk4e/common/config" "hk4e/gs/app"
gdc "hk4e/gs/config" "hk4e/pkg/statsviz_serve"
"hk4e/gs/constant" )
"hk4e/gs/dao"
"hk4e/gs/game"
"hk4e/gs/mq"
"hk4e/pkg/logger"
"hk4e/protocol/cmd"
"github.com/arl/statsviz" var (
config = flag.String("config", "application.toml", "config file")
) )
func main() { func main() {
filePath := "./application.toml" flag.Parse()
config.InitConfig(filePath) go statsviz_serve.Serve("0.0.0.0:3456")
err := app.Run(context.TODO(), *config)
logger.InitLogger("gs", config.CONF.Logger) if err != nil {
logger.LOG.Info("gs start") fmt.Println(err)
os.Exit(1)
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
}
} }
} }

23
cmd/hk4e/dispatch.go Normal file
View File

@@ -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
}

23
cmd/hk4e/gate.go Normal file
View File

@@ -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
}

23
cmd/hk4e/gm.go Normal file
View File

@@ -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
}

23
cmd/hk4e/gs.go Normal file
View File

@@ -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
}

33
cmd/hk4e/main.go Normal file
View File

@@ -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)
}
}

48
dispatch/app/app.go Normal file
View File

@@ -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
}
}
}
}

62
gate/app/app.go Normal file
View File

@@ -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
}
}
}
}

42
gm/app/app.go Normal file
View File

@@ -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
}
}
}
}

5
go.mod
View File

@@ -51,6 +51,7 @@ require (
github.com/golang/protobuf v1.5.0 // indirect github.com/golang/protobuf v1.5.0 // indirect
github.com/golang/snappy v0.0.1 // indirect github.com/golang/snappy v0.0.1 // indirect
github.com/gorilla/websocket v1.4.2 // 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/json-iterator/go v1.1.9 // indirect
github.com/klauspost/compress v1.15.11 // indirect github.com/klauspost/compress v1.15.11 // indirect
github.com/klauspost/cpuid/v2 v2.0.6 // 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/nkeys v0.3.0 // indirect
github.com/nats-io/nuid v1.0.1 // indirect github.com/nats-io/nuid v1.0.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // 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/templexxx/cpu v0.0.1 // indirect
github.com/ugorji/go/codec v1.1.7 // indirect github.com/ugorji/go/codec v1.1.7 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // 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/sys v0.0.0-20220928140112-f11e5e49a4ec // indirect
golang.org/x/text v0.3.6 // indirect golang.org/x/text v0.3.6 // indirect
gopkg.in/yaml.v2 v2.2.8 // 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
) )

9
go.sum
View File

@@ -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/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= 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/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.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 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 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/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 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= 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 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns=
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/jszwec/csvutil v1.7.1 h1:btxPxFwms8lHMgl0OIgOQ4Tayfqo0xid0hGkq1kM510= 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 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 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/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/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.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= 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.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 h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 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-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=

68
gs/app/app.go Normal file
View File

@@ -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
}
}
}
}

View File

@@ -15,17 +15,17 @@ type Dao struct {
db *mongo.Database db *mongo.Database
} }
func NewDao() (r *Dao) { func NewDao() (r *Dao, err error) {
r = new(Dao) r = new(Dao)
clientOptions := options.Client().ApplyURI(config.CONF.Database.Url) clientOptions := options.Client().ApplyURI(config.CONF.Database.Url)
client, err := mongo.Connect(context.TODO(), clientOptions) client, err := mongo.Connect(context.TODO(), clientOptions)
if err != nil { if err != nil {
logger.LOG.Error("mongo connect error: %v", err) logger.LOG.Error("mongo connect error: %v", err)
return nil return nil, err
} }
r.client = client r.client = client
r.db = client.Database("game_hk4e") r.db = client.Database("game_hk4e")
return r return r, nil
} }
func (d *Dao) CloseDao() { func (d *Dao) CloseDao() {

View File

@@ -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
}