示例#1
0
 def send_to_level2(self, request):
     """
     请求level2服务
     :param request: pb格式请求
     """
     g_log.debug("%s", request)
     environ = get_level2_environ()
     message = package.serial_pb(request)
     request = async_udp.UDPRequest(environ[0], environ[1], message)
     udp_client = async_udp.AsyncUDPClient()
     udp_client.fetch(request=request, callback=self.on_response)
     g_log.debug("send request to level2")
示例#2
0
def send_to_branch(name, request):
    """
    组pb包,异步发送给旁路,switch到loop,当loop收到旁路回包后,switch到当前协程
    :param name: 旁路名称
    :param request: 发送到旁路的pb格式请求包
    :param timeout: 旁路超时时间,缺省没有超时
    :return: 错误/0,timeout/超时,旁路返回包/正确
    """
    try:
        # 请求包补上协程UUID
        uuid = coroutine_manage.generate_coroutine_uuid()
        if uuid == 0:
            return 0
        # g_log.debug("generate coroutine uuid %s", uuid)
        request.head.coroutine_uuid = uuid

        # 组包
        request_serial = package.serial_pb(request)
        branch = BranchSocket()
        branch_sock = branch.branch_sock.get(name)
        if branch_sock:
            timeout = branch_sock.get("timeout", None)
            g_log.debug("branch %s timeout %sms", name, timeout)
            timeout = float(timeout) / 1000

        result = 0
        if branch.send_to_branch(name, request_serial):
            result = coroutine_manage.coroutine_yield(uuid, timeout)
            if result == 0:
                pass
            elif result == "timeout":
                pass
            else:
                g_log.debug("return from branch %s", name)
        coroutine_manage.drop_coroutine_event(uuid)
        return result
    except Exception as e:
        g_log.error("%s", e)
        if uuid in dir():
            coroutine_manage.drop_coroutine_event(uuid)
        return 0
示例#3
0
def pack_send_receive(req):
    address = ('127.0.0.1', 9527)
    request = package.serial_pb(req)

    # 发包
    sock = socket.socket(type=socket.SOCK_DGRAM)
    sock.connect(address)
    g_log.debug("Sending %s bytes to %s", len(request), address)
    g_log.debug("%s", req)
    send_to_sock(sock, request)

    # 收包
    result = receive_from_sock(sock)
    if result == 0:
        return 0
    response, _ = result
    g_log.debug('receive %s bytes from %s', len(response), _)

    res = common_pb2.Response()
    res.ParseFromString(response[6:-2])
    g_log.debug('%s', res)
示例#4
0
    def enter(self, message):
        """
        主线处理入口,启动定时器,解析请求,返回业务逻辑处理结果
        :param message: 请求包
        :return: 0/不回包,pb/超时、程序异常、正常返回
        """
        timeout = gevent.Timeout(self.expire)
        timeout.start()
        try:
            g_log.debug("%s: begin service logic ...", gevent.getcurrent())
            request = common_pb2.Request()
            request.ParseFromString(message[6:-2])
            g_log.debug("receive request %s", request)
            head = request.head

            if head.cmd < 100:
                # 注册登录模块
                response = account.enter(request)
            elif head.cmd < 200:
                # 用户资料模块
                response = consumer.enter(request)
            elif head.cmd < 300:
                # 商户资料模块
                response = merchant.enter(request)
            elif head.cmd < 400:
                # 积分模块
                response = credit.enter(request)
            elif head.cmd < 500:
                # 商家经营参数
                response = business.enter(request)
            elif head.cmd < 600:
                # 商家积分流动模块
                response = flow.enter(request)
            elif head.cmd < 700:
                # 七牛云存储
                response = qiniu_token.enter(request)
            elif head.cmd < 800:
                # 活动
                response = activity.enter(request)
            elif head.cmd < 900:
                response = voucher.enter(request)
            elif head.cmd < 1000:
                response = maintain.enter(request)
            else:
                # 非法请求,无效命令,不回包
                # return 0
                response = 0
            if response == "timeout":
                response = package.timeout_response(head.cmd, head.seq)
        except Timeout as e:
            # 超时,关闭定时器
            # timeout.cancel()
            g_log.debug("%s", e)
            g_log.debug("deal request timeout")
            response = package.timeout_response(head.cmd, head.seq)
            # message = package.serial_pb(response)
        except Exception as e:
            g_log.error("%s", e)
            response = package.exception_response(head.cmd, head.seq)
            # message = package.serial_pb(response)
        g_log.debug("response %s", response)

        try:
            if response == 0:
                return 0
            else:
                message = package.serial_pb(response)
                g_log.debug("%d", len(message))
                return message
        finally:
            g_log.debug("%s: end service logic ...", gevent.getcurrent())
            timeout.cancel()