forked from QuantBox/QuantBox_XAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBaseApi.cs
304 lines (250 loc) · 9.41 KB
/
BaseApi.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
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
using NLog;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
namespace QuantBox.XAPI.Callback
{
public class BaseApi : IDisposable
{
public Logger Log;
protected Proxy proxy;
protected IntPtr Handle = IntPtr.Zero;
public string LibPath;
protected XCall _XRespone;
public ServerInfoField Server;
public UserInfoField User;
public List<UserInfoField> UserList = new List<UserInfoField>();
public DelegateOnConnectionStatus OnConnectionStatus
{
get { return OnConnectionStatus_; }
set { OnConnectionStatus_ = value; }
}
public DelegateOnRtnError OnRtnError
{
get { return OnRtnError_; }
set { OnRtnError_ = value; }
}
private DelegateOnConnectionStatus OnConnectionStatus_;
private DelegateOnRtnError OnRtnError_;
public RspUserLoginField UserLoginField;
protected object locker = new object();
public bool IsConnected { get; protected set; }
private int _reconnectInterval;
public int ReconnectInterval
{
get { return _reconnectInterval; }
set {
if (_reconnectInterval == value)
return;
_reconnectInterval = value;
_Timer.Elapsed -= _Timer_Elapsed;
if (_reconnectInterval >= 10)
{
_Timer.Elapsed += _Timer_Elapsed;
_Timer.Interval = _reconnectInterval*1000;
}
_Timer.Enabled = _reconnectInterval >= 10;
}
}
private System.Timers.Timer _Timer = new System.Timers.Timer();
public BaseApi()
{
// 这里是因为回调函数可能被GC回收
_XRespone = _OnRespone;
ReconnectInterval = 0;
}
public BaseApi(string path)
: this()
{
LibPath = path;
}
void _Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
_Timer.Enabled = false;
lock (locker)
{
// 定时检查是否需要销毁对象重连
if (!IsConnected)
{
Disconnect();
Connect();
}
}
_Timer.Enabled = true;
}
~BaseApi()
{
Dispose(false);
}
private bool disposed;
// 一定要先调用API的,再调用队列的,否则会出错
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);
}
public virtual void Connect()
{
lock(locker)
{
// XSpeed多次连接出问题,发现会生成多次
if (proxy == null)
{
proxy = new Proxy(LibPath);
}
Handle = proxy.XRequest((byte)RequestType.Create, IntPtr.Zero, IntPtr.Zero, 0, 0, IntPtr.Zero, 0, IntPtr.Zero, 0, IntPtr.Zero, 0);
RegisterAndStart(Marshal.GetFunctionPointerForDelegate(_XRespone));
IntPtr ServerIntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(ServerInfoField)));
Marshal.StructureToPtr(Server, ServerIntPtr, false);
IntPtr UserListIntPtr = IntPtr.Zero;
int count = UserList.Count;
if (UserList.Count > 0)
{
UserListIntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(UserInfoField)) * count);
for (int i = 0; i < UserList.Count; ++i)
{
Marshal.StructureToPtr(UserList[i], new IntPtr(UserListIntPtr.ToInt64() + i * Marshal.SizeOf(typeof(UserInfoField))), false);
}
}
else
{
count = 1;
UserListIntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(UserInfoField)));
Marshal.StructureToPtr(User, UserListIntPtr, false);
}
// 进行连接
proxy.XRequest((byte)RequestType.Connect, Handle, IntPtr.Zero, 0, 0, ServerIntPtr, 0, UserListIntPtr, count, Marshal.StringToHGlobalAnsi(Path.GetTempPath()), 0);
Marshal.FreeHGlobal(ServerIntPtr);
Marshal.FreeHGlobal(UserListIntPtr);
}
}
public virtual void Disconnect()
{
lock(locker)
{
_Timer.Enabled = false;
IsConnected = false;
if (proxy != null && Handle.ToInt64() !=0)
{
proxy.XRequest((byte)RequestType.Disconnect, Handle, IntPtr.Zero, 0, 0, IntPtr.Zero, 0, IntPtr.Zero, 0, IntPtr.Zero, 0);
proxy.XRequest((byte)RequestType.Release, Handle, IntPtr.Zero, 0, 0, IntPtr.Zero, 0, IntPtr.Zero, 0, IntPtr.Zero, 0);
proxy.Dispose();
}
proxy = null;
Handle = IntPtr.Zero;
}
}
public void RegisterAndStart(IntPtr ptr1)
{
lock (this)
{
if (proxy != null)
{
proxy.XRequest((byte)RequestType.Register, Handle, IntPtr.Zero, 0, 0, ptr1, 0, IntPtr.Zero, 0, IntPtr.Zero, 0);
}
}
}
public ApiType GetApiType
{
get{
if (proxy == null)
{
proxy = new Proxy(LibPath);
}
return (ApiType)proxy.XRequest((byte)RequestType.GetApiType, IntPtr.Zero, IntPtr.Zero, 0, 0, IntPtr.Zero, 0, IntPtr.Zero, 0, IntPtr.Zero, 0);
}
}
public string GetApiVersion
{
get
{
if (proxy == null)
{
proxy = new Proxy(LibPath);
}
IntPtr ptr = proxy.XRequest((byte)RequestType.GetApiVersion, IntPtr.Zero, IntPtr.Zero, 0, 0, IntPtr.Zero, 0, IntPtr.Zero, 0, IntPtr.Zero, 0);
return Marshal.PtrToStringAnsi(ptr);
}
}
public string GetApiName
{
get
{
if (proxy == null)
{
proxy = new Proxy(LibPath);
}
IntPtr ptr = proxy.XRequest((byte)RequestType.GetApiName, IntPtr.Zero, IntPtr.Zero, 0, 0, IntPtr.Zero, 0, IntPtr.Zero, 0, IntPtr.Zero, 0);
return Marshal.PtrToStringAnsi(ptr);
}
}
private IntPtr _OnRespone(byte type, IntPtr pApi1, IntPtr pApi2, double double1, double double2, IntPtr ptr1, int size1, IntPtr ptr2, int size2, IntPtr ptr3, int size3)
{
// 队列过来的消息,如何处理?
switch((ResponeType)type)
{
case ResponeType.OnConnectionStatus:
_OnConnectionStatus(double1, ptr1,size1);
break;
case ResponeType.OnRtnError:
_OnRtnError(ptr1);
break;
default:
return OnRespone(type, pApi1, pApi2, double1, double2, ptr1, size1, ptr2, size2, ptr3, size3);
}
return IntPtr.Zero;
}
protected virtual IntPtr OnRespone(byte type, IntPtr pApi1, IntPtr pApi2, double double1, double double2, IntPtr ptr1, int size1, IntPtr ptr2, int size2, IntPtr ptr3, int size3)
{
return IntPtr.Zero;
}
private void _OnConnectionStatus(double double1, IntPtr ptr1, int size1)
{
ConnectionStatus status = (ConnectionStatus)double1;
// 连接状态更新
// 这种写法是为了解决多账号登录时,先进行Done然后再Logining或Doing的问题
if (ConnectionStatus.Disconnected == status
|| ConnectionStatus.Uninitialized == status)
{
IsConnected = false;
}
else if (ConnectionStatus.Done == status)
{
IsConnected = true;
}
RspUserLoginField obj = default(RspUserLoginField);
if(size1>0)
{
obj = PInvokeUtility.GetObjectFromIntPtr<RspUserLoginField>(ptr1);
UserLoginField = obj;
}
if (OnConnectionStatus_ != null)
OnConnectionStatus_(this, status, ref obj, size1);
}
private void _OnRtnError(IntPtr ptr1)
{
if (OnRtnError_ == null)
return;
ErrorField obj = PInvokeUtility.GetObjectFromIntPtr<ErrorField>(ptr1);
OnRtnError_(this, ref obj);
}
}
}