def _handlerMsgCommand(markParams, msg): handler = markParams['handler'] fun_method = markParams['fun_method'] # ftlog.debug('_handlerMsgCommand->', handler, fun_method) errmsg, values = _checkCmdParams(handler, markParams['paramkeys']) if errmsg: ftlog.error('the command params error !! msg=', getMsgPack(), 'errmsg=', errmsg) if router.isQuery(): if errmsg: if not isinstance(errmsg, MsgPack): errmsg = newErrorMsgPack(2, errmsg) router.responseQurery(errmsg) try: filterErrorMessage(msg) except: ftlog.error("filterErrorMessage error. msg:", msg) else: try: response = fun_method(*values) if not response: response = newOkMsgPack(1) except Exception, e: ftlog.error('method->', fun_method, 'Exception !', ftsvr.getTaskRunArg()) response = newErrorMsgPack(1, str(e)) targs = ftsvr.getTaskRunArg() if not targs.get('responsed'): if targs.get('userheader1') == 'CQ': if not isinstance(response, MsgPack): response = newOkMsgPack(1) router.responseQurery(response)
def doSomeLogic(self): if not startup.IS_INIT_OK : print 'not init OK !!' try: self.finish() except: pass return try: taskarg = ftsvr.getTaskRunArg() heads = str(taskarg["userheader1"]) ftlog.debug('WriteTcpRequest 1->', taskarg, heads) _type, _group, rec_id = heads.split('_') global_conf = ftcon.getConf("freetime:global") data_dir = global_conf["data_path"] type_conf = ftcon.getConf("freetime:log_type") log_conf = type_conf[_type] rec_type = int(log_conf["rec_type"]) reserved_size = int(log_conf["reserved_size"]) log_size = int(log_conf["record_size"]) + reserved_size + self.EXTRA_SIZE record_count = log_conf["single_file_record_count"] body = base64.b64decode(taskarg["pack"]) ftlog.debug('WriteTcpRequest 2->', body) cur_time = int(time.time()) record = "tu" + struct.pack("<BIQ", rec_type, cur_time, int(rec_id)) + body + struct.pack("<%ds"%reserved_size, str()) + "yo" self.writeFile(data_dir, _type, _group, int(rec_id), record_count, log_size, record) except: ftlog.error() finally: try: self.finish() except: pass
def doSomeLogic(self): if not startup.IS_INIT_OK: print 'not init OK !!' try: self.finish() except: pass return try: taskarg = ftsvr.getTaskRunArg() heads = str(taskarg["userheader1"]) ftlog.debug('WriteTcpRequest 1->', taskarg, heads) _type, _group, rec_id = heads.split('_') global_conf = ftcon.getConf("freetime:global") data_dir = global_conf["data_path"] type_conf = ftcon.getConf("freetime:log_type") log_conf = type_conf[_type] rec_type = int(log_conf["rec_type"]) reserved_size = int(log_conf["reserved_size"]) log_size = int(log_conf["record_size"]) + reserved_size + self.EXTRA_SIZE record_count = log_conf["single_file_record_count"] body = base64.b64decode(taskarg["pack"]) ftlog.debug('WriteTcpRequest 2->', body) cur_time = int(time.time()) record = "tu" + struct.pack("<BIQ", rec_type, cur_time, int(rec_id)) + body + struct.pack( "<%ds" % reserved_size, str()) + "yo" self.writeFile(data_dir, _type, _group, int(rec_id), record_count, log_size, record) except: ftlog.error() finally: try: self.finish() except: pass
def handleRequest(self): if not startup.IS_INIT_OK: print 'not init OK !!' try: self.finish() except: pass return request = None try: taskarg = ftsvr.getTaskRunArg() request = taskarg['data'] heads = request.getAllHeaders() if not "log-type" in heads: return self._doHttpManager(request) _type = heads["log-type"] _group = heads["log-group"] body = request.content.read() pbody = self.parseBody(body, _type) if pbody: logid = ftsvr.doRedis(_group, "INCR", "global:logid:%s:%s" % (_group, _type)) _date = time.strftime("%Y%m%d", time.localtime(int(time.time()))) ftsvr.doRedis(_group, "HSETNX", "day1st:%s:%s" % (_group, _type), _date, logid) self.updateIndex(_type, _group, logid, pbody) self.sendToWriter(_type, _group, logid, body) except: ftlog.error() finally: try: request.finish() except: pass
def handleRequest(self): taskarg = ftsvr.getTaskRunArg() request = taskarg['data'] if not gdata.initializeOk(): ftlog.info('TYCommonHttpRequest not initialize ok, ignore this request :', request.path) request.setResponseCode(503) request.finish() return runhttp.handlerHttpRequest(request)
def doSomeLogic(self): ftlog.debug(ftsvr.getId(), "receive from agent", ftsvr.getTaskPack()) taskarg = ftsvr.getTaskRunArg() print taskarg #这里简单调用response即可,不需要指定dst #框架会使用保存在tasklet的arg字典里的信息 #自动设置回应包的src和dst ftagent.response("RESPONSE-TCP-QUERY")
def handleRequest(self): taskarg = ftsvr.getTaskRunArg() request = taskarg['data'] if not gdata.initializeOk(): ftlog.info( 'TYCommonHttpRequest not initialize ok, ignore this request :', request.path) request.setResponseCode(503) request.finish() return runhttp.handlerHttpRequest(request)
def doSomeLogic2(self): ftlog.info("MyProto do some logic in tasklet") #test redis aio ftsvr.doRedis("user01", "SET", "zx", 1) #Testing doHttp... #code, page = ftsvr.doHttp("GET", "http://www.google.com", # {'User-Agent': ['Twisted']}) #test connect timeout #code, page = ftsvr.doHttp("GET", "http://news.sina.com.cn/", # {'User-Agent': ['Twisted']}, '', 15, 0.1) #test request timeout #page = ftsvr.doHttp("GET", "http://news.sina.com.cn/sssss", # {'User-Agent': ['Twisted']}) #test 404 code, page = ftsvr.doHttp("POST", "http://127.0.0.1:8003/", {'User-Agent': ['Twisted']}, "HAHAHA") print code, page print "HTTP", code, len(page), page log_data = struct.pack("64s", "HAHAHA") ftlog.sendHttpLog("user01", "chip", log_data) #pipeline需要加锁,否则结果可能混乱,框架中已经加了lock,参见ftsvr代码 count = 1000 pipecount = 100 st = time.time() for x in xrange(count / pipecount): if x % 100 == 0: ftlog.info(x) pl = ftsvr.getRedisPipe("user01") for y in xrange(pipecount): pl.set("zx", 1) result = ftsvr.doRedisPipe(pl) if len(result) != pipecount: ftlog.debug("ERRORPIPELINE") ftlog.info("SetPerS:", count / (time.time() - st)) #test ftagent and udpquery ftlog.debug(ftcon.getConf("server"), caller=self) ftlog.debug(ftsvr.getTaskRunArg()) ftlog.debug(ftsvr.doUdpQuery("LO01", "abcdefgh")) ftlog.debug(ftagent.query("GA01", "TCPTCPSENDBYAGENT")) #test cron _tstr = time.strftime('%Y%m%d', time.localtime()) cron2 = FTCron( '{"times_in_day":{"first":"15:00","interval":5, "count":32},\ "days":{"first":"%s", "interval":"2m", "count":10}}' % (_tstr)) ftlog.debug(cron2.getTimeList(), cron2.getDaysList()) ftlog.debug("SEC=", cron2.getTodayNextLater()) #test timer tr = FTTimer(3, self.timerFunc, 1, 2, 3, aaa=111)
def _getRpcId(): targs = ftsvr.getTaskRunArg() rpcid = targs.get('rpcid', None) if not rpcid: rpcid = targs.get('userheader1', None) if rpcid and rpcid.find('RPC.') == 0: targs['rpcid'] = rpcid else: global _RPC_ID_COUNT_ _RPC_ID_COUNT_ += 1 rpcid = 'RPC.' + gdata.serverId() + '.' + str(_RPC_ID_COUNT_) targs['rpcid'] = rpcid return rpcid
def doSomeLogic(self): args = ftsvr.getTaskRunArg() src = args.get('src') dst = args.get('dst') userheader1 = args.get('userheader1') userheader2 = args.get('userheader2') msg = ftsvr.getTaskPack() # ftlog.debug('TYCommonS2AProto id=', id(self), 'src=', src, 'dst=', dst, 'h1=', userheader1, 'h2=', userheader2, 'pack=', msg) if not gdata.initializeOk(): ftlog.info('TYCommonS2AProto not initialize ok, ignore this message :', src, dst, userheader1, userheader2, msg) return runcmd.handlerCommand(msg)
def doSomeLogic(self): args = ftsvr.getTaskRunArg() src = args.get('src') dst = args.get('dst') userheader1 = args.get('userheader1') userheader2 = args.get('userheader2') msg = ftsvr.getTaskPack() # ftlog.debug('TYCommonS2AProto id=', id(self), 'src=', src, 'dst=', dst, 'h1=', userheader1, 'h2=', userheader2, 'pack=', msg) if not gdata.initializeOk(): ftlog.info( 'TYCommonS2AProto not initialize ok, ignore this message :', src, dst, userheader1, userheader2, msg) return runcmd.handlerCommand(msg)
def _handlerRpcCommand(msg): ''' 处理接收到的一个远程RPC调用 ''' t1 = time() if performance.PERFORMANCE_NET: netkey = msg.getKey(performance.NET_KEY) if netkey: netkey.append(gdata.serverId()) netkey.append('DO') netkey.append(float('%0.4f' % t1)) try: rpc = msg.getKey('rpc') argl = msg.getKey('argl') argd = msg.getKey('argd') markParams = _runenv._rpc_methods.get(rpc, None) ret = _invokeMethodLocked(markParams, argl, argd) if markParams['syncCall']: if performance.PERFORMANCE_NET: netkey = msg.getKey(performance.NET_KEY) if not netkey: netkey = [] mo = strutil.dumps({'ret': ret, performance.NET_KEY: netkey}) else: mo = strutil.dumps({'ret': ret}) router.responseQurery(mo) except Exception, e: ftlog.error('_handlerRpcCommand msg=', msg) try: if router.isQuery(): targs = ftsvr.getTaskRunArg() if not targs.get('responsed'): if performance.PERFORMANCE_NET: netkey = msg.getKey(performance.NET_KEY) if not netkey: netkey = [] mo = strutil.dumps({'ex': '_handlerRpcCommand Exception : ' + str(e), performance.NET_KEY: netkey}) else: mo = strutil.dumps({'ex': '_handlerRpcCommand Exception : ' + str(e)}) router.responseQurery(mo) except: ftlog.error()
def handleRequest(self): taskarg = ftsvr.getTaskRunArg() request = taskarg['data'] # render path request.write('<html>') request.write('<div>Host=%s</div>' % self.host) request.write('<div>Path=%s</div>' % request.path) request.write('<div>Args=%s</div>' % request.args) request.write('<div>Method=%s</div>' % request.method) request.write('<div>Client=%s</div>' % request.getClientIP()) request.write('<div>Body=%s</div>' % request.content.read()) # render headers headers = [] for k, v in request.getAllHeaders().items(): headers.append('<li>%s=%s</li>' % (k, v)) request.write('<h1>Header:</h1>') request.write('<div>%s</div>' % '\n'.join(headers)) request.write('</html>') request.finish()
try: if router.isQuery(): targs = ftsvr.getTaskRunArg() if not targs.get('responsed'): if performance.PERFORMANCE_NET: netkey = msg.getKey(performance.NET_KEY) if not netkey: netkey = [] mo = strutil.dumps({'ex': '_handlerRpcCommand Exception : ' + str(e), performance.NET_KEY: netkey}) else: mo = strutil.dumps({'ex': '_handlerRpcCommand Exception : ' + str(e)}) router.responseQurery(mo) except: ftlog.error() time_recv = ftsvr.getTaskRunArg().get('time_recv', 0) if time_recv and time_recv > 0: t = t1 - time_recv if t > 0.2: ftlog.warn('RPC RESPONSE SLOW !! TIME=%0.4f' % (t), 'RECVTIME=%0.4f' % time_recv, msg) def _getRpcDstServerId(rsrv, groupId): if groupId <= 0: if rsrv.srvType == gdata.serverType(): return gdata.serverId() groupId = rsrv.sididx rsrv.sididx += 1 dst = rsrv.sids[groupId % rsrv.sidlen] return dst
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 timerFunc(self): ftlog.debug(ftsvr.getTaskRunArg())
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)
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)