net_func

来源:互联网 发布:tpp ttip 知乎 编辑:程序博客网 时间:2024/05/17 09:45
#include <winsock2.h>  #include <stdio.h> #pragma once struct HQServerInfo{char   szServer[32];short iPort; int iCmdId;}; BOOL WinSockInit(); unsigned int  __stdcall GetHQAvgCostvoid * pInfo  ); unsigned int  __stdcall TestFuncvoid * pInfo  ); unsigned int  __stdcall HQRefreshCheckvoid *pPara );   

#include "stdafx.h" #include <stdio.h>    #include <string.h>    #include <iostream>     #include "StockIo.h"#include "request.h"#include "Channel.h"#include "net_func.h"#include "util.hpp"#include "NBSocket.h" #pragma comment(lib,"ws2_32.lib")     using namespace std;   BOOL WinSockInit()  {  WSADATA data = {0};  if ( WSAStartupMAKEWORD( 2, 2 ), &data ) ) {return FALSE; } if ( LOBYTE(data.wVersion) !=2 || HIBYTE(data.wVersion) != 2 ){  WSACleanup();  return FALSE;  }  return TRUE;  }     unsigned int  __stdcall GetHQAvgCostvoid *pPara )  {CmdQueue_t g_Queue;NBSocket  * pSocket = new NBSocket(); Channel * pChannel = new Channel(); pSocket->SetChannel( pChannel ); HQServerInfo * pInfo = (HQServerInfo *)pPara; //cout << "begin to connect to " << pInfo->szServer << ":" << pInfo->iPort << endl;struct timeval tv;tv.tv_sec = 3;tv.tv_usec = 0; int iRet = pSocket->Connect( pInfo->szServer, pInfo->iPort, &tv ); if ( iRet == 0 ){//cout << "connect succ " << endl;}else{cout << "connect failed " << endl;return -1;} pChannel->SetServerInfo( pInfo );  // just to calculate the total stocks in each market//AddStockListRequest( false ); int iCount = 0; while ( true ){CmdQueue_t &q = g_Queue; if ( q.empty() &&  iCount < 100   ){//CmdData_t r( new GetStockCountReq( (uchar)0x00 ) );vector<QuotePara> vec;QuotePara stPara;stPara.cMarket = 0x00;memcpy( stPara.sStockCode, "000001"sizeof( stPara.sStockCode ) );vec.push_back( stPara );CmdData_t r( new GetStockQuotesReq(  vec ) );q.push_back( r );//cout << "add one GetStockCountReq cmd" << endl;//cout << "add one GetStockQuotesReq cmd" << endl;++iCount;} while ( !q.empty() ){CmdData_t  d = q.front();// retreive a commandd->SetSeqId( pChannel->GetSeqId() );d->send( pSocket ); ReqTimeInfo stInfo;d->SetRequestInfo( stInfo ); pChannel->StartRequest( stInfo ); q.pop_front();} iRet = pSocket->Recv();if ( iRet != 0 ){break;} } delete pInfo;pInfo = NULL; delete pChannel;pChannel = NULL; delete pSocket;pSocket = NULL; return -2;}  void addRequestint iCmdId, CmdQueue_t & q){if ( iCmdId == CMD_GET_STOCK_QUOTES ){vector<QuotePara> vec;QuotePara stPara;stPara.cMarket = 0x00;memcpy( stPara.sStockCode, "000001"sizeof( stPara.sStockCode ) );vec.push_back( stPara );CmdData_t r( new GetStockQuotesReq( vec ) );q.push_back( r );}else if ( iCmdId == CMD_GET_MINUTE_DATA ){CmdData_t r( new GetMinuteTimeDataReq(  1, "600300") );q.push_back( r );}else if ( iCmdId == CMD_GET_HIS_MINUTE_DATA ){CmdData_t r( new GetHisMinuteTimeDataReq(  20171026, 1, "600300") );q.push_back( r );}else if ( iCmdId == CMD_GET_TRANSACTION_DATA ){CmdData_t r( new GetTransactionDataReq(    0, "000001", 0, 30 ) );q.push_back( r );}else if ( iCmdId == CMD_GET_HIS_TRANSACTION_DATA ){CmdData_t r( new GetHisTransactionDataReq(   20171026,  0, "000001", 0, 30 ) );q.push_back( r );}else if ( iCmdId == CMD_GET_INDEX_BARS ){CmdData_t r( new GetIndexBarsReq(   9, 1, "000001", 1, 2 ) );q.push_back( r );}}  unsigned int  __stdcall TestFuncvoid *pPara )  {CmdQueue_t g_Queue;NBSocket  * pSocket = new NBSocket(); Channel * pChannel = new Channel(); pSocket->SetChannel( pChannel ); HQServerInfo * pInfo = (HQServerInfo *)pPara; //cout << "begin to connect to " << pInfo->szServer << ":" << pInfo->iPort << endl;struct timeval tv;tv.tv_sec = 3;tv.tv_usec = 0; //114.67.61.70//int iRet = pSocket->Connect( pInfo->szServer, pInfo->iPort, &tv );int iRet = pSocket->Connect(  "114.67.61.70",  7709, &tv );if ( iRet == 0 ){//cout << "connect succ " << endl;}else{cout << "connect failed " << endl;return -1;} pChannel->SetServerInfo( pInfo );  // just to calculate the total stocks in each market//AddStockListRequest( false ); int iCount = 0; while ( true ){CmdQueue_t &q = g_Queue; if ( q.empty() &&  iCount < 1   ){addRequest( pInfo->iCmdId,  g_Queue );++iCount;} while ( !q.empty() ){CmdData_t  d = q.front();// retreive a commandd->SetSeqId( pChannel->GetSeqId() );d->send( pSocket ); ReqTimeInfo stInfo;d->SetRequestInfo( stInfo );pChannel->StartRequest( stInfo ); q.pop_front();} iRet = pSocket->Recv();if ( iRet != 0 ){break;} } delete pInfo;pInfo = NULL; delete pChannel;pChannel = NULL; delete pSocket;pSocket = NULL; return -2;}   unsigned int  __stdcall HQRefreshCheckvoid *pPara )  {CmdQueue_t g_Queue;NBSocket  * pSocket = new NBSocket(); Channel * pChannel = new Channel(); pSocket->SetChannel( pChannel ); HQServerInfo * pInfo = (HQServerInfo *)pPara; //cout << "begin to connect to " << pInfo->szServer << ":" << pInfo->iPort << endl;struct timeval tv;tv.tv_sec = 3;tv.tv_usec = 0; //114.67.61.70//int iRet = pSocket->Connect( pInfo->szServer, pInfo->iPort, &tv );int iRet = pSocket->Connect(  "114.67.61.70",  7709, &tv );if ( iRet == 0 ){//cout << "connect succ " << endl;}else{cout << "connect failed " << endl;return -1;} pChannel->SetServerInfo( pInfo );  // just to calculate the total stocks in each market//AddStockListRequest( false ); int iCount = 0; while ( true ){CmdQueue_t &q = g_Queue; if ( q.empty()     ){addRequest( pInfo->iCmdId,  g_Queue );} while ( !q.empty() ){CmdData_t  d = q.front();// retreive a commandd->SetSeqId( pChannel->GetSeqId() );d->send( pSocket ); ReqTimeInfo stInfo;d->SetRequestInfo( stInfo );pChannel->StartRequest( stInfo ); q.pop_front();} iRet = pSocket->Recv();if ( iRet != 0 ){break;} Sleep( 1000 ); } delete pInfo;pInfo = NULL; delete pChannel;pChannel = NULL; delete pSocket;pSocket = NULL; return -2;} 

原创粉丝点击