def cancelOrder(orderId=None, originOrderId=None, reqId=None, sync=False):
        if not reqId:
            reqId = str(uuid.uuid4())
        operatorId = Config.operatorId
        sourceNodeId = Config.nodeId

        commonReq = CommonReqField()
        commonReq.sourceNodeId = sourceNodeId
        commonReq.targetNodeId = 0
        commonReq.operatorId = operatorId
        commonReq.reqId = reqId

        cancelOrderReq = CancelOrderReqField()
        if not orderId and not originOrderId:
            logger.error("定单ID和原始定单ID不可同时为空")
            return False
        if originOrderId:
            cancelOrderReq.originOrderId = originOrderId
        if orderId:
            cancelOrderReq.orderId = orderId

        rpcCancelOrderReq = RpcCancelOrderReq()
        rpcCancelOrderReq.commonReq.CopyFrom(commonReq)
        rpcCancelOrderReq.cancelOrderReq.CopyFrom(cancelOrderReq)

        if sync:
            RpcClientRspHandler.registerWaitReqId(reqId)

        sendResult = RpcClientProcessService.sendRoutineCoreRpc(
            0, rpcCancelOrderReq.SerializeToString(), reqId,
            RpcId.CANCEL_ORDER_REQ)

        if sync and not sendResult:
            RpcClientRspHandler.unregisterWaitReqId(reqId)
            return False

        if sync:
            startTime = time.time()
            while True:
                if time.time() - startTime < Config.rpcTimeOut:
                    rpcCancelOrderRsp = RpcClientRspHandler.getAndRemoveRpcCancelOrderRsp(
                        reqId)
                    if not rpcCancelOrderRsp:
                        rpcExceptionRsp = RpcClientRspHandler.getAndRemoveRpcExceptionRsp(
                            reqId)
                        if rpcExceptionRsp:
                            logger.error("撤销定单错误,请求ID: %s, 远程错误回报 %s", reqId,
                                         rpcExceptionRsp.info)
                            return False
                        time.sleep(0.02)
                    else:
                        commonRsp = rpcCancelOrderRsp.commonRsp
                        requestStatus = commonRsp.requestStatus
                        if requestStatus == CommonStatusEnum.SUCCESS:
                            return True
                        elif requestStatus == CommonStatusEnum.INFO:
                            logger.info("撤销定单错误,请求ID:%s,远程信息回报:%s", reqId,
                                        commonRsp.info)
                            return True
                        elif requestStatus == CommonStatusEnum.WARN:
                            logger.warning("撤销定单错误,请求ID:%s,远程警告回报:%s", reqId,
                                           commonRsp.info)
                            return True
                        elif requestStatus == CommonStatusEnum.ERROR:
                            logger.error("撤销定单错误,请求ID:%s,远程错误回报:%s", reqId,
                                         commonRsp.info)
                            return False
                        else:
                            logger.error("撤销定单错误,请求ID:%s,未知的请求状态", reqId)
                            return False
                else:
                    RpcClientRspHandler.unregisterWaitReqId(reqId)
                    logger.error("撤销定单错误,请求ID: %s,等待回报超时", reqId)
                    return False
    def subscribe(contract, gatewayId=None, reqId=None, sync=False):
        if not reqId:
            reqId = str(uuid.uuid4())
        operatorId = Config.operatorId
        sourceNodeId = Config.nodeId

        commonReq = CommonReqField()
        commonReq.sourceNodeId = sourceNodeId
        commonReq.targetNodeId = 0
        commonReq.operatorId = operatorId
        commonReq.reqId = reqId

        rpcSubscribeReq = RpcSubscribeReq()

        rpcSubscribeReq.commonReq.CopyFrom(commonReq)
        rpcSubscribeReq.contract.CopyFrom(contract)

        if gatewayId:
            rpcSubscribeReq.gatewayId = gatewayId

        if sync:
            RpcClientRspHandler.registerWaitReqId(reqId)

        sendResult = RpcClientProcessService.sendRoutineCoreRpc(
            0, rpcSubscribeReq.SerializeToString(), reqId, RpcId.SUBSCRIBE_REQ)

        if sync and not sendResult:
            RpcClientRspHandler.unregisterWaitReqId(reqId)
            return False

        if sync:
            startTime = time.time()
            while True:
                if time.time() - startTime < Config.rpcTimeOut:
                    rpcSubscribeRsp = RpcClientRspHandler.getAndRemoveRpcSubscribeRsp(
                        reqId)
                    if not rpcSubscribeRsp:
                        rpcExceptionRsp = RpcClientRspHandler.getAndRemoveRpcExceptionRsp(
                            reqId)
                        if rpcExceptionRsp:
                            logger.error("订阅错误,请求ID: %s, 远程错误回报 %s", reqId,
                                         rpcExceptionRsp.info)
                            return False
                        time.sleep(0.02)
                    else:
                        commonRsp = rpcSubscribeRsp.commonRsp
                        requestStatus = commonRsp.requestStatus
                        if requestStatus == CommonStatusEnum.SUCCESS:
                            return True
                        elif requestStatus == CommonStatusEnum.INFO:
                            logger.info("订阅错误,请求ID:%s,远程信息回报:%s", reqId,
                                        commonRsp.info)
                            return True
                        elif requestStatus == CommonStatusEnum.WARN:
                            logger.warning("订阅错误,请求ID:%s,远程警告回报:%s", reqId,
                                           commonRsp.info)
                            return True
                        elif requestStatus == CommonStatusEnum.ERROR:
                            logger.error("订阅错误,请求ID:%s,远程错误回报:%s", reqId,
                                         commonRsp.info)
                            return False
                        else:
                            logger.error("订阅错误,请求ID:%s,未知的请求状态", reqId)
                            return False
                else:
                    RpcClientRspHandler.unregisterWaitReqId(reqId)
                    logger.error("订阅错误,请求ID: %s,等待回报超时", reqId)
                    return False
    def unsubscribe(contract, reqId=None, sync=False):
        if not reqId:
            reqId = str(uuid.uuid4())
        operatorId = RtConfig.operatorId
        sourceNodeId = RtConfig.nodeId

        commonReq = CommonReqField()
        commonReq.sourceNodeId = sourceNodeId
        commonReq.targetNodeId = 0
        commonReq.operatorId = operatorId
        commonReq.reqId = reqId

        rpcUnsubscribeReq = RpcUnsubscribeReq()

        rpcUnsubscribeReq.commonReq.CopyFrom(commonReq)
        rpcUnsubscribeReq.contract.CopyFrom(contract)

        if sync:
            RpcClientRspHandler.registerWaitReqId(reqId)

        sendResult = RpcClientProcessService.sendCoreRpc(
            0, rpcUnsubscribeReq.SerializeToString(), reqId,
            RpcId.UNSUBSCRIBE_REQ)

        if sync and not sendResult:
            RpcClientRspHandler.unregisterWaitReqId(reqId)
            return False

        if sync:
            startTime = time.time()
            while True:
                if time.time() - startTime < RtConfig.rpcTimeOut:
                    rpcUnsubscribeRsp = RpcClientRspHandler.getAndRemoveRpcUnsubscribeRsp(
                        reqId)
                    if not rpcUnsubscribeRsp:
                        rpcExceptionRsp = RpcClientRspHandler.getAndRemoveRpcExceptionRsp(
                            reqId)
                        if rpcExceptionRsp:
                            logger.error("取消订阅错误,请求ID: %s, 远程错误回报 %s", reqId,
                                         rpcExceptionRsp.info)
                            return False
                        time.sleep(0.02)
                    else:
                        commonRsp = rpcUnsubscribeRsp.commonRsp
                        errorId = commonRsp.errorId
                        if errorId == 0:
                            if contract.unifiedSymbol + "@" + str(
                                    contract.gatewayId
                            ) in RpcClientApiService.subscribedContractDict.keys(
                            ):
                                del RpcClientApiService.subscribedContractDict[
                                    contract.unifiedSymbol + "@" +
                                    str(contract.gatewayId)]
                            return True
                        else:
                            logger.error("取消订阅错误,请求ID:%s,错误ID:%s,远程错误回报:%s",
                                         reqId, errorId, commonRsp.errorMsg)
                            return False
                else:
                    RpcClientRspHandler.unregisterWaitReqId(reqId)
                    logger.error("取消订阅错误,请求ID: %s,等待回报超时", reqId)
                    return False
Esempio n. 4
0
    def doCoreRpc(sourceNodeId, rpcId, reqId, contentByteString, timestamp):
        if rpcId == RpcId.UNKNOWN_RPC_ID:
            logger.warning("处理RPC,来源节点ID:%s,RPC ID:%s", sourceNodeId, rpcId)
            return
        elif rpcId == RpcId.SUBSCRIBE_RSP:
            try:
                rpcSubscribeRsp = RpcSubscribeRsp()
                rpcSubscribeRsp.ParseFromString(contentByteString)
                RpcClientProcessService.checkCommonRsp(
                    rpcSubscribeRsp.commonRsp, sourceNodeId, reqId)
                logger.info("处理RPC记录,来源节点ID:%s,请求ID:%s,RPC:SUBSCRIBE_RSP",
                            sourceNodeId, reqId)
                RpcClientRspHandler.onSubscribeRsp(rpcSubscribeRsp)
            except Exception as e:
                logger.error("处理RPC异常,来源节点ID:%s,RPC:SUBSCRIBE_RSP",
                             sourceNodeId,
                             exc_info=True)
                RpcClientProcessService.sendExceptionRsp(
                    sourceNodeId, rpcId, reqId, timestamp, str(e))
        elif rpcId == RpcId.UNSUBSCRIBE_RSP:
            try:
                rpcUnsubscribeRsp = RpcUnsubscribeRsp()
                rpcUnsubscribeRsp.ParseFromString(contentByteString)
                RpcClientProcessService.checkCommonRsp(
                    rpcUnsubscribeRsp.commonRsp, sourceNodeId, reqId)
                logger.info("处理RPC记录,来源节点ID:%s,请求ID:%s,RPC:UNSUBSCRIBE_RSP",
                            sourceNodeId, reqId)
                RpcClientRspHandler.onUnsubscribeRsp(rpcUnsubscribeRsp)
            except Exception as e:
                logger.error("处理RPC异常,来源节点ID:%s,RPC:UNSUBSCRIBE_RSP",
                             sourceNodeId,
                             exc_info=True)
                RpcClientProcessService.sendExceptionRsp(
                    sourceNodeId, rpcId, reqId, timestamp, str(e))
        elif rpcId == RpcId.SUBMIT_ORDER_RSP:
            try:
                rpcSubmitOrderRsp = RpcSubmitOrderRsp()
                rpcSubmitOrderRsp.ParseFromString(contentByteString)
                RpcClientProcessService.checkCommonRsp(
                    rpcSubmitOrderRsp.commonRsp, sourceNodeId, reqId)
                logger.info("处理RPC记录,来源节点ID:%s,请求ID:%s,RPC:SUBMIT_ORDER_RSP",
                            sourceNodeId, reqId)
                RpcClientRspHandler.onSubmitOrderRsp(rpcSubmitOrderRsp)
            except Exception as e:
                logger.error("处理RPC异常,来源节点ID:%s,RPC:SUBMIT_ORDER_RSP",
                             sourceNodeId,
                             exc_info=True)
                RpcClientProcessService.sendExceptionRsp(
                    sourceNodeId, rpcId, reqId, timestamp, str(e))
        elif rpcId == RpcId.CANCEL_ORDER_RSP:
            try:
                rpcCancelOrderRsp = RpcCancelOrderRsp()
                rpcCancelOrderRsp.ParseFromString(contentByteString)
                RpcClientProcessService.checkCommonRsp(
                    rpcCancelOrderRsp.commonRsp, sourceNodeId, reqId)
                logger.info("处理RPC记录,来源节点ID:%s,请求ID:%s,RPC:CANCEL_ORDER_RSP",
                            sourceNodeId, reqId)
                RpcClientRspHandler.onCancelOrderRsp(rpcCancelOrderRsp)
            except Exception as e:
                logger.error("处理RPC异常,来源节点ID:%s,RPC:CANCEL_ORDER_RSP",
                             sourceNodeId,
                             exc_info=True)
                RpcClientProcessService.sendExceptionRsp(
                    sourceNodeId, rpcId, reqId, timestamp, str(e))
        elif rpcId == RpcId.SEARCH_CONTRACT_RSP:
            try:
                rpcSearchContractRsp = RpcSearchContractRsp()
                rpcSearchContractRsp.ParseFromString(contentByteString)
                RpcClientProcessService.checkCommonRsp(
                    rpcSearchContractRsp.commonRsp, sourceNodeId, reqId)
                logger.info(
                    "处理RPC记录,来源节点ID:%s,请求ID:%s,RPC:SEARCH_CONTRACT_RSP",
                    sourceNodeId, reqId)
                RpcClientRspHandler.onSearchContractRsp(rpcSearchContractRsp)
            except Exception as e:
                logger.error("处理RPC异常,来源节点ID:%s,RPC:SEARCH_CONTRACT_RSP",
                             sourceNodeId,
                             exc_info=True)
                RpcClientProcessService.sendExceptionRsp(
                    sourceNodeId, rpcId, reqId, timestamp, str(e))
        elif rpcId == RpcId.GET_MIX_CONTRACT_LIST_RSP:
            try:
                rpcGetMixContractListRsp = RpcGetMixContractListRsp()
                rpcGetMixContractListRsp.ParseFromString(contentByteString)
                RpcClientProcessService.checkCommonRsp(
                    rpcGetMixContractListRsp.commonRsp, sourceNodeId, reqId)
                logger.info(
                    "处理RPC记录,来源节点ID:%s,请求ID:%s,RPC:GET_MIX_CONTRACT_LIST_RSP",
                    sourceNodeId, reqId)
                RpcClientRspHandler.onGetMixContractListRsp(
                    rpcGetMixContractListRsp)
            except Exception as e:
                logger.error("处理RPC异常,来源节点ID:%s,RPC:GET_MIX_CONTRACT_LIST_RSP",
                             sourceNodeId,
                             exc_info=True)
                RpcClientProcessService.sendExceptionRsp(
                    sourceNodeId, rpcId, reqId, timestamp, str(e))
        elif rpcId == RpcId.GET_POSITION_LIST_RSP:
            try:
                rpcGetPositionListRsp = RpcGetPositionListRsp()
                rpcGetPositionListRsp.ParseFromString(contentByteString)
                RpcClientProcessService.checkCommonRsp(
                    rpcGetPositionListRsp.commonRsp, sourceNodeId, reqId)
                logger.info(
                    "处理RPC记录,来源节点ID:%s,请求ID:%s,RPC:GET_POSITION_LIST_RSP",
                    sourceNodeId, reqId)
                RpcClientRspHandler.onGetPositionListRsp(rpcGetPositionListRsp)
            except Exception as e:
                logger.error("处理RPC异常,来源节点ID:%s,RPC:GET_POSITION_LIST_RSP",
                             sourceNodeId,
                             exc_info=True)
                RpcClientProcessService.sendExceptionRsp(
                    sourceNodeId, rpcId, reqId, timestamp, str(e))
        elif rpcId == RpcId.GET_ACCOUNT_LIST_RSP:
            try:
                rpcGetAccountListRsp = RpcGetAccountListRsp()
                rpcGetAccountListRsp.ParseFromString(contentByteString)
                RpcClientProcessService.checkCommonRsp(
                    rpcGetAccountListRsp.commonRsp, sourceNodeId, reqId)
                logger.info(
                    "处理RPC记录,来源节点ID:%s,请求ID:%s,RPC:GET_ACCOUNT_LIST_RSP",
                    sourceNodeId, reqId)
                RpcClientRspHandler.onGetAccountListRsp(rpcGetAccountListRsp)
            except Exception as e:
                logger.error("处理RPC异常,来源节点ID:%s,RPC:GET_ACCOUNT_LIST_RSP",
                             sourceNodeId,
                             exc_info=True)
                RpcClientProcessService.sendExceptionRsp(
                    sourceNodeId, rpcId, reqId, timestamp, str(e))
        elif rpcId == RpcId.GET_TRADE_LIST_RSP:
            try:
                rpcGetTradeListRsp = RpcGetTradeListRsp()
                rpcGetTradeListRsp.ParseFromString(contentByteString)
                RpcClientProcessService.checkCommonRsp(
                    rpcGetTradeListRsp.commonRsp, sourceNodeId, reqId)
                logger.info("处理RPC记录,来源节点ID:%s,请求ID:%s,RPC:GET_TRADE_LIST_RSP",
                            sourceNodeId, reqId)
                RpcClientRspHandler.onGetTradeListRsp(rpcGetTradeListRsp)
            except Exception as e:
                logger.error("处理RPC异常,来源节点ID:%s,RPC:GET_TRADE_LIST_RSP",
                             sourceNodeId,
                             exc_info=True)
                RpcClientProcessService.sendExceptionRsp(
                    sourceNodeId, rpcId, reqId, timestamp, str(e))
        elif rpcId == RpcId.GET_ORDER_LIST_RSP:
            try:
                rpcGetOrderListRsp = RpcGetOrderListRsp()
                rpcGetOrderListRsp.ParseFromString(contentByteString)
                RpcClientProcessService.checkCommonRsp(
                    rpcGetOrderListRsp.commonRsp, sourceNodeId, reqId)
                logger.info("处理RPC记录,来源节点ID:%s,请求ID:%s,RPC:GET_ORDER_LIST_RSP",
                            sourceNodeId, reqId)
                RpcClientRspHandler.onGetOrderListRsp(rpcGetOrderListRsp)
            except Exception as e:
                logger.error("处理RPC异常,来源节点ID:%s,RPC:GET_ORDER_LIST_RSP",
                             sourceNodeId,
                             exc_info=True)
                RpcClientProcessService.sendExceptionRsp(
                    sourceNodeId, rpcId, reqId, timestamp, str(e))
        elif rpcId == RpcId.GET_TICK_LIST_RSP:
            try:
                rpcGetTickListRsp = RpcGetTickListRsp()
                rpcGetTickListRsp.ParseFromString(contentByteString)
                RpcClientProcessService.checkCommonRsp(
                    rpcGetTickListRsp.commonRsp, sourceNodeId, reqId)
                logger.info("处理RPC记录,来源节点ID:%s,请求ID:%s,RPC:GET_TICK_LIST_RSP",
                            sourceNodeId, reqId)
                RpcClientRspHandler.onGetTickListRsp(rpcGetTickListRsp)
            except Exception as e:
                logger.error("处理RPC异常,来源节点ID:%s,RPC:GET_TICK_LIST_RSP",
                             sourceNodeId,
                             exc_info=True)
                RpcClientProcessService.sendExceptionRsp(
                    sourceNodeId, rpcId, reqId, timestamp, str(e))
        elif rpcId == RpcId.EXCEPTION_RSP:
            try:
                rpcExceptionRsp = RpcExceptionRsp()
                rpcExceptionRsp.ParseFromString(contentByteString)
                logger.info("处理RPC记录,来源节点ID:%s,请求ID:%s,RPC:EXCEPTION_RSP",
                            sourceNodeId, reqId)
                RpcClientRspHandler.onExceptionRsp(rpcExceptionRsp)
            except Exception as e:
                logger.error("处理RPC异常,来源节点ID:%s,RPC:EXCEPTION_RSP",
                             sourceNodeId,
                             exc_info=True)
                RpcClientProcessService.sendExceptionRsp(
                    sourceNodeId, rpcId, reqId, timestamp, str(e))

        elif rpcId == RpcId.ORDER_RTN:
            try:
                rpcOrderRtn = RpcOrderRtn()
                rpcOrderRtn.ParseFromString(contentByteString)
                logger.info("处理RPC记录,来源节点ID:%s,请求ID:%s,RPC:ORDER_RTN",
                            sourceNodeId, reqId)
                RpcClientRtnHandler.onRpcOrderRtn(rpcOrderRtn)
            except Exception as e:
                logger.error("处理RPC异常,来源节点ID:%s,RPC:ORDER_RTN",
                             sourceNodeId,
                             exc_info=True)
                RpcClientProcessService.sendExceptionRsp(
                    sourceNodeId, rpcId, reqId, timestamp, str(e))

        elif rpcId == RpcId.TRADE_RTN:
            try:
                rpcTradeRtn = RpcTradeRtn()
                rpcTradeRtn.ParseFromString(contentByteString)
                logger.info("处理RPC记录,来源节点ID:%s,请求ID:%s,RPC:TRADE_RTN",
                            sourceNodeId, reqId)
                RpcClientRtnHandler.onRpcTradeRtn(rpcTradeRtn)
            except Exception as e:
                logger.error("处理RPC异常,来源节点ID:%s,RPC:TRADE_RTN",
                             sourceNodeId,
                             exc_info=True)
                RpcClientProcessService.sendExceptionRsp(
                    sourceNodeId, rpcId, reqId, timestamp, str(e))

        elif rpcId == RpcId.POSITION_RTN:
            try:
                rpcPositionRtn = RpcPositionRtn()
                rpcPositionRtn.ParseFromString(contentByteString)
                logger.info("处理RPC记录,来源节点ID:%s,请求ID:%s,RPC:POSITION_RTN",
                            sourceNodeId, reqId)
                RpcClientRtnHandler.onRpcPositionRtn(rpcPositionRtn)
            except Exception as e:
                logger.error("处理RPC异常,来源节点ID:%s,RPC:POSITION_RTN",
                             sourceNodeId,
                             exc_info=True)
                RpcClientProcessService.sendExceptionRsp(
                    sourceNodeId, rpcId, reqId, timestamp, str(e))

        elif rpcId == RpcId.ACCOUNT_RTN:
            try:
                rpcAccountRtn = RpcAccountRtn()
                rpcAccountRtn.ParseFromString(contentByteString)
                logger.info("处理RPC记录,来源节点ID:%s,请求ID:%s,RPC:ACCOUNT_RTN",
                            sourceNodeId, reqId)
                RpcClientRtnHandler.onRpcAccountRtn(rpcAccountRtn)
            except Exception as e:
                logger.error("处理RPC异常,来源节点ID:%s,RPC:ACCOUNT_RTN",
                             sourceNodeId,
                             exc_info=True)
                RpcClientProcessService.sendExceptionRsp(
                    sourceNodeId, rpcId, reqId, timestamp, str(e))

        elif rpcId == RpcId.CONTRACT_RTN:
            try:
                rpcContractRtn = RpcContractRtn()
                rpcContractRtn.ParseFromString(contentByteString)
                logger.info("处理RPC记录,来源节点ID:%s,请求ID:%s,RPC:CONTRACT_RTN",
                            sourceNodeId, reqId)
                RpcClientRtnHandler.onRpcContractRtn(rpcContractRtn)
            except Exception as e:
                logger.error("处理RPC异常,来源节点ID:%s,RPC:CONTRACT_RTN",
                             sourceNodeId,
                             exc_info=True)
                RpcClientProcessService.sendExceptionRsp(
                    sourceNodeId, rpcId, reqId, timestamp, str(e))

        elif rpcId == RpcId.TICK_RTN:
            try:
                rpcTickRtn = RpcTickRtn()
                rpcTickRtn.ParseFromString(contentByteString)
                logger.info("处理RPC记录,来源节点ID:%s,请求ID:%s,RPC:TICK_RTN",
                            sourceNodeId, reqId)
                RpcClientRtnHandler.onRpcTickRtn(rpcTickRtn)
            except Exception as e:
                logger.error("处理RPC异常,来源节点ID:%s,RPC:TICK_RTN",
                             sourceNodeId,
                             exc_info=True)
                RpcClientProcessService.sendExceptionRsp(
                    sourceNodeId, rpcId, reqId, timestamp, str(e))

        elif rpcId == RpcId.ORDER_LIST_RTN:
            try:
                rpcOrderListRtn = RpcOrderListRtn()
                rpcOrderListRtn.ParseFromString(contentByteString)
                logger.info("处理RPC记录,来源节点ID:%s,请求ID:%s,RPC:ORDER_LIST_RTN",
                            sourceNodeId, reqId)
                RpcClientRtnHandler.onRpcOrderListRtn(rpcOrderListRtn)
            except Exception as e:
                logger.error("处理RPC异常,来源节点ID:%s,RPC:ORDER_LIST_RTN",
                             sourceNodeId,
                             exc_info=True)
                RpcClientProcessService.sendExceptionRsp(
                    sourceNodeId, rpcId, reqId, timestamp, str(e))

        elif rpcId == RpcId.TRADE_LIST_RTN:
            try:
                rpcTradeListRtn = RpcTradeListRtn()
                rpcTradeListRtn.ParseFromString(contentByteString)
                logger.info("处理RPC记录,来源节点ID:%s,请求ID:%s,RPC:TRADE_LIST_RTN",
                            sourceNodeId, reqId)
                RpcClientRtnHandler.onRpcTradeListRtn(rpcTradeListRtn)
            except Exception as e:
                logger.error("处理RPC异常,来源节点ID:%s,RPC:TRADE_LIST_RTN",
                             sourceNodeId,
                             exc_info=True)
                RpcClientProcessService.sendExceptionRsp(
                    sourceNodeId, rpcId, reqId, timestamp, str(e))

        elif rpcId == RpcId.CONTRACT_LIST_RTN:
            try:
                rpcContractListRtn = RpcContractListRtn()
                rpcContractListRtn.ParseFromString(contentByteString)
                logger.info("处理RPC记录,来源节点ID:%s,请求ID:%s,RPC:CONTRACT_LIST_RTN",
                            sourceNodeId, reqId)
                RpcClientRtnHandler.onRpcContractListRtn(rpcContractListRtn)
            except Exception as e:
                logger.error("处理RPC异常,来源节点ID:%s,RPC:CONTRACT_LIST_RTN",
                             sourceNodeId,
                             exc_info=True)
                RpcClientProcessService.sendExceptionRsp(
                    sourceNodeId, rpcId, reqId, timestamp, str(e))

        elif rpcId == RpcId.POSITION_LIST_RTN:
            try:
                rpcPositionListRtn = RpcPositionListRtn()
                rpcPositionListRtn.ParseFromString(contentByteString)
                logger.info("处理RPC记录,来源节点ID:%s,请求ID:%s,RPC:POSITION_LIST_RTN",
                            sourceNodeId, reqId)
                RpcClientRtnHandler.onRpcPositionListRtn(rpcPositionListRtn)
            except Exception as e:
                logger.error("处理RPC异常,来源节点ID:%s,RPC:POSITION_LIST_RTN",
                             sourceNodeId,
                             exc_info=True)
                RpcClientProcessService.sendExceptionRsp(
                    sourceNodeId, rpcId, reqId, timestamp, str(e))

        elif rpcId == RpcId.ACCOUNT_LIST_RTN:
            try:
                rpcAccountListRtn = RpcAccountListRtn()
                rpcAccountListRtn.ParseFromString(contentByteString)
                logger.info("处理RPC记录,来源节点ID:%s,请求ID:%s,RPC:ACCOUNT_LIST_RTN",
                            sourceNodeId, reqId)
                RpcClientRtnHandler.onRpcAccountListRtn(rpcAccountListRtn)
            except Exception as e:
                logger.error("处理RPC异常,来源节点ID:%s,RPC:ACCOUNT_LIST_RTN",
                             sourceNodeId,
                             exc_info=True)
                RpcClientProcessService.sendExceptionRsp(
                    sourceNodeId, rpcId, reqId, timestamp, str(e))

        elif rpcId == RpcId.TICK_LIST_RTN:
            try:
                rpcTickListRtn = RpcTickListRtn()
                rpcTickListRtn.ParseFromString(contentByteString)
                logger.info("处理RPC记录,来源节点ID:%s,请求ID:%s,RPC:TICK_LIST_RTN",
                            sourceNodeId, reqId)
                RpcClientRtnHandler.onRpcTickListRtn(rpcTickListRtn)
            except Exception as e:
                logger.error("处理RPC异常,来源节点ID:%s,RPC:TICK_LIST_RTN",
                             sourceNodeId,
                             exc_info=True)
                RpcClientProcessService.sendExceptionRsp(
                    sourceNodeId, rpcId, reqId, timestamp, str(e))

        elif rpcId == RpcId.NOTICE_RTN:
            try:
                rpcNoticeRtn = RpcNoticeRtn()
                rpcNoticeRtn.ParseFromString(contentByteString)
                logger.info("处理RPC记录,来源节点ID:%s,请求ID:%s,RPC:NOTICE_RTN",
                            sourceNodeId, reqId)
                RpcClientRtnHandler.onRpcNoticeRtn(rpcNoticeRtn)
            except Exception as e:
                logger.error("处理RPC异常,来源节点ID:%s,RPC:NOTICE_RTN",
                             sourceNodeId,
                             exc_info=True)
                RpcClientProcessService.sendExceptionRsp(
                    sourceNodeId, rpcId, reqId, timestamp, str(e))
        elif rpcId == RpcId.QUERY_DB_BAR_LIST_RSP:
            try:
                rpcQueryDBBarListRsp = RpcQueryDBBarListRsp()
                rpcQueryDBBarListRsp.ParseFromString(contentByteString)
                RpcClientProcessService.checkCommonRsp(
                    rpcQueryDBBarListRsp.commonRsp, sourceNodeId, reqId)
                logger.info(
                    "处理RPC记录,来源节点ID:%s,请求ID:%s,RPC:QUERY_DB_BAR_LIST_RSP",
                    sourceNodeId, reqId)
                RpcClientRspHandler.onQueryDBBarListRsp(rpcQueryDBBarListRsp)
            except Exception as e:
                logger.error("处理RPC异常,来源节点ID:%s,RPC:QUERY_DB_BAR_LIST_RSP",
                             sourceNodeId,
                             exc_info=True)
                RpcClientProcessService.sendExceptionRsp(
                    sourceNodeId, rpcId, reqId, timestamp, str(e))
        else:
            logger.error("处理RPC错误, 来源节点ID:%s, RPC ID:%s, 请求ID:%s 不支持此功能",
                         sourceNodeId, rpcId, reqId)
    def queryDBBarList(startTimestamp,
                       endTimestamp,
                       unifiedSymbol,
                       barCycle,
                       marketDataDBType,
                       reqId=None,
                       timeoutSeconds=None):
        if not reqId:
            reqId = str(uuid.uuid4())
        if not timeoutSeconds:
            timeoutSeconds = RtConfig.rpcTimeOut
        operatorId = RtConfig.operatorId
        sourceNodeId = RtConfig.nodeId

        commonReq = CommonReqField()
        commonReq.sourceNodeId = sourceNodeId
        commonReq.targetNodeId = 0
        commonReq.operatorId = operatorId
        commonReq.reqId = reqId

        rpcQueryDBBarListReq = RpcQueryDBBarListReq()

        rpcQueryDBBarListReq.commonReq.CopyFrom(commonReq)

        rpcQueryDBBarListReq.startTimestamp = startTimestamp
        rpcQueryDBBarListReq.endTimestamp = endTimestamp
        rpcQueryDBBarListReq.unifiedSymbol = unifiedSymbol
        rpcQueryDBBarListReq.barCycle = barCycle
        rpcQueryDBBarListReq.marketDataDBType = marketDataDBType

        RpcClientRspHandler.registerWaitReqId(reqId)

        sendResult = RpcClientProcessService.sendCoreRpc(
            0, rpcQueryDBBarListReq.SerializeToString(), reqId,
            RpcId.QUERY_DB_BAR_LIST_REQ)

        if not sendResult:
            RpcClientRspHandler.unregisterWaitReqId(reqId)
            return None

        startTime = time.time()
        while True:
            if time.time() - startTime < timeoutSeconds:
                rpcQueryDBBarListRsp = RpcClientRspHandler.getAndRemoveRpcQueryDBBarListRsp(
                    reqId)
                if not rpcQueryDBBarListRsp:
                    rpcExceptionRsp = RpcClientRspHandler.getAndRemoveRpcExceptionRsp(
                        reqId)
                    if rpcExceptionRsp:
                        logger.error("获取Bar列表错误,请求ID: %s, 远程错误回报 %s", reqId,
                                     rpcExceptionRsp.info)
                        return None
                    time.sleep(0.02)
                else:
                    commonRsp = rpcQueryDBBarListRsp.commonRsp
                    errorId = commonRsp.errorId
                    if errorId == 0:
                        return rpcQueryDBBarListRsp.bar
                    else:
                        logger.error("获取Bar列表错误,请求ID:%s,错误ID:%s,远程错误回报:%s",
                                     reqId, errorId, commonRsp.errorMsg)
                        return None
            else:
                RpcClientRspHandler.unregisterWaitReqId(reqId)
                logger.error("获取Bar列表错误,请求ID: %s,等待回报超时", reqId)
                return None