From 459fc4770fd6235c13d94e59c591ce61fc6f8863 Mon Sep 17 00:00:00 2001 From: nanoric Date: Thu, 14 Mar 2019 23:20:11 -0400 Subject: [PATCH] [Fix] OesGateway:Optimized connect status detection --- vnpy/gateway/oes/oes_md.py | 7 +++---- vnpy/gateway/oes/oes_td.py | 7 +++---- vnpy/gateway/oes/utils.py | 13 +++++++++++++ 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/vnpy/gateway/oes/oes_md.py b/vnpy/gateway/oes/oes_md.py index 57ce2b77..763bc93e 100644 --- a/vnpy/gateway/oes/oes_md.py +++ b/vnpy/gateway/oes/oes_md.py @@ -11,9 +11,10 @@ from vnpy.api.oes.vnoes import MdsApiClientEnvT, MdsApi_DestoryAll, MdsApi_InitL MdsMktDataRequestEntryT, MdsMktDataRequestReqBufT, MdsMktDataRequestReqT, MdsMktRspMsgBodyT, \ MdsStockSnapshotBodyT, SGeneralClientChannelT, SMsgHeadT, SPlatform_IsNegEpipe, cast, \ eMdsExchangeIdT, eMdsMktSubscribeFlagT, eMdsMsgTypeT, eMdsSecurityTypeT, eMdsSubscribeDataTypeT, \ - eMdsSubscribeModeT, eMdsSubscribedTickExpireTypeT, eMdsSubscribedTickTypeT, eSMsgProtocolTypeT + eMdsSubscribeModeT, eMdsSubscribedTickExpireTypeT, eMdsSubscribedTickTypeT, eSMsgProtocolTypeT, \ + SPlatform_IsNegEconnaborted -from vnpy.gateway.oes.utils import create_remote_config +from vnpy.gateway.oes.utils import create_remote_config, is_disconnected from vnpy.trader.constant import Exchange from vnpy.trader.gateway import BaseGateway from vnpy.trader.object import SubscribeRequest, TickData @@ -118,8 +119,6 @@ class OesMdMessageLoop: """""" tcp_channel = self.env.tcpChannel timeout_ms = 1000 - # is_timeout = SPlatform_IsNegEtimeout - is_disconnected = SPlatform_IsNegEpipe while self._alive: ret = MdsApi_WaitOnMsg(tcp_channel, timeout_ms, diff --git a/vnpy/gateway/oes/oes_td.py b/vnpy/gateway/oes/oes_td.py index 0d7f40ec..b7ac7656 100644 --- a/vnpy/gateway/oes/oes_td.py +++ b/vnpy/gateway/oes/oes_td.py @@ -18,7 +18,7 @@ from vnpy.api.oes.vnoes import OesApiClientEnvT, OesApiSubscribeInfoT, OesApi_De eOesOrdStatusT, eOesOrdTypeShT, eOesOrdTypeSzT, eOesSubscribeReportTypeT from vnpy.gateway.oes.error_code import error_to_str -from vnpy.gateway.oes.utils import create_remote_config +from vnpy.gateway.oes.utils import create_remote_config, is_disconnected from vnpy.trader.constant import Direction, Exchange, Offset, PriceType, Product, Status from vnpy.trader.gateway import BaseGateway from vnpy.trader.object import AccountData, CancelRequest, ContractData, OrderData, OrderRequest, \ @@ -177,7 +177,6 @@ class OesTdMessageLoop: """""" rpt_channel = self._env.rptChannel timeout_ms = 1000 - is_disconnected = SPlatform_IsNegEpipe while self._alive: ret = OesApi_WaitReportMsg(rpt_channel, @@ -643,7 +642,7 @@ class OesTdApi: else: order.status = Status.REJECTED self.gateway.write_log(_("下单失败")) # todo: can I stringify error? - if ret == -108: # is here any other ret code indicating connection lost? + if is_disconnected(ret): self.gateway.write_log(_("下单时连接发现连接已断开,正在尝试重连")) self._schedule_reconnect_ord_channel() self.gateway.on_order(order) @@ -667,7 +666,7 @@ class OesTdApi: oes_req) if ret < 0: self.gateway.write_log(_("撤单失败")) # todo: can I stringify error? - if ret == -108: # is here any other ret code indicating connection lost? + if is_disconnected(ret): # is here any other ret code indicating connection lost? self.gateway.write_log(_("撤单时连接发现连接已断开,正在尝试重连")) self._schedule_reconnect_ord_channel() diff --git a/vnpy/gateway/oes/utils.py b/vnpy/gateway/oes/utils.py index 6d1272a6..436e9e5e 100644 --- a/vnpy/gateway/oes/utils.py +++ b/vnpy/gateway/oes/utils.py @@ -9,6 +9,9 @@ with open(config_template_path, "rt", encoding='utf-8') as f: def create_remote_config(server: str, username: str, password: str): + """ + create a SGeneralClientRemoteCfgT. + """ cfg = SGeneralClientRemoteCfgT() cfg.username = username cfg.password = password @@ -28,3 +31,13 @@ def create_remote_config(server: str, username: str, password: str): cfg.socketOpt.keepCnt = 9 return cfg + +def is_disconnected(ret: int): + """ + check whether connection is lost by return value of OesApi/MdsApi + 106 : ECONNABORTED + 107 : ECONNREFUSED + 108 : ECONNRESET + maybe there is more than there error codes indicating a disconnected state + """ + return ret == -106 or ret == -107 or ret == -108