forked from QuantBox/QuantBox_XAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLevel2UserApi.cpp
462 lines (381 loc) · 9.91 KB
/
Level2UserApi.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
#include "stdafx.h"
#include "Level2UserApi.h"
#include "../include/QueueEnum.h"
#include "../include/QueueHeader.h"
#include "../include/ApiHeader.h"
#include "../include/ApiStruct.h"
#include "../include/toolkit.h"
#include <mutex>
#include <vector>
#include <cstring>
#include <assert.h>
using namespace std;
using namespace DFITC_L2;
CLevel2UserApi::CLevel2UserApi(void)
{
m_pApi = nullptr;
m_msgQueue = nullptr;
m_lRequestID = 0;
}
CLevel2UserApi::~CLevel2UserApi(void)
{
Disconnect();
}
void CLevel2UserApi::StartThread()
{
if (nullptr == m_hThread)
{
m_bRunning = true;
m_hThread = new thread(ProcessThread, this);
}
}
void CLevel2UserApi::StopThread()
{
m_bRunning = false;
if (m_hThread)
{
m_hThread->join();
delete m_hThread;
m_hThread = nullptr;
}
}
void CLevel2UserApi::Register(void* pMsgQueue)
{
m_msgQueue = pMsgQueue;
}
bool CLevel2UserApi::IsErrorRspInfo_Output(struct ErrorRtnField * pErrorField)
{
bool bRet = ((pErrorField) && (pErrorField->ErrorID != 0));
if (bRet)
{
ErrorField field = { 0 };
field.ErrorID = pErrorField->ErrorID;
strcpy(field.ErrorMsg, pErrorField->ErrorMsg);
XRespone(ResponeType::OnRtnError, m_msgQueue, this, true, 0, &field, sizeof(ErrorField), nullptr, 0, nullptr, 0);
}
return bRet;
}
bool CLevel2UserApi::IsErrorRspInfo(struct ErrorRtnField * pErrorField)
{
bool bRet = ((pErrorField) && (pErrorField->ErrorID != 0));
return bRet;
}
void CLevel2UserApi::Connect(const string& szPath,
ServerInfoField* pServerInfo,
UserInfoField* pUserInfo)
{
m_szPath = szPath;
memcpy(&m_ServerInfo, pServerInfo, sizeof(ServerInfoField));
memcpy(&m_UserInfo, pUserInfo, sizeof(UserInfoField));
m_pApi = NEW_CONNECTOR();
XRespone(ResponeType::OnConnectionStatus, m_msgQueue, this, ConnectionStatus::Initialized, 0, nullptr, 0, nullptr, 0, nullptr, 0);
if (m_pApi)
{
// 停止已有线程,并清理
StopThread();
ReleaseRequestListBuf();
ReleaseRequestMapBuf();
SRequest* pRequest = MakeRequestBuf(E_Init);
if (pRequest)
{
AddToSendQueue(pRequest);
}
}
}
CLevel2UserApi::SRequest* CLevel2UserApi::MakeRequestBuf(RequestType type)
{
SRequest *pRequest = new SRequest;
if (nullptr == pRequest)
return nullptr;
memset(pRequest, 0, sizeof(SRequest));
pRequest->type = type;
switch (type)
{
case E_Init:
break;
case E_UserLoginField:
pRequest->pBuf = new DFITCUserLoginField();
break;
}
return pRequest;
}
void CLevel2UserApi::ReleaseRequestListBuf()
{
lock_guard<mutex> cl(m_csList);
while (!m_reqList.empty())
{
SRequest * pRequest = m_reqList.front();
delete pRequest->pBuf;
delete pRequest;
m_reqList.pop_front();
}
}
void CLevel2UserApi::ReleaseRequestMapBuf()
{
lock_guard<mutex> cl(m_csMap);
for (map<int, SRequest*>::iterator it = m_reqMap.begin(); it != m_reqMap.end(); ++it)
{
SRequest * pRequest = it->second;
delete pRequest->pBuf;
delete pRequest;
}
m_reqMap.clear();
}
void CLevel2UserApi::ReleaseRequestMapBuf(int nRequestID)
{
lock_guard<mutex> cl(m_csMap);
map<int, SRequest*>::iterator it = m_reqMap.find(nRequestID);
if (it != m_reqMap.end())
{
SRequest * pRequest = it->second;
delete pRequest->pBuf;
delete pRequest;
m_reqMap.erase(nRequestID);
}
}
void CLevel2UserApi::AddRequestMapBuf(int nRequestID, SRequest* pRequest)
{
if (nullptr == pRequest)
return;
lock_guard<mutex> cl(m_csMap);
map<int, SRequest*>::iterator it = m_reqMap.find(nRequestID);
if (it != m_reqMap.end())
{
SRequest* p = it->second;
if (pRequest != p)//如果实际上指的是同一内存,不再插入
{
delete p->pBuf;
delete p;
m_reqMap[nRequestID] = pRequest;
}
}
}
void CLevel2UserApi::AddToSendQueue(SRequest * pRequest)
{
if (nullptr == pRequest)
return;
lock_guard<mutex> cl(m_csList);
bool bFind = false;
if (!bFind)
m_reqList.push_back(pRequest);
if (!m_reqList.empty())
{
StartThread();
}
}
void CLevel2UserApi::RunInThread()
{
int iRet = 0;
while (!m_reqList.empty() && m_bRunning)
{
SRequest * pRequest = m_reqList.front();
long lRequest = ++m_lRequestID;
switch (pRequest->type)
{
case E_Init:
iRet = ReqInit();
if (iRet != 0 && m_bRunning)
this_thread::sleep_for(chrono::milliseconds(1000 * 20));
break;
case E_UserLoginField:
iRet = m_pApi->ReqUserLogin((DFITCUserLoginField*)pRequest->pBuf);
break;
default:
assert(false);
break;
}
if (0 == iRet)
{
//返回成功,填加到已发送池
m_nSleep = 1;
AddRequestMapBuf(lRequest, pRequest);
lock_guard<mutex> cl(m_csList);
m_reqList.pop_front();
}
else
{
//失败,按4的幂进行延时,但不超过1s
m_nSleep *= 4;
m_nSleep %= 1023;
}
this_thread::sleep_for(chrono::milliseconds(m_nSleep));
}
// 清理线程
m_hThread = nullptr;
m_bRunning = false;
}
int CLevel2UserApi::ReqInit()
{
XRespone(ResponeType::OnConnectionStatus, m_msgQueue, this, ConnectionStatus::Connecting, 0, nullptr, 0, nullptr, 0, nullptr, 0);
//初始化连接
int iRet = m_pApi->Connect(m_ServerInfo.Address, this, m_ServerInfo.IsUsingUdp);
if (0 == iRet)
{
}
else
{
RspUserLoginField field = { 0 };
field.ErrorID = iRet;
strcpy(field.ErrorMsg, "连接超时");
XRespone(ResponeType::OnConnectionStatus, m_msgQueue, this, ConnectionStatus::Disconnected, 0, &field, sizeof(RspUserLoginField), nullptr, 0, nullptr, 0);
}
return iRet;
}
void CLevel2UserApi::ReqUserLogin()
{
if (nullptr == m_pApi)
return;
DFITCUserLoginField request = { 0 };
strcpy(request.accountID, m_UserInfo.UserID);
strcpy(request.passwd, m_UserInfo.Password);
//只有这一处用到了m_nRequestID,没有必要每次重连m_nRequestID都从0开始
m_pApi->ReqUserLogin(&request);
XRespone(ResponeType::OnConnectionStatus, m_msgQueue, this, ConnectionStatus::Logining, 0, nullptr, 0, nullptr, 0, nullptr, 0);
}
void CLevel2UserApi::Disconnect()
{
if (m_pApi)
{
DELETE_CONNECTOR(m_pApi);
m_pApi = nullptr;
XRespone(ResponeType::OnConnectionStatus, m_msgQueue, this, ConnectionStatus::Disconnected, 0, nullptr, 0, nullptr, 0, nullptr, 0);
}
}
void CLevel2UserApi::OnConnected()
{
XRespone(ResponeType::OnConnectionStatus, m_msgQueue, this, ConnectionStatus::Connected, 0, nullptr, 0, nullptr, 0, nullptr, 0);
//连接成功后自动请求登录
ReqUserLogin();
}
void CLevel2UserApi::OnDisconnected(int nReason)
{
RspUserLoginField field = { 0 };
//连接失败返回的信息是拼接而成,主要是为了统一输出
field.ErrorID = nReason;
GetOnFrontDisconnectedMsg(nReason, field.ErrorMsg);
XRespone(ResponeType::OnConnectionStatus, m_msgQueue, this, ConnectionStatus::Disconnected, 0, &field, sizeof(RspUserLoginField), nullptr, 0, nullptr, 0);
}
void CLevel2UserApi::OnRspUserLogin(struct ErrorRtnField * pErrorField)
{
RspUserLoginField field = { 0 };
if (!IsErrorRspInfo(pErrorField))
{
XRespone(ResponeType::OnConnectionStatus, m_msgQueue, this, ConnectionStatus::Logined, 0, &field, sizeof(RspUserLoginField), nullptr, 0, nullptr, 0);
XRespone(ResponeType::OnConnectionStatus, m_msgQueue, this, ConnectionStatus::Done, 0, nullptr, 0, nullptr, 0, nullptr, 0);
//有可能断线了,本处是断线重连后重新订阅
set<string> mapOld = m_setInstrumentIDs;//记下上次订阅的合约
//Unsubscribe(mapOld);//由于已经断线了,没有必要再取消订阅
Subscribe(mapOld,"");//订阅
}
else
{
field.ErrorID = pErrorField->ErrorID;
strcpy(field.ErrorMsg, pErrorField->ErrorMsg);
XRespone(ResponeType::OnConnectionStatus, m_msgQueue, this, ConnectionStatus::Disconnected, 0, &field, sizeof(RspUserLoginField), nullptr, 0, nullptr, 0);
}
}
void CLevel2UserApi::Subscribe(const string& szInstrumentIDs, const string& szExchageID)
{
if(nullptr == m_pApi)
return;
vector<char*> vct;
set<char*> st;
lock_guard<mutex> cl(m_csMapInstrumentIDs);
char* pBuf = GetSetFromString(szInstrumentIDs.c_str(), _QUANTBOX_SEPS_, vct, st, 1, m_setInstrumentIDs);
if (vct.size()>0)
{
//转成字符串数组
char** pArray = new char*[vct.size()];
for (size_t j = 0; j<vct.size(); ++j)
{
pArray[j] = vct[j];
}
//订阅
m_pApi->SubscribeMarketData(pArray, (int)vct.size());
delete[] pArray;
}
delete[] pBuf;
}
void CLevel2UserApi::Subscribe(const set<string>& instrumentIDs, const string& szExchageID)
{
if (nullptr == m_pApi)
return;
string szInstrumentIDs;
for (set<string>::iterator i = instrumentIDs.begin(); i != instrumentIDs.end(); ++i)
{
szInstrumentIDs.append(*i);
szInstrumentIDs.append(";");
}
if (szInstrumentIDs.length()>1)
{
Subscribe(szInstrumentIDs, szExchageID);
}
}
void CLevel2UserApi::Unsubscribe(const string& szInstrumentIDs, const string& szExchageID)
{
if (nullptr == m_pApi)
return;
vector<char*> vct;
set<char*> st;
lock_guard<mutex> cl(m_csMapInstrumentIDs);
char* pBuf = GetSetFromString(szInstrumentIDs.c_str(), _QUANTBOX_SEPS_, vct, st, -1, m_setInstrumentIDs);
if (vct.size()>0)
{
//转成字符串数组
char** pArray = new char*[vct.size()];
for (size_t j = 0; j<vct.size(); ++j)
{
pArray[j] = vct[j];
}
//订阅
m_pApi->UnSubscribeMarketData(pArray, (int)vct.size());
delete[] pArray;
}
delete[] pBuf;
}
void CLevel2UserApi::SubscribeAll()
{
if (nullptr == m_pApi)
return;
m_pApi->SubscribeAll();
}
void CLevel2UserApi::UnsubscribeAll()
{
if (nullptr == m_pApi)
return;
m_pApi->UnSubscribeAll();
}
void CLevel2UserApi::OnRspSubscribeMarketData(struct ErrorRtnField * pErrorField)
{
IsErrorRspInfo_Output(pErrorField);
}
void CLevel2UserApi::OnRspUnSubscribeMarketData(struct ErrorRtnField * pErrorField)
{
IsErrorRspInfo_Output(pErrorField);
}
void CLevel2UserApi::OnRspSubscribeAll(struct ErrorRtnField * pErrorField)
{
IsErrorRspInfo_Output(pErrorField);
}
void CLevel2UserApi::OnRspUnSubscribeAll(struct ErrorRtnField * pErrorField)
{
IsErrorRspInfo_Output(pErrorField);
}
void CLevel2UserApi::OnBestAndDeep(MDBestAndDeep * const pQuote)
{
}
void CLevel2UserApi::OnArbi(MDBestAndDeep * const pQuote)
{
}
void CLevel2UserApi::OnTenEntrust(MDTenEntrust * const pQuote)
{
}
void CLevel2UserApi::OnRealtime(MDRealTimePrice * const pQuote)
{
}
void CLevel2UserApi::OnOrderStatistic(MDOrderStatistic * const pQuote)
{
}
void CLevel2UserApi::OnMarchPrice(MDMarchPriceQty * const pQuote)
{
}