msg_send.go
7.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package main
import (
db "WorldTester/db"
pb "WorldTester/pb"
"bytes"
"common/logger"
"encoding/binary"
"log"
"math/rand"
"net"
"strconv"
"time"
"github.com/golang/protobuf/proto"
)
type CommandItem struct {
CommandID int
CommandDesc string
}
type CommandItemArray []*CommandItem
func (r CommandItemArray) Len() int { return len(r) }
func (r CommandItemArray) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
func (r CommandItemArray) Less(i, j int) bool {
if r[i].CommandID < r[j].CommandID {
return true
}
return false
}
func RemoveDuplicates(elements []int) []int {
encountered := map[int]bool{}
result := []int{}
for v := range elements {
if encountered[elements[v]] == true {
} else {
encountered[elements[v]] = true
result = append(result, elements[v])
}
}
return result
}
type CommandHandler func(conn net.Conn, args []string)
type CommandHandlerInfo struct {
desc string
handler CommandHandler
}
var (
map_commands = make(map[int]CommandHandlerInfo)
m_matchuid uint32 = 0
m_roomid uint32 = 0
m_playerid uint32 = 0
m_seq uint32 = 0
m_clubseq uint32 = 0
crack_str = "1234567890~!@#$%^&*()_+|}{:L?><bxcnzcaasuDDYCTDGHSAWEXXBVM>><?:L{}PITEQQ!~456abcedfsfsfdilekso"
)
func registCommandHandler(command_id int, command_desc string, handler CommandHandler) {
var info CommandHandlerInfo
info.desc = command_desc
info.handler = handler
map_commands[command_id] = info
}
func init() {
}
func SendLogin(player *PlayerInfo) {
var req pb.RequestLogon
req.Version = "1.1.36"
req.Token = "LeaveMeAlone"
player.state = STATE_PLAYER_INIT
player.seq++
SendMessage(player.conn, player.seq, player.uid, player.rid, pb.MSGID_MsgID_Logon_Request, &req)
}
func SendPurchaseClubLevel(player *PlayerInfo, clubid int32) {
var req pb.RequestPurchaseClubLevel
req.ClubId = clubid
req.TypeId = 5 //5 star
player.seq++
SendMessage(player.conn, player.seq, player.uid, player.rid, pb.MSGID_MsgID_PurchaseClubLevel_Request, &req)
}
func SendRechargeClubFund(player *PlayerInfo, clubid int32) {
var req pb.RequestRechargeClubFund
req.Param = new(pb.RechargeClubFundParams)
req.Param.ClubId = clubid
req.Param.BuyCount = 1
player.seq++
SendMessage(player.conn, player.seq, player.uid, player.rid, pb.MSGID_MsgID_RechargeClubFund_Request, &req)
}
func SendModifyClubInfo(player *PlayerInfo, clubid int32) {
log.Println("SendModifyClubInfo")
var req pb.RequestModifyClubInfo
req.Param = new(pb.ModifyClubInfoParams)
req.Param.ClubId = clubid
req.Param.ClubUid = int32(player.uid)
player.seq++
req.Param.ClubName = "robet new" + strconv.Itoa(int(player.seq))
req.Param.ClubDescrption = "robet new description"
SendMessage(player.conn, player.seq, player.uid, player.rid, pb.MSGID_MsgID_ModifyClubInfo_Request, &req)
}
func SendJoinClubReply(player *PlayerInfo, result int32, clubId int32, uid int32) {
log.Println("SendJoinClubReply")
var req pb.ReplyJoinClub
req.Result = result
req.ClubId = clubId
req.Uid = uid
req.Reason = "a valid reason"
player.seq++
SendMessage(player.conn, player.seq, player.uid, player.rid, pb.MSGID_MsgID_JoinClub_Reply, &req)
}
func SendCreateClub(player *PlayerInfo) {
log.Println("SendCreateClub")
var req pb.RequestCreateClub
req.Param = new(pb.ClubParams)
m_clubseq++
req.Param.ClubName = "阿明正俱乐部" + strconv.Itoa(int(m_clubseq))
req.Param.ClubArea = "上海"
req.Param.ClubIcon = "阿明正.ico"
player.seq++
SendMessage(player.conn, player.seq, player.uid, player.rid, pb.MSGID_MsgID_CreateClub_Request, &req)
}
func SendJoinClub(player *PlayerInfo) {
log.Println("SendJoinClub")
var req pb.RequestJoinClub
var cids []int
for _, vv := range m_map_club_creater.Items() {
creater := vv.(*PlayerInfo)
if creater != nil {
clubIds, err := db.GetCreatedClubs(int(creater.uid))
if err != nil {
log.Println("SendJoinClub-GetCreaterIdClubId-err:", err)
break
}
cids = append(cids, clubIds...)
}
}
cids = RemoveDuplicates(cids)
log.Println("cids:", cids)
req.ClubUid = int32(player.uid)
for _, clubId := range cids {
req.ClubId = int32(clubId)
player.seq++
SendMessage(player.conn, player.seq, player.uid, player.rid, pb.MSGID_MsgID_JoinClub_Request, &req)
log.Println("SendJoinClub-SendMessage-clubId:", clubId, " player.uid:", player.uid)
}
}
func SendGetClubList(player *PlayerInfo) {
log.Println("SendGetClubList")
var req pb.RequestClubSnapshotList
req.Uid = int32(player.uid)
player.seq++
SendMessage(player.conn, player.seq, player.uid, player.rid, pb.MSGID_MsgID_ClubSnapshotList_Request, &req)
}
func SendGetClubCreaterInfo(player *PlayerInfo, ClubId int32) {
log.Println("SendGetClubCreaterInfo")
var req pb.RequestClubCreaterInfo
req.ClubId = ClubId
player.seq++
SendMessage(player.conn, player.seq, player.uid, player.rid, pb.MSGID_MsgID_ClubCreaterInfo_Request, &req)
}
func SendSearchClubInfo(player *PlayerInfo, ClubId int32) {
log.Println("SendSearchClubInfo")
var req pb.RequestSearchClubInfo
req.ClubId = ClubId
player.seq++
SendMessage(player.conn, player.seq, player.uid, player.rid, pb.MSGID_MsgID_SearchClubInfo_Request, &req)
}
func SendUserData(player *PlayerInfo, uId int32) {
log.Println("SendUserData")
var req pb.RequestGetUserData
req.UserId = uId
player.seq++
SendMessage(player.conn, player.seq, player.uid, player.rid, pb.MSGID_MsgID_GetUserData_Request, &req)
}
func SendMessage(conn net.Conn, seq uint32, uid uint32, rid uint32, msgid pb.MSGID, msg interface{}) {
tmp, err := proto.Marshal(msg.(proto.Message))
if err != nil {
logger.Info("marshal msgid:%v response body error:%v", msgid, err)
return
}
if conn == nil {
logger.Info("conn is nil for uid:%v rid:%v msgid:%v", uid, rid, msgid)
return
}
total_len := len(tmp) + int(Min_Message_Size)
bys := new(bytes.Buffer)
binary.Write(bys, binary.BigEndian, uint16(total_len))
binary.Write(bys, binary.BigEndian, uint16(msgid))
binary.Write(bys, binary.BigEndian, uint32(seq))
binary.Write(bys, binary.BigEndian, uint32(uid))
binary.Write(bys, binary.BigEndian, uint32(rid))
logger.Info("--> send message header, len:%v roomid:%v msgid:%v seq:%v uid:%v",
total_len, rid, msgid, seq, uid)
var buf []byte
if do_trash {
r := rand.Intn(1000)
if r < 300 {
trash := GetTranshBytesByLen(total_len)
buf = append(buf, trash...)
logger.Info("***** do trash for msgid:%v uid:%v rid:%v", pb.MSGID(msgid), uid, rid)
} else {
buf = append(bys.Bytes(), tmp...)
}
} else {
buf = append(bys.Bytes(), tmp...)
}
conn.SetWriteDeadline(time.Now().Add(1 * time.Second))
_, err = conn.Write(buf)
if err != nil {
logger.Info("uid:%+v rid:%+v msgid:%+v send socket err:%+v",
uid, rid, msgid, err)
}
}
func SendGetClubMemberSnapshotList(player *PlayerInfo, ClubId int32) {
log.Println("SendGetClubMemberSnapshotList")
var req pb.RequestClubMemberSnapshotList
req.Param = new(pb.ClubMemberSnapshotListParams)
req.Param.ClubId = ClubId
req.Param.ClubUid = int32(player.uid)
player.seq++
SendMessage(player.conn, player.seq, player.uid, player.rid, pb.MSGID_MsgID_ClubMemberSnapshotList_Request, &req)
}
func SendModifyClubMember(player *PlayerInfo, ClubId int32, targetid int32) {
log.Println("SendModifyClubMember")
var req pb.RequestModifyClubMember
req.Param = new(pb.ModifyClubMemberParams)
req.Param.ClubId = ClubId
req.Param.ClubUid = int32(player.uid)
req.Param.ActionType = 0
req.Param.TargetId = targetid
player.seq++
SendMessage(player.conn, player.seq, player.uid, player.rid, pb.MSGID_MsgID_ModifyClubMember_Request, &req)
}
func SendGrantClubFund(player *PlayerInfo, ClubId int32, targetid int32) {
log.Println("SendGrantClubFund")
var req pb.RequestGrantClubFund
req.Param = new(pb.GrantClubFundParams)
req.Param.ClubId = ClubId
req.Param.ClubUid = int32(player.uid)
req.Param.Amount = 100
req.Param.TargetId = targetid
player.seq++
SendMessage(player.conn, player.seq, player.uid, player.rid, pb.MSGID_MsgID_GrantClubFund_Request, &req)
}