Commit c92a54a3773caccd7e21af9d52174735384b9b54

Authored by zhangqijia
1 parent a0c46341

循环引用的问题

actions/HttpAction.go
... ... @@ -5,6 +5,7 @@ import (
5 5 "github.com/gin-gonic/gin"
6 6 "net/http"
7 7 "pro2d/components/db"
  8 + "pro2d/components/etcd"
8 9 "pro2d/conf"
9 10 "pro2d/models"
10 11 "pro2d/protos/pb"
... ... @@ -16,6 +17,7 @@ import (
16 17 type HttpAction struct {
17 18 version string
18 19 port []string
  20 + EtcdClient *etcd.EtcdClient
19 21 }
20 22  
21 23 func NewHttpAction(version string, port ...string) *HttpAction {
... ... @@ -84,7 +86,7 @@ func (h *HttpAction) Login(c *gin.Context) {
84 86 }
85 87  
86 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 90 gs = append(gs, &pb.ServiceInfo{
89 91 Id: k,
90 92 Name: conf.GlobalConf.GameConf.Name,
... ... @@ -104,7 +106,8 @@ func (h *HttpAction) Start() error {
104 106 models.InitAccountServerModels()
105 107  
106 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 112 //gin初始化
110 113 r := gin.Default()
... ...
actions/protocode.go 0 → 100644
... ... @@ -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 16 \ No newline at end of file
... ...
common/protocode.go deleted
... ... @@ -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 0 \ No newline at end of file
components/net/server.go
... ... @@ -4,8 +4,8 @@ import (
4 4 "fmt"
5 5 "github.com/golang/protobuf/proto"
6 6 "net"
7   - "pro2d/common"
8 7 "pro2d/components/db"
  8 + "pro2d/components/etcd"
9 9 "pro2d/conf"
10 10 "pro2d/models"
11 11 "pro2d/protos/pb"
... ... @@ -13,9 +13,13 @@ import (
13 13 "sync"
14 14 )
15 15  
  16 +type ActionHandler func (msg *MsgPkg) (int32, proto.Message)
  17 +var ActionMap map[pb.ProtoCode]ActionHandler
  18 +
16 19 type Server struct {
17 20 SConf *conf.SConf
18 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 32  
29 33 func (s *Server) OnRecv(msg *MsgPkg) {
30 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 36 errCode, protomsg := md(msg)
33 37 rsp, err := proto.Marshal(protomsg)
34 38 if err != nil {
... ... @@ -51,7 +55,8 @@ func (s *Server)Start() error {
51 55 models.InitGameServerModels()
52 56  
53 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 61 port := fmt.Sprintf(":%d", s.SConf.Port)
57 62 l, err := net.Listen("tcp", port)
... ...
conf/conf.go
... ... @@ -6,7 +6,6 @@ import (
6 6 "gopkg.in/yaml.v3"
7 7 "io/ioutil"
8 8 "pro2d/components/db"
9   - "pro2d/components/etcd"
10 9 "pro2d/utils"
11 10 )
12 11  
... ... @@ -61,7 +60,6 @@ type ServerConf struct {
61 60 var(
62 61 GlobalConf ServerConf
63 62 SnowFlack *utils.Snowflake
64   - EtcdClient *etcd.EtcdClient
65 63 )
66 64  
67 65 func init() {
... ... @@ -87,5 +85,4 @@ func init() {
87 85 utils.Sugar.Errorf("connect db err: %v", err)
88 86 }
89 87  
90   - EtcdClient = etcd.NewEtcdClient(GlobalConf.Etcd)
91 88 }
92 89 \ No newline at end of file
... ...
1   -Subproject commit f39667f517b4d302674707983210d7ac53460c6b
  1 +Subproject commit 432c66d9981fa1f6423f3e5aa27543a98ce8895c
... ...
tools/protostostruct.go
... ... @@ -14,8 +14,8 @@ var (
14 14 ProtoCode = "syntax = \"proto3\";\noption go_package = \"./pb;pb\";\n\npackage protocode;\n\nenum ProtoCode\n{\n UNKNOWN = 0x000;\n %s\n}"
15 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 21 func ProtoToCode(readPath, filename string) (string, string) {
... ... @@ -98,7 +98,7 @@ func ReadProtos(readPath, OutPath string ) error {
98 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 102 if err != nil {
103 103 return fmt.Errorf("WriteNewFile|OpenFile is err:%v", err)
104 104 }
... ...