-
Notifications
You must be signed in to change notification settings - Fork 13
/
CMdWrapper.cpp
538 lines (438 loc) · 16.2 KB
/
CMdWrapper.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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
#include <CMdWrapper.h>
#include <json/json.h>
#include <comhelper.h>
#include <string>
#include <stdlib.h>
#include <string.h>
/// 构造函数
CMdWrapper::CMdWrapper(MdConfigure * pConfigure) {
// 读取配置数据信息
this->pConfigure = pConfigure;
// 创建CTP API工作对象
pMdApi = CThostFtdcMdApi::CreateFtdcMdApi();
// 创建SPI工作对象并让其和API关联
pMdHandler = new CMdHandler(pConfigure);
pMdApi->RegisterSpi(pMdHandler);
// 初始化RequestID序列
RequestID = 0;
// 初始化上次出错代码和出错信息
lastErrorID = 0;
lastErrorMsg = "";
}
// 将所有api函数映射到名称
void CMdWrapper::initApiMap() {
//ReqUserLogout
apiMap["ReqUserLogout"] = &CMdWrapper::ReqUserLogout;
//ReqUserLogin
apiMap["ReqUserLogin"] = &CMdWrapper::ReqUserLogin;
//SubscribeMarketData
apiMap["SubscribeMarketData"] = &CMdWrapper::SubscribeMarketData;
//UnSubscribeMarketData
apiMap["UnSubscribeMarketData"] = &CMdWrapper::UnSubscribeMarketData;
}
/// 启动CTP连接
void CMdWrapper::init() {
std::cout << "CMdWrapper::init():开始执行..." << std::endl;
// 初始化api名称对照表
initApiMap();
// 创建zmq通讯环境
zmq::context_t context(1);
zmq::socket_t receiver(context, ZMQ_PULL);
PushbackMessage message;
/// 设置服务器地址
pMdApi->RegisterFront(pConfigure->frontAddress);
std::cout << "pConfigure->frontAddress=" << pConfigure->frontAddress << std::endl;
// 连接spi的Pushback管道
std::cout << "CMdWrapper::init():Pushback管道地址为:" << pConfigure->pushbackPipe << std::endl;
receiver.connect(pConfigure->pushbackPipe);
// 连接交易系统
std::cout << "CMdWrapper::init():尝试连接服务器..." << std::endl;
pMdApi->Init();
// 等待服务器发出OnFrontConnected事件
std::cout << "CMdWrapper::init():等待服务器响应消息..." << std::endl;
message.recv(receiver);
std::cout << "CMdWrapper::init():已收到服务器响应消息..." << std::endl;
// 确认收到的返回信息是由OnFrontConnected发出
assert(message.requestID.compare("0") == 0);
assert(message.apiName.compare("OnFrontConnected") == 0);
assert(message.respInfo.compare("") == 0);
// 发出登陆请求
std::cout << "CMdWrapper::init():发出登录请求..." << std::endl;
CThostFtdcReqUserLoginField userLoginField;
strcpy(userLoginField.BrokerID,pConfigure->brokerID);
strcpy(userLoginField.UserID,pConfigure->userID);
strcpy(userLoginField.Password,pConfigure->password);
pMdApi->ReqUserLogin(&userLoginField,getNextRequestID());
// 等待登录成功返回信息
std::cout << "CMdWrapper::init():等待登录结果..." << std::endl;
message.recv(receiver);
std::cout << "CMdWrapper::init():已收到登录返回信息..." << std::endl;
//std::cout << "message.requestID=" << message.requestID << std::endl;
//assert(message.requestID.compare("1") == 0);
assert(message.apiName.compare("OnRspUserLogin") == 0);
// 检查是否登录成功,如果不成功将终止程序
Json::Reader jsonReader;
Json::Value jsonData;
if (!jsonReader.parse(message.respInfo,jsonData)) {
throw std::exception();
}
int ErrorID = jsonData["Parameters"]["Data"]["RspInfo"]["ErrorID"].asInt();
assert(ErrorID == 0);
std::cout << "CMdWrapper::init():登录成功..." << std::endl;
std::cout << "CMdWrapper::init():执行完毕..." << std::endl;
}
/// 获取下一个RequestID序列
int CMdWrapper::getNextRequestID() {
return ++this->RequestID;
}
/// 获取当前RequestID序列
int CMdWrapper::getCurrentRequestID() {
return this->RequestID;
}
/// 获取上次出错代码
int CMdWrapper::getLastErrorID() {
return lastErrorID;
}
/// 获取上次错误信息
std::string CMdWrapper::getLastErrorMsg() {
return lastErrorMsg;
}
# define MAX_SUBCRIBE_COUNT 1000
void freeMemory(char ** ppInstrumentID) {
for (int i = 0; i < MAX_SUBCRIBE_COUNT; i++) {
if(ppInstrumentID[i] != NULL) {
free(ppInstrumentID[i]);
ppInstrumentID[i] = NULL;
} else {
break;
}
}
}
///订阅行情。
int CMdWrapper::SubscribeMarketData(std::string jsonString) {
char *ppInstrumentID[MAX_SUBCRIBE_COUNT];
int nCount,result;
memset(ppInstrumentID,'\0',sizeof(ppInstrumentID));
try {
std::cout << jsonString << std::endl;
//1. 解析json格式
Json::Reader jsonReader;
Json::Value jsonData;
if (!jsonReader.parse(jsonString, jsonData)) {
throw std::exception();
}
Json::Value Parameters = jsonData["Parameters"];
Assert<std::exception>(!Parameters.empty());
Json::Value Data = Parameters["Data"];
if ( Data.size() > MAX_SUBCRIBE_COUNT ) {
lastErrorID = -1002;
lastErrorMsg = "一次订阅或取消行情的品种数量太多";
return -1;
}
// 分配内存拷贝准备调用结构
//std::cout << "sizeof(ppInstrumentID) = " << sizeof(ppInstrumentID) << std::endl;
nCount = Data.size();
for (int i = 0; i < Data.size(); i++) {
const char * instrumentID = Data[i].asString().c_str();
ppInstrumentID[i] = (char *) malloc(strlen(instrumentID)+1);
strcpy(ppInstrumentID[i],instrumentID);
//std::cout << i << ":*+*" << instrumentID << std::endl;
}
// 调用SubscribeMarketData
result = pMdApi->SubscribeMarketData(ppInstrumentID,nCount);
} catch (...) {
freeMemory(ppInstrumentID);
lastErrorID = -1001;
lastErrorMsg = "json数据格式错误";
return -1;
}
freeMemory(ppInstrumentID);
// TODO:检查API调用是否失败,并设置LastError信息
if ( result != 0 ) {
lastErrorID = result;
switch(result) {
case -1 :
lastErrorMsg = "网络连接失败";
break;
case -2 :
lastErrorMsg = "未处理请求超过许可数";
break;
case -3 :
lastErrorMsg = "每秒发送请求超过许可数";
break;
default :
lastErrorMsg = "未知错误";
}
return -1;
}
// 如果执行成功重置最近错误信息,并将RequestID返回调用程序
lastErrorID = 0;
lastErrorMsg = "";
return 0;
}
///退订行情。
int CMdWrapper::UnSubscribeMarketData(std::string jsonString) {
char *ppInstrumentID[MAX_SUBCRIBE_COUNT];
int nCount,result;
memset(ppInstrumentID,'\0',sizeof(ppInstrumentID));
try {
std::cout << jsonString << std::endl;
//1. 解析json格式
Json::Reader jsonReader;
Json::Value jsonData;
if (!jsonReader.parse(jsonString, jsonData)) {
throw std::exception();
}
Json::Value Parameters = jsonData["Parameters"];
Assert<std::exception>(!Parameters.empty());
Json::Value Data = Parameters["Data"];
if ( Data.size() > MAX_SUBCRIBE_COUNT ) {
lastErrorID = -1002;
lastErrorMsg = "一次订阅或取消行情的品种数量太多";
return -1;
}
// 分配内存拷贝准备调用结构
//std::cout << "sizeof(ppInstrumentID) = " << sizeof(ppInstrumentID) << std::endl;
nCount = Data.size();
for (int i = 0; i < Data.size(); i++) {
const char * instrumentID = Data[i].asString().c_str();
ppInstrumentID[i] = (char *) malloc(strlen(instrumentID)+1);
strcpy(ppInstrumentID[i],instrumentID);
//std::cout << i << ":*+*" << instrumentID << std::endl;
}
// 调用SubscribeMarketData
result = pMdApi->UnSubscribeMarketData(ppInstrumentID,nCount);
} catch (...) {
freeMemory(ppInstrumentID);
lastErrorID = -1001;
lastErrorMsg = "json数据格式错误";
return -1;
}
freeMemory(ppInstrumentID);
// TODO:检查API调用是否失败,并设置LastError信息
if ( result != 0 ) {
lastErrorID = result;
switch(result) {
case -1 :
lastErrorMsg = "网络连接失败";
break;
case -2 :
lastErrorMsg = "未处理请求超过许可数";
break;
case -3 :
lastErrorMsg = "每秒发送请求超过许可数";
break;
default :
lastErrorMsg = "未知错误";
}
return -1;
}
// 如果执行成功重置最近错误信息,并将RequestID返回调用程序
lastErrorID = 0;
lastErrorMsg = "";
return 0;
}
///登出请求
/// 调用成功返回RequestID,失败返回-1
/// 通过查看lastErrorID和lastErrorMsg查看出错的原因
int CMdWrapper::ReqUserLogout(std::string jsonString)
{
printf("ReqUserLogout():被执行...\n");
CThostFtdcUserLogoutField data;
int nRequestID;
// 解析json格式数据
try {
Json::Reader jsonReader;
Json::Value jsonData;
if (!jsonReader.parse(jsonString, jsonData)) {
throw std::exception();
}
Json::Value Parameters = jsonData["Parameters"];
Assert<std::exception>(!Parameters.empty());
Json::Value Data = Parameters["Data"];
//Assert<std::exception>(!Data.empty());
// TODO:这里将pJsonData转化为对应的结构参数
///经纪公司代码 char BrokerID[11];
if (!Data["BrokerID"].empty()) {
data.BrokerID[sizeof(data.BrokerID)-1] = 0;
strncpy(data.BrokerID,Data["BrokerID"].asCString(),sizeof(data.BrokerID)-1);
} else {
strcpy(data.BrokerID,"");
}
///用户代码 char UserID[16];
if (!Data["UserID"].empty()) {
data.UserID[sizeof(data.UserID)-1] = 0;
strncpy(data.UserID,Data["UserID"].asCString(),sizeof(data.UserID)-1);
} else {
strcpy(data.UserID,"");
}
} catch (...) {
lastErrorID = -1001;
lastErrorMsg = "json数据格式错误";
return -1;
}
// 获取RequestID
nRequestID = getNextRequestID();
// 调用对应的CTP API函数
int result =
pMdApi->ReqUserLogout(&data, nRequestID);
// TODO:检查API调用是否失败,并设置LastError信息
if ( result != 0 ) {
lastErrorID = result;
switch(result) {
case -1 :
lastErrorMsg = "网络连接失败";
break;
case -2 :
lastErrorMsg = "未处理请求超过许可数";
break;
case -3 :
lastErrorMsg = "每秒发送请求超过许可数";
break;
default :
lastErrorMsg = "未知错误";
}
//return result;
return -1;
}
// 如果执行成功重置最近错误信息,并将RequestID返回调用程序
lastErrorID = 0;
lastErrorMsg = "";
//return nRequestID;
return 0;
}
///用户登录请求
/// 调用成功返回RequestID,失败返回-1
/// 通过查看lastErrorID和lastErrorMsg查看出错的原因
int CMdWrapper::ReqUserLogin(std::string jsonString)
{
printf("ReqUserLogin():被执行...\n");
CThostFtdcReqUserLoginField data;
int nRequestID;
// 解析json格式数据
try {
Json::Reader jsonReader;
Json::Value jsonData;
if (!jsonReader.parse(jsonString, jsonData)) {
throw std::exception();
}
Json::Value Parameters = jsonData["Parameters"];
Assert<std::exception>(!Parameters.empty());
Json::Value Data = Parameters["Data"];
//Assert<std::exception>(!Data.empty());
// TODO:这里将pJsonData转化为对应的结构参数
///交易日 char TradingDay[9];
if (!Data["TradingDay"].empty()) {
data.TradingDay[sizeof(data.TradingDay)-1] = 0;
strncpy(data.TradingDay,Data["TradingDay"].asCString(),sizeof(data.TradingDay)-1);
} else {
strcpy(data.TradingDay,"");
}
///经纪公司代码 char BrokerID[11];
if (!Data["BrokerID"].empty()) {
data.BrokerID[sizeof(data.BrokerID)-1] = 0;
strncpy(data.BrokerID,Data["BrokerID"].asCString(),sizeof(data.BrokerID)-1);
} else {
strcpy(data.BrokerID,"");
}
///用户代码 char UserID[16];
if (!Data["UserID"].empty()) {
data.UserID[sizeof(data.UserID)-1] = 0;
strncpy(data.UserID,Data["UserID"].asCString(),sizeof(data.UserID)-1);
} else {
strcpy(data.UserID,"");
}
///密码 char Password[41];
if (!Data["Password"].empty()) {
data.Password[sizeof(data.Password)-1] = 0;
strncpy(data.Password,Data["Password"].asCString(),sizeof(data.Password)-1);
} else {
strcpy(data.Password,"");
}
///用户端产品信息 char UserProductInfo[11];
if (!Data["UserProductInfo"].empty()) {
data.UserProductInfo[sizeof(data.UserProductInfo)-1] = 0;
strncpy(data.UserProductInfo,Data["UserProductInfo"].asCString(),sizeof(data.UserProductInfo)-1);
} else {
strcpy(data.UserProductInfo,"");
}
///接口端产品信息 char InterfaceProductInfo[11];
if (!Data["InterfaceProductInfo"].empty()) {
data.InterfaceProductInfo[sizeof(data.InterfaceProductInfo)-1] = 0;
strncpy(data.InterfaceProductInfo,Data["InterfaceProductInfo"].asCString(),sizeof(data.InterfaceProductInfo)-1);
} else {
strcpy(data.InterfaceProductInfo,"");
}
///协议信息 char ProtocolInfo[11];
if (!Data["ProtocolInfo"].empty()) {
data.ProtocolInfo[sizeof(data.ProtocolInfo)-1] = 0;
strncpy(data.ProtocolInfo,Data["ProtocolInfo"].asCString(),sizeof(data.ProtocolInfo)-1);
} else {
strcpy(data.ProtocolInfo,"");
}
///Mac地址 char MacAddress[21];
if (!Data["MacAddress"].empty()) {
data.MacAddress[sizeof(data.MacAddress)-1] = 0;
strncpy(data.MacAddress,Data["MacAddress"].asCString(),sizeof(data.MacAddress)-1);
} else {
strcpy(data.MacAddress,"");
}
///动态密码 char OneTimePassword[41];
if (!Data["OneTimePassword"].empty()) {
data.OneTimePassword[sizeof(data.OneTimePassword)-1] = 0;
strncpy(data.OneTimePassword,Data["OneTimePassword"].asCString(),sizeof(data.OneTimePassword)-1);
} else {
strcpy(data.OneTimePassword,"");
}
///终端IP地址 char ClientIPAddress[16];
if (!Data["ClientIPAddress"].empty()) {
data.ClientIPAddress[sizeof(data.ClientIPAddress)-1] = 0;
strncpy(data.ClientIPAddress,Data["ClientIPAddress"].asCString(),sizeof(data.ClientIPAddress)-1);
} else {
strcpy(data.ClientIPAddress,"");
}
} catch (...) {
lastErrorID = -1001;
lastErrorMsg = "json数据格式错误";
return -1;
}
// 获取RequestID
nRequestID = getNextRequestID();
// 调用对应的CTP API函数
int result =
pMdApi->ReqUserLogin(&data, nRequestID);
// TODO:检查API调用是否失败,并设置LastError信息
if ( result != 0 ) {
lastErrorID = result;
switch(result) {
case -1 :
lastErrorMsg = "网络连接失败";
break;
case -2 :
lastErrorMsg = "未处理请求超过许可数";
break;
case -3 :
lastErrorMsg = "每秒发送请求超过许可数";
break;
default :
lastErrorMsg = "未知错误";
}
//return result;
return -1;
}
// 如果执行成功重置最近错误信息,并将RequestID返回调用程序
lastErrorID = 0;
lastErrorMsg = "";
//return nRequestID;
return 0;
}
// 通过名称调用api
int CMdWrapper::callApiByName(std::string apiName,std::string jsonString) {
if ( apiMap.find(apiName) != apiMap.end() ) {
return (this->*apiMap[apiName])(jsonString);
} else {
lastErrorID = -1000;
lastErrorMsg = "没有这个接口函数";
return -1;
}
}