Commit 3e92be6732e35706d94116282645326b32b8b73f
1 parent
db8164b6
fix: 优化协议,除了登录和创建角色接口,其他协议都直接把role数据传递进去。
Showing
5 changed files
with
71 additions
and
68 deletions
Show diff stats
cmd/gameserver/action/RoleAction.go
... | ... | @@ -12,7 +12,7 @@ import ( |
12 | 12 | "pro2d/pb" |
13 | 13 | ) |
14 | 14 | |
15 | -func HeartRpc(agent components.IAgent, msg components.IMessage) (int32, interface{}) { | |
15 | +func HeartRpc(role *models.RoleModel, msg components.IMessage) (int32, interface{}) { | |
16 | 16 | //msg.Conn.SetLastHeartCheckTime() |
17 | 17 | return 0, nil |
18 | 18 | } |
... | ... | @@ -36,7 +36,7 @@ func getRandomName() string { |
36 | 36 | return name |
37 | 37 | } |
38 | 38 | |
39 | -func CreateRpc(agent components.IAgent, msg components.IMessage) (int32, interface{}) { | |
39 | +func CreateRpc(role1 *models.RoleModel, msg components.IMessage) (int32, interface{}) { | |
40 | 40 | req := pb.CreateReq{} |
41 | 41 | if err := proto.Unmarshal(msg.GetData(), &req); err != nil { |
42 | 42 | logger.Error("CreateRpc err: %v", err) |
... | ... | @@ -63,59 +63,27 @@ func CreateRpc(agent components.IAgent, msg components.IMessage) (int32, interfa |
63 | 63 | return 0, nil |
64 | 64 | } |
65 | 65 | |
66 | -func LoginRpc(agent components.IAgent, msg components.IMessage) (int32, interface{}) { | |
67 | - //logger.Debug("11111111cmd: %v, msg: %s", msg.GetHeader().GetMsgID(), msg.GetData()) | |
68 | - req := pb.LoginReq{} | |
69 | - if err := proto.Unmarshal(msg.GetData(), &req); err != nil { | |
70 | - logger.Error("loginRpc err: %v", err) | |
71 | - return 1, nil | |
72 | - } | |
73 | - | |
74 | - role := models.RoleExistByUid(req.Token) | |
75 | - if role == nil { | |
76 | - return 2, nil | |
77 | - } | |
78 | - role.SetProperty("Device", req.Device) | |
79 | - protoMsg := &pb.RoleRsp{ | |
80 | - Role: role.Role, | |
81 | - Hero: role.GetAllHero(), | |
82 | - Team: role.GetAllTeam(), | |
83 | - Equipments: role.GetEquipments(), | |
84 | - } | |
85 | - //登录成功,存储agent role | |
86 | - agent.SetSchema(role) | |
87 | - return 0, protoMsg | |
88 | -} | |
89 | - | |
90 | -func ChangeTeamRpc(agent components.IAgent, msg components.IMessage) (int32, interface{}) { | |
66 | +func ChangeTeamRpc(role *models.RoleModel, msg components.IMessage) (int32, interface{}) { | |
91 | 67 | req := pb.ChangeTeamReq{} |
92 | 68 | if err := proto.Unmarshal(msg.GetData(), &req); err != nil { |
93 | 69 | logger.Error("loginRpc err: %v", err) |
94 | 70 | return 1, nil |
95 | 71 | } |
96 | - role := agent.GetSchema().(*models.RoleModel) | |
97 | - if role == nil { | |
98 | - return 2, nil | |
99 | - } | |
100 | 72 | |
101 | 73 | role.UpdateTeam(req.Team) |
102 | 74 | return 0, nil |
103 | 75 | } |
104 | 76 | |
105 | -func HeroEquipReferRpc(agent components.IAgent, msg components.IMessage) (int32, interface{}) { | |
77 | +func HeroEquipReferRpc(role *models.RoleModel, msg components.IMessage) (int32, interface{}) { | |
106 | 78 | req := pb.HeroEquipReferReq{} |
107 | 79 | if err := proto.Unmarshal(msg.GetData(), &req); err != nil { |
108 | 80 | logger.Error("loginRpc err: %v", err) |
109 | 81 | return 1, nil |
110 | 82 | } |
111 | - role := agent.GetSchema().(*models.RoleModel) | |
112 | - if role == nil { | |
113 | - return 2, nil | |
114 | - } | |
115 | 83 | |
116 | 84 | e, ok := role.Equipments[req.EquipId] |
117 | 85 | if !ok { |
118 | - return 3, nil | |
86 | + return 2, nil | |
119 | 87 | } |
120 | 88 | |
121 | 89 | equip := e.(*models.EquipModel) |
... | ... | @@ -150,38 +118,30 @@ func HeroEquipReferRpc(agent components.IAgent, msg components.IMessage) (int32, |
150 | 118 | return 0, nil |
151 | 119 | } |
152 | 120 | |
153 | -func RoleClearItemsRpc(agent components.IAgent, msg components.IMessage) (int32, interface{}) { | |
121 | +func RoleClearItemsRpc(role *models.RoleModel, msg components.IMessage) (int32, interface{}) { | |
154 | 122 | req := pb.RoleClearItemsReq{} |
155 | 123 | if err := proto.Unmarshal(msg.GetData(), &req); err != nil { |
156 | 124 | logger.Error("loginRpc err: %v", err) |
157 | 125 | return 1, nil |
158 | 126 | } |
159 | - role := agent.GetSchema().(*models.RoleModel) | |
160 | - if role == nil { | |
161 | - return 2, nil | |
162 | - } | |
163 | 127 | |
164 | 128 | if !role.CostItem(req.Id, req.Count) { |
165 | 129 | logger.Error("cost err: %s, %d", req.Id, req.Count) |
166 | - return 3, nil | |
130 | + return 2, nil | |
167 | 131 | } |
168 | 132 | return 0, nil |
169 | 133 | } |
170 | 134 | |
171 | -func EquipmentDelRpc(agent components.IAgent, msg components.IMessage) (int32, interface{}) { | |
135 | +func EquipmentDelRpc(role *models.RoleModel, msg components.IMessage) (int32, interface{}) { | |
172 | 136 | req := pb.EquipmentDelReq{} |
173 | 137 | if err := proto.Unmarshal(msg.GetData(), &req); err != nil { |
174 | 138 | logger.Error("loginRpc err: %v", err) |
175 | 139 | return 1, nil |
176 | 140 | } |
177 | - role := agent.GetSchema().(*models.RoleModel) | |
178 | - if role == nil { | |
179 | - return 2, nil | |
180 | - } | |
181 | 141 | |
182 | 142 | if err := mongoproxy.DelOne("equip", "id", req.Id); err != nil { |
183 | 143 | logger.Error(err.Error()) |
184 | - return 3, nil | |
144 | + return 2, nil | |
185 | 145 | } |
186 | 146 | delete(role.Equipments, req.Id) |
187 | 147 | ... | ... |
cmd/gameserver/action/protocode.go
... | ... | @@ -9,7 +9,6 @@ func GetActionMap() map[interface{}]interface{} { |
9 | 9 | logger.Debug("init protocode...") |
10 | 10 | am := make(map[interface{}]interface{}) |
11 | 11 | am[uint32(pb.ProtoCode_HeartReq)] = HeartRpc |
12 | - am[uint32(pb.ProtoCode_LoginReq)] = LoginRpc | |
13 | 12 | am[uint32(pb.ProtoCode_CreateReq)] = CreateRpc |
14 | 13 | am[uint32(pb.ProtoCode_ChangeTeamReq)] = ChangeTeamRpc |
15 | 14 | am[uint32(pb.ProtoCode_HeroEquipReferReq)] = HeroEquipReferRpc | ... | ... |
cmd/gameserver/service/agent.go
... | ... | @@ -58,40 +58,75 @@ func (c *Agent) OnConnection(conn components.IConnection) { |
58 | 58 | c.IConnection = conn |
59 | 59 | } |
60 | 60 | |
61 | +func (c *Agent) OnLoginQuery(msg components.IMessage) (int32, proto.Message) { | |
62 | + //logger.Debug("11111111cmd: %v, msg: %s", msg.GetHeader().GetMsgID(), msg.GetData()) | |
63 | + req := pb.LoginReq{} | |
64 | + if err := proto.Unmarshal(msg.GetData(), &req); err != nil { | |
65 | + logger.Error("loginRpc err: %v", err) | |
66 | + return 1, nil | |
67 | + } | |
68 | + | |
69 | + role := models.RoleExistByUid(req.Token) | |
70 | + if role == nil { | |
71 | + return 2, nil | |
72 | + } | |
73 | + role.SetProperty("Device", req.Device) | |
74 | + protoMsg := &pb.RoleRsp{ | |
75 | + Role: role.Role, | |
76 | + Hero: role.GetAllHero(), | |
77 | + Team: role.GetAllTeam(), | |
78 | + Equipments: role.GetEquipments(), | |
79 | + } | |
80 | + //登录成功,存储agent role | |
81 | + c.SetSchema(role) | |
82 | + return 0, protoMsg | |
83 | +} | |
84 | + | |
85 | +func (c *Agent) SendMsg(errCode int32, cmd uint32, msg interface{}) { | |
86 | + if msg == nil || errCode != 0 { | |
87 | + c.Send(errCode, cmd, nil) | |
88 | + return | |
89 | + } | |
90 | + rsp, err := proto.Marshal(msg.(proto.Message)) | |
91 | + if err != nil { | |
92 | + c.Send(-100, cmd, nil) | |
93 | + return | |
94 | + } | |
95 | + c.Send(errCode, cmd, rsp) | |
96 | +} | |
97 | + | |
61 | 98 | func (c *Agent) OnMessage(msg components.IMessage) { |
62 | 99 | atomic.StoreInt64(&c.lastHeartCheckTime, common.Timex()) |
100 | + //heart | |
63 | 101 | if msg.GetHeader().GetMsgID() == uint32(pb.ProtoCode_HeartReq) { |
64 | 102 | return |
65 | 103 | } |
66 | 104 | |
105 | + //login | |
106 | + if msg.GetHeader().GetMsgID() == uint32(pb.ProtoCode_LoginReq) { | |
107 | + code, protoMsg := c.OnLoginQuery(msg) | |
108 | + c.SendMsg(code, msg.GetHeader().GetMsgID(), protoMsg) | |
109 | + return | |
110 | + } | |
111 | + | |
112 | + //get handler by msgid | |
67 | 113 | md := c.Server.GetAction(msg.GetHeader().GetMsgID()) |
68 | 114 | if md == nil { |
69 | 115 | logger.Debug("cmd: %d, handler is nil", msg.GetHeader().GetMsgID()) |
70 | 116 | return |
71 | 117 | } |
72 | - logger.Debug("protocolID: %d", msg.GetHeader().GetMsgID()) | |
73 | - //fmt.Printf("errCode: %d, protoMsg:%v\n", errCode, protoMsg) | |
74 | - | |
75 | - f := md.(func(agent components.IAgent, msg components.IMessage) (int32, interface{})) | |
76 | - errCode, protoMsg := f(c, msg) | |
77 | 118 | |
78 | - if protoMsg == nil { | |
79 | - c.Send(errCode, msg.GetHeader().GetMsgID(), nil) | |
80 | - return | |
81 | - } | |
119 | + logger.Debug("protocolID: %d", msg.GetHeader().GetMsgID()) | |
82 | 120 | |
83 | - if errCode != 0 { | |
84 | - logger.Error("errCode %d, msg: %v", errCode, protoMsg) | |
85 | - c.Send(errCode, msg.GetHeader().GetMsgID(), nil) | |
121 | + if msg.GetHeader().GetMsgID() != uint32(pb.ProtoCode_CreateReq) && c.Role == nil { | |
122 | + c.Send(-101, msg.GetHeader().GetMsgID(), nil) | |
86 | 123 | return |
87 | 124 | } |
88 | 125 | |
89 | - rsp, err := proto.Marshal(protoMsg.(proto.Message)) | |
90 | - if err != nil { | |
91 | - c.Send(-100, msg.GetHeader().GetMsgID(), nil) | |
92 | - return | |
93 | - } | |
94 | - c.Send(errCode, msg.GetHeader().GetMsgID(), rsp) | |
126 | + //调用协议号对应的逻辑函数 | |
127 | + f := md.(func(role *models.RoleModel, msg components.IMessage) (int32, interface{})) | |
128 | + errCode, protoMsg := f(c.Role, msg) | |
129 | + c.SendMsg(errCode, msg.GetHeader().GetMsgID(), protoMsg) | |
95 | 130 | } |
96 | 131 | |
97 | 132 | func (c *Agent) OnTimer() { | ... | ... |
common/components/icompontents.go
... | ... | @@ -2,6 +2,7 @@ package components |
2 | 2 | |
3 | 3 | import ( |
4 | 4 | "github.com/gin-gonic/gin" |
5 | + "github.com/golang/protobuf/proto" | |
5 | 6 | "google.golang.org/protobuf/reflect/protoreflect" |
6 | 7 | "reflect" |
7 | 8 | ) |
... | ... | @@ -93,6 +94,7 @@ type ( |
93 | 94 | |
94 | 95 | GetServer() IServer |
95 | 96 | SetServer(server IServer) |
97 | + SendMsg(errCode int32, cmd uint32, msg proto.Message) | |
96 | 98 | } |
97 | 99 | |
98 | 100 | //Connector | ... | ... |
tools/generator.py
... | ... | @@ -41,10 +41,17 @@ def generatorProto(path): |
41 | 41 | messageStr = sline[1].replace('\n', '').replace('{', "") |
42 | 42 | n1 = messageStr.find('Req') |
43 | 43 | n2 = messageStr.find('Rsp') |
44 | + loginReq = messageStr.find('LoginReq') | |
45 | + | |
46 | + | |
44 | 47 | |
45 | 48 | if n1 != -1: |
46 | 49 | code += 1 |
47 | 50 | ProtoCodeData += ProtoCodeLineReq.format(messageStr[:n1], code) |
51 | + | |
52 | + if loginReq != -1: | |
53 | + continue | |
54 | + | |
48 | 55 | GoCodeData += GoProtoCodeLine.format(messageStr[:n1], messageStr[:n1]) |
49 | 56 | elif n2 != -1: |
50 | 57 | code += 1 | ... | ... |