def connect(self, host, port): """ 连接服务器 :parameters: * host : IP地址 * port : 端口 """ try: self.host = host self.port = port if self.connectionState == True: ex = ApiException(*ErrTypeList.EX_CONNECT_REPEAT) raise ex addr = (host, port) Logger.debug('客户端版本号:%s' % CTSlib.__version__) Logger.debug('connect server:%s' % host) self.tcpCliSock = socket(AF_INET, SOCK_STREAM) self.tcpCliSock.setsockopt(IPPROTO_TCP, TCP_NODELAY, 1) self.tcpCliSock.settimeout(30) try: self.tcpCliSock.connect(addr) except: ex = ApiException(*ErrTypeList.EX_CONNECT_ERROR) raise ex self.tcpCliSock.settimeout(None) Logger.debug('before DispatchThread...') self.t = DispatchThread(self) Logger.debug('after DispatchThread...') self.t.start() self.connectionState = True msgHead = MsgHead(MsgTypeList.MSG_TYPE_SYS_CONNECT, self.sessionId) msgRequest = ParaTerminalInfo() msgData = MsgData(msgHead, msgRequest) reqDataValue = self.syncExchangeData(msgData, 30) reqData = reqDataValue.data self.passkey = str(reqData.passkey) del reqData.passkey Logger.info('连接成功') return reqData except ApiException as ex: raise ex except AttributeError as ex: Logger.error('连接超时... %s' % ex) self.disConnect() raise ex except Exception as ex: Logger.error('Connect error... %s' % ex) self.disConnect() raise ex
def run(self): """ 运行线程 """ threadname = threading.currentThread().getName() Logger.debug('Thread [%s] is running... ' % threadname) fileobject = None if _pythonMajorVersion < 3: fileobject = self.ctsServer.tcpCliSock.makefile() else: fileobject = self.ctsServer.tcpCliSock.makefile(encoding='UTF-8') reqMap = self.ctsServer.reqMap while not self.thread_stop: try: data = fileobject.readline() Logger.debug('receiveData, %s ' % data) if not data: break jsonData = json.loads(data) jsonHead = jsonData['head'] jsonRespond = jsonData['respond'] jsonData = jsonData['data'] msgRespond = MsgRespond() msgRespond.__dict__ = jsonRespond msgType = jsonHead['msgType'] reqId = jsonHead['requestId'] msgHead = MsgHead(msgType) msgHead.__dict__ = jsonHead if MsgTypeList.MSG_TYPE_SYS_CONNECT == msgType: pass if reqId in reqMap: msgData = MsgConnectInfo() msgData.__dict__ = jsonData self.ctsServer.sessionId = jsonHead['sessionId'] self.ctsServer.handleSyncRequest2(reqId, msgRespond, msgData) else: if MsgTypeList.MSG_TYPE_SYS_DISCONNECT == msgType: if reqId in reqMap: msgData = MsgDisConnectInfo() msgData.__dict__ = jsonData self.ctsServer.sessionId = jsonHead['sessionId'] self.ctsServer.handleSyncRequest2( reqId, msgRespond, msgData) break else: if MsgTypeList.MSG_TYPE_QRY_STKINFO == msgType: msgData = MsgStkInfo() if 'stkInfo' in jsonData: msgData.__dict__ = jsonData['stkInfo'] if reqId in reqMap: self.ctsServer.handleSyncRequest2( reqId, msgRespond, msgData) else: self.ctsServer.onQueryStkInfo( msgData, msgRespond) else: if MsgTypeList.MSG_TYPE_QRY_FUTURE == msgType: msgData = [] if 'futureList' in jsonData: msgFutrueList = jsonData['futureList'] for msgFutureDictTmp in msgFutrueList: msgFutureDict = MsgQueryFutureInfo() msgFutureDict.__dict__ = msgFutureDictTmp msgData.append(msgFutureDict) if reqId in reqMap: self.ctsServer.handleSyncRequest2( reqId, msgRespond, msgData) if MsgTypeList.MSG_TYPE_ACCT_LOGIN == msgType: if reqId in reqMap: msgData = MsgAccount() if 'acctInfo' in jsonData: msgData.__dict__ = jsonData['acctInfo'] self.ctsServer.handleSyncRequest2( reqId, msgRespond, msgData) else: if MsgTypeList.MSG_TYPE_OPT_LOGIN == msgType: pass if reqId in reqMap: if msgRespond.successFlg == 0: msgData = '柜员登录成功!' else: if msgRespond.successFlg == 1: msgData = '柜员登录失败!' self.ctsServer.handleSyncRequest2(reqId, msgRespond, msgData) else: if MsgTypeList.MSG_TYPE_SUB_TRADE_RESP == msgType: if msgRespond.successFlg == 0: msgData = '订阅成功!' else: if msgRespond.successFlg == 1: msgData = '订阅失败!' self.ctsServer.onSubscriptTrade(msgData, msgRespond) else: if MsgTypeList.MSG_TYPE_SUB_TRADE_RETURN == msgType: msgData = MsgSubscriptTradeReturn() if 'subKnockInfo' in jsonData: msgData.__dict__ = jsonData['subKnockInfo'] self.ctsServer.onTradeEvent(msgData, msgRespond) else: if MsgTypeList.MSG_TYPE_SUB_QUOTE_RESP == msgType: if msgRespond.successFlg == 0: msgData = '订阅成功!' else: if msgRespond.successFlg == 1: msgData = '订阅失败!' self.ctsServer.onSubscriptQuota( msgData, msgRespond) else: if MsgTypeList.MSG_TYPE_SUB_QUOTE_RETURN == msgType: msgData = MsgSubQuoteReturn() if 'quotaInfo' in jsonData: msgData.__dict__ = jsonData[ 'quotaInfo'] self.ctsServer.onQuoteEvent( msgData, msgRespond) else: if MsgTypeList.MSG_TYPE_ORDER_NEW == msgType: msgData = MsgOrderNew() if 'contractNum' in jsonData: msgData.__dict__ = jsonData if reqId in reqMap: self.ctsServer.handleSyncRequest2( reqId, msgRespond, msgData) else: self.ctsServer.onOrderNew( msgData, msgRespond, msgHead) else: if MsgTypeList.MSG_TYPE_ORDER_CANCEL == msgType: msgData = MsgOrderCancel() if 'contractNum' in jsonData: msgData.__dict__ = jsonData if reqId in reqMap: self.ctsServer.handleSyncRequest2( reqId, msgRespond, msgData) else: self.ctsServer.onOrderCancel( msgData, msgRespond, msgHead) else: if MsgTypeList.MSG_TYPE_QRY_ACCOUNT == msgType: msgData = MsgAccontQuery() if 'acctInfo' in jsonData: msgData.__dict__ = jsonData[ 'acctInfo'] if reqId in reqMap: self.ctsServer.handleSyncRequest2( reqId, msgRespond, msgData) else: self.ctsServer.onQueryAccountInfo( msgData, msgRespond) if MsgTypeList.MSG_TYPE_FUTURE_ACCOUNT == msgType: msgData = MsgQueryFutAcctInfo() if 'acctInfo' in jsonData: msgData.__dict__ = jsonData['acctInfo'] if reqId in reqMap: self.ctsServer.handleSyncRequest2( reqId, msgRespond, msgData) else: if MsgTypeList.MSG_TYPE_QRY_ORDER == msgType: if reqId in reqMap: msgData = [] if 'orderInfo' in jsonData: msgDataList = jsonData['orderInfo'] for msgOrderDictTmp in msgDataList: msgDataDict = MsgQueryOrderInfo() msgDataDict.__dict__ = msgOrderDictTmp msgData.append(msgDataDict) self.ctsServer.handleSyncRequest2( reqId, msgRespond, msgData) else: msgData = MsgQueryOrderInfo() if 'orderInfo' in jsonData: msgData.__dict__ = jsonData['orderInfo'] self.ctsServer.onQueryOrderList( msgData, msgRespond, msgHead) else: if MsgTypeList.MSG_TYPE_QRY_KNOCK == msgType: if reqId in reqMap: msgData = [] if 'knockInfo' in jsonData: msgDataList = jsonData['knockInfo'] for msgDataDictTmp in msgDataList: msgDataDict = MsgQueryKnockInfo() msgDataDict.__dict__ = msgDataDictTmp msgData.append(msgDataDict) self.ctsServer.handleSyncRequest2( reqId, msgRespond, msgData) else: msgData = MsgQueryKnockInfo() if 'knockInfo' in jsonData: msgData.__dict__ = jsonData[ 'knockInfo'] self.ctsServer.onQueryKnockList( msgData, msgRespond, msgHead) else: if MsgTypeList.MSG_TYPE_QRY_POSITION == msgType: if reqId in reqMap: msgData = [] if 'positionInfo' in jsonData: msgDataList = jsonData[ 'positionInfo'] for msgDataDictTmp in msgDataList: msgDataDict = MsgQueryPositionInfo( ) msgDataDict.__dict__ = msgDataDictTmp msgData.append(msgDataDict) self.ctsServer.handleSyncRequest2( reqId, msgRespond, msgData) else: msgData = MsgQueryPositionInfo() if 'positionInfo' in jsonData: msgData.__dict__ = jsonData[ 'positionInfo'] self.ctsServer.onQueryPositionList( msgData, msgRespond, msgHead) else: if msgType in self.ctsServer.callbackFuncMap: self.ctsServer.callbackFuncMap[ msgType](jsonData, msgRespond, msgHead) else: Logger.debug('msgType: %s' % msgType) Logger.debug('msgData: %s' % msgData) except socketError as ex: Logger.error('Socket error... %s' % ex) Logger.error('Socket error... %s' % (ApiException(*ErrTypeList.EX_CONNECT_ERROR))) self.ctsServer.connectionState = False self.ctsServer.onSocketError(-1) break except Exception as ex: print(traceback.format_exc()) Logger.error('DispatchThread error... %s' % ex)