Commit c92a54a3773caccd7e21af9d52174735384b9b54
1 parent
a0c46341
循环引用的问题
Showing
7 changed files
with
32 additions
and
31 deletions
Show diff stats
actions/HttpAction.go
@@ -5,6 +5,7 @@ import ( | @@ -5,6 +5,7 @@ import ( | ||
5 | "github.com/gin-gonic/gin" | 5 | "github.com/gin-gonic/gin" |
6 | "net/http" | 6 | "net/http" |
7 | "pro2d/components/db" | 7 | "pro2d/components/db" |
8 | + "pro2d/components/etcd" | ||
8 | "pro2d/conf" | 9 | "pro2d/conf" |
9 | "pro2d/models" | 10 | "pro2d/models" |
10 | "pro2d/protos/pb" | 11 | "pro2d/protos/pb" |
@@ -16,6 +17,7 @@ import ( | @@ -16,6 +17,7 @@ import ( | ||
16 | type HttpAction struct { | 17 | type HttpAction struct { |
17 | version string | 18 | version string |
18 | port []string | 19 | port []string |
20 | + EtcdClient *etcd.EtcdClient | ||
19 | } | 21 | } |
20 | 22 | ||
21 | func NewHttpAction(version string, port ...string) *HttpAction { | 23 | func NewHttpAction(version string, port ...string) *HttpAction { |
@@ -84,7 +86,7 @@ func (h *HttpAction) Login(c *gin.Context) { | @@ -84,7 +86,7 @@ func (h *HttpAction) Login(c *gin.Context) { | ||
84 | } | 86 | } |
85 | 87 | ||
86 | var gs []*pb.ServiceInfo | 88 | var gs []*pb.ServiceInfo |
87 | - for k, v := range conf.EtcdClient.GetByPrefix(conf.GlobalConf.GameConf.Name) { | 89 | + for k, v := range h.EtcdClient.GetByPrefix(conf.GlobalConf.GameConf.Name) { |
88 | gs = append(gs, &pb.ServiceInfo{ | 90 | gs = append(gs, &pb.ServiceInfo{ |
89 | Id: k, | 91 | Id: k, |
90 | Name: conf.GlobalConf.GameConf.Name, | 92 | Name: conf.GlobalConf.GameConf.Name, |
@@ -104,7 +106,8 @@ func (h *HttpAction) Start() error { | @@ -104,7 +106,8 @@ func (h *HttpAction) Start() error { | ||
104 | models.InitAccountServerModels() | 106 | models.InitAccountServerModels() |
105 | 107 | ||
106 | //Etcd 初始化 | 108 | //Etcd 初始化 |
107 | - conf.EtcdClient.PutWithLeasePrefix(conf.GlobalConf.AccountConf.Name, conf.GlobalConf.AccountConf.ID, fmt.Sprintf("%s:%d", conf.GlobalConf.AccountConf.IP, conf.GlobalConf.AccountConf.Port), 5) | 109 | + h.EtcdClient = etcd.NewEtcdClient(conf.GlobalConf.Etcd) |
110 | + h.EtcdClient.PutWithLeasePrefix(conf.GlobalConf.AccountConf.Name, conf.GlobalConf.AccountConf.ID, fmt.Sprintf("%s:%d", conf.GlobalConf.AccountConf.IP, conf.GlobalConf.AccountConf.Port), 5) | ||
108 | 111 | ||
109 | //gin初始化 | 112 | //gin初始化 |
110 | r := gin.Default() | 113 | r := gin.Default() |
@@ -0,0 +1,15 @@ | @@ -0,0 +1,15 @@ | ||
1 | +package actions | ||
2 | + | ||
3 | +import ( | ||
4 | + "pro2d/components/net" | ||
5 | + "pro2d/protos/pb" | ||
6 | +) | ||
7 | + | ||
8 | +func init() { | ||
9 | + net.ActionMap = make(map[pb.ProtoCode]net.ActionHandler) | ||
10 | + | ||
11 | + net.ActionMap[pb.ProtoCode_HeartRpc] = HeartRpc | ||
12 | + net.ActionMap[pb.ProtoCode_LoginRpc] = LoginRpc | ||
13 | + net.ActionMap[pb.ProtoCode_CreateRpc] = CreateRpc | ||
14 | + | ||
15 | +} | ||
0 | \ No newline at end of file | 16 | \ No newline at end of file |
common/protocode.go deleted
@@ -1,19 +0,0 @@ | @@ -1,19 +0,0 @@ | ||
1 | -package common | ||
2 | - | ||
3 | -import ( | ||
4 | - "github.com/golang/protobuf/proto" | ||
5 | - "pro2d/actions" | ||
6 | - "pro2d/components/net" | ||
7 | - "pro2d/protos/pb" | ||
8 | -) | ||
9 | - | ||
10 | -type ActionHandler func (msg *net.MsgPkg) (int32, proto.Message) | ||
11 | - | ||
12 | -var ActionMap map[pb.ProtoCode]ActionHandler | ||
13 | - | ||
14 | -func init() { | ||
15 | - ActionMap[pb.ProtoCode_HeartRpc] = actions.HeartRpc | ||
16 | - ActionMap[pb.ProtoCode_LoginRpc] = actions.LoginRpc | ||
17 | - ActionMap[pb.ProtoCode_CreateRpc] = actions.CreateRpc | ||
18 | - | ||
19 | -} | ||
20 | \ No newline at end of file | 0 | \ No newline at end of file |
components/net/server.go
@@ -4,8 +4,8 @@ import ( | @@ -4,8 +4,8 @@ import ( | ||
4 | "fmt" | 4 | "fmt" |
5 | "github.com/golang/protobuf/proto" | 5 | "github.com/golang/protobuf/proto" |
6 | "net" | 6 | "net" |
7 | - "pro2d/common" | ||
8 | "pro2d/components/db" | 7 | "pro2d/components/db" |
8 | + "pro2d/components/etcd" | ||
9 | "pro2d/conf" | 9 | "pro2d/conf" |
10 | "pro2d/models" | 10 | "pro2d/models" |
11 | "pro2d/protos/pb" | 11 | "pro2d/protos/pb" |
@@ -13,9 +13,13 @@ import ( | @@ -13,9 +13,13 @@ import ( | ||
13 | "sync" | 13 | "sync" |
14 | ) | 14 | ) |
15 | 15 | ||
16 | +type ActionHandler func (msg *MsgPkg) (int32, proto.Message) | ||
17 | +var ActionMap map[pb.ProtoCode]ActionHandler | ||
18 | + | ||
16 | type Server struct { | 19 | type Server struct { |
17 | SConf *conf.SConf | 20 | SConf *conf.SConf |
18 | Clients *sync.Map | 21 | Clients *sync.Map |
22 | + EtcdClient *etcd.EtcdClient | ||
19 | 23 | ||
20 | } | 24 | } |
21 | 25 | ||
@@ -28,7 +32,7 @@ func NewServer(sConf *conf.SConf) *Server { | @@ -28,7 +32,7 @@ func NewServer(sConf *conf.SConf) *Server { | ||
28 | 32 | ||
29 | func (s *Server) OnRecv(msg *MsgPkg) { | 33 | func (s *Server) OnRecv(msg *MsgPkg) { |
30 | utils.Sugar.Debugf("cmd: %d, data: %s", msg.Head.Cmd, msg.Body) | 34 | utils.Sugar.Debugf("cmd: %d, data: %s", msg.Head.Cmd, msg.Body) |
31 | - if md, ok := common.ActionMap[pb.ProtoCode(msg.Head.Cmd)]; ok { | 35 | + if md, ok := ActionMap[pb.ProtoCode(msg.Head.Cmd)]; ok { |
32 | errCode, protomsg := md(msg) | 36 | errCode, protomsg := md(msg) |
33 | rsp, err := proto.Marshal(protomsg) | 37 | rsp, err := proto.Marshal(protomsg) |
34 | if err != nil { | 38 | if err != nil { |
@@ -51,7 +55,8 @@ func (s *Server)Start() error { | @@ -51,7 +55,8 @@ func (s *Server)Start() error { | ||
51 | models.InitGameServerModels() | 55 | models.InitGameServerModels() |
52 | 56 | ||
53 | //Etcd 初始化 | 57 | //Etcd 初始化 |
54 | - conf.EtcdClient.PutWithLeasePrefix(conf.GlobalConf.GameConf.Name, conf.GlobalConf.GameConf.ID, fmt.Sprintf("%s:%d", conf.GlobalConf.GameConf.IP, conf.GlobalConf.GameConf.Port), 5) | 58 | + s.EtcdClient = etcd.NewEtcdClient(conf.GlobalConf.Etcd) |
59 | + s.EtcdClient.PutWithLeasePrefix(conf.GlobalConf.GameConf.Name, conf.GlobalConf.GameConf.ID, fmt.Sprintf("%s:%d", conf.GlobalConf.GameConf.IP, conf.GlobalConf.GameConf.Port), 5) | ||
55 | 60 | ||
56 | port := fmt.Sprintf(":%d", s.SConf.Port) | 61 | port := fmt.Sprintf(":%d", s.SConf.Port) |
57 | l, err := net.Listen("tcp", port) | 62 | l, err := net.Listen("tcp", port) |
conf/conf.go
@@ -6,7 +6,6 @@ import ( | @@ -6,7 +6,6 @@ import ( | ||
6 | "gopkg.in/yaml.v3" | 6 | "gopkg.in/yaml.v3" |
7 | "io/ioutil" | 7 | "io/ioutil" |
8 | "pro2d/components/db" | 8 | "pro2d/components/db" |
9 | - "pro2d/components/etcd" | ||
10 | "pro2d/utils" | 9 | "pro2d/utils" |
11 | ) | 10 | ) |
12 | 11 | ||
@@ -61,7 +60,6 @@ type ServerConf struct { | @@ -61,7 +60,6 @@ type ServerConf struct { | ||
61 | var( | 60 | var( |
62 | GlobalConf ServerConf | 61 | GlobalConf ServerConf |
63 | SnowFlack *utils.Snowflake | 62 | SnowFlack *utils.Snowflake |
64 | - EtcdClient *etcd.EtcdClient | ||
65 | ) | 63 | ) |
66 | 64 | ||
67 | func init() { | 65 | func init() { |
@@ -87,5 +85,4 @@ func init() { | @@ -87,5 +85,4 @@ func init() { | ||
87 | utils.Sugar.Errorf("connect db err: %v", err) | 85 | utils.Sugar.Errorf("connect db err: %v", err) |
88 | } | 86 | } |
89 | 87 | ||
90 | - EtcdClient = etcd.NewEtcdClient(GlobalConf.Etcd) | ||
91 | } | 88 | } |
92 | \ No newline at end of file | 89 | \ No newline at end of file |
tools/protostostruct.go
@@ -14,8 +14,8 @@ var ( | @@ -14,8 +14,8 @@ var ( | ||
14 | ProtoCode = "syntax = \"proto3\";\noption go_package = \"./pb;pb\";\n\npackage protocode;\n\nenum ProtoCode\n{\n UNKNOWN = 0x000;\n %s\n}" | 14 | ProtoCode = "syntax = \"proto3\";\noption go_package = \"./pb;pb\";\n\npackage protocode;\n\nenum ProtoCode\n{\n UNKNOWN = 0x000;\n %s\n}" |
15 | ProtoCodeLine = "\t%sRpc = %02x;\n" | 15 | ProtoCodeLine = "\t%sRpc = %02x;\n" |
16 | 16 | ||
17 | - 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}" | ||
18 | - GoProtoCodeLine = "\tActionMap[pb.ProtoCode_%sRpc] = actions.%sRpc\n" | 17 | + 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}" |
18 | + GoProtoCodeLine = "\tnet.ActionMap[pb.ProtoCode_%sRpc] = %sRpc\n" | ||
19 | ) | 19 | ) |
20 | 20 | ||
21 | func ProtoToCode(readPath, filename string) (string, string) { | 21 | func ProtoToCode(readPath, filename string) (string, string) { |
@@ -98,7 +98,7 @@ func ReadProtos(readPath, OutPath string ) error { | @@ -98,7 +98,7 @@ func ReadProtos(readPath, OutPath string ) error { | ||
98 | return fmt.Errorf("WriteNewFile|Write is err:%v", err) | 98 | return fmt.Errorf("WriteNewFile|Write is err:%v", err) |
99 | } | 99 | } |
100 | 100 | ||
101 | - fw, err = os.OpenFile( OutPath+"common/protocode.go", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) | 101 | + fw, err = os.OpenFile( OutPath+"actions/protocode.go", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) |
102 | if err != nil { | 102 | if err != nil { |
103 | return fmt.Errorf("WriteNewFile|OpenFile is err:%v", err) | 103 | return fmt.Errorf("WriteNewFile|OpenFile is err:%v", err) |
104 | } | 104 | } |