int CAccountTrade::OnRspUserLogin(char *pMsg,int client_fd) { int ret = 0; int iSendLen = sizeof(MsgHead) + sizeof(TradeRspUserLoginFromATF); TradeRspUserLoginFromATF *pRsp = (TradeRspUserLoginFromATF *)(pMsg + sizeof(MsgHead)); FillMsgOnRspLogin(pRsp); ret = TcpSendData(m_clientSock,pMsg,iSendLen); if(ret != iSendLen) { g_log.error("[ExchgServer]TCP send OnRspUserLogin error,expect %dB, actual %dB.\n",iSendLen,ret); memset(m_curOpErrReason,0,sizeof(m_curOpErrReason)); sprintf(m_curOpErrReason,"TCP send OnRspUserLogin error,expect %dB, actual %dB.",iSendLen,ret); return ATF_FAIL; } else { if(IS_DEBUG) { printf_red("[ExchgServer]Send OnRspUserLogin(%dB)\n",ret); } g_log.info("[ExchgServer]Send OnRspUserLogin(%dB)\n",ret); return ATF_SUCC; } }
int CAccountTrade::OnFrontDisconnected(char *pMsg,int client_fd) { int ret = 0; int iSendLen = sizeof(MsgHead); ret = TcpSendData(m_clientSock,pMsg,iSendLen); if(ret != iSendLen) { g_log.error("[ExchgServer]TCP send OnFrontDisconnected error,expect %dB, actual %dB.\n",iSendLen,ret); memset(m_curOpErrReason,0,sizeof(m_curOpErrReason)); sprintf(m_curOpErrReason,"TCP send OnFrontDisconnected error,expect %dB, actual %dB.",iSendLen,ret); return ATF_FAIL; } else { if(IS_DEBUG) { printf_red("[ExchgServer]Send OnFrontDisconnected(%dB)\n",ret); } g_log.info("[ExchgServer]Send OnFrontDisconnected(%dB)\n",ret); } m_connect = DISCONNECTED; m_login = NOT_LOGIN; m_sessionID++; CloseFd(); return ATF_SUCC; }
int CAccountTrade::OnErrRtnOrderAction(char *pMsg,int client_fd) { int ret = 0; int iSendLen = sizeof(MsgHead) + sizeof(OnErrRtnOrderActionFromATF); OnErrRtnOrderActionFromATF *pRsp = (OnErrRtnOrderActionFromATF *)(pMsg+sizeof(MsgHead)); int iCancelID = pRsp->inputOrder.RequestID; //利用RequestID存放对应的撤单ID std::map<int, ReqOrderActionToATF>::iterator iter = m_mCancelOrder.find(iCancelID); if(iter == m_mCancelOrder.end()) { g_log.error("[%s]No cancel order:%d\n",__FUNCTION__,iCancelID); memset(m_curOpErrReason,0,sizeof(m_curOpErrReason)); sprintf(m_curOpErrReason,"[%s]No cancel order:%d !\n",__FUNCTION__,iCancelID); return ATF_FAIL; } ReqOrderActionToATF *pReq = &(iter->second); FillMsgOnErrRtnOrderAction(&pReq->order,&pRsp->inputOrder); //memcpy(&pRsp->inputOrder,pReq,sizeof(pRsp->inputOrder)); ret = TcpSendData(client_fd,pMsg,iSendLen); if(ret != iSendLen) { memset(m_curOpErrReason,0,sizeof(m_curOpErrReason)); sprintf(m_curOpErrReason,"[%s]send OnErrRtnOrderAction,expect %dB, actual %dB.\n",__FUNCTION__,iSendLen,ret); g_log.error("[%s]send OnErrRtnOrderAction,expect %dB, actual %dB.\n",__FUNCTION__,iSendLen,ret); return ATF_FAIL; } else { g_log.info("[%s]send OnErrRtnOrderAction %dB.\n",__FUNCTION__,ret); return ATF_SUCC; } }
int CAccountTrade::OnRtnTrade(char *pMsg,int client_fd) { int iSendLen = 0; int ret = 0; OnRtnTradeFromATF *pRsp = (OnRtnTradeFromATF *)(pMsg+sizeof(MsgHead)); int iOrderID = pRsp->trade.SequenceNo; //利用SequenceNo存放对应的报单ID std::map<int, ReqOrderInsertToATF>::iterator iter = m_mOrder.find(iOrderID); if(iter == m_mOrder.end()) { g_log.error("No order:%d\n",iOrderID); memset(m_curOpErrReason,0,sizeof(m_curOpErrReason)); sprintf(m_curOpErrReason,"[%s]No order:%d !\n",__FUNCTION__,iOrderID); return ATF_FAIL; } ReqOrderInsertToATF *pReq = &(iter->second); FillMsgOnRtnTrade(&pReq->order,&pRsp->trade); iSendLen = sizeof(MsgHead) + sizeof(OnRtnTradeFromATF); ret = TcpSendData(client_fd,pMsg,iSendLen); if(ret != iSendLen) { memset(m_curOpErrReason,0,sizeof(m_curOpErrReason)); sprintf(m_curOpErrReason,"[%s]send OnRtnTrade,expect %dB, actual %dB.\n",__FUNCTION__,iSendLen,ret); g_log.error("[%s]send OnRtnTrade,expect %dB, actual %dB.\n",__FUNCTION__,iSendLen,ret); return ATF_FAIL; } else { g_log.info("[%s]send OnRtnTrade %dB.\n",__FUNCTION__,ret); return ATF_SUCC; } }
uint8_t ModbusInput(uint8_t *pIn, uint32_t inSize, uint8_t * g_modbusReceiveBuf, uint8_t * g_modbusSendBuf, int16u* g_modbusReceiveBufNumber, uint16_t * g_modbusSendBufNumber, uint8_t type, uint16_t * longth, uint16_t * address) { uint16_t i = 0; uint16_t receiveSize = 0; uint8_t tempbuf[4] = {0}; while (receiveSize < inSize) { if (*g_modbusReceiveBufNumber >= MODBUS_BUFFER_SIZE) InitModbus(type); g_modbusReceiveBuf[*g_modbusReceiveBufNumber] = *((pIn) + receiveSize); (*g_modbusReceiveBufNumber)++; receiveSize++; if (g_modbusReceiveBuf[0] == 0x03) {//read if (*g_modbusReceiveBufNumber > 4) { *address = g_modbusReceiveBuf[2] + ((uint16_t)(g_modbusReceiveBuf[1]) << 8); *longth = g_modbusReceiveBuf[4] + ((uint16_t)(g_modbusReceiveBuf[3]) << 8); if (type == 1) { if (*g_modbusReceiveBufNumber > 6) { if (usMBCRC16(&g_modbusReceiveBuf[0] - 1, 8)) { InitModbus(type); ModbusReportError(0x3, type); continue; } } else continue; } if ((((*address + *longth) << 1) > MODBUS_REG4_SIZE) || (*longth > 125)) { InitModbus(type); ModbusReportError(0x3, type); } else { if (type == 1) { g_modbusSendBuf++; } g_modbusSendBuf[0] = 0x03; g_modbusSendBuf[1] = *longth << 1; memcpy(g_modbusSendBuf+2, g_modbusReg4+(*address << 1), (*longth << 1)); tempbuf[0] = g_modbusSendBuf[2]; tempbuf[1] = g_modbusSendBuf[3]; tempbuf[2] = g_modbusSendBuf[4]; tempbuf[3] = g_modbusSendBuf[5]; g_modbusSendBuf[2] = tempbuf[1]; g_modbusSendBuf[3] = tempbuf[0]; g_modbusSendBuf[4] = tempbuf[3]; g_modbusSendBuf[5] = tempbuf[2]; *g_modbusSendBufNumber = (*longth << 1) + 2; // ModbusSendData(type); TcpSendData(g_modbusSendBuf, *g_modbusSendBufNumber); // InitModbus(type); InitModbusTcp(); } } continue; } // 最多接收5个 if (g_modbusReceiveBuf[0] == 0x06) {//write single if (*g_modbusReceiveBufNumber > 4) { if (type == 1) { if (*g_modbusReceiveBufNumber > 6) { if (usMBCRC16(g_modbusReceiveBuf - 1, 8)) { InitModbus(type); ModbusReportError(0x6, type); continue; } } else continue; } *address = g_modbusReceiveBuf[2] + ((int16u)(g_modbusReceiveBuf[1]) << 8); if ((*address << 1) >= MODBUS_REG4_SIZE) { InitModbus(type); ModbusReportError(0x6, type); } else { if (type == 1) { g_modbusSendBuf++; } // 更新寄存器数据,MODBUS 寄存器显示的是高位在后 需要逆转一下 g_modbusReg4[((*address) << 1)] = g_modbusReceiveBuf[4]; g_modbusReg4[((*address) << 1) + 1] = g_modbusReceiveBuf[3]; TcpSendData(g_modbusReceiveBuf, 5); // InitModbus(type); InitModbusTcp(); } } continue; } // 写多个寄存器 if (g_modbusReceiveBuf[0] == 0x10) {//write mul if (*g_modbusReceiveBufNumber > 4 && *longth == 0) { *address = g_modbusReceiveBuf[2] + ((int16u)(g_modbusReceiveBuf[1]) << 8); *longth = g_modbusReceiveBuf[4] + ((int16u)(g_modbusReceiveBuf[3]) << 8); if (((*address + *longth) << 1) > MODBUS_REG4_SIZE || *longth > 0x7d) { InitModbus(type); ModbusReportError(0x10, type); } } else { if (*g_modbusReceiveBufNumber >= ((*longth << 1) + 6)) { if (type == 1) { if (*g_modbusReceiveBufNumber >= ((*longth << 1) + 8)) { if (usMBCRC16(g_modbusReceiveBuf - 1, *g_modbusReceiveBufNumber + 3)) { InitModbus(type); ModbusReportError(0x10, type); continue; } } else continue; } tempbuf[0] = g_modbusReceiveBuf[6]; tempbuf[1] = g_modbusReceiveBuf[7]; tempbuf[2] = g_modbusReceiveBuf[8]; tempbuf[3] = g_modbusReceiveBuf[9]; g_modbusReceiveBuf[7] = tempbuf[0]; g_modbusReceiveBuf[6] = tempbuf[1]; g_modbusReceiveBuf[9] = tempbuf[2]; g_modbusReceiveBuf[8] = tempbuf[3]; for(i = 0; i < (*longth << 1); i++) { g_modbusReg4[(*address << 1) + i] = g_modbusReceiveBuf[6 + i]; } if (type == 1) g_modbusSendBuf++; TcpSendData(g_modbusReceiveBuf, 5); InitModbusTcp(); // InitModbus(type); } } continue; } // 扩展指令 if (g_modbusReceiveBuf[0] == 0x41) { if (*g_modbusReceiveBufNumber > 4) { *address = g_modbusReceiveBuf[2] + ((int16u)(g_modbusReceiveBuf[1]) << 8); *longth = g_modbusReceiveBuf[4] + ((int16u)(g_modbusReceiveBuf[3]) << 8); //sprintf(tempbuf, "%d %d %d %d\r\n", address, longth, ((int16u)(g_modbusReceiveBuf[1]) << 8), ((int16u)(g_modbusReceiveBuf[3]) << 8)); //DebugLog(tempbuf); if (((*address + *longth) << 1) > MODBUS_REG5_SIZE || *longth > 0x7d) { InitModbus(type); ModbusReportError(0x41, type); } else { if (type == 1) { g_modbusSendBuf++; } g_modbusSendBuf[0] = 0x41; g_modbusSendBuf[1] = *longth << 1; for(i = 0; i < (*longth << 1); i++) { g_modbusSendBuf[i + 2] = g_modbusReg5[(*address << 1) + i]; } *g_modbusSendBufNumber = (*longth << 1) + 2; ModbusSendData(type); InitModbus(type); } } continue; } if (g_modbusReceiveBuf[0] == 0x42) { if (*g_modbusReceiveBufNumber > 4 && *longth == 0) { *address = g_modbusReceiveBuf[2] + ((int16u)(g_modbusReceiveBuf[1]) << 8); *longth = g_modbusReceiveBuf[4] + ((int16u)(g_modbusReceiveBuf[3]) << 8); if (((*address + *longth) << 1) > MODBUS_REG5_SIZE || *longth > 0x7d) { InitModbus(type); ModbusReportError(0x42, type); } } else if (*g_modbusReceiveBufNumber == ((*longth << 1) + 6)) { for(i = 0; i < (*longth << 1); i++) { g_modbusReg5[(*address << 1) + i] = g_modbusReceiveBuf[6 + i]; } if (type == 1) { g_modbusSendBuf++; } g_modbusSendBuf[0] = 0x42; g_modbusSendBuf[1] = g_modbusReceiveBuf[1]; g_modbusSendBuf[2] = g_modbusReceiveBuf[2]; g_modbusSendBuf[3] = g_modbusReceiveBuf[3]; g_modbusSendBuf[4] = g_modbusReceiveBuf[4]; *g_modbusSendBufNumber = 5; ModbusSendData(type); InitModbus(type); } continue; } if (g_modbusReceiveBuf[0] == 0x43) { if (*g_modbusReceiveBufNumber > 4) { *address = g_modbusReceiveBuf[2] + ((int16u)(g_modbusReceiveBuf[1]) << 8); *longth = g_modbusReceiveBuf[4] + ((int16u)(g_modbusReceiveBuf[3]) << 8); if (((*address + *longth) << 1) > MODBUS_REG6_SIZE || *longth > 0x7d) { InitModbus(type); ModbusReportError(0x43, type); } else { if (type == 1) { g_modbusSendBuf++; } g_modbusSendBuf[0] = 0x43; g_modbusSendBuf[1] = *longth << 1; for(i = 0; i < (*longth << 1); i++) { g_modbusSendBuf[i + 2] = g_modbusReg6[(*address << 1) + i]; } *g_modbusSendBufNumber = (*longth << 1) + 2; ModbusSendData(type); InitModbus(type); } } continue; } if (g_modbusReceiveBuf[0] == 0x44) { if (*g_modbusReceiveBufNumber > 4 && *longth == 0) { *address = g_modbusReceiveBuf[2] + ((int16u)(g_modbusReceiveBuf[1]) << 8); *longth = g_modbusReceiveBuf[4] + ((int16u)(g_modbusReceiveBuf[3]) << 8); if (((*address + *longth) << 1) > MODBUS_REG6_SIZE || *longth > 0x7d) { InitModbus(type); ModbusReportError(0x44, type); } } else if (*g_modbusReceiveBufNumber == ((*longth << 1) + 6)) { for(i = 0; i < (*longth << 1); i++) { g_modbusReg6[(*address << 1) + i] = g_modbusReceiveBuf[6 + i]; } if (type == 1) { g_modbusSendBuf++; } g_modbusSendBuf[0] = 0x44; g_modbusSendBuf[1] = g_modbusReceiveBuf[1]; g_modbusSendBuf[2] = g_modbusReceiveBuf[2]; g_modbusSendBuf[3] = g_modbusReceiveBuf[3]; g_modbusSendBuf[4] = g_modbusReceiveBuf[4]; *g_modbusSendBufNumber = 5; ModbusSendData(type); InitModbus(type); } continue; } ModbusReportError(g_modbusReceiveBuf[0], type); InitModbus(type); continue; } return MODBUS_OK; }
int CTask::CommuWithAgent(MSG_ID id) { char sendBuf[2048] = {0}; char rcvBuf[2048] = {0}; char *pSendBuf = sendBuf; char *pRcvBuf = rcvBuf; char agentIp[32] = {0}; int agentPort = ATFA_PORT; int ret = 0; MsgHead stHead; MsgHead *pMsgHead = NULL; int iRcvLen = 0; /*if(ATF_FAIL == m_pQuoteSvr->GotAgentIp(agentIp)) { g_log.error("Unknown ATF agent IP !)\n"); return ATF_FAIL; }*/ strncpy(agentIp,m_agentConf.agentIP,sizeof(agentIp)); agentPort = m_agentConf.agentPort; m_agentSocket = TcpConnect(agentIp,agentPort); if(m_agentSocket < 0) { g_log.error("Failed to connect ATF agent(ip:%s, port:%d)\n",agentIp,agentPort); return ATF_FAIL; } switch(id) { case MSGID_S2C_REQ_TRADE_PROCESS_STATIC_TIME: { stHead.iMsgID = htons(MSGID_S2C_REQ_TRADE_PROCESS_STATIC_TIME); stHead.iMsgBodyLen = htons(0); pSendBuf = sendBuf; memcpy(pSendBuf, &stHead, sizeof(stHead)); int iSendLen = sizeof(stHead); ret = TcpSendData(m_agentSocket,(char*)pSendBuf,iSendLen); if(iSendLen != ret) { g_log.error("Send MSGID_S2C_REQ_TRADE_PROCESS_STATIC_TIME error,expect %dB,actual %dB\n",iSendLen,ret); return ATF_FAIL; } g_log.info("Send MSGID_S2C_REQ_TRADE_PROCESS_STATIC_TIME %dB\n",ret); //recv msg head iRcvLen = sizeof(MsgHead); ret = TcpRecvData(m_agentSocket,pRcvBuf,iRcvLen); if(ret != iRcvLen) { g_log.error("[%s]Recv msg head error,expect %dB, actual %dB!\n",__FUNCTION__,iRcvLen,ret); return ATF_FAIL; } g_log.info("[%s]Recv msg head %dB\n",__FUNCTION__,ret); pMsgHead = (MsgHead *)pRcvBuf; if(MSGID_C2S_RSP_TRADE_PROCESS_STATIC_TIME == ntohs(pMsgHead->iMsgID)) { //rcv msg body iRcvLen = sizeof(m_stTradeProcTimeStatic); ret = TcpRecvData(m_agentSocket,pRcvBuf,iRcvLen); if(ret != iRcvLen) { g_log.error("[%s]Recv msg body error,expect %dB, actual %dB!\n",__FUNCTION__,iRcvLen,ret); return ATF_FAIL; } g_log.info("[%s]Recv msg %dB\n",__FUNCTION__,ret); InfoTradeProcessTime *pInfo = (InfoTradeProcessTime *)pRcvBuf; m_stTradeProcTimeStatic.avgTimeTradeProcess = pInfo->avgTimeTradeProcess; m_stTradeProcTimeStatic.maxTimeTradeProcess = pInfo->maxTimeTradeProcess; m_stTradeProcTimeStatic.mixTimeTradeProcess = pInfo->mixTimeTradeProcess; m_stTradeProcTimeStatic.tradeNum = pInfo->tradeNum; return ATF_SUCC; } else { g_log.error("[%s]Expect msg %d, actual %d!\n",__FUNCTION__,MSGID_C2S_RSP_TRADE_PROCESS_STATIC_TIME,pMsgHead->iMsgID); return ATF_FAIL; } } break; case MSGID_S2C_REQ_QUOTE_INTERVAL_STATIC_TIME: { stHead.iMsgID = htons(MSGID_S2C_REQ_QUOTE_INTERVAL_STATIC_TIME); stHead.iMsgBodyLen = htons(0); pSendBuf = sendBuf; memcpy(pSendBuf, &stHead, sizeof(stHead)); int iSendLen = sizeof(stHead); ret = TcpSendData(m_agentSocket,(char*)pSendBuf,iSendLen); if(iSendLen != ret) { g_log.error("Send MSGID_S2C_REQ_QUOTE_INTERVAL_STATIC_TIME error,expect %dB,actual %dB\n",iSendLen,ret); return ATF_FAIL; } g_log.info("Send MSGID_S2C_REQ_QUOTE_INTERVAL_STATIC_TIME %dB\n",ret); //recv msg head iRcvLen = sizeof(MsgHead); ret = TcpRecvData(m_agentSocket,pRcvBuf,iRcvLen); if(ret != iRcvLen) { g_log.error("[%s]Recv msg head error,expect %dB, actual %dB!\n",__FUNCTION__,iRcvLen,ret); return ATF_FAIL; } g_log.info("[%s]Recv msg head %dB\n",__FUNCTION__,ret); pMsgHead = (MsgHead *)pRcvBuf; if(MSGID_C2S_RSP_QUOTE_INTERVAL_STATIC_TIME == ntohs(pMsgHead->iMsgID)) { //rcv msg body iRcvLen = sizeof(StaticQuoteInterval_t); ret = TcpRecvData(m_agentSocket,pRcvBuf,iRcvLen); if(ret != iRcvLen) { g_log.error("[%s]Recv msg body error,expect %dB, actual %dB!\n",__FUNCTION__,iRcvLen,ret); return ATF_FAIL; } g_log.info("[%s]Recv msg %dB\n",__FUNCTION__,ret); StaticQuoteInterval_t *pInfo = (StaticQuoteInterval_t *)pRcvBuf; m_stQuoteIntervalStatic.avgQuoteInterval = pInfo->avgQuoteInterval; m_stQuoteIntervalStatic.maxQuoteInterval = pInfo->maxQuoteInterval; m_stQuoteIntervalStatic.minQuoteInterval = pInfo->minQuoteInterval; m_stQuoteIntervalStatic.quoteNum = pInfo->quoteNum; return ATF_SUCC; } else { g_log.error("[%s]Expect msg %d, actual %d!\n",__FUNCTION__,MSGID_C2S_RSP_QUOTE_INTERVAL_STATIC_TIME,pMsgHead->iMsgID); return ATF_FAIL; } } break; case MSGID_S2C_REDDY_FOR_PERFORM_TEST: { stHead.iMsgID = htons(MSGID_S2C_REDDY_FOR_PERFORM_TEST); stHead.iMsgBodyLen = htons(0); pSendBuf = sendBuf; memcpy(pSendBuf, &stHead, sizeof(stHead)); int iSendLen = sizeof(stHead); ret = TcpSendData(m_agentSocket,(char*)pSendBuf,iSendLen); if(iSendLen != ret) { g_log.error("Send MSGID_S2C_REDDY_FOR_PERFORM_TEST error,expect %dB,actual %dB\n",iSendLen,ret); return ATF_FAIL; } g_log.info("Send MSGID_S2C_REDDY_FOR_PERFORM_TEST %dB\n",ret); return ATF_SUCC; } break; default: { } break; } close(m_agentSocket); return ATF_FAIL; }
//除了一档行情需要调用ctp的login接口外,别的行情不需要进行相关的login/logout工作。 void CProvider::HandleMsgFromClient() { int ret = 0; int rcvMsgSize = 0; char msgName[64] = {0}; char *pBuf = m_tcpbuf; if(m_clientSock < 0) return; memset(m_curOpErrReason,0,sizeof(m_curOpErrReason)); OP &rcvOpe = *m_iterCur; ret = MsgIDToMsgName(rcvOpe.msgID,msgName,sizeof(msgName)); if(ATF_FAIL == ret) return; if(RCV == rcvOpe.direction) { int expectLen = GotMsgSize(rcvOpe.msgID); if(ATF_FAIL == expectLen) return; rcvMsgSize = TcpRecvData(m_clientSock, pBuf,expectLen); if(rcvMsgSize < 1) { g_log.error("[QuoteServer]TCP rcv %s error,expect %dB, actual %dB.\n",msgName,rcvOpe.msgSize,rcvMsgSize); rcvOpe.result = OPERATE_FAIL; sprintf(rcvOpe.errReason,"%s: TCP rcv error, expect %dB, actual %dB.",msgName,rcvOpe.msgSize,rcvMsgSize); m_iterCur = m_iterEnd - 2; } else { if(ATF_FAIL == CheckMsg(rcvOpe.pMsg, rcvOpe.msgSize, m_tcpbuf, rcvMsgSize)) { rcvOpe.result = OPERATE_FAIL; sprintf(rcvOpe.errReason,"%s",m_curOpErrReason); m_iterCur = m_iterEnd - 2; } else { rcvOpe.result = OPERATE_SUCC; g_log.info("[QuoteServer]Receive %s(%dB), right.\n",msgName,rcvMsgSize); if(IS_DEBUG) { printf_green("[QuoteServer]Receive %s(%dB), right.\n",msgName,rcvMsgSize); } } } m_iterCur++; while(m_iterCur != m_iterEnd) { memset(m_curOpErrReason,0,sizeof(m_curOpErrReason)); OP &sendOpe = *m_iterCur; MsgIDToMsgName(sendOpe.msgID,msgName,sizeof(msgName)); if(SEND == sendOpe.direction) { if(MSGID_S2C_QUOTE_RSP_LOGIN == sendOpe.msgID) { QuoteRspUserLoginFromATF *pRsp = (QuoteRspUserLoginFromATF *)(sendOpe.pMsg + sizeof(MsgHead)); FillMsgOnRspLogin(pRsp); } else if(MSGID_S2C_QUOTE_DISCONNECT == sendOpe.msgID) { m_connect = DISCONNECTED; m_login = NOT_LOGIN; m_sessionID++; } else if(MSGID_S2C_QUOTE_RSP_SUBSCRIBE == sendOpe.msgID) { RspSubMarketFromATF *pRsp = (RspSubMarketFromATF *)(sendOpe.pMsg + sizeof(MsgHead)); strcpy(pRsp->specificInstrument.InstrumentID,m_acInstrSubReq); printf_blue("InstrumentID:%s,%s\n,",m_acInstrSubReq,pRsp->specificInstrument.InstrumentID); } else if(MSGID_S2C_QUOTE_RSP_UNSUBSCRIBE == sendOpe.msgID) { RspUnsubMarketFromATF *pRsp = (RspUnsubMarketFromATF *)(sendOpe.pMsg + sizeof(MsgHead)); strcpy(pRsp->specificInstrument.InstrumentID,m_acInstrUnsubReq); printf_blue("InstrumentID:%s,%s\n,",m_acInstrUnsubReq,pRsp->specificInstrument.InstrumentID); } ret = TcpSendData(m_clientSock,sendOpe.pMsg,sendOpe.msgSize); if(ret != sendOpe.msgSize) { g_log.error("[QuoteServer]TCP send error,expect %dB, actual %dB.\n",sendOpe.msgSize,ret); sendOpe.result = OPERATE_FAIL; sprintf(sendOpe.errReason,"%s: TCP send error",msgName); m_iterCur = m_iterEnd - 2; } else { sendOpe.result = OPERATE_SUCC; g_log.info("[QuoteServer]Send %s(%dB).\n",msgName,ret); if(IS_DEBUG) { printf_green("[QuoteServer]Send %s(%dB)\n",msgName,rcvMsgSize); } } if(MSGID_S2C_QUOTE_RSP_CONN == sendOpe.msgID) m_connect = CONNECTED; if(DISCONNECTED == m_connect) CloseFd(); m_iterCur++; } else if(LOCAL == sendOpe.direction) { if(IS_DEBUG) { printf_green("[QuoteServer]handle %s\n",msgName); } if(MSGID_LOCAL_SLEEP == sendOpe.msgID) { SleepTime *pMsgBody = (SleepTime *)(sendOpe.pMsg + sizeof(MsgHead)); sleep(pMsgBody->time); sendOpe.result = OPERATE_SUCC; } m_iterCur++; } else if(RCV == sendOpe.direction) { return; } else { g_log.error("[QuoteServer]op.direction is not defined !\n"); sendOpe.result = OPERATE_FAIL; sprintf(sendOpe.errReason,"%s: op.direction is not defined.",msgName); m_iterCur = m_iterEnd - 2; } if(m_iterCur == m_iterEnd) { m_iProgress = DONE; printf_blue("provider %u had finished his job.\n",m_frontID); //g_log.info("[quoteServer]task done.\n"); } } } else return; }
int CUstpFtdcMduserApi::HandleUnsubmarket() { int ret = 0; char sendBuf[BUFSIZE] = {0}; char rcvBuf[BUFSIZE] = {0}; MsgHead stHead; int iSendLen = 0; int iExpectRcvLen = 0; char * pSendBuf = NULL; char * pRcvBuf = NULL; MsgHead *pMsg = NULL; RspUnsubMarketFromATF stRsp; RspUnsubMarketFromATF *pRsp = NULL; //no a instrument has been subscribed if(NULL == m_pInstrSubSucc) { g_log.error("No an instrument subscribed !\n"); strcpy(stRsp.specificInstrument.InstrumentID,m_acInstrUnsubReq); stRsp.rspInfo.ErrorID = 16; pRsp = &stRsp; } else { SubscribeInstrument *pTmp = m_pInstrSubSucc; while(NULL != pTmp) { //the instrument unsubscribed has been subscribed if(0 == strcasecmp(pTmp->instrument,m_acInstrUnsubReq)) { //send msg to ATF ReqUnsubMarketToATF unsubInfo; strcpy(unsubInfo.instr,m_acInstrUnsubReq); stHead.iMsgID = htons(MSGID_C2S_QUOTE_REQ_UNSUBSCRIBE); stHead.iMsgBodyLen = htons(sizeof(unsubInfo)); pSendBuf = sendBuf; memcpy(pSendBuf , &stHead, sizeof(stHead)); memcpy(pSendBuf+sizeof(stHead),&unsubInfo,sizeof(unsubInfo)); iSendLen = sizeof(stHead) + sizeof(unsubInfo); ret = TcpSendData(m_fdConnQuoteSvr,(char*)pSendBuf,iSendLen); if(ret != iSendLen) { g_log.error("[STATUS_QUOTE_REQ_UNSUBSCRIBE]TCP send error,expect %dB,actual %dB.\n",iSendLen,ret); return ATF_FAIL; } g_log.debug("[STATUS_QUOTE_REQ_UNSUBSCRIBE]TCP send %dB \n",ret); //rcv msg from ATF iExpectRcvLen = sizeof(MsgHead) + sizeof(RspUnsubMarketFromATF); pRcvBuf = rcvBuf; ret = TcpRecvData(m_fdConnQuoteSvr,pRcvBuf,iExpectRcvLen); if(ret != iExpectRcvLen) { g_log.error("[STATUS_QUOTE_REQ_UNSUBSCRIBE]TCP rcv error,expect %dB,actual %dB.\n",iExpectRcvLen,ret); return ATF_FAIL; } g_log.debug("[STATUS_QUOTE_REQ_UNSUBSCRIBE]TCP rcv %dB \n",iExpectRcvLen); pMsg = (MsgHead *)pRcvBuf; if(MSGID_S2C_QUOTE_RSP_UNSUBSCRIBE == ntohs(pMsg->iMsgID)) { pRsp = (RspUnsubMarketFromATF *)(pRcvBuf + sizeof(MsgHead)); if(0 == pRsp->rspInfo.ErrorID) { //remove the instrument unsubscribed from m_pInstrSubSucc RemoveInstr(&pRsp->specificInstrument); } } else { g_log.error("[STATUS_QUOTE_REQ_UNSUBSCRIBE]TCP rcv error,expect %dB,actual %dB.\n",MSGID_S2C_QUOTE_RSP_UNSUBSCRIBE,ntohs(pMsg->iMsgID)); strcpy(stRsp.specificInstrument.InstrumentID,m_acInstrUnsubReq); stRsp.rspInfo.ErrorID = 16; pRsp = &stRsp; } break; } else { pTmp = pTmp->pNext; } } //the instrument unsubscribed had not been found in the subscribe list if(NULL == pTmp) { strcpy(stRsp.specificInstrument.InstrumentID,m_acInstrUnsubReq); stRsp.rspInfo.ErrorID = 16; pRsp = &stRsp; g_log.error("the instrument unsubscribed had not been found in the subscribe list !\n"); } } //PrintInstr(); //return rsp to trader3.0 m_FtdcMdSpi_instance->OnRspUnSubMarketData(&pRsp->specificInstrument,&pRsp->rspInfo,m_iRequestID,true); return ATF_SUCC; }
int CUstpFtdcMduserApi::HandleSubmarket() { int ret = 0; char sendBuf[BUFSIZE] = {0}; char rcvBuf[BUFSIZE] = {0}; MsgHead stHead; int iSendLen = 0; int iExpectRcvLen = 0; char * pSendBuf = NULL; char * pRcvBuf = NULL; MsgHead *pMsg = NULL; //send msg to ATF ReqSubMarketToATF subInfo; strcpy(subInfo.instr,m_acInstrSubReq); stHead.iMsgID = htons(MSGID_C2S_QUOTE_REQ_SUBSCRIBE); stHead.iMsgBodyLen = htons(sizeof(subInfo)); pSendBuf = sendBuf; memcpy(pSendBuf , &stHead, sizeof(stHead)); memcpy(pSendBuf+sizeof(stHead),&subInfo,sizeof(subInfo)); iSendLen = sizeof(stHead) + sizeof(subInfo); ret = TcpSendData(m_fdConnQuoteSvr,(char*)pSendBuf,iSendLen); if(ret != iSendLen) { g_log.error("[STATUS_QUOTE_REQ_SUBSCRIBE]TCP send error,expect %dB,actual %dB.\n",iSendLen,ret); return ATF_FAIL; } g_log.debug("[STATUS_QUOTE_REQ_SUBSCRIBE]TCP send %dB \n",ret); //rcv msg from ATF iExpectRcvLen = sizeof(MsgHead) + sizeof(RspSubMarketFromATF); pRcvBuf = rcvBuf; ret = TcpRecvData(m_fdConnQuoteSvr,pRcvBuf,iExpectRcvLen); if(ret != iExpectRcvLen) { g_log.error("[STATUS_QUOTE_REQ_SUBSCRIBE]TCP rcv error,expect %dB,actual %dB.\n",iExpectRcvLen,ret); return ATF_FAIL; } g_log.debug("[STATUS_QUOTE_REQ_SUBSCRIBE]TCP rcv %dB \n",iExpectRcvLen); pRcvBuf = rcvBuf; pMsg = (MsgHead *)pRcvBuf; if(MSGID_S2C_QUOTE_RSP_SUBSCRIBE == ntohs(pMsg->iMsgID)) { RspSubMarketFromATF *pRsp = (RspSubMarketFromATF *)(pRcvBuf + sizeof(MsgHead)); if(0 == pRsp->rspInfo.ErrorID) { //对于同时订阅多个合约的场景 //目前采用从一个单合约文件中读取行情,每个行情数据被发送多次,每次根据订阅信息改变合约名,发送给trader3.0 //仅根据第一次收到的行情参数来进行配置 if((false == m_bQuoteReady)) { if(ATF_SUCC == ParseQuotePara(pRsp->QuoteSendPara)) { m_bQuoteReady = true; QuoteProc(); } } //insert new instrument to m_pInstrSubSucc InsertNewInstr(&pRsp->specificInstrument); } m_FtdcMdSpi_instance->OnRspSubMarketData(&pRsp->specificInstrument,&pRsp->rspInfo,m_iRequestID,true); return ATF_SUCC; } else { g_log.error("[STATUS_QUOTE_REQ_SUBSCRIBE]TCP rcv error,expect %dB,actual %dB.\n",MSGID_S2C_QUOTE_RSP_SUBSCRIBE,ntohs(pMsg->iMsgID)); return ATF_FAIL; } }
int CUstpFtdcMduserApi::HandleLogin() { int ret = 0; char sendBuf[BUFSIZE] = {0}; char rcvBuf[BUFSIZE] = {0}; MsgHead stHead; int iSendLen = 0; int iExpectRcvLen = 0; char * pSendBuf = NULL; char * pRcvBuf = NULL; MsgHead *pMsg = NULL; //send msg to ATF QuoteReqUserLoginToATF * pReqUserLoginField = &m_userLoginField; stHead.iMsgID = htons(MSGID_C2S_QUOTE_REQ_LOGIN); stHead.iMsgBodyLen = htons(sizeof(*pReqUserLoginField)); pSendBuf = sendBuf; memcpy(pSendBuf, &stHead, sizeof(stHead)); memcpy(pSendBuf+sizeof(stHead),pReqUserLoginField,sizeof(*pReqUserLoginField)); iSendLen = sizeof(stHead) + sizeof(*pReqUserLoginField); ret = TcpSendData(m_fdConnQuoteSvr,pSendBuf,iSendLen); if(ret != iSendLen) { g_log.error("[STATUS_QUOTE_REQ_LOGIN]TCP send error,expect %dB, actual %dB !\n",iSendLen,ret); return ATF_FAIL; } g_log.debug("[STATUS_QUOTE_REQ_LOGIN]TCP send %dB.\n",iSendLen); //rcv msg from ATF pRcvBuf = rcvBuf; iExpectRcvLen = sizeof(MsgHead) + sizeof(QuoteRspUserLoginFromATF); ret = TcpRecvData(m_fdConnQuoteSvr,pRcvBuf,iExpectRcvLen); if(ret != iExpectRcvLen) { g_log.error("[STATUS_QUOTE_REQ_LOGIN]TCP rcv error,expect %dB, actual %dB !\n",iExpectRcvLen,ret); return ATF_FAIL; } g_log.debug("[STATUS_QUOTE_REQ_LOGIN]TCP rcv %dB.\n",ret); pMsg = (MsgHead *)pRcvBuf; if(MSGID_S2C_QUOTE_RSP_LOGIN == ntohs(pMsg->iMsgID)) { QuoteRspUserLoginFromATF *pRsp = (QuoteRspUserLoginFromATF *)(pRcvBuf + sizeof(MsgHead)); m_FtdcMdSpi_instance->OnRspUserLogin(&pRsp->rspUserLogin,&pRsp->rspInfo,pReqUserLoginField->nRequestID, true); m_bLogined = true; /*if(true == m_autoReconn) SetState(STATUS_QUOTE_REQ_SUBSCRIBE);*/ return ATF_SUCC; } else { g_log.debug("[STATUS_QUOTE_REQ_LOGIN]msgID error, %d,%d\n",pMsg->iMsgID,ntohs(pMsg->iMsgID)); return ATF_FAIL; } }
int CUstpFtdcMduserApi::HandleInit() { int ret = 0; char sendBuf[BUFSIZE] = {0}; char rcvBuf[BUFSIZE] = {0}; MsgHead stHead; int iSendLen = 0; int iExpectRcvLen = 0; char * pSendBuf = NULL; char * pRcvBuf = NULL; MsgHead *pMsg = NULL; m_fdConnQuoteSvr = TcpConnect(m_quoteSvrIP,m_shListenPort); if(m_fdConnQuoteSvr < 0) { g_log.error("Failed to connect QUOTE SERVER(ip:%s, port:%d, socket:%d)\n",m_quoteSvrIP,m_shListenPort,m_fdConnQuoteSvr); sleep(5);//客户端与服务端连接断开后,交易接口会自动尝试重新连接,频率是每5 秒一次。 return ATF_FALSE; } g_log.info("Succeed to connect QUOTE SERVER(ip:%s, port:%d, socket:%d)\n",m_quoteSvrIP,m_shListenPort,m_fdConnQuoteSvr); //send msg to ATF stHead.iMsgID = htons(MSGID_C2S_QUOTE_REQ_CONN); stHead.iMsgBodyLen = htons(0); pSendBuf = sendBuf; iSendLen = sizeof(stHead); memcpy(pSendBuf, &stHead, iSendLen); ret = TcpSendData(m_fdConnQuoteSvr,(char*)pSendBuf,iSendLen); if(ret != iSendLen) { g_log.error("[STATUS_QUOTE_INIT]TCP send error! \n"); return ATF_FAIL; } g_log.debug("[STATUS_QUOTE_INIT]TCP send out %d bytes\n",ret); //receive msg from ATF iExpectRcvLen = sizeof(MsgHead) + sizeof(RspInfoFromATF); pRcvBuf = rcvBuf; ret = TcpRecvData(m_fdConnQuoteSvr,pRcvBuf,iExpectRcvLen); if(ret != iExpectRcvLen) { g_log.error("[STATUS_QUOTE_INIT]TCP receive error! \n"); return ATF_FAIL; } g_log.debug("[STATUS_QUOTE_INIT]recv rsp %d bytes\n",ret); pMsg = (MsgHead *)pRcvBuf; if(MSGID_S2C_QUOTE_RSP_CONN == ntohs(pMsg->iMsgID)) { RspInfoFromATF *pRsp = (RspInfoFromATF *)(pRcvBuf + sizeof(MsgHead)); //tell client if(0 == pRsp->ErrorID) { m_FtdcMdSpi_instance->OnFrontConnected(); //if(true == m_autoReconn) //SetState(STATUS_QUOTE_REQ_LOGIN); } else m_FtdcMdSpi_instance->OnFrontDisconnected(-1); return ATF_SUCC; } else { g_log.debug("[STATUS_QUOTE_INIT]msgID error, %d,%d\n",pMsg->iMsgID,ntohs(pMsg->iMsgID)); return ATF_FAIL; } }
void CUstpFtdcMduserApi::SendOneQuote(int sig, siginfo_t *si, void *uc) { int ret = 0; int iOldErr; int iOverRun; char sendBuf[BUFSIZE] = {0}; MsgHead stHead; int iSendLen = 0; char * pSendBuf = NULL; CUstpFtdcDepthMarketDataField data; //在信号处理函数开始时保存errno iOldErr = errno; CUstpFtdcMduserApi* pObj = (CUstpFtdcMduserApi*)si->si_value.sival_ptr; iOverRun = timer_getoverrun(pObj->m_timerid); if (-1 == iOverRun) { printf_blue("timer_getoverrun() return %d.\n",iOverRun);//g_log.error } else { printf_blue("timer_getoverrun() return %d.\n",iOverRun); } //no instrument subscribed if(NULL == pObj->m_pInstrSubSucc) { errno = iOldErr; //在信号处理函数结束的时候恢复被中断线程的 errno 值 g_log.error("No instrument subscribed !\n"); return ; } if(ATF_SUCC == pObj->m_QuoteQueue.PopOneData(&data)) { AddQuoteCnt(); SubscribeInstrument *pTmp = pObj->m_pInstrSubSucc; while(pTmp != NULL) { printf_green("ready to send a quote\n"); strcpy(data.InstrumentID,pTmp->instrument); pObj->m_FtdcMdSpi_instance->OnRtnDepthMarketData(&data); pTmp = pTmp->pNext; } } else { //read quote over if((1 == pObj->m_quoteFetcher.GetFlagQuoteReadOver())) { if(true == pObj->m_bQuoteOver) { errno = iOldErr; //在信号处理函数结束的时候恢复被中断线程的 errno 值 return; } printf_green("read quote over\n"); pObj->m_bQuoteOver = true; pObj->m_quoteFetcher.Terminate(); memset(sendBuf,0,sizeof(sendBuf)); memset(&stHead,0,sizeof(stHead)); //send msg to ATF ResultOfSendQuote result; result.isSucc = true; pSendBuf = sendBuf; stHead.iMsgID = htons(MSGID_C2S_QUOTE_RESULT_SEND_QUOTE); stHead.iMsgBodyLen = htons(sizeof(ResultOfSendQuote)); memcpy(pSendBuf , &stHead, sizeof(stHead)); memcpy(pSendBuf+sizeof(stHead),&result,sizeof(result)); iSendLen = sizeof(stHead) + sizeof(result); ret = TcpSendData(pObj->m_fdConnQuoteSvr,(char*)pSendBuf,iSendLen); if(ret != iSendLen) { g_log.error("[MSGID_C2S_QUOTE_RESULT_SEND_QUOTE]TCP send error,expect %dB,actual %dB.\n",iSendLen,ret); errno = iOldErr; //在信号处理函数结束的时候恢复被中断线程的 errno 值 return ; } g_log.debug("[MSGID_C2S_QUOTE_RESULT_SEND_QUOTE]TCP send %dB \n",ret); } else { g_log.error("quote pop error!\n"); } } errno = iOldErr; //在信号处理函数结束的时候恢复被中断线程的 errno 值 return ; }