forked from QuantBox/QuantBox_XAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTraderApi.cpp
271 lines (222 loc) · 6.72 KB
/
TraderApi.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#include "stdafx.h"
#include "TraderApi.h"
#include "../include/QueueEnum.h"
#include "../include/QueueHeader.h"
#include "../include/ApiHeader.h"
#include "../include/ApiStruct.h"
#include "../include/toolkit.h"
#include "../QuantBox_Queue/MsgQueue.h"
#include "TypeConvert.h"
#include <cstring>
#include <assert.h>
//客户端实现推送回调函数
void __stdcall OnReadPushData(ETX_APP_FUNCNO FuncNO, void* pEtxPushData)
{
char szmsg[2048] = { 0 };
PST16203PushData pCJData = NULL;
PST16204PushData pCDFDData = NULL;
if (ETX_16203 == FuncNO)
{
pCJData = (PST16203PushData)pEtxPushData;
//cout << pCJData->cust_no << " " << pCJData->order_no << endl;
}
else if (ETX_16204 == FuncNO)
{
//cout << pCDFDData->cust_no << " " << pCDFDData->order_no << endl;
}
else
{
sprintf(szmsg, "无法识别的推送数据[%d]", FuncNO);
printf(szmsg);
}
}
void* __stdcall Query(char type, void* pApi1, void* pApi2, double double1, double double2, void* ptr1, int size1, void* ptr2, int size2, void* ptr3, int size3)
{
// 由内部调用,不用检查是否为空
CTraderApi* pApi = (CTraderApi*)pApi1;
pApi->QueryInThread(type, pApi1, pApi2, double1, double2, ptr1, size1, ptr2, size2, ptr3, size3);
return nullptr;
}
void CTraderApi::QueryInThread(char type, void* pApi1, void* pApi2, double double1, double double2, void* ptr1, int size1, void* ptr2, int size2, void* ptr3, int size3)
{
int iRet = 0;
switch (type)
{
case E_Init:
iRet = _Init();
break;
case E_ReqUserLoginField:
iRet = _ReqUserLogin(type, pApi1, pApi2, double1, double2, ptr1, size1, ptr2, size2, ptr3, size3);
break;
default:
break;
}
if (0 == iRet)
{
//返回成功,填加到已发送池
m_nSleep = 1;
}
else
{
m_msgQueue_Query->Input(type, pApi1, pApi2, double1, double2, ptr1, size1, ptr2, size2, ptr3, size3);
//失败,按4的幂进行延时,但不超过1s
m_nSleep *= 4;
m_nSleep %= 1023;
}
this_thread::sleep_for(chrono::milliseconds(m_nSleep));
}
int CTraderApi::_Init()
{
//网关地址与代理信息由客户自行配置(根据实际情况配置,无则不配置)
//配置网关信息
SPX_API_SetParam(MAINSERVER_IP, m_ServerInfo.Address, &m_err_msg);
SPX_API_SetParam(MAINSERVER_PORT, "8085", &m_err_msg);
//SPX_API_SetParam(BACKSERVER_IP, "127.0.0.1", &m_err_msg);
//SPX_API_SetParam(BACKSERVER_PORT, "17990", &m_err_msg);
//配置代理信息
//SPX_API_SetParam(PROXY_TYPE, "1", &m_err_msg);
//SPX_API_SetParam(PROXY_IP, "127.0.0.1", &m_err_msg);
//SPX_API_SetParam(PROXY_PORT, "9999", &m_err_msg);
//SPX_API_SetParam(PROXY_USER, "", &m_err_msg);
//SPX_API_SetParam(PROXY_PASS, "", &m_err_msg);
STInitPara init_para;
init_para.pOnReadPushData = OnReadPushData;
init_para.bWriteLog = true;
init_para.emLogLevel = LL_INFO;
init_para.nTimeOut = 60000;
m_msgQueue->Input(ResponeType::OnConnectionStatus, m_msgQueue, this, ConnectionStatus::Uninitialized, 0, nullptr, 0, nullptr, 0, nullptr, 0);
bool bRet = SPX_API_Initialize(&init_para, &m_err_msg);
//IsErrorRspInfo(&m_err_msg, 0, true);
if (!bRet)
{
RspUserLoginField field = { 0 };
//连接失败返回的信息是拼接而成,主要是为了统一输出
field.ErrorID = m_err_msg.error_no;
strcpy(field.ErrorMsg, m_err_msg.msg);
m_msgQueue->Input(ResponeType::OnConnectionStatus, m_msgQueue, this, ConnectionStatus::Disconnected, 0, &field, sizeof(RspUserLoginField), nullptr, 0, nullptr, 0);
return 0;
}
m_pApi = SPX_API_CreateHandle(&m_err_msg);
IsErrorRspInfo(&m_err_msg, 0, true);
ReqUserLogin();
return 0;
}
void CTraderApi::ReqUserLogin()
{
STTraderLogin body = { 0 };
strncpy(body.cust_no, m_UserInfo.UserID, sizeof(TCustNoType));
strncpy(body.cust_pwd, m_UserInfo.Password, sizeof(TCustPwdType));
m_msgQueue_Query->Input(RequestType::E_ReqUserLoginField, this, nullptr, 0, 0,
&body, sizeof(STTraderLogin), nullptr, 0, nullptr, 0);
}
int CTraderApi::_ReqUserLogin(char type, void* pApi1, void* pApi2, double double1, double double2, void* ptr1, int size1, void* ptr2, int size2, void* ptr3, int size3)
{
int row_num = 0;
STTraderLoginRsp *p_login_rsp = nullptr;
bool bRet = SPX_API_Login(m_pApi, (STTraderLogin*)ptr1, &p_login_rsp, &row_num, &m_err_msg);
if (bRet && m_err_msg.error_no == 0)
{
if (row_num <= 0)
{
printf("返回结果为空");
}
else if (p_login_rsp != nullptr)
{
for (int i = 0; i<row_num; i++)
{
//cout << p_login_rsp[i].cust_name << " " << p_login_rsp[i].market_code << " " << p_login_rsp[i].holder_acc_no << endl;
}
}
}
//m_msgQueue->Input(ResponeType::OnConnectionStatus, m_msgQueue, this, ConnectionStatus::Logining, 0, nullptr, 0, nullptr, 0, nullptr, 0);
//return m_pApi->ReqUserLogin((CThostFtdcReqUserLoginField*)ptr1, ++m_lRequestID);
return 0;
}
CTraderApi::CTraderApi(void)
{
m_pApi = nullptr;
m_lRequestID = 0;
m_nSleep = 1;
// 自己维护两个消息队列
m_msgQueue = new CMsgQueue();
m_msgQueue_Query = new CMsgQueue();
m_msgQueue_Query->Register(Query);
m_msgQueue_Query->StartThread();
}
CTraderApi::~CTraderApi(void)
{
Disconnect();
}
void CTraderApi::Register(void* pCallback)
{
if (m_msgQueue == nullptr)
return;
m_msgQueue_Query->Register(Query);
m_msgQueue->Register(pCallback);
if (pCallback)
{
m_msgQueue_Query->StartThread();
m_msgQueue->StartThread();
}
else
{
m_msgQueue_Query->StopThread();
m_msgQueue->StopThread();
}
}
bool CTraderApi::IsErrorRspInfo(STRspMsg *pRspInfo, int nRequestID, bool bIsLast)
{
bool bRet = ((pRspInfo) && (pRspInfo->error_no != 0));
if (bRet)
{
ErrorField field = { 0 };
field.ErrorID = pRspInfo->error_no;
strcpy(field.ErrorMsg, pRspInfo->msg);
m_msgQueue->Input(ResponeType::OnRtnError, m_msgQueue, this, bIsLast, 0, &field, sizeof(ErrorField), nullptr, 0, nullptr, 0);
}
return bRet;
}
bool CTraderApi::IsErrorRspInfo(STRspMsg *pRspInfo)
{
bool bRet = ((pRspInfo) && (pRspInfo->error_no != 0));
return bRet;
}
void CTraderApi::Connect(const string& szPath,
ServerInfoField* pServerInfo,
UserInfoField* pUserInfo)
{
m_szPath = szPath;
memcpy(&m_ServerInfo, pServerInfo, sizeof(ServerInfoField));
memcpy(&m_UserInfo, pUserInfo, sizeof(UserInfoField));
m_msgQueue_Query->Input(E_Init, this, nullptr, 0, 0, nullptr, 0, nullptr, 0, nullptr, 0);
}
void CTraderApi::Disconnect()
{
if (m_msgQueue_Query)
{
m_msgQueue_Query->StopThread();
m_msgQueue_Query->Register(nullptr);
m_msgQueue_Query->Clear();
delete m_msgQueue_Query;
m_msgQueue_Query = nullptr;
}
if(m_pApi)
{
SPX_API_DestroyHandle(&m_pApi, &m_err_msg);
m_pApi = nullptr;
// 全清理,只留最后一个
m_msgQueue->Clear();
m_msgQueue->Input(ResponeType::OnConnectionStatus, m_msgQueue, this, ConnectionStatus::Disconnected, 0, nullptr, 0, nullptr, 0, nullptr, 0);
// 主动触发
m_msgQueue->Process();
}
SPX_API_Finalize(&m_err_msg);
if (m_msgQueue)
{
m_msgQueue->StopThread();
m_msgQueue->Register(nullptr);
m_msgQueue->Clear();
delete m_msgQueue;
m_msgQueue = nullptr;
}
}