[Fix] OesGateway:Optimized connect status detection

This commit is contained in:
nanoric 2019-03-14 23:20:11 -04:00
parent 71d34d5854
commit 459fc4770f
3 changed files with 19 additions and 8 deletions

View File

@ -11,9 +11,10 @@ from vnpy.api.oes.vnoes import MdsApiClientEnvT, MdsApi_DestoryAll, MdsApi_InitL
MdsMktDataRequestEntryT, MdsMktDataRequestReqBufT, MdsMktDataRequestReqT, MdsMktRspMsgBodyT, \ MdsMktDataRequestEntryT, MdsMktDataRequestReqBufT, MdsMktDataRequestReqT, MdsMktRspMsgBodyT, \
MdsStockSnapshotBodyT, SGeneralClientChannelT, SMsgHeadT, SPlatform_IsNegEpipe, cast, \ MdsStockSnapshotBodyT, SGeneralClientChannelT, SMsgHeadT, SPlatform_IsNegEpipe, cast, \
eMdsExchangeIdT, eMdsMktSubscribeFlagT, eMdsMsgTypeT, eMdsSecurityTypeT, eMdsSubscribeDataTypeT, \ 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.constant import Exchange
from vnpy.trader.gateway import BaseGateway from vnpy.trader.gateway import BaseGateway
from vnpy.trader.object import SubscribeRequest, TickData from vnpy.trader.object import SubscribeRequest, TickData
@ -118,8 +119,6 @@ class OesMdMessageLoop:
"""""" """"""
tcp_channel = self.env.tcpChannel tcp_channel = self.env.tcpChannel
timeout_ms = 1000 timeout_ms = 1000
# is_timeout = SPlatform_IsNegEtimeout
is_disconnected = SPlatform_IsNegEpipe
while self._alive: while self._alive:
ret = MdsApi_WaitOnMsg(tcp_channel, ret = MdsApi_WaitOnMsg(tcp_channel,
timeout_ms, timeout_ms,

View File

@ -18,7 +18,7 @@ from vnpy.api.oes.vnoes import OesApiClientEnvT, OesApiSubscribeInfoT, OesApi_De
eOesOrdStatusT, eOesOrdTypeShT, eOesOrdTypeSzT, eOesSubscribeReportTypeT eOesOrdStatusT, eOesOrdTypeShT, eOesOrdTypeSzT, eOesSubscribeReportTypeT
from vnpy.gateway.oes.error_code import error_to_str 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.constant import Direction, Exchange, Offset, PriceType, Product, Status
from vnpy.trader.gateway import BaseGateway from vnpy.trader.gateway import BaseGateway
from vnpy.trader.object import AccountData, CancelRequest, ContractData, OrderData, OrderRequest, \ from vnpy.trader.object import AccountData, CancelRequest, ContractData, OrderData, OrderRequest, \
@ -177,7 +177,6 @@ class OesTdMessageLoop:
"""""" """"""
rpt_channel = self._env.rptChannel rpt_channel = self._env.rptChannel
timeout_ms = 1000 timeout_ms = 1000
is_disconnected = SPlatform_IsNegEpipe
while self._alive: while self._alive:
ret = OesApi_WaitReportMsg(rpt_channel, ret = OesApi_WaitReportMsg(rpt_channel,
@ -643,7 +642,7 @@ class OesTdApi:
else: else:
order.status = Status.REJECTED order.status = Status.REJECTED
self.gateway.write_log(_("下单失败")) # todo: can I stringify error? 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.gateway.write_log(_("下单时连接发现连接已断开,正在尝试重连"))
self._schedule_reconnect_ord_channel() self._schedule_reconnect_ord_channel()
self.gateway.on_order(order) self.gateway.on_order(order)
@ -667,7 +666,7 @@ class OesTdApi:
oes_req) oes_req)
if ret < 0: if ret < 0:
self.gateway.write_log(_("撤单失败")) # todo: can I stringify error? 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.gateway.write_log(_("撤单时连接发现连接已断开,正在尝试重连"))
self._schedule_reconnect_ord_channel() self._schedule_reconnect_ord_channel()

View File

@ -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): def create_remote_config(server: str, username: str, password: str):
"""
create a SGeneralClientRemoteCfgT.
"""
cfg = SGeneralClientRemoteCfgT() cfg = SGeneralClientRemoteCfgT()
cfg.username = username cfg.username = username
cfg.password = password cfg.password = password
@ -28,3 +31,13 @@ def create_remote_config(server: str, username: str, password: str):
cfg.socketOpt.keepCnt = 9 cfg.socketOpt.keepCnt = 9
return cfg 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