def _lockResource(lockName, lockval, relockKey): # TODO 处理重入锁定 lockkey = 'lock:' + lockName + ':' + str(lockval) ftlock = _FTLOCKS.get(lockkey, None) if ftlock == None: ftlock = FTLock(lockkey, relockKey) _FTLOCKS[lockkey] = ftlock ftlog.debug('lock resource of', lockkey, 'wait !!', relockKey) ftlock.lock(relockKey) ftlog.debug('lock resource of', lockkey, 'locked !!', relockKey) return ftlock
def handlerCommand(msg): """ TCP消息命令处理总入口 """ orgcmd = msg.getCmd() if orgcmd == _runenv._CMD_RPC_: return rpccore._handlerRpcCommand(msg) orgaction = msg.getParam('action') gameId = msg.getParam('gameId') cmd, action = None, None try: if orgaction == None: orgaction = '' # ftlog.debug('handlerCommand->cmd, action->', cmd, action) cmdpath, cmd, action = oldcmd.convertCmdPath(orgcmd, orgaction, msg) # ftlog.debug('handlerCommand->cmd, action, cmdpath, ver, gameId ->', cmd, action, cmdpath, ver, gameId) # 先以cmd#action#gameId去查找命令处理器 vercalls = None if gameId: vercalls = _runenv._cmd_path_methods.get(cmdpath + '#' + str(gameId), None) # 再以cmd#action去查找命令处理器 if not vercalls: vercalls = _runenv._cmd_path_methods.get(cmdpath, None) # 再以cmd#*去查找名利处理器 if not vercalls: vercalls = _runenv._cmd_path_methods.get(str(cmd) + '#*#' + str(gameId), None) # 再以cmd#*去查找名利处理器 if not vercalls: vercalls = _runenv._cmd_path_methods.get(str(cmd) + '#*', None) # ftlog.debug('handlerCommand->vercalls->', vercalls) # 再经过clietnId的版本号过滤 markParams = None if vercalls: if len(vercalls) > 1: ver = getClientIdVer(msg) for vercall in vercalls: if ver >= vercall[0]: markParams = vercall[1] break else: markParams = vercalls[0][1] else: vercalls = [] # 若未找到对应的mark定义, 错误返回 if not markParams: raise Exception('the cmd path method not found ! cmdpath=' + str(cmdpath) + ' clientIdVer=' + str(getClientIdVer(msg)) + ' vercalls len=' + str(len(vercalls)) + ' gameId=' + str(gameId) + ' msg=' + str(msg)) # 锁定当前mark定义的资源 lockParamName = markParams.get('lockParamName', None) if lockParamName: lockval = msg.getParam(lockParamName) if lockParamName == 'userId' or lockParamName == 'tableId': assert (isinstance(lockval, int)) lockkey = 'lock:' + lockParamName + ':' + str(lockval) ftlock = _runenv._FTLOCKS.get(lockkey, None) if ftlock == None: ftlock = FTLock(lockkey) _runenv._FTLOCKS[lockkey] = ftlock ftlog.debug('lock resource of', lockkey, 'wait !!') ftlock.lock() ftlog.debug('lock resource of', lockkey, 'locked !!') try: if markParams['isRpc']: # RPC CALL _handlerRpcCommand(markParams, msg) else: # CLIENT MSG CALL _handlerMsgCommand(markParams, msg) finally: # 释放当前mark定义的资源 if lockParamName: ftlog.debug('lock resource of', lockkey, 'released !!') if ftlock.unlock() == 0: del _runenv._FTLOCKS[lockkey] pass except Exception, e: ftlog.error('cmd=' + str(cmd) + ' action=' + str(action) + ' gameId=' + str(gameId), 'orgcmdpath=' + str(orgcmd) + '#' + str(orgaction)) if router.isQuery(): targs = ftsvr.getTaskRunArg() if not targs.get('responsed') and targs.get('userheader1') == 'CQ': response = newErrorMsgPack(1, str(e)) router.responseQurery(response)
def handlerCommand(msg): """ TCP消息命令处理总入口 """ orgcmd = msg.getCmd() if orgcmd == _runenv._CMD_RPC_: return rpccore._handlerRpcCommand(msg) orgaction = msg.getParam('action') gameId = msg.getParam('gameId') cmd, action = None, None try: if orgaction == None: orgaction = '' # ftlog.debug('handlerCommand->cmd, action->', cmd, action) cmdpath, cmd, action = oldcmd.convertCmdPath(orgcmd, orgaction, msg) # ftlog.debug('handlerCommand->cmd, action, cmdpath, ver, gameId ->', cmd, action, cmdpath, ver, gameId) # 先以cmd#action#gameId去查找命令处理器 vercalls = None if gameId: vercalls = _runenv._cmd_path_methods.get( cmdpath + '#' + str(gameId), None) # 再以cmd#action去查找命令处理器 if not vercalls: vercalls = _runenv._cmd_path_methods.get(cmdpath, None) # 再以cmd#*去查找名利处理器 if not vercalls: vercalls = _runenv._cmd_path_methods.get( str(cmd) + '#*#' + str(gameId), None) # 再以cmd#*去查找名利处理器 if not vercalls: vercalls = _runenv._cmd_path_methods.get(str(cmd) + '#*', None) # ftlog.debug('handlerCommand->vercalls->', vercalls) # 再经过clietnId的版本号过滤 markParams = None if vercalls: if len(vercalls) > 1: ver = getClientIdVer(msg) for vercall in vercalls: if ver >= vercall[0]: markParams = vercall[1] break else: markParams = vercalls[0][1] else: vercalls = [] # 若未找到对应的mark定义, 错误返回 if not markParams: raise Exception('the cmd path method not found ! cmdpath=' + str(cmdpath) + ' clientIdVer=' + str(getClientIdVer(msg)) + ' vercalls len=' + str(len(vercalls)) + ' gameId=' + str(gameId) + ' msg=' + str(msg)) # 锁定当前mark定义的资源 lockParamName = markParams.get('lockParamName', None) if lockParamName: lockval = msg.getParam(lockParamName) if lockParamName == 'userId' or lockParamName == 'tableId': assert (isinstance(lockval, int)) lockkey = 'lock:' + lockParamName + ':' + str(lockval) ftlock = _runenv._FTLOCKS.get(lockkey, None) if ftlock == None: ftlock = FTLock(lockkey) _runenv._FTLOCKS[lockkey] = ftlock ftlog.debug('lock resource of', lockkey, 'wait !!') ftlock.lock() ftlog.debug('lock resource of', lockkey, 'locked !!') try: if markParams['isRpc']: # RPC CALL _handlerRpcCommand(markParams, msg) else: # CLIENT MSG CALL _handlerMsgCommand(markParams, msg) finally: # 释放当前mark定义的资源 if lockParamName: ftlog.debug('lock resource of', lockkey, 'released !!') if ftlock.unlock() == 0: del _runenv._FTLOCKS[lockkey] pass except Exception, e: ftlog.error( 'cmd=' + str(cmd) + ' action=' + str(action) + ' gameId=' + str(gameId), 'orgcmdpath=' + str(orgcmd) + '#' + str(orgaction)) if router.isQuery(): targs = ftsvr.getTaskRunArg() if not targs.get('responsed') and targs.get('userheader1') == 'CQ': response = newErrorMsgPack(1, str(e)) router.responseQurery(response)