-
Notifications
You must be signed in to change notification settings - Fork 1
/
nodectptd.cpp
5919 lines (4437 loc) · 198 KB
/
nodectptd.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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include<stdio.h>
#include <cstring>
#include <iconv.h>
#include "common.h"
#include "nodectptd.h"
using namespace std;
napi_ref CTPTraderClient::Singleton = nullptr;
napi_ref CTPTraderClient::prototype;
int CTPTraderClient::requestID;
map<string, int> CTPTraderClient::eventName_map;
map<int, napi_ref> CTPTraderClient::callback_map;
//------------------libuv channel机制 子线程通知主线程uv_async_send------>主线程MainThreadCallback
CTPTraderClient::CTPTraderClient()
{
//channel的data固定不变, 为当前实例
channel.data = this;
//打开libuv通道
uv_async_init(uv_default_loop(), &channel, (uv_async_cb)CTPTraderClient::MainThreadCallback);
//激活互斥锁
uv_mutex_init(&eventQueueMutex);
}
//子线程
void CTPTraderClient::queueEvent(OnEventCbRtnField* event)
{
//1.插入一个子线程On_xxx函数的Event到队列,很快
//2.这里如果处理时间过长,子线程会卡死,ctp程序会卡死
uv_mutex_lock(&eventQueueMutex);
//写eventQueue
eventQueue.push_back(event);
uv_mutex_unlock(&eventQueueMutex);
//子线程向主线程发送通知,处理事件队列中的events
//uv_async_send函数是线程安全的
uv_async_send(&channel);
}
//主线程响应
void CTPTraderClient::MainThreadCallback(uv_async_t* channel) {
CTPTraderClient * traderClient = (CTPTraderClient*)channel->data;
traderClient->processEventQueue();
}
//主线程
void CTPTraderClient::processEventQueue() {
//1.1.事件队列中的事件,全部执行,这里在锁内执行函数要耗时间!!!
//1.2.减少耗时,使用二级缓存!!!事件队列中的事件后,就立即释放锁
uv_mutex_lock(&eventQueueMutex);
vector<OnEventCbRtnField*> mainThreadEventList;
//读eventQueue
mainThreadEventList.swap(eventQueue);
uv_mutex_unlock(&eventQueueMutex);
for (int i = 0, size = mainThreadEventList.size(); i < size; i++) {
process_event(mainThreadEventList[i]);
}
}
CTPTraderClient::~CTPTraderClient()
{
//需要再调用一次,确保执行所有事件
processEventQueue();
if (Api)
{
//退出后,释放CTP的Api主线程(Spi线程也会被释放)
Api->Release();
Api = NULL;
}
//销毁后再当前实例的事件相应callback
map<int, napi_ref>::iterator callback_it = callback_map.begin();
while (callback_it != callback_map.end()) {
napi_ref callback = callback_it->second;
napi_delete_reference(env_, callback);
callback_it++;
}
callback_map.clear();
//保存的env_在这里起作用
napi_delete_reference(env_, wrapper_);
//关闭libuv通道
uv_close((uv_handle_t*)&channel, (uv_close_cb)CTPTraderClient::ChannelClosedCallback);
//销毁事件队列的互斥锁
uv_mutex_destroy(&eventQueueMutex);
//释放后单例模式
napi_delete_reference(env_, Singleton);
Singleton = nullptr;
//对象销毁,不需要销毁类的prototype对象引用,它跟随进程销毁
//napi_delete_reference(env_, prototype);
}
void CTPTraderClient::ChannelClosedCallback(uv_async_t* pChannel) {
}
//------------------libuv channel机制 子线程通知主线程uv_async_send------>主线程MainThreadCallback
void CTPTraderClient::Destructor(napi_env env,
void* nativeObject,
void* /*finalize_hint*/) {
CTPTraderClient* obj = static_cast<CTPTraderClient*>(nativeObject);
delete obj;
}
napi_status CTPTraderClient::Init(napi_env env)
{
//初始化static变量eventName_map,规定所有的事件,不允许未定义的事件
if (eventName_map.size() == 0)
initEventNameMap();
napi_status status;
napi_property_descriptor properties[] = {
DECLARE_NAPI_PROPERTY("on", on),
DECLARE_NAPI_PROPERTY("connect", connect),
DECLARE_NAPI_PROPERTY("getApiVersion", getApiVersion),
DECLARE_NAPI_PROPERTY("authenticate", authenticate),
DECLARE_NAPI_PROPERTY("login", login),
DECLARE_NAPI_PROPERTY("querySettlementInfo", querySettlementInfo),
DECLARE_NAPI_PROPERTY("confirmSettlementInfo", confirmSettlementInfo),
DECLARE_NAPI_PROPERTY("queryInstrument", queryInstrument),
DECLARE_NAPI_PROPERTY("sendOrder", sendOrder),
DECLARE_NAPI_PROPERTY("cancelOrder", cancelOrder),
DECLARE_NAPI_PROPERTY("logout", logout),
DECLARE_NAPI_PROPERTY("queryTradingAccount", queryTradingAccount),
DECLARE_NAPI_PROPERTY("queryInvestorPosition", queryInvestorPosition),
DECLARE_NAPI_PROPERTY("queryCommissionRate", queryCommissionRate),
};
//创建Js对象prototype
napi_value _proto_;
//1.创建prototype Js对象
//2.创建New函数为Js层函数,并且命名
//3.给prototype对象的constructor属性指向New函数
//4.给prototype对象绑定多个属性
status = napi_define_class(
env, "CTPTraderClient", NAPI_AUTO_LENGTH, New, nullptr,
sizeof(properties) / sizeof(*properties), properties, &_proto_);
if (status != napi_ok) return status;
//防止销毁,为prototype创建ref对象,驻存到进程的静态区中
status = napi_create_reference(env, _proto_, 1, &prototype);
if (status != napi_ok) return status;
return napi_ok;
}
void CTPTraderClient::initEventNameMap()
{
eventName_map["FrontConnected"] = T_On_FrontConnected;
eventName_map["FrontDisconnected"] = T_On_FrontDisconnected;
eventName_map["RspError"] = T_On_RspError;
eventName_map["RspAuthenticate"] = T_On_RspAuthenticate;
eventName_map["RspUserLogin"] = T_On_RspUserLogin;
eventName_map["RspQrySettlementInfo"] = T_On_RspQrySettlementInfo;
eventName_map["RspSettlementInfoConfirm"] = T_On_RspSettlementInfoConfirm;
eventName_map["RspQryInstrument"] = T_On_RspQryInstrument;
eventName_map["RspUserLogout"] = T_On_RspUserLogout;
eventName_map["RspQryInvestorPosition"] = T_On_RspQryInvestorPosition;
eventName_map["RspQryTradingAccount"] = T_On_RspQryTradingAccount;
eventName_map["RspOrderInsert"] = T_On_RspOrderInsert;
eventName_map["ErrRtnOrderInsert"] = T_On_ErrRtnOrderInsert;
eventName_map["RtnOrder"] = T_On_RtnOrder;
eventName_map["RtnTrade"] = T_On_RtnTrade;
eventName_map["RspOrderAction"] = T_On_RspOrderAction;
eventName_map["ErrRtnOrderAction"] = T_On_ErrRtnOrderAction;
eventName_map["RspQryInstrumentCommissionRate"] = T_On_RspQryInstrumentCommissionRate;
callback_map[T_On_FrontConnected] = nullptr;
callback_map[T_On_FrontDisconnected] = nullptr;
callback_map[T_On_RspError] = nullptr;
callback_map[T_On_RspAuthenticate] = nullptr;
callback_map[T_On_RspUserLogin] = nullptr;
callback_map[T_On_RspQrySettlementInfo] = nullptr;
callback_map[T_On_RspSettlementInfoConfirm] = nullptr;
callback_map[T_On_RspQryInstrument] = nullptr;
callback_map[T_On_RspUserLogout] = nullptr;
callback_map[T_On_RspQryInvestorPosition] = nullptr;
callback_map[T_On_RspQryTradingAccount] = nullptr;
callback_map[T_On_RspOrderInsert] = nullptr;
callback_map[T_On_ErrRtnOrderInsert] = nullptr;
callback_map[T_On_RtnOrder] = nullptr;
callback_map[T_On_RtnTrade] = nullptr;
callback_map[T_On_RspOrderAction] = nullptr;
callback_map[T_On_ErrRtnOrderAction] = nullptr;
callback_map[T_On_RspQryInstrumentCommissionRate] = nullptr;
}
napi_status CTPTraderClient::NewInstance(napi_env env, napi_value arg, napi_value* instance) {
napi_status status;
napi_value _proto_;
status = napi_get_reference_value(env, prototype, &_proto_);
if (status != napi_ok) return status;
status = napi_new_instance(env, _proto_, 0, nullptr, instance);
if (status != napi_ok) return status;
return napi_ok;
}
napi_value CTPTraderClient::New(napi_env env, napi_callback_info info)
{
napi_value _this;
NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &_this, nullptr));
CTPTraderClient* obj = new CTPTraderClient();
obj->env_ = env;
NAPI_CALL(env, napi_wrap(env,
_this,
obj,
CTPTraderClient::Destructor,
nullptr, /* finalize_hint */
&obj->wrapper_));
return _this;
}
napi_value CTPTraderClient::on(napi_env env, napi_callback_info info)
{
size_t argc = 2;
napi_value args[2];
napi_value _this;
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &_this, nullptr));
if (argc != 2) {
napi_value msg;
napi_create_string_utf8(env,
"Wrong number of arguments.Right Format:on(string:eventName, function:eventCallBack)",
NAPI_AUTO_LENGTH, &msg);
napi_value err;
napi_create_error(env, NULL, msg, &err);
napi_fatal_exception(env, err);
return NULL;
}
napi_valuetype eventNameType;
NAPI_CALL(env, napi_typeof(env, args[0], &eventNameType));
napi_valuetype eventCallbackType;
NAPI_CALL(env, napi_typeof(env, args[1], &eventCallbackType));
if (eventNameType != napi_string || eventCallbackType != napi_function) {
napi_value msg;
napi_create_string_utf8(env,
"Parameter Type Error,Right Format:on(string:eventName, function:eventCallBack)",
NAPI_AUTO_LENGTH, &msg);
napi_value err;
napi_create_error(env, NULL, msg, &err);
napi_fatal_exception(env, err);
return NULL;
}
char buffer[128];
size_t buffer_size = 128;
size_t copied;
NAPI_CALL(env,
napi_get_value_string_utf8(env, args[0], buffer, buffer_size, &copied));
string eventName = buffer;
map<string, int>::iterator eventItem = eventName_map.find(eventName);
if (eventItem == eventName_map.end()) {
napi_value msg;
napi_create_string_utf8(env,
"NodeQuant has no register this event",
NAPI_AUTO_LENGTH, &msg);
napi_value err;
napi_create_error(env, NULL, msg, &err);
napi_fatal_exception(env, err);
return NULL;
}
//已经设置了,不再更改响应函数
if (nullptr != callback_map[eventItem->second]) {
return NULL;
}
NAPI_CALL(env, napi_create_reference(env, args[1], 1, &callback_map[eventItem->second]));
return NULL;
}
napi_value CTPTraderClient::connect(napi_env env, napi_callback_info info)
{
size_t argc = 2;
napi_value args[2];
napi_value _this;
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &_this, nullptr));
if (argc != 2) {
napi_value msg;
napi_create_string_utf8(env,
"Wrong number of arguments.Right Format:connect(String:address, String:traderFlowPath)",
NAPI_AUTO_LENGTH, &msg);
napi_value err;
napi_create_error(env, NULL, msg, &err);
napi_fatal_exception(env, err);
return NULL;
}
napi_valuetype addressType;
NAPI_CALL(env, napi_typeof(env, args[0], &addressType));
napi_valuetype tdFlowPathType;
NAPI_CALL(env, napi_typeof(env, args[1], &tdFlowPathType));
if (addressType != napi_string || tdFlowPathType != napi_string) {
napi_value msg;
napi_create_string_utf8(env,
"Parameter Type Error,Right Format:connect(String:address, String:traderFlowPath)",
NAPI_AUTO_LENGTH, &msg);
napi_value err;
napi_create_error(env, NULL, msg, &err);
napi_fatal_exception(env, err);
return NULL;
}
//栈变量自动释放
ConnectField req;
memset(&req, 0, sizeof(req));
size_t buffer_size = 200;
size_t copied;
NAPI_CALL(env,
napi_get_value_string_utf8(env, args[0], req.front_addr, buffer_size, &copied));
buffer_size = 400;
NAPI_CALL(env,
napi_get_value_string_utf8(env, args[1], req.flowPath, buffer_size, &copied));
CTPTraderClient* traderClient;
NAPI_CALL(env, napi_unwrap(env, _this, reinterpret_cast<void**>(&traderClient)));
//Js主线程请求号递增
requestID++;
//c++ void*泛型指针
int nResult = traderClient->invoke(&req, T_CONNECT_RE, requestID);
napi_value result;
NAPI_CALL(env, napi_create_int32(env, nResult, &result));
return result;
}
napi_value CTPTraderClient::getApiVersion(napi_env env, napi_callback_info info)
{
size_t argc = 0;
napi_value _this;
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, nullptr, &_this, nullptr));
if (argc != 0) {
napi_value msg;
napi_create_string_utf8(env,
"Wrong number of arguments.Right Format: getApiVersion()",
NAPI_AUTO_LENGTH, &msg);
napi_value err;
napi_create_error(env, NULL, msg, &err);
napi_fatal_exception(env, err);
return NULL;
}
CTPTraderClient* traderClient;
NAPI_CALL(env, napi_unwrap(env, _this, reinterpret_cast<void**>(&traderClient)));
if (traderClient->Api) {
napi_value result;
string apiVersion = traderClient->Api->GetApiVersion();
NAPI_CALL(env, napi_create_string_utf8(env, apiVersion.c_str(), apiVersion.length(), &result));
return result;
}
else {
return NULL;
}
}
napi_value CTPTraderClient::authenticate(napi_env env, napi_callback_info info)
{
size_t argc = 5;
napi_value args[5];
napi_value _this;
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &_this, nullptr));
if (argc != 5) {
napi_value msg;
napi_create_string_utf8(env,
"Wrong number of arguments.Right Format:authenticate(String:userID, String::brokerID, String:AppID,String:AuthCode,String:userProductInfo)",
NAPI_AUTO_LENGTH, &msg);
napi_value err;
napi_create_error(env, NULL, msg, &err);
napi_fatal_exception(env, err);
return NULL;
}
napi_valuetype userIDType;
NAPI_CALL(env, napi_typeof(env, args[0], &userIDType));
napi_valuetype brokerIDType;
NAPI_CALL(env, napi_typeof(env, args[1], &brokerIDType));
napi_valuetype AppIDType;
NAPI_CALL(env, napi_typeof(env, args[2], &AppIDType));
napi_valuetype AuthCodeType;
NAPI_CALL(env, napi_typeof(env, args[3], &AuthCodeType));
napi_valuetype userProductInfoType;
NAPI_CALL(env, napi_typeof(env, args[4], &userProductInfoType));
if (userIDType != napi_string ||
brokerIDType != napi_string ||
AppIDType != napi_string ||
AuthCodeType != napi_string ||
userProductInfoType != napi_string) {
napi_value msg;
napi_create_string_utf8(env,
"Parameter Type Error,Right Format:authenticate(String:userID, String::brokerID, String:AppID,String:AuthCode,String:userProductInfo)",
NAPI_AUTO_LENGTH, &msg);
napi_value err;
napi_create_error(env, NULL, msg, &err);
napi_fatal_exception(env, err);
return NULL;
}
CThostFtdcReqAuthenticateField req;
memset(&req, 0, sizeof(req));
//UserID 16字符
size_t buffer_size = 16;
size_t copied;
NAPI_CALL(env,
napi_get_value_string_utf8(env, args[0], req.UserID, buffer_size, &copied));
//BrokerID 11字符
buffer_size = 11;
NAPI_CALL(env,
napi_get_value_string_utf8(env, args[1], req.BrokerID, buffer_size, &copied));
//AppID 33字符
buffer_size = 33;
NAPI_CALL(env,
napi_get_value_string_utf8(env, args[2], req.AppID, buffer_size, &copied));
//AuthCode 17字符
buffer_size = 17;
NAPI_CALL(env,
napi_get_value_string_utf8(env, args[3], req.AuthCode, buffer_size, &copied));
//UserProductInfo 11字符
buffer_size = 11;
NAPI_CALL(env,
napi_get_value_string_utf8(env, args[4], req.UserProductInfo, buffer_size, &copied));
CTPTraderClient* traderClient;
NAPI_CALL(env, napi_unwrap(env, _this, reinterpret_cast<void**>(&traderClient)));
//Js主线程请求号递增
requestID++;
//c++ void*泛型指针
int nResult = traderClient->invoke(&req, T_Authenticate_RE, requestID);
napi_value result;
NAPI_CALL(env, napi_create_int32(env, nResult, &result));
return result;
}
napi_value CTPTraderClient::login(napi_env env, napi_callback_info info)
{
size_t argc = 4;
napi_value args[4];
napi_value _this;
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &_this, nullptr));
if (argc != 4) {
napi_value msg;
napi_create_string_utf8(env,
"Wrong number of arguments.Right Format:login(String:userID, String:password, String::brokerID,String::userProductInfo)",
NAPI_AUTO_LENGTH, &msg);
napi_value err;
napi_create_error(env, NULL, msg, &err);
napi_fatal_exception(env, err);
return NULL;
}
napi_valuetype userIDType;
NAPI_CALL(env, napi_typeof(env, args[0], &userIDType));
napi_valuetype passwordType;
NAPI_CALL(env, napi_typeof(env, args[1], &passwordType));
napi_valuetype brokerIDType;
NAPI_CALL(env, napi_typeof(env, args[2], &brokerIDType));
napi_valuetype userProductInfoType;
NAPI_CALL(env, napi_typeof(env, args[3], &userProductInfoType));
if (userIDType != napi_string
|| passwordType != napi_string
|| brokerIDType != napi_string
|| userProductInfoType != napi_string) {
napi_value msg;
napi_create_string_utf8(env,
"Parameter Type Error,Right Format:login(String:userID, String:password, String::brokerID,String::userProductInfo)",
NAPI_AUTO_LENGTH, &msg);
napi_value err;
napi_create_error(env, NULL, msg, &err);
napi_fatal_exception(env, err);
return NULL;
}
CThostFtdcReqUserLoginField req;
memset(&req, 0, sizeof(req));
//UserID 16字符
size_t buffer_size = 16;
size_t copied;
NAPI_CALL(env,
napi_get_value_string_utf8(env, args[0], req.UserID, buffer_size, &copied));
//Password 41字符
buffer_size = 41;
NAPI_CALL(env,
napi_get_value_string_utf8(env, args[1], req.Password, buffer_size, &copied));
//BrokerID 11字符
buffer_size = 11;
NAPI_CALL(env,
napi_get_value_string_utf8(env, args[2], req.BrokerID, buffer_size, &copied));
//UserProductInfo 11字符
buffer_size = 11;
NAPI_CALL(env,
napi_get_value_string_utf8(env, args[3], req.UserProductInfo, buffer_size, &copied));
CTPTraderClient* traderClient;
NAPI_CALL(env, napi_unwrap(env, _this, reinterpret_cast<void**>(&traderClient)));
//Js主线程请求号递增
requestID++;
//c++ void*泛型指针
int nResult = traderClient->invoke(&req, T_LOGIN_RE, requestID);
napi_value result;
NAPI_CALL(env, napi_create_int32(env, nResult, &result));
return result;
}
napi_value CTPTraderClient::querySettlementInfo(napi_env env, napi_callback_info info)
{
size_t argc = 2;
napi_value args[2];
napi_value _this;
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &_this, nullptr));
if (argc != 2) {
napi_value msg;
napi_create_string_utf8(env,
"Wrong number of arguments.Right Format:querySettlementInfo(String:userID, String::brokerID)",
NAPI_AUTO_LENGTH, &msg);
napi_value err;
napi_create_error(env, NULL, msg, &err);
napi_fatal_exception(env, err);
return NULL;
}
napi_valuetype investorIDType;
NAPI_CALL(env, napi_typeof(env, args[0], &investorIDType));
napi_valuetype brokerIDType;
NAPI_CALL(env, napi_typeof(env, args[1], &brokerIDType));
if (investorIDType != napi_string
|| brokerIDType != napi_string) {
napi_value msg;
napi_create_string_utf8(env,
"Parameter Type Error,Right Format:querySettlementInfo(String:userID, String::brokerID)",
NAPI_AUTO_LENGTH, &msg);
napi_value err;
napi_create_error(env, NULL, msg, &err);
napi_fatal_exception(env, err);
return NULL;
}
CThostFtdcQrySettlementInfoField req;
memset(&req, 0, sizeof(req));
//InvestorID 13字符
size_t buffer_size = 13;
size_t copied;
NAPI_CALL(env,
napi_get_value_string_utf8(env, args[0], req.InvestorID, buffer_size, &copied));
//BrokerID 11字符
buffer_size = 11;
NAPI_CALL(env,
napi_get_value_string_utf8(env, args[1], req.BrokerID, buffer_size, &copied));
CTPTraderClient* traderClient;
NAPI_CALL(env, napi_unwrap(env, _this, reinterpret_cast<void**>(&traderClient)));
//Js主线程请求号递增
requestID++;
//c++ void*泛型指针
int nResult = traderClient->invoke(&req, T_QuerySettlementInfo_RE, requestID);
napi_value result;
NAPI_CALL(env, napi_create_int32(env, nResult, &result));
return result;
}
napi_value CTPTraderClient::confirmSettlementInfo(napi_env env, napi_callback_info info)
{
size_t argc = 2;
napi_value args[2];
napi_value _this;
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &_this, nullptr));
if (argc != 2) {
napi_value msg;
napi_create_string_utf8(env,
"Wrong number of arguments.Right Format:confirmSettlementInfo(String:userID, String::brokerID)",
NAPI_AUTO_LENGTH, &msg);
napi_value err;
napi_create_error(env, NULL, msg, &err);
napi_fatal_exception(env, err);
return NULL;
}
napi_valuetype investorIDType;
NAPI_CALL(env, napi_typeof(env, args[0], &investorIDType));
napi_valuetype brokerIDType;
NAPI_CALL(env, napi_typeof(env, args[1], &brokerIDType));
if (investorIDType != napi_string
|| brokerIDType != napi_string) {
napi_value msg;
napi_create_string_utf8(env,
"Parameter Type Error,Right Format:confirmSettlementInfo(String:userID, String::brokerID)",
NAPI_AUTO_LENGTH, &msg);
napi_value err;
napi_create_error(env, NULL, msg, &err);
napi_fatal_exception(env, err);
return NULL;
}
CThostFtdcSettlementInfoConfirmField req;
memset(&req, 0, sizeof(req));
//InvestorID 13字符
size_t buffer_size = 13;
size_t copied;
NAPI_CALL(env,
napi_get_value_string_utf8(env, args[0], req.InvestorID, buffer_size, &copied));
//BrokerID 11字符
buffer_size = 11;
NAPI_CALL(env,
napi_get_value_string_utf8(env, args[1], req.BrokerID, buffer_size, &copied));
CTPTraderClient* traderClient;
NAPI_CALL(env, napi_unwrap(env, _this, reinterpret_cast<void**>(&traderClient)));
//Js主线程请求号递增
requestID++;
//c++ void*泛型指针
int nResult = traderClient->invoke(&req, T_CONFIRMSETTLEMENT_RE, requestID);
napi_value result;
NAPI_CALL(env, napi_create_int32(env, nResult, &result));
return result;
}
napi_value CTPTraderClient::queryInstrument(napi_env env, napi_callback_info info)
{
size_t argc = 0;
napi_value _this;
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, nullptr, &_this, nullptr));
if (argc != 0) {
napi_value msg;
napi_create_string_utf8(env,
"Wrong number of arguments.Right Format: queryInstrument()",
NAPI_AUTO_LENGTH, &msg);
napi_value err;
napi_create_error(env, NULL, msg, &err);
napi_fatal_exception(env, err);
return NULL;
}
CThostFtdcQryInstrumentField req;
memset(&req, 0, sizeof(req));
CTPTraderClient* traderClient;
NAPI_CALL(env, napi_unwrap(env, _this, reinterpret_cast<void**>(&traderClient)));
//Js主线程请求号递增
requestID++;
//c++ void*泛型指针
int nResult = traderClient->invoke(&req, T_QRYINSTRUMENT_RE, requestID);
napi_value result;
NAPI_CALL(env, napi_create_int32(env, nResult, &result));
return result;
}
napi_value CTPTraderClient::queryTradingAccount(napi_env env, napi_callback_info info)
{
size_t argc = 2;
napi_value args[2];
napi_value _this;
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &_this, nullptr));
if (argc != 2) {
napi_value msg;
napi_create_string_utf8(env,
"Wrong number of arguments.Right Format:queryTradingAccount(String:UserID, String:BrokerID)",
NAPI_AUTO_LENGTH, &msg);
napi_value err;
napi_create_error(env, NULL, msg, &err);
napi_fatal_exception(env, err);
return NULL;
}
napi_valuetype investorIDType;
NAPI_CALL(env, napi_typeof(env, args[0], &investorIDType));
napi_valuetype brokerIDType;
NAPI_CALL(env, napi_typeof(env, args[1], &brokerIDType));
if (investorIDType != napi_string
|| brokerIDType != napi_string) {
napi_value msg;
napi_create_string_utf8(env,
"Parameter Type Error,Right Format:queryTradingAccount(String:UserID, String:BrokerID)",
NAPI_AUTO_LENGTH, &msg);
napi_value err;
napi_create_error(env, NULL, msg, &err);
napi_fatal_exception(env, err);
return NULL;
}
CThostFtdcQryTradingAccountField req;
memset(&req, 0, sizeof(req));
//InvestorID 13字符
size_t buffer_size = 13;
size_t copied;
NAPI_CALL(env,
napi_get_value_string_utf8(env, args[0], req.InvestorID, buffer_size, &copied));
//BrokerID 11字符
buffer_size = 11;
NAPI_CALL(env,
napi_get_value_string_utf8(env, args[1], req.BrokerID, buffer_size, &copied));
CTPTraderClient* traderClient;
NAPI_CALL(env, napi_unwrap(env, _this, reinterpret_cast<void**>(&traderClient)));
//Js主线程请求号递增
requestID++;
//c++ void*泛型指针
int nResult = traderClient->invoke(&req, T_QRYTRADINGACCOUNT_RE, requestID);
if (nResult == 0)
{
nResult = requestID;
}
napi_value result;
NAPI_CALL(env, napi_create_int32(env, nResult, &result));
return result;
}
napi_value CTPTraderClient::queryInvestorPosition(napi_env env, napi_callback_info info)
{
size_t argc = 2;
napi_value args[2];
napi_value _this;
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &_this, nullptr));
if (argc != 2) {
napi_value msg;
napi_create_string_utf8(env,
"Wrong number of arguments.Right Format:queryInvestorPosition(String:UserID, String:BrokerID)",
NAPI_AUTO_LENGTH, &msg);
napi_value err;
napi_create_error(env, NULL, msg, &err);
napi_fatal_exception(env, err);
return NULL;
}
napi_valuetype investorIDType;
NAPI_CALL(env, napi_typeof(env, args[0], &investorIDType));
napi_valuetype brokerIDType;
NAPI_CALL(env, napi_typeof(env, args[1], &brokerIDType));
if (investorIDType != napi_string
|| brokerIDType != napi_string) {
napi_value msg;
napi_create_string_utf8(env,
"Parameter Type Error,Right Format:queryInvestorPosition(String:UserID, String:BrokerID)",
NAPI_AUTO_LENGTH, &msg);
napi_value err;
napi_create_error(env, NULL, msg, &err);
napi_fatal_exception(env, err);
return NULL;
}
CThostFtdcQryInvestorPositionField req;
memset(&req, 0, sizeof(req));
//InvestorID 13字符
size_t buffer_size = 13;
size_t copied;
NAPI_CALL(env,
napi_get_value_string_utf8(env, args[0], req.InvestorID, buffer_size, &copied));
//BrokerID 11字符
buffer_size = 11;
NAPI_CALL(env,
napi_get_value_string_utf8(env, args[1], req.BrokerID, buffer_size, &copied));
CTPTraderClient* traderClient;
NAPI_CALL(env, napi_unwrap(env, _this, reinterpret_cast<void**>(&traderClient)));
//Js主线程请求号递增
requestID++;
//c++ void*泛型指针
int nResult = traderClient->invoke(&req, T_QRYINVESTORPOSITION_RE, requestID);
napi_value result;
NAPI_CALL(env, napi_create_int32(env, nResult, &result));
return result;
}
napi_value CTPTraderClient::queryCommissionRate(napi_env env, napi_callback_info info)
{
size_t argc = 3;
napi_value args[3];
napi_value _this;
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &_this, nullptr));
if (argc != 3) {
napi_value msg;
napi_create_string_utf8(env,
"Wrong number of arguments.Right Format:queryCommissionRate(String:UserID, String:BrokerID,String:InstrumentID)",
NAPI_AUTO_LENGTH, &msg);
napi_value err;
napi_create_error(env, NULL, msg, &err);
napi_fatal_exception(env, err);
return NULL;
}
napi_valuetype investorIDType;
NAPI_CALL(env, napi_typeof(env, args[0], &investorIDType));
napi_valuetype brokerIDType;
NAPI_CALL(env, napi_typeof(env, args[1], &brokerIDType));
napi_valuetype instrumentIDType;
NAPI_CALL(env, napi_typeof(env, args[2], &instrumentIDType));
if (investorIDType != napi_string
|| brokerIDType != napi_string
|| instrumentIDType != napi_string) {
napi_value msg;
napi_create_string_utf8(env,
"Wrong number of arguments.Right Format:queryCommissionRate(String:UserID, String:BrokerID,String:InstrumentID)",
NAPI_AUTO_LENGTH, &msg);
napi_value err;
napi_create_error(env, NULL, msg, &err);
napi_fatal_exception(env, err);
return NULL;
}
CThostFtdcQryInstrumentCommissionRateField req;
memset(&req, 0, sizeof(req));
//InvestorID 13字符
size_t buffer_size = 13;
size_t copied;
NAPI_CALL(env,
napi_get_value_string_utf8(env, args[0], req.InvestorID, buffer_size, &copied));
//BrokerID 11字符
buffer_size = 11;
NAPI_CALL(env,
napi_get_value_string_utf8(env, args[1], req.BrokerID, buffer_size, &copied));
//InstrumentID 31字符
buffer_size = 31;
NAPI_CALL(env,
napi_get_value_string_utf8(env, args[2], req.InstrumentID, buffer_size, &copied));
CTPTraderClient* traderClient;
NAPI_CALL(env, napi_unwrap(env, _this, reinterpret_cast<void**>(&traderClient)));
//Js主线程请求号递增
requestID++;
//c++ void*泛型指针
int nResult = traderClient->invoke(&req, T_QueryCommissionRate_RE, requestID);
napi_value result;
NAPI_CALL(env, napi_create_int32(env, nResult, &result));
return result;
}
napi_value CTPTraderClient::sendOrder(napi_env env, napi_callback_info info)
{
size_t argc = 1;
napi_value args[1];
napi_value _this;
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &_this, nullptr));
if (argc != 1) {
napi_value msg;
napi_create_string_utf8(env,
"Wrong number of arguments.Right Format: sendOrder(Object:requestObj)",
NAPI_AUTO_LENGTH, &msg);
napi_value err;
napi_create_error(env, NULL, msg, &err);
napi_fatal_exception(env, err);
return NULL;
}
napi_valuetype sendOrderInfoType;
NAPI_CALL(env, napi_typeof(env, args[0], &sendOrderInfoType));
if (sendOrderInfoType != napi_object) {
napi_value msg;
napi_create_string_utf8(env,
"Parameter Type Error,Right Format: sendOrder(Object:requestObj)",
NAPI_AUTO_LENGTH, &msg);
napi_value err;
napi_create_error(env, NULL, msg, &err);
napi_fatal_exception(env, err);
return NULL;
}
napi_value OrderInfo = args[0];
size_t buffer_size = 100;
size_t copied;
///1.经纪公司代码
//TThostFtdcBrokerIDType BrokerID;