def parseData(self, data): msg = MsgPack() try: msg.unpack(data) return msg except: raise Exception('the json data error 1 !! [' + repr(data) + ']')
def onMsg(self): if self.robotUser: pack = stackless.getcurrent()._fttask.pack # ftlog.debug('RobotClientProtocol.onMsg', self.robotUser.snsId, '[' + pack + ']') msg = MsgPack() msg.unpack(pack) self.robotUser.msgQueue.append(msg) else: # ftlog.debug('RobotClientProtocol.onMsg robot user is none !!') self.closeConnection(1)
def convertOldRoomId(roomId, msgstr): if roomId in OLDROOMDI_MAP: rid = OLDROOMDI_MAP[roomId] ftlog.info('convertOldRoomId', roomId, '->', rid) msg = MsgPack() try: msg.unpack(msgstr) except: raise Exception('the json data error 7 !! [' + repr(msgstr) + ']') msg.setParam('roomId', rid) msgstr = msg.pack() return rid, msgstr return roomId, msgstr
def encode(msgstr): if not msgstr: return msgstr if msgstr[0] != "{": return msgstr if msgstr.find('"cmdId"') < 0: return msgstr msgPack = MsgPack() msgPack.unpack(msgstr) cmdId = msgPack.getKey("cmdId") if not cmdId: return msgstr cmdObj = _getCmdObj(cmdId) if not cmdObj: return msgstr binStr = cmdObj.structMsg(msgPack) return struct.pack("!3sB", "$fs", cmdId) + binStr
def __changeMsgRoomId(msgpack, newRoomId, clientRoomId): """ 处理结果返回给客户端时,部分游戏(例如德州、三顺)需要判断返回的roomId是否与本地一致 """ if isinstance(msgpack, MsgPack): msgpack.setParam("roomId", newRoomId) msgpack.setParam("clientRoomId", clientRoomId) return msgpack else: newMsgPack = MsgPack() try: newMsgPack.unpack(msgpack) except: raise Exception('the json data error 3 !! [' + repr(msgpack) + ']') newMsgPack.setParam("roomId", newRoomId) newMsgPack.setParam("clientRoomId", clientRoomId) return newMsgPack
def onAgentSelfCommand(agentProtocol, src, queryid, userheader1, userheader2, message): # 处理AGENT自身的命令, 而非转发的命令 msg = MsgPack() try: msg.unpack(message) except: raise Exception('the json data error 2 !! [' + repr(message) + ']') task = FTTasklet.getCurrentFTTasklet() task.pack = msg task.run_args['src'] = src task.run_args['pack'] = msg if queryid: task.run_args['query_id'] = queryid task.run_args['userheader1'] = userheader1 task.run_args['userheader2'] = userheader2 runcmd.handlerCommand(msg)
def __getRoomIdByTableId(msgpack): """ 部分游戏(例如德州、三顺)老版本牌桌周边功能需要使用bigRoomId,发送给GT的协议里roomId也是bigRoomId """ if isinstance(msgpack, MsgPack): tableId = msgpack.getParam("tableId", 0) shadowRoomId = tableId / 10000 msgpack.setParam("roomId", shadowRoomId) return msgpack, shadowRoomId else: newMsgPack = MsgPack() try: newMsgPack.unpack(msgpack) except: raise Exception('the json data error 4 !! [' + repr(msgpack) + ']') tableId = newMsgPack.getParam("tableId", 0) shadowRoomId = tableId / 10000 newMsgPack.setParam("roomId", shadowRoomId) return newMsgPack, shadowRoomId
def _remoteCall(markParams, argl, argd): srvtype = markParams['remoteServerType'] syncCall = markParams['remoteSyncCall'] remoteGroupByIndex = markParams['remoteGroupByIndex'] remoteGroupBy = markParams['remoteGroupBy'] cmd = markParams['remoteCmd'] action = markParams['remoteAction'] groupVal = argl[remoteGroupByIndex] msgpack = MsgPack() msgpack.setCmdAction(cmd, action) msgpack.setParam(remoteGroupBy, groupVal) msgpack.setParam('argl', argl[1:]) # 去掉self, cls msgpack.setParam('argd', argd) msgpack.setParam('clientId', configure.CLIENTID_RPC) msgpack = msgpack.pack() jstr = None if srvtype == gdata.SRV_TYPE_UTIL: jstr = _communicateServer(_utilServer, groupVal, groupVal, msgpack, 'RQ', syncCall) elif srvtype == gdata.SRV_TYPE_ROOM: assert (groupVal in gdata.roomIdDefineMap()) jstr = _communicateRoomServer(0, groupVal, msgpack, 'RQ', syncCall) elif srvtype == gdata.SRV_TYPE_TABLE: assert (groupVal in gdata.roomIdDefineMap()) jstr = _communicateTableServer(0, groupVal, msgpack, 'RQ', syncCall) elif srvtype == gdata.SRV_TYPE_CENTER: jstr = _communicateServer(_centerServer, groupVal, groupVal, msgpack, 'RQ', syncCall) else: raise Exception('ERROR RPC cmd route false !!' + cmd + '.' + action) ret = None if syncCall and jstr: mo = MsgPack() try: mo.unpack(jstr) except: raise Exception('the json data error 5 !! [' + repr(jstr) + ']') ret = mo.getKey('result') return ret
def doOldRemoteCallNew(userId, msgline): ''' 必须要返回dict的实例 ''' try: ftlog.debug('doOldRemoteCallNew->msgline=', msgline) msg = MsgPack() msg.unpack(msgline) action = msg.getAction() gameId = msg.getParam('gameId', -1) userId = msg.getParam('userId', -1) userBag = hallitem.itemSystem.loadUserAssets(userId).getUserBag() clientId = sessiondata.getClientId(userId) if pkgamedata.setnxGameAttr(userId, HALL_GAMEID, 'flag.item.trans', 1) == 1: hallitem._tranformItems(gameId, userId, clientId, userBag) mo = MsgPack() mo.setResult('action', action) if action == 'getUserItem': kindId = msg.getParam('itemId', -1) val = getUserItem(userId, gameId, kindId, userBag) mo.setResult('item', val) elif action == 'getUserItemList': result = getUserItemList(userId, gameId, userBag) mo.setResult('item', result) elif action == 'addUserItem': kindId = msg.getParam('itemId', -1) count = msg.getParam('count', -1) eventId = msg.getParam('eventId', -1) eventId = eventIdToString(eventId) ftlog.debug('addUserItem itemId=', kindId, 'count=', count, 'eventId=', eventId) val = addUserItem(userId, gameId, kindId, count, eventId, userBag) mo.setResult('item', val) elif action == 'addUserItems': pass elif action == 'consumeUserItem': kindId = msg.getParam('itemId', -1) count = msg.getParam('count', -1) eventId = msg.getParam('eventId', -1) eventId = eventIdToString(eventId) ftlog.debug('consumeUserItem itemId=', kindId, 'count=', count, 'eventId=', eventId) val = consumeUserItem(userId, gameId, kindId, count, eventId, userBag) mo.setResult('item', val) elif action == 'useUserItem': kindId = msg.getParam('itemId', -1) val = useUserItem(userId, gameId, kindId, userBag) mo.setResult('item', val) elif action == 'updateTimingUserItems': isDayFirst = msg.getParam('isDayFirst', False) val = updateTimingUserItems(userId, gameId, isDayFirst, userBag) mo.setResult('item', val) elif action == 'loadUserDecroation': val = loadUserDecroation(userId, gameId, userBag) mo.setResult('item', val) else: ftlog.error('doOldRemoteCallNew unknown rpc action action=', action, 'gameId=', gameId, 'userId=', userId) ftlog.debug('doOldRemoteCallNew->mo=', mo._ht) mo.setResult('code', 0) return mo._ht except TYItemException: mo = MsgPack() mo.setResult('code', -1) return mo._ht except: ftlog.error() mo = MsgPack() mo.setResult('code', -1) return mo._ht
def doServerTcpMsg(self): ''' 其他服务发送至CONN服务的消息处理 绝大部分需要转发至用户客户端 ''' args = ftsvr.getTaskRunArg() src = args.get('src') dst = args.get('dst') userheader1 = args.get('userheader1') userheader2 = args.get('userheader2') msgstr = ftsvr.getTaskPack() cmd = strutil.getJsonStr(msgstr, 'cmd', '') ftlog.debug('COS2AProto-doServerTcpMsg src=', src, 'dst=', dst, 'h1=', userheader1, 'h2=', userheader2, 'cmd=', cmd, 'pack=', msgstr) if not gdata.initializeOk(): ftlog.info( 'COS2AProto-doServerTcpMsg not initialize ok, ignore this message :', ftsvr.getTaskPack()) return if userheader1 == 'S0': # 发送给用户客户端的消息标记 toUserId = int(userheader2) if toUserId in _ONLINE_USERS: user = _ONLINE_USERS[toUserId] if cmd == 'user_info': # 链接建立后, 第一个返回给客户端的命令必须是user_info isFirst = 0 if user.firstUserInfo == 0: isFirst = 1 user.sendTcpMessage(msgstr) user.firstUserInfo = 1 # 强制进行第一次心跳处理, 发送led,返回比赛报名情况等 if isFirst: msg = '{"cmd":"heart_beat","params":{"userId":%d,"gameId":%d,"clientId":"%s","must":1}}' % ( toUserId, user.gameId, user.clientId) router.sendUtilServer(msg, toUserId) if len(user.delaySendMsg) > 0: mlist = user.delaySendMsg user.delaySendMsg = [] for m in mlist: user.sendTcpMessage(m) else: # 只有第一个命令user_info完成后, 后续的消息才会发送给客户端 if user.firstUserInfo == 1: user.sendTcpMessage(msgstr) else: if user.delaySendMsg != None and len( user.delaySendMsg) < 20: user.delaySendMsg.append(msgstr) else: ftlog.info('ERROR, the user tcp bind not ok !', toUserId, cmd) else: ftlog.info('ERROR, the user is already offline !', toUserId, cmd) else: # 当前进程需要处理的消息, 例如更新配置,热更新,强制用户退出等 msg = MsgPack() try: msg.unpack(msgstr) except: raise Exception('the json data error 6 !! [' + repr(msgstr) + ']') task = FTTasklet.getCurrentFTTasklet() task.pack = msg task.run_args['pack'] = msg runcmd.handlerCommand(msg)