diff --git a/actions/HttpAction.go b/actions/HttpAction.go index 5493f28..e623070 100644 --- a/actions/HttpAction.go +++ b/actions/HttpAction.go @@ -5,6 +5,7 @@ import ( "github.com/gin-gonic/gin" "net/http" "pro2d/components/db" + "pro2d/components/etcd" "pro2d/conf" "pro2d/models" "pro2d/protos/pb" @@ -16,6 +17,7 @@ import ( type HttpAction struct { version string port []string + EtcdClient *etcd.EtcdClient } func NewHttpAction(version string, port ...string) *HttpAction { @@ -84,7 +86,7 @@ func (h *HttpAction) Login(c *gin.Context) { } var gs []*pb.ServiceInfo - for k, v := range conf.EtcdClient.GetByPrefix(conf.GlobalConf.GameConf.Name) { + for k, v := range h.EtcdClient.GetByPrefix(conf.GlobalConf.GameConf.Name) { gs = append(gs, &pb.ServiceInfo{ Id: k, Name: conf.GlobalConf.GameConf.Name, @@ -104,7 +106,8 @@ func (h *HttpAction) Start() error { models.InitAccountServerModels() //Etcd 初始化 - conf.EtcdClient.PutWithLeasePrefix(conf.GlobalConf.AccountConf.Name, conf.GlobalConf.AccountConf.ID, fmt.Sprintf("%s:%d", conf.GlobalConf.AccountConf.IP, conf.GlobalConf.AccountConf.Port), 5) + h.EtcdClient = etcd.NewEtcdClient(conf.GlobalConf.Etcd) + h.EtcdClient.PutWithLeasePrefix(conf.GlobalConf.AccountConf.Name, conf.GlobalConf.AccountConf.ID, fmt.Sprintf("%s:%d", conf.GlobalConf.AccountConf.IP, conf.GlobalConf.AccountConf.Port), 5) //gin初始化 r := gin.Default() diff --git a/actions/protocode.go b/actions/protocode.go new file mode 100644 index 0000000..f160b1f --- /dev/null +++ b/actions/protocode.go @@ -0,0 +1,15 @@ +package actions + +import ( + "pro2d/components/net" + "pro2d/protos/pb" +) + +func init() { + net.ActionMap = make(map[pb.ProtoCode]net.ActionHandler) + + net.ActionMap[pb.ProtoCode_HeartRpc] = HeartRpc + net.ActionMap[pb.ProtoCode_LoginRpc] = LoginRpc + net.ActionMap[pb.ProtoCode_CreateRpc] = CreateRpc + +} \ No newline at end of file diff --git a/common/protocode.go b/common/protocode.go deleted file mode 100644 index 46b3d49..0000000 --- a/common/protocode.go +++ /dev/null @@ -1,19 +0,0 @@ -package common - -import ( - "github.com/golang/protobuf/proto" - "pro2d/actions" - "pro2d/components/net" - "pro2d/protos/pb" -) - -type ActionHandler func (msg *net.MsgPkg) (int32, proto.Message) - -var ActionMap map[pb.ProtoCode]ActionHandler - -func init() { - ActionMap[pb.ProtoCode_HeartRpc] = actions.HeartRpc - ActionMap[pb.ProtoCode_LoginRpc] = actions.LoginRpc - ActionMap[pb.ProtoCode_CreateRpc] = actions.CreateRpc - -} \ No newline at end of file diff --git a/components/net/server.go b/components/net/server.go index b0dc6fe..4b4cb5a 100644 --- a/components/net/server.go +++ b/components/net/server.go @@ -4,8 +4,8 @@ import ( "fmt" "github.com/golang/protobuf/proto" "net" - "pro2d/common" "pro2d/components/db" + "pro2d/components/etcd" "pro2d/conf" "pro2d/models" "pro2d/protos/pb" @@ -13,9 +13,13 @@ import ( "sync" ) +type ActionHandler func (msg *MsgPkg) (int32, proto.Message) +var ActionMap map[pb.ProtoCode]ActionHandler + type Server struct { SConf *conf.SConf Clients *sync.Map + EtcdClient *etcd.EtcdClient } @@ -28,7 +32,7 @@ func NewServer(sConf *conf.SConf) *Server { func (s *Server) OnRecv(msg *MsgPkg) { utils.Sugar.Debugf("cmd: %d, data: %s", msg.Head.Cmd, msg.Body) - if md, ok := common.ActionMap[pb.ProtoCode(msg.Head.Cmd)]; ok { + if md, ok := ActionMap[pb.ProtoCode(msg.Head.Cmd)]; ok { errCode, protomsg := md(msg) rsp, err := proto.Marshal(protomsg) if err != nil { @@ -51,7 +55,8 @@ func (s *Server)Start() error { models.InitGameServerModels() //Etcd 初始化 - conf.EtcdClient.PutWithLeasePrefix(conf.GlobalConf.GameConf.Name, conf.GlobalConf.GameConf.ID, fmt.Sprintf("%s:%d", conf.GlobalConf.GameConf.IP, conf.GlobalConf.GameConf.Port), 5) + s.EtcdClient = etcd.NewEtcdClient(conf.GlobalConf.Etcd) + s.EtcdClient.PutWithLeasePrefix(conf.GlobalConf.GameConf.Name, conf.GlobalConf.GameConf.ID, fmt.Sprintf("%s:%d", conf.GlobalConf.GameConf.IP, conf.GlobalConf.GameConf.Port), 5) port := fmt.Sprintf(":%d", s.SConf.Port) l, err := net.Listen("tcp", port) diff --git a/conf/conf.go b/conf/conf.go index a3e73c9..a7a02e6 100644 --- a/conf/conf.go +++ b/conf/conf.go @@ -6,7 +6,6 @@ import ( "gopkg.in/yaml.v3" "io/ioutil" "pro2d/components/db" - "pro2d/components/etcd" "pro2d/utils" ) @@ -61,7 +60,6 @@ type ServerConf struct { var( GlobalConf ServerConf SnowFlack *utils.Snowflake - EtcdClient *etcd.EtcdClient ) func init() { @@ -87,5 +85,4 @@ func init() { utils.Sugar.Errorf("connect db err: %v", err) } - EtcdClient = etcd.NewEtcdClient(GlobalConf.Etcd) } \ No newline at end of file diff --git a/protos b/protos index f39667f..432c66d 160000 --- a/protos +++ b/protos @@ -1 +1 @@ -Subproject commit f39667f517b4d302674707983210d7ac53460c6b +Subproject commit 432c66d9981fa1f6423f3e5aa27543a98ce8895c diff --git a/tools/protostostruct.go b/tools/protostostruct.go index 0d6a96a..2294e0c 100644 --- a/tools/protostostruct.go +++ b/tools/protostostruct.go @@ -14,8 +14,8 @@ var ( ProtoCode = "syntax = \"proto3\";\noption go_package = \"./pb;pb\";\n\npackage protocode;\n\nenum ProtoCode\n{\n UNKNOWN = 0x000;\n %s\n}" ProtoCodeLine = "\t%sRpc = %02x;\n" - GoProtoCodeStr = "package common\n\nimport (\n\t\"github.com/golang/protobuf/proto\"\n\t\"pro2d/actions\"\n\t\"pro2d/components/net\"\n\t\"pro2d/protos/pb\"\n)\n\ntype ActionHandler func (msg *net.MsgPkg) (int32, proto.Message)\n\nvar ActionMap map[pb.ProtoCode]ActionHandler\n\nfunc init() {\n%s\n}" - GoProtoCodeLine = "\tActionMap[pb.ProtoCode_%sRpc] = actions.%sRpc\n" + GoProtoCodeStr = "package actions\n\nimport (\n\t\"pro2d/components/net\"\n\t\"pro2d/protos/pb\"\n)\n\nfunc init() {\n\tnet.ActionMap = make(map[pb.ProtoCode]net.ActionHandler)\n\n%s\n}" + GoProtoCodeLine = "\tnet.ActionMap[pb.ProtoCode_%sRpc] = %sRpc\n" ) func ProtoToCode(readPath, filename string) (string, string) { @@ -98,7 +98,7 @@ func ReadProtos(readPath, OutPath string ) error { return fmt.Errorf("WriteNewFile|Write is err:%v", err) } - fw, err = os.OpenFile( OutPath+"common/protocode.go", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) + fw, err = os.OpenFile( OutPath+"actions/protocode.go", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) if err != nil { return fmt.Errorf("WriteNewFile|OpenFile is err:%v", err) } -- libgit2 0.21.2