Skip to content

Commit ca5b6da

Browse files
committed
C版接口与C++版接口初始提交
1 parent 1b0fb3e commit ca5b6da

40 files changed

+1403
-150
lines changed

QuantBox.XAPI/Callback/XApi.Trade.cs

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public void ReqQrySettlementInfo(string szTradingDay)
8686
Marshal.FreeHGlobal(szTradingDayPtr);
8787
}
8888

89-
public void SendOrder(int OrderRef, ref OrderField[] orders,out string[] OrderRefs)
89+
public void SendOrder(ref OrderField[] orders,out string[] OrderRefs)
9090
{
9191
int OrderField_size = Marshal.SizeOf(typeof(OrderField));
9292
int OrderIDType_size = Marshal.SizeOf(typeof(OrderIDType));
@@ -100,9 +100,9 @@ public void SendOrder(int OrderRef, ref OrderField[] orders,out string[] OrderRe
100100
Marshal.StructureToPtr(orders[i], OrderField_Ptr + i * OrderField_size, false);
101101
}
102102

103-
IntPtr ptr = proxy.XRequest((byte)RequestType.ReqOrderInsert, Handle, OrderIDType_Ptr,
104-
OrderRef, 0,
105-
OrderField_Ptr, orders.Length, IntPtr.Zero, 0, IntPtr.Zero, 0);
103+
IntPtr ptr = proxy.XRequest((byte)RequestType.ReqOrderInsert, Handle, IntPtr.Zero,
104+
0, 0,
105+
OrderField_Ptr, orders.Length, OrderIDType_Ptr, 0, IntPtr.Zero, 0);
106106

107107
OrderRefs = new string[orders.Length];
108108

@@ -133,8 +133,8 @@ public void CancelOrder(string[] szId,out string[] errs)
133133
Marshal.StructureToPtr(_szId, Input_Ptr + i * OrderIDType_size, false);
134134
}
135135

136-
IntPtr ptr = proxy.XRequest((byte)RequestType.ReqOrderAction, Handle, Output_Ptr, 0, 0,
137-
Input_Ptr, szId.Length, IntPtr.Zero, 0, IntPtr.Zero, 0);
136+
IntPtr ptr = proxy.XRequest((byte)RequestType.ReqOrderAction, Handle, IntPtr.Zero, 0, 0,
137+
Input_Ptr, szId.Length, Output_Ptr, 0, IntPtr.Zero, 0);
138138

139139
errs = new string[szId.Length];
140140

@@ -150,35 +150,63 @@ public void CancelOrder(string[] szId,out string[] errs)
150150
Marshal.FreeHGlobal(Output_Ptr);
151151
}
152152

153-
public string SendQuote(int QuoteRef, ref QuoteField quote)
153+
public void SendQuote(ref QuoteField quote,out string AskRef,out string BidRef)
154154
{
155-
int size = Marshal.SizeOf(typeof(QuoteField));
155+
int QuoteField_size = Marshal.SizeOf(typeof(QuoteField));
156+
int OrderIDType_size = Marshal.SizeOf(typeof(OrderIDType));
157+
158+
IntPtr QuoteField_Ptr = Marshal.AllocHGlobal(QuoteField_size);
159+
IntPtr AskRef_Ptr = Marshal.AllocHGlobal(OrderIDType_size);
160+
IntPtr BidRef_Ptr = Marshal.AllocHGlobal(OrderIDType_size);
156161

157-
IntPtr quotePtr = Marshal.AllocHGlobal(size);
158-
Marshal.StructureToPtr(quote, quotePtr, false);
162+
// 将结构体写成内存块
163+
for (int i = 0; i < 1; ++i)
164+
{
165+
Marshal.StructureToPtr(quote, QuoteField_Ptr + i * QuoteField_size, false);
166+
}
159167

160168
IntPtr ptr = proxy.XRequest((byte)RequestType.ReqQuoteInsert, Handle, IntPtr.Zero,
161-
QuoteRef, 0,
162-
quotePtr, size, IntPtr.Zero, 0, IntPtr.Zero, 0);
169+
0, 0,
170+
QuoteField_Ptr, 1, AskRef_Ptr, 0, BidRef_Ptr, 0);
163171

164-
Marshal.FreeHGlobal(quotePtr);
172+
AskRef = string.Empty;
173+
BidRef = string.Empty;
165174

166-
if (ptr.ToInt64() == 0)
167-
return null;
175+
for (int i = 0; i < 1; ++i)
176+
{
177+
// 这里定义一个ID占64字节
178+
OrderIDType output = (OrderIDType)Marshal.PtrToStructure(AskRef_Ptr + i * OrderIDType_size, typeof(OrderIDType));
179+
AskRef = output.ID;
180+
output = (OrderIDType)Marshal.PtrToStructure(BidRef_Ptr + i * OrderIDType_size, typeof(OrderIDType));
181+
BidRef = output.ID;
182+
}
168183

169-
return Marshal.PtrToStringAnsi(ptr);
184+
Marshal.FreeHGlobal(QuoteField_Ptr);
185+
Marshal.FreeHGlobal(AskRef_Ptr);
186+
Marshal.FreeHGlobal(BidRef_Ptr);
170187
}
171188

172-
public int CancelQuote(string szId)
189+
public void CancelQuote(string szId,out string err)
173190
{
174191
IntPtr szIdPtr = Marshal.StringToHGlobalAnsi(szId);
192+
int OrderIDType_size = Marshal.SizeOf(typeof(OrderIDType));
193+
IntPtr OrderIDType_Ptr = Marshal.AllocHGlobal(OrderIDType_size);
175194

176195
IntPtr ptr = proxy.XRequest((byte)RequestType.ReqQuoteAction, Handle, IntPtr.Zero, 0, 0,
177-
szIdPtr, 0, IntPtr.Zero, 0, IntPtr.Zero, 0);
196+
szIdPtr, 0, OrderIDType_Ptr, 0, IntPtr.Zero, 0);
178197

179-
Marshal.FreeHGlobal(szIdPtr);
198+
err = string.Empty;
199+
200+
for (int i = 0; i < 1; ++i)
201+
{
202+
// 这里定义一个ID占64字节
203+
OrderIDType output = (OrderIDType)Marshal.PtrToStructure(OrderIDType_Ptr + i * OrderIDType_size, typeof(OrderIDType));
180204

181-
return ptr.ToInt32();
205+
err = output.ID;
206+
}
207+
208+
Marshal.FreeHGlobal(szIdPtr);
209+
Marshal.FreeHGlobal(OrderIDType_Ptr);
182210
}
183211

184212
private void _OnRspQryTradingAccount(IntPtr ptr1, int size1, double double1)

QuantBox.XAPI/DllInvoke.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Runtime.InteropServices;

QuantBox.XAPI/Interface/IXApi.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ public interface IXTrade
4545
void ReqQryTradingAccount();
4646
void ReqQryInvestorPosition(string szInstrument, string szExchange);
4747
void ReqQrySettlementInfo(string szTradingDay);
48-
void SendOrder(int OrderRef, ref OrderField[] orders, out string[] OrderRefs);
48+
void SendOrder(ref OrderField[] orders, out string[] OrderRefs);
4949
void CancelOrder(string[] szId,out string[] errs);
50-
string SendQuote(int QuoteRef, ref QuoteField quote);
51-
int CancelQuote(string szId);
50+
void SendQuote(ref QuoteField quote,out string AskRef,out string BidRef);
51+
void CancelQuote(string szId,out string err);
5252
}
5353

5454
public interface IXHistoricalData

QuantBox_CTP_Quote/MdUserApi.cpp

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,11 @@ using namespace std;
1919
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)
2020
{
2121
// 由内部调用,不用检查是否为空
22-
CMdUserApi* pApi = (CMdUserApi*)pApi1;
22+
CMdUserApi* pApi = (CMdUserApi*)pApi2;
2323
pApi->QueryInThread(type, pApi1, pApi2, double1, double2, ptr1, size1, ptr2, size2, ptr3, size3);
2424
return nullptr;
2525
}
2626

27-
2827
CMdUserApi::CMdUserApi(void)
2928
{
3029
m_pApi = nullptr;
@@ -35,7 +34,7 @@ CMdUserApi::CMdUserApi(void)
3534
m_msgQueue = new CMsgQueue();
3635
m_msgQueue_Query = new CMsgQueue();
3736

38-
m_msgQueue_Query->Register((void*)Query);
37+
m_msgQueue_Query->Register((void*)Query, this);
3938
m_msgQueue_Query->StartThread();
4039
}
4140

@@ -74,13 +73,14 @@ void CMdUserApi::QueryInThread(char type, void* pApi1, void* pApi2, double doubl
7473
this_thread::sleep_for(chrono::milliseconds(m_nSleep));
7574
}
7675

77-
void CMdUserApi::Register(void* pCallback)
76+
void CMdUserApi::Register(void* pCallback,void* pClass)
7877
{
78+
m_pClass = pClass;
7979
if (m_msgQueue == nullptr)
8080
return;
8181

82-
m_msgQueue_Query->Register((void*)Query);
83-
m_msgQueue->Register(pCallback);
82+
m_msgQueue_Query->Register((void*)Query,this);
83+
m_msgQueue->Register(pCallback,this);
8484
if (pCallback)
8585
{
8686
m_msgQueue_Query->StartThread();
@@ -108,7 +108,7 @@ bool CMdUserApi::IsErrorRspInfo(CThostFtdcRspInfoField *pRspInfo, int nRequestID
108108
pField->ErrorID = pRspInfo->ErrorID;
109109
strcpy(pField->ErrorMsg, pRspInfo->ErrorMsg);
110110

111-
m_msgQueue->Input_NoCopy(ResponeType::OnRtnError, m_msgQueue, this, bIsLast, 0, pField, sizeof(ErrorField), nullptr, 0, nullptr, 0);
111+
m_msgQueue->Input_NoCopy(ResponeType::OnRtnError, m_msgQueue, m_pClass, bIsLast, 0, pField, sizeof(ErrorField), nullptr, 0, nullptr, 0);
112112
}
113113
return bRet;
114114
}
@@ -129,7 +129,7 @@ void CMdUserApi::Connect(const string& szPath,
129129
memcpy(&m_ServerInfo, pServerInfo, sizeof(ServerInfoField));
130130
memcpy(&m_UserInfo, pUserInfo, sizeof(UserInfoField));
131131

132-
m_msgQueue_Query->Input_NoCopy(RequestType::E_Init, this, nullptr, 0, 0,
132+
m_msgQueue_Query->Input_NoCopy(RequestType::E_Init, m_msgQueue_Query, this, 0, 0,
133133
nullptr, 0, nullptr, 0, nullptr, 0);
134134
}
135135

@@ -143,7 +143,7 @@ int CMdUserApi::_Init()
143143
m_pApi = CThostFtdcMdApi::CreateFtdcMdApi(pszPath, m_ServerInfo.IsUsingUdp, m_ServerInfo.IsMulticast);
144144
delete[] pszPath;
145145

146-
m_msgQueue->Input_NoCopy(ResponeType::OnConnectionStatus, m_msgQueue, this, ConnectionStatus::Initialized, 0, nullptr, 0, nullptr, 0, nullptr, 0);
146+
m_msgQueue->Input_NoCopy(ResponeType::OnConnectionStatus, m_msgQueue, m_pClass, ConnectionStatus::Initialized, 0, nullptr, 0, nullptr, 0, nullptr, 0);
147147

148148
if (m_pApi)
149149
{
@@ -167,7 +167,7 @@ int CMdUserApi::_Init()
167167

168168
//初始化连接
169169
m_pApi->Init();
170-
m_msgQueue->Input_NoCopy(ResponeType::OnConnectionStatus, m_msgQueue, this, ConnectionStatus::Connecting, 0, nullptr, 0, nullptr, 0, nullptr, 0);
170+
m_msgQueue->Input_NoCopy(ResponeType::OnConnectionStatus, m_msgQueue, m_pClass, ConnectionStatus::Connecting, 0, nullptr, 0, nullptr, 0, nullptr, 0);
171171
}
172172

173173
return 0;
@@ -181,13 +181,13 @@ void CMdUserApi::ReqUserLogin()
181181
strncpy(pBody->UserID, m_UserInfo.UserID, sizeof(TThostFtdcInvestorIDType));
182182
strncpy(pBody->Password, m_UserInfo.Password, sizeof(TThostFtdcPasswordType));
183183

184-
m_msgQueue_Query->Input_NoCopy(RequestType::E_ReqUserLoginField, this, nullptr, 0, 0,
184+
m_msgQueue_Query->Input_NoCopy(RequestType::E_ReqUserLoginField, m_msgQueue_Query, this, 0, 0,
185185
pBody, sizeof(CThostFtdcReqUserLoginField), nullptr, 0, nullptr, 0);
186186
}
187187

188188
int CMdUserApi::_ReqUserLogin(char type, void* pApi1, void* pApi2, double double1, double double2, void* ptr1, int size1, void* ptr2, int size2, void* ptr3, int size3)
189189
{
190-
m_msgQueue->Input_NoCopy(ResponeType::OnConnectionStatus, m_msgQueue, this, ConnectionStatus::Logining, 0, nullptr, 0, nullptr, 0, nullptr, 0);
190+
m_msgQueue->Input_NoCopy(ResponeType::OnConnectionStatus, m_msgQueue, m_pClass, ConnectionStatus::Logining, 0, nullptr, 0, nullptr, 0, nullptr, 0);
191191
return m_pApi->ReqUserLogin((CThostFtdcReqUserLoginField*)ptr1, ++m_lRequestID);
192192
}
193193

@@ -197,7 +197,7 @@ void CMdUserApi::Disconnect()
197197
if (m_msgQueue_Query)
198198
{
199199
m_msgQueue_Query->StopThread();
200-
m_msgQueue_Query->Register(nullptr);
200+
m_msgQueue_Query->Register(nullptr,nullptr);
201201
m_msgQueue_Query->Clear();
202202
delete m_msgQueue_Query;
203203
m_msgQueue_Query = nullptr;
@@ -211,7 +211,7 @@ void CMdUserApi::Disconnect()
211211

212212
// 全清理,只留最后一个
213213
m_msgQueue->Clear();
214-
m_msgQueue->Input_NoCopy(ResponeType::OnConnectionStatus, m_msgQueue, this, ConnectionStatus::Disconnected, 0, nullptr, 0, nullptr, 0, nullptr, 0);
214+
m_msgQueue->Input_NoCopy(ResponeType::OnConnectionStatus, m_msgQueue, m_pClass, ConnectionStatus::Disconnected, 0, nullptr, 0, nullptr, 0, nullptr, 0);
215215
// 主动触发
216216
m_msgQueue->Process();
217217
}
@@ -220,7 +220,7 @@ void CMdUserApi::Disconnect()
220220
if (m_msgQueue)
221221
{
222222
m_msgQueue->StopThread();
223-
m_msgQueue->Register(nullptr);
223+
m_msgQueue->Register(nullptr,nullptr);
224224
m_msgQueue->Clear();
225225
delete m_msgQueue;
226226
m_msgQueue = nullptr;
@@ -378,7 +378,7 @@ void CMdUserApi::UnsubscribeQuote(const string& szInstrumentIDs, const string& s
378378

379379
void CMdUserApi::OnFrontConnected()
380380
{
381-
m_msgQueue->Input_NoCopy(ResponeType::OnConnectionStatus, m_msgQueue, this, ConnectionStatus::Connected, 0, nullptr, 0, nullptr, 0, nullptr, 0);
381+
m_msgQueue->Input_NoCopy(ResponeType::OnConnectionStatus, m_msgQueue, m_pClass, ConnectionStatus::Connected, 0, nullptr, 0, nullptr, 0, nullptr, 0);
382382

383383
//连接成功后自动请求登录
384384
ReqUserLogin();
@@ -391,7 +391,7 @@ void CMdUserApi::OnFrontDisconnected(int nReason)
391391
pField->ErrorID = nReason;
392392
GetOnFrontDisconnectedMsg(nReason, pField->ErrorMsg);
393393

394-
m_msgQueue->Input_NoCopy(ResponeType::OnConnectionStatus, m_msgQueue, this, ConnectionStatus::Disconnected, 0, pField, sizeof(RspUserLoginField), nullptr, 0, nullptr, 0);
394+
m_msgQueue->Input_NoCopy(ResponeType::OnConnectionStatus, m_msgQueue, m_pClass, ConnectionStatus::Disconnected, 0, pField, sizeof(RspUserLoginField), nullptr, 0, nullptr, 0);
395395
}
396396

397397
void CMdUserApi::OnRspUserLogin(CThostFtdcRspUserLoginField *pRspUserLogin, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
@@ -406,8 +406,8 @@ void CMdUserApi::OnRspUserLogin(CThostFtdcRspUserLoginField *pRspUserLogin, CTho
406406

407407
sprintf(pField->SessionID, "%d:%d", pRspUserLogin->FrontID, pRspUserLogin->SessionID);
408408

409-
m_msgQueue->Input_NoCopy(ResponeType::OnConnectionStatus, m_msgQueue, this, ConnectionStatus::Logined, 0, pField, sizeof(RspUserLoginField), nullptr, 0, nullptr, 0);
410-
m_msgQueue->Input_NoCopy(ResponeType::OnConnectionStatus, m_msgQueue, this, ConnectionStatus::Done, 0, nullptr, 0, nullptr, 0, nullptr, 0);
409+
m_msgQueue->Input_NoCopy(ResponeType::OnConnectionStatus, m_msgQueue, m_pClass, ConnectionStatus::Logined, 0, pField, sizeof(RspUserLoginField), nullptr, 0, nullptr, 0);
410+
m_msgQueue->Input_NoCopy(ResponeType::OnConnectionStatus, m_msgQueue, m_pClass, ConnectionStatus::Done, 0, nullptr, 0, nullptr, 0, nullptr, 0);
411411

412412
//有可能断线了,本处是断线重连后重新订阅
413413
set<string> mapOld = m_setInstrumentIDs;//记下上次订阅的合约
@@ -423,7 +423,7 @@ void CMdUserApi::OnRspUserLogin(CThostFtdcRspUserLoginField *pRspUserLogin, CTho
423423
pField->ErrorID = pRspInfo->ErrorID;
424424
strncpy(pField->ErrorMsg, pRspInfo->ErrorMsg, sizeof(ErrorMsgType));
425425

426-
m_msgQueue->Input_NoCopy(ResponeType::OnConnectionStatus, m_msgQueue, this, ConnectionStatus::Disconnected, 0, pField, sizeof(RspUserLoginField), nullptr, 0, nullptr, 0);
426+
m_msgQueue->Input_NoCopy(ResponeType::OnConnectionStatus, m_msgQueue, m_pClass, ConnectionStatus::Disconnected, 0, pField, sizeof(RspUserLoginField), nullptr, 0, nullptr, 0);
427427
}
428428
}
429429

@@ -551,7 +551,7 @@ void CMdUserApi::OnRtnDepthMarketData(CThostFtdcDepthMarketDataField *pDepthMark
551551
pField->AskVolume5 = pDepthMarketData->AskVolume5;
552552
} while (false);
553553

554-
m_msgQueue->Input_NoCopy(ResponeType::OnRtnDepthMarketData, m_msgQueue, this, 0, 0, pField, sizeof(DepthMarketDataField), nullptr, 0, nullptr, 0);
554+
m_msgQueue->Input_NoCopy(ResponeType::OnRtnDepthMarketData, m_msgQueue, m_pClass, 0, 0, pField, sizeof(DepthMarketDataField), nullptr, 0, nullptr, 0);
555555
//}
556556
}
557557

@@ -590,5 +590,5 @@ void CMdUserApi::OnRtnForQuoteRsp(CThostFtdcForQuoteRspField *pForQuoteRsp)
590590
sprintf(pField->Symbol, "%s.%s", pField->InstrumentID, pField->ExchangeID);
591591
strcpy(pField->QuoteID, pForQuoteRsp->ForQuoteSysID);
592592

593-
m_msgQueue->Input_NoCopy(ResponeType::OnRtnQuoteRequest, m_msgQueue, this, 0, 0, pField, sizeof(QuoteRequestField), nullptr, 0, nullptr, 0);
593+
m_msgQueue->Input_NoCopy(ResponeType::OnRtnQuoteRequest, m_msgQueue, m_pClass, 0, 0, pField, sizeof(QuoteRequestField), nullptr, 0, nullptr, 0);
594594
}

QuantBox_CTP_Quote/MdUserApi.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class CMdUserApi :
3333
CMdUserApi(void);
3434
virtual ~CMdUserApi(void);
3535

36-
void Register(void* pCallback);
36+
void Register(void* pCallback, void* pClass);
3737
ConfigInfoField* Config(ConfigInfoField* pConfigInfo);
3838

3939
void Connect(const string& szPath,
@@ -95,5 +95,6 @@ class CMdUserApi :
9595

9696
CMsgQueue* m_msgQueue; //消息队列指针
9797
CMsgQueue* m_msgQueue_Query;
98+
void* m_pClass;
9899
};
99100

QuantBox_CTP_Quote/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ void* __stdcall XRequest(char type, void* pApi1, void* pApi2, double double1, do
3939
delete pApi;
4040
return 0;
4141
case Register:
42-
pApi->Register(ptr1);
42+
pApi->Register(ptr1,ptr2);
4343
break;
4444
case Config:
4545
return (void*)pApi->Config((ConfigInfoField*)ptr1);

0 commit comments

Comments
 (0)