forked from QuantBox/CTP
/
MdApiWrapper.cs
209 lines (182 loc) · 6.52 KB
/
MdApiWrapper.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
namespace QuantBox.CSharp2CTP
{
public class MdApiWrapper : IDisposable
{
public event OnConnectHander OnConnect;
public event OnDisconnectHander OnDisconnect;
public event OnRspErrorHander OnRspError;
public event OnRtnDepthMarketDataHander OnRtnDepthMarketData;
private readonly fnOnConnect _fnOnConnect_Holder;
private readonly fnOnDisconnect _fnOnDisconnect_Holder;
private readonly fnOnRspError _fnOnRspError_Holder;
private readonly fnOnRtnDepthMarketData _fnOnRtnDepthMarketData_Holder;
private readonly object _lockMd = new object();
private readonly object _lockMsgQueue = new object();
private IntPtr m_pMdApi = IntPtr.Zero;
private IntPtr m_pMsgQueue = IntPtr.Zero;
private volatile bool _bMdConnected;
private bool disposed;
private string szPath;
private string szAddresses;
private string szBrokerId;
private string szInvestorId;
private string szPassword;
public MdApiWrapper()
{
_fnOnConnect_Holder = OnConnect_callback;
_fnOnDisconnect_Holder = OnDisconnect_callback;
_fnOnRspError_Holder = OnRspError_callback;
_fnOnRtnDepthMarketData_Holder = OnRtnDepthMarketData_callback;
}
//Implement IDisposable.
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
// Free other state (managed objects).
}
// Free your own state (unmanaged objects).
// Set large fields to null.
Disconnect();
disposed = true;
}
//base.Dispose(disposing);
}
// Use C# destructor syntax for finalization code.
~MdApiWrapper()
{
// Simply call Dispose(false).
Dispose(false);
}
public void Connect(string szPath, string szAddresses, string szBrokerId, string szInvestorId, string szPassword)
{
this.szPath = szPath;
this.szAddresses = szAddresses;
this.szBrokerId = szBrokerId;
this.szInvestorId = szInvestorId;
this.szPassword = szPassword;
Disconnect_MD();
Connect_MsgQueue();
Connect_MD();
}
public void Disconnect()
{
Disconnect_MD();
Disconnect_MsgQueue();
}
//建立行情
private void Connect_MD()
{
lock (_lockMd)
{
if (null == m_pMdApi || IntPtr.Zero == m_pMdApi)
{
m_pMdApi = MdApi.MD_CreateMdApi();
MdApi.CTP_RegOnRtnDepthMarketData(m_pMsgQueue, _fnOnRtnDepthMarketData_Holder);
MdApi.MD_RegMsgQueue2MdApi(m_pMdApi, m_pMsgQueue);
MdApi.MD_Connect(m_pMdApi, szPath, szAddresses, szBrokerId, szInvestorId, szPassword);
}
}
}
private void Disconnect_MD()
{
lock (_lockMd)
{
if (null != m_pMdApi && IntPtr.Zero != m_pMdApi)
{
MdApi.MD_RegMsgQueue2MdApi(m_pMdApi, IntPtr.Zero);
MdApi.MD_ReleaseMdApi(m_pMdApi);
m_pMdApi = IntPtr.Zero;
}
_bMdConnected = false;
}
}
private void Connect_MsgQueue()
{
lock (_lockMsgQueue)
{
if (null == m_pMsgQueue || IntPtr.Zero == m_pMsgQueue)
{
m_pMsgQueue = CommApi.CTP_CreateMsgQueue();
CommApi.CTP_RegOnConnect(m_pMsgQueue, _fnOnConnect_Holder);
CommApi.CTP_RegOnDisconnect(m_pMsgQueue, _fnOnDisconnect_Holder);
CommApi.CTP_RegOnRspError(m_pMsgQueue, _fnOnRspError_Holder);
//由底层启动线程
CommApi.CTP_StartMsgQueue(m_pMsgQueue);
}
}
}
private void Disconnect_MsgQueue()
{
lock (_lockMsgQueue)
{
if (null != m_pMsgQueue && IntPtr.Zero != m_pMsgQueue)
{
//停止底层线程
CommApi.CTP_StopMsgQueue(m_pMsgQueue);
CommApi.CTP_ReleaseMsgQueue(m_pMsgQueue);
m_pMsgQueue = IntPtr.Zero;
}
}
}
public void Subscribe(string inst)
{
lock (_lockMd)
{
if (null != m_pMdApi && IntPtr.Zero != m_pMdApi)
{
MdApi.MD_Subscribe(m_pMdApi, inst,null);
}
}
}
public void Unsubscribe(string inst)
{
lock (_lockMd)
{
if (null != m_pMdApi && IntPtr.Zero != m_pMdApi)
{
MdApi.MD_Unsubscribe(m_pMdApi, inst,null);
}
}
}
private void OnConnect_callback(IntPtr pApi, ref CThostFtdcRspUserLoginField pRspUserLogin, ConnectionStatus result)
{
_bMdConnected = (ConnectionStatus.E_logined == result);
if (null != OnConnect)
{
OnConnect(this, new OnConnectArgs(pApi, ref pRspUserLogin, result));
}
}
private void OnDisconnect_callback(IntPtr pApi, ref CThostFtdcRspInfoField pRspInfo, ConnectionStatus step)
{
if (null != OnDisconnect)
{
OnDisconnect(this, new OnDisconnectArgs(pApi, ref pRspInfo, step));
}
}
private void OnRspError_callback(IntPtr pApi, ref CThostFtdcRspInfoField pRspInfo, int nRequestID, bool bIsLast)
{
if (null != OnRspError)
{
OnRspError(this, new OnRspErrorArgs(pApi, ref pRspInfo, nRequestID, bIsLast));
}
}
private void OnRtnDepthMarketData_callback(IntPtr pMdUserApi, ref CThostFtdcDepthMarketDataField pDepthMarketData)
{
if (null != OnRtnDepthMarketData)
{
OnRtnDepthMarketData(this, new OnRtnDepthMarketDataArgs(pMdUserApi, ref pDepthMarketData));
}
}
}
}