Example #1
0
def handlerHttpRequest(httprequest):
    if InitFlg.isInitOk != 1:
        mo = MsgPack()
        mo.setError(1, 'http system not startup')
        mo = __stringifyResponse(0, mo)
        doFinish(mo, CONTENT_TYPE_HTML)
        return

    mo = None
    try:
        session = stackless.getcurrent()._fttask.session
        session['ishttp'] = 1

        rpath = httprequest.path
        if TRACE_RESPONSE:
            tylog.info('HTTPREQUEST', rpath, httprequest.args)

        # 当前服务处理
        funhttp, entry = __http_path_methods.get(rpath, (None, None))
        if funhttp == None or entry == None:
            __handlerHttpStatic(httprequest)
            return  # 查找静态资源返回

        # IP 地址过滤
        if entry['need_ip_filter']:
            ip = getClientIp()
            mo = entry['ip_filter'](ip)
            if mo:
                mo = __stringifyResponse(entry['jsonp'], mo)
                doFinish(mo, entry['content_type'])
                return  # IP 过滤失败, 返回

        # 执行动态调用
        try:
            mo = funhttp()  # 最新版本的 @http_request_entry 方法
            if mo == None:
                mo = MsgPack()
                mo.setCmd(rpath)
                mo.setError(1, 'http api return None')
        except:
            tylog.error()
            mo = MsgPack()
            mo.setCmd(rpath)
            mo.setError(1, 'http api exception')

        mo = __stringifyResponse(entry['jsonp'], mo)
        doFinish(mo, entry['content_type'], rpath)
    except:
        tylog.error()
        mo = MsgPack()
        mo.setCmd(rpath)
        mo.setError(1, 'system exception return')
        mo = __stringifyResponse(0, mo)
        doFinish(mo, CONTENT_TYPE_HTML)
Example #2
0
def handlerHttpRequest(httprequest):
    if InitFlg.isInitOk != 1:
        mo = MsgPack()
        mo.setError(1, 'http system not startup')
        mo = __stringifyResponse(0, mo)
        doFinish(mo, CONTENT_TYPE_HTML)
        return

    mo = None
    try:
        session = stackless.getcurrent()._fttask.session
        session['ishttp'] = 1

        rpath = httprequest.path
        if TRACE_RESPONSE :
            tylog.info('HTTPREQUEST', rpath, httprequest.args)
        
        # 当前服务处理
        funhttp, entry = __http_path_methods.get(rpath, (None, None))
        if funhttp == None or entry == None:
            __handlerHttpStatic(httprequest)
            return  # 查找静态资源返回

        # IP 地址过滤
        if entry['need_ip_filter'] :
            ip = getClientIp()
            mo = entry['ip_filter'](ip)
            if mo :
                mo = __stringifyResponse(entry['jsonp'], mo)
                doFinish(mo, entry['content_type'])
                return  # IP 过滤失败, 返回

        # 执行动态调用
        try:
            mo = funhttp()  # 最新版本的 @http_request_entry 方法
            if mo == None:
                mo = MsgPack()
                mo.setCmd(rpath)
                mo.setError(1, 'http api return None')
        except:
            tylog.error()
            mo = MsgPack()
            mo.setCmd(rpath)
            mo.setError(1, 'http api exception')

        mo = __stringifyResponse(entry['jsonp'], mo)
        doFinish(mo, entry['content_type'], rpath)
    except:
        tylog.error()
        mo = MsgPack()
        mo.setCmd(rpath)
        mo.setError(1, 'system exception return')
        mo = __stringifyResponse(0, mo)
        doFinish(mo, CONTENT_TYPE_HTML)
Example #3
0
def handlerWsRequest(wsprotocol):
    if InitFlg.isInitOk != 1:
        mo = MsgPack()
        mo.setError(1, 'ws system not startup')
        mo = __stringifyResponse(0, mo)
        wsprotocol.sendData(mo)
        wsprotocol.wsClose()
        return

    mo = None
    try:
        tasklet = stackless.getcurrent()._fttask
        session = tasklet.session
        msg = tasklet.run_args['pack']
        session['isws'] = 1
        
        wsgid = msg.getParamInt()
        wscmd = msg.getCmd()
        wsact = msg.getParamAct()

        rpath = '%s-%s-%s' % (str(wsgid), str(wscmd), str(wsact))
        if TRACE_RESPONSE :
            tylog.info('WSREQUEST', rpath, msg)

        # 当前服务处理
        
        funws, entry = None, None
        gamecmds = __ws_commands.get(wsgid, None)
        if gamecmds != None :
            wscmds = gamecmds.get(wscmd, None)
            if wscmds != None :
                funws, entry = wscmds.get(wsact, (None, None))

        if funws == None or entry == None:
            mo = MsgPack()
            mo.setResult('gameId', wsgid)
            mo.setResultActCmd(wsact, wscmd)
            mo.setError(1, 'the command not inplement !')
            mo = __stringifyResponse(entry['jsonp'], mo)
            wsprotocol.sendData(mo)
            return  # 未找到对应的调用失败, 返回

        # IP 地址过滤
        if entry['need_ip_filter'] :
            ip = getClientIp()
            mo = entry['ip_filter'](ip)
            if mo :
                mo = __stringifyResponse(entry['jsonp'], mo)
                wsprotocol.sendData(mo)
                wsprotocol.wsClose()
                return  # IP 过滤失败, 返回

        # 执行动态调用
        try:
            mo = funws()
            if mo == None:
                mo = MsgPack()
                mo.setResult('gameId', wsgid)
                mo.setResultActCmd(wsact, wscmd)
                mo.setError(1, 'ws api return None')
        except:
            tylog.error()
            mo = MsgPack()
            mo.setResult('gameId', wsgid)
            mo.setResultActCmd(wsact, wscmd)
            mo.setError(1, 'ws api exception')

        mo = __stringifyResponse(entry['jsonp'], mo)
        wsprotocol.sendData(mo)
    except:
        tylog.error()
        mo = MsgPack()
        mo.setError(1, 'system exception return')
        mo = __stringifyResponse(0, mo)
        wsprotocol.sendData(mo)
Example #4
0
def handlerWsRequest(wsprotocol):
    if InitFlg.isInitOk != 1:
        mo = MsgPack()
        mo.setError(1, 'ws system not startup')
        mo = __stringifyResponse(0, mo)
        wsprotocol.sendData(mo)
        wsprotocol.wsClose()
        return

    mo = None
    try:
        tasklet = stackless.getcurrent()._fttask
        session = tasklet.session
        msg = tasklet.run_args['pack']
        session['isws'] = 1

        wsgid = msg.getParamInt()
        wscmd = msg.getCmd()
        wsact = msg.getParamAct()

        rpath = '%s-%s-%s' % (str(wsgid), str(wscmd), str(wsact))
        if TRACE_RESPONSE:
            tylog.info('WSREQUEST', rpath, msg)

        # 当前服务处理

        funws, entry = None, None
        gamecmds = __ws_commands.get(wsgid, None)
        if gamecmds != None:
            wscmds = gamecmds.get(wscmd, None)
            if wscmds != None:
                funws, entry = wscmds.get(wsact, (None, None))

        if funws == None or entry == None:
            mo = MsgPack()
            mo.setResult('gameId', wsgid)
            mo.setResultActCmd(wsact, wscmd)
            mo.setError(1, 'the command not inplement !')
            mo = __stringifyResponse(entry['jsonp'], mo)
            wsprotocol.sendData(mo)
            return  # 未找到对应的调用失败, 返回

        # IP 地址过滤
        if entry['need_ip_filter']:
            ip = getClientIp()
            mo = entry['ip_filter'](ip)
            if mo:
                mo = __stringifyResponse(entry['jsonp'], mo)
                wsprotocol.sendData(mo)
                wsprotocol.wsClose()
                return  # IP 过滤失败, 返回

        # 执行动态调用
        try:
            mo = funws()
            if mo == None:
                mo = MsgPack()
                mo.setResult('gameId', wsgid)
                mo.setResultActCmd(wsact, wscmd)
                mo.setError(1, 'ws api return None')
        except:
            tylog.error()
            mo = MsgPack()
            mo.setResult('gameId', wsgid)
            mo.setResultActCmd(wsact, wscmd)
            mo.setError(1, 'ws api exception')

        mo = __stringifyResponse(entry['jsonp'], mo)
        wsprotocol.sendData(mo)
    except:
        tylog.error()
        mo = MsgPack()
        mo.setError(1, 'system exception return')
        mo = __stringifyResponse(0, mo)
        wsprotocol.sendData(mo)