-
Notifications
You must be signed in to change notification settings - Fork 3
/
mdapi_impl.go
350 lines (283 loc) · 12.7 KB
/
mdapi_impl.go
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
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package goctp
/*
#include <stdint.h>
#include <stdlib.h>
#include "ThostFtdcUserApiDataType.h"
#include "ThostFtdcUserApiStruct.h"
// typedef long long intgo;
typedef struct { char *p; int64_t n; } _gostring_;
typedef struct { void* array; int64_t len; int64_t cap; } _goslice_;
extern uintptr_t _wrap_CThostFtdcMdApi_CreateFtdcMdApi();
extern uintptr_t _wrap_CThostFtdcMdApi_CreateFtdcMdApi2(uintptr_t goUserApi, char*, _Bool, _Bool);
extern const char * _wrap_CThostFtdcMdApi_GetApiVersion(uintptr_t);
extern void _wrap_CThostFtdcMdApi_Release(uintptr_t);
extern void _wrap_CThostFtdcMdApi_Init(uintptr_t);
extern int64_t _wrap_CThostFtdcMdApi_Join(uintptr_t);
extern const char * _wrap_CThostFtdcMdApi_GetTradingDay(uintptr_t);
extern void _wrap_CThostFtdcMdApi_RegisterFront(uintptr_t, char *);
extern void _wrap_CThostFtdcMdApi_RegisterNameServer(uintptr_t, char *);
extern void _wrap_CThostFtdcMdApi_RegisterFensUserInfo(uintptr_t, struct CThostFtdcFensUserInfoField *);
extern int64_t _wrap_CThostFtdcMdApi_SubscribeMarketData(uintptr_t, char **, int);
extern int64_t _wrap_CThostFtdcMdApi_UnSubscribeMarketData(uintptr_t, char **, int);
extern int64_t _wrap_CThostFtdcMdApi_SubscribeForQuoteRsp(uintptr_t, char **, int);
extern int64_t _wrap_CThostFtdcMdApi_UnSubscribeForQuoteRsp(uintptr_t, char **, int);
extern int64_t _wrap_CThostFtdcMdApi_ReqUserLogin(uintptr_t, struct CThostFtdcReqUserLoginField *, int);
extern int64_t _wrap_CThostFtdcMdApi_ReqUserLogout(uintptr_t, struct CThostFtdcUserLogoutField *, int);
extern int64_t _wrap_CThostFtdcMdApi_ReqQryMulticastInstrument(uintptr_t, struct CThostFtdcQryMulticastInstrumentField *, int);
*/
import "C"
import (
"os"
"runtime/cgo"
"unsafe"
"github.com/pseudocodes/goctp/thost"
)
const (
defaultFlowPath = ""
defaultIsUsingUdp = false
defaultIsMulticast = false
)
type MdOption func(api *mdApi)
type mdApi struct {
apiPtr uintptr
spi MdSpi
flowPath string
usingUDP bool
multicast bool
}
func CreateMdApi(options ...MdOption) *mdApi {
api := &mdApi{
flowPath: defaultFlowPath,
usingUDP: defaultIsUsingUdp,
multicast: defaultIsMulticast,
}
handle := cgo.NewHandle(api)
for _, opt := range options {
opt(api)
}
if api.flowPath != "" {
if err := os.MkdirAll(api.flowPath, os.ModePerm); err != nil && !os.IsExist(err) {
panic(err)
}
}
cflowPath := C.CString(api.flowPath)
defer C.free(unsafe.Pointer(cflowPath))
api.apiPtr = uintptr(C._wrap_CThostFtdcMdApi_CreateFtdcMdApi2(C.uintptr_t(handle), cflowPath, C._Bool(api.usingUDP), C._Bool(api.multicast)))
return api
}
// /获取API的版本信息
// /@retrun 获取到的版本号
func (c *mdApi) GetApiVersion() string {
cString := C._wrap_CThostFtdcMdApi_GetApiVersion(C.uintptr_t(c.apiPtr))
return C.GoString(cString)
}
// 删除接口对象本身
// /@remark 不再使用本接口对象时,调用该函数删除接口对象
func (c *mdApi) Release() {
C._wrap_CThostFtdcMdApi_Release(C.uintptr_t(c.apiPtr))
}
// 初始化
// /@remark 初始化运行环境,只有调用后,接口才开始工作
func (c *mdApi) Init() {
C._wrap_CThostFtdcMdApi_Init(C.uintptr_t(c.apiPtr))
}
// 等待接口线程结束运行
// /@return 线程退出代码
func (c *mdApi) Join() int {
return (int)(C._wrap_CThostFtdcMdApi_Join(C.uintptr_t(c.apiPtr)))
}
// /获取当前交易日
// /@retrun 获取到的交易日
// /@remark 只有登录成功后,才能得到正确的交易日
func (c *mdApi) GetTradingDay() string {
cString := C._wrap_CThostFtdcMdApi_GetTradingDay(C.uintptr_t(c.apiPtr))
return C.GoString(cString)
}
func (c *mdApi) RegisterFront(frontAddress string) {
addr := C.CString(frontAddress)
defer C.free(unsafe.Pointer(addr))
C._wrap_CThostFtdcMdApi_RegisterFront(C.uintptr_t(c.apiPtr), addr)
}
// 注册名字服务器用户信息
func (c *mdApi) RegisterNameServer(nsAddress string) {
addr := C.CString(nsAddress)
defer C.free(unsafe.Pointer(addr))
C._wrap_CThostFtdcMdApi_RegisterNameServer(C.uintptr_t(c.apiPtr), addr)
}
// /@param pFensUserInfo:用户信息。
func (c *mdApi) RegisterFensUserInfo(pFensUserInfo *thost.CThostFtdcFensUserInfoField) {
C._wrap_CThostFtdcMdApi_RegisterFensUserInfo(C.uintptr_t(c.apiPtr), (*C.struct_CThostFtdcFensUserInfoField)(unsafe.Pointer(pFensUserInfo)))
}
// 注册回调接口
// /@param pSpi 派生自回调接口类的实例
func (c *mdApi) RegisterSpi(pSpi MdSpi) {
c.spi = pSpi
}
// /订阅行情。
// /@param ppInstrumentID 合约ID
// /@param nCount 要订阅/退订行情的合约个数
// /@remark
func (c *mdApi) SubscribeMarketData(instrumentIDs ...string) int {
cinlist := []*C.char{}
for _, ins := range instrumentIDs {
cinlist = append(cinlist, C.CString(ins))
}
defer func() {
for i := range cinlist {
C.free(unsafe.Pointer(cinlist[i]))
}
}()
return (int)(C._wrap_CThostFtdcMdApi_SubscribeMarketData(C.uintptr_t(c.apiPtr), (**C.char)(unsafe.Pointer(&cinlist[0])), C.int(len(cinlist))))
}
// /退订行情。
// /@param ppInstrumentID 合约ID
// /@param nCount 要订阅/退订行情的合约个数
// /@remark
func (c *mdApi) UnSubscribeMarketData(instrumentIDs ...string) int {
cinlist := []*C.char{}
for _, ins := range instrumentIDs {
cinlist = append(cinlist, C.CString(ins))
}
defer func() {
for i := range cinlist {
C.free(unsafe.Pointer(cinlist[i]))
}
}()
return (int)(C._wrap_CThostFtdcMdApi_UnSubscribeMarketData(C.uintptr_t(c.apiPtr), (**C.char)(unsafe.Pointer(&cinlist[0])), C.int(len(cinlist))))
}
// /订阅询价。
// /@param ppInstrumentID 合约ID
// /@param nCount 要订阅/退订行情的合约个数
// /@remark
func (c *mdApi) SubscribeForQuoteRsp(instrumentIDs ...string) int {
cinlist := []*C.char{}
for _, ins := range instrumentIDs {
cinlist = append(cinlist, C.CString(ins))
}
defer func() {
for i := range cinlist {
C.free(unsafe.Pointer(cinlist[i]))
}
}()
return (int)(C._wrap_CThostFtdcMdApi_SubscribeForQuoteRsp(C.uintptr_t(c.apiPtr), (**C.char)(unsafe.Pointer(&cinlist[0])), C.int(len(cinlist))))
}
// /退订询价。
// /@param ppInstrumentID 合约ID
// /@param nCount 要订阅/退订行情的合约个数
// /@remark
func (c *mdApi) UnSubscribeForQuoteRsp(instrumentIDs ...string) int {
cinlist := []*C.char{}
for _, ins := range instrumentIDs {
cinlist = append(cinlist, C.CString(ins))
}
defer func() {
for i := range cinlist {
C.free(unsafe.Pointer(cinlist[i]))
}
}()
return (int)(C._wrap_CThostFtdcMdApi_UnSubscribeForQuoteRsp(C.uintptr_t(c.apiPtr), (**C.char)(unsafe.Pointer(&cinlist[0])), C.int(len(cinlist))))
}
// 用户登录请求
func (c *mdApi) ReqUserLogin(pReqUserLoginField *thost.CThostFtdcReqUserLoginField, nRequestID int) int {
return (int)(C._wrap_CThostFtdcMdApi_ReqUserLogin(C.uintptr_t(c.apiPtr), (*C.struct_CThostFtdcReqUserLoginField)(unsafe.Pointer(pReqUserLoginField)), C.int(nRequestID)))
}
// 登出请求
func (c *mdApi) ReqUserLogout(pUserLogout *thost.CThostFtdcUserLogoutField, nRequestID int) int {
return (int)(C._wrap_CThostFtdcMdApi_ReqUserLogout(C.uintptr_t(c.apiPtr), (*C.struct_CThostFtdcUserLogoutField)(unsafe.Pointer(pUserLogout)), C.int(nRequestID)))
}
// 请求查询组播合约
func (c *mdApi) ReqQryMulticastInstrument(pQryMulticastInstrument *thost.CThostFtdcQryMulticastInstrumentField, nRequestID int) int {
return (int)(C._wrap_CThostFtdcMdApi_ReqQryMulticastInstrument(C.uintptr_t(c.apiPtr), (*C.struct_CThostFtdcQryMulticastInstrumentField)(unsafe.Pointer(pQryMulticastInstrument)), C.int(nRequestID)))
}
//export wrapMdOnFrontConnected
func wrapMdOnFrontConnected(v uintptr) {
api := cgo.Handle(v).Value().(*mdApi)
api.spi.OnFrontConnected()
}
//export wrapMdOnFrontDisconnected
func wrapMdOnFrontDisconnected(v uintptr, nReason C.int) {
api := cgo.Handle(v).Value().(*mdApi)
api.spi.OnFrontDisconnected(int(nReason))
}
//export wrapMdOnHeartBeatWarning
func wrapMdOnHeartBeatWarning(v uintptr, nTimeLapse C.int) {
api := cgo.Handle(v).Value().(*mdApi)
api.spi.OnHeartBeatWarning(int(nTimeLapse))
}
//export wrapMdOnRspUserLogin
func wrapMdOnRspUserLogin(v uintptr, pRspUserLogin *C.struct_CThostFtdcRspUserLoginField, pRspInfo *C.struct_CThostFtdcRspInfoField, nRequestID C.int, bIsLast C._Bool) {
api := cgo.Handle(v).Value().(*mdApi)
api.spi.OnRspUserLogin((*thost.CThostFtdcRspUserLoginField)(unsafe.Pointer(pRspUserLogin)), (*thost.CThostFtdcRspInfoField)(unsafe.Pointer(pRspInfo)), int(nRequestID), bool(bIsLast))
}
//export wrapMdOnRspUserLogout
func wrapMdOnRspUserLogout(v uintptr, pUserLogout *C.struct_CThostFtdcUserLogoutField, pRspInfo *C.struct_CThostFtdcRspInfoField, nRequestID C.int, bIsLast C._Bool) {
api := cgo.Handle(v).Value().(*mdApi)
api.spi.OnRspUserLogout((*thost.CThostFtdcUserLogoutField)(unsafe.Pointer(pUserLogout)), (*thost.CThostFtdcRspInfoField)(unsafe.Pointer(pRspInfo)), int(nRequestID), bool(bIsLast))
}
//export wrapMdOnRspQryMulticastInstrument
func wrapMdOnRspQryMulticastInstrument(v uintptr, pMulticastInstrument *C.struct_CThostFtdcMulticastInstrumentField, pRspInfo *C.struct_CThostFtdcRspInfoField, nRequestID C.int, bIsLast C._Bool) {
api := cgo.Handle(v).Value().(*mdApi)
api.spi.OnRspQryMulticastInstrument((*thost.CThostFtdcMulticastInstrumentField)(unsafe.Pointer(pMulticastInstrument)), (*thost.CThostFtdcRspInfoField)(unsafe.Pointer(pRspInfo)), int(nRequestID), bool(bIsLast))
}
//export wrapMdOnRspError
func wrapMdOnRspError(v uintptr, pRspInfo *C.struct_CThostFtdcRspInfoField, nRequestID C.int, bIsLast C._Bool) {
api := cgo.Handle(v).Value().(*mdApi)
api.spi.OnRspError((*thost.CThostFtdcRspInfoField)(unsafe.Pointer(pRspInfo)), int(nRequestID), bool(bIsLast))
}
//export wrapMdOnRspSubMarketData
func wrapMdOnRspSubMarketData(v uintptr, pSpecificInstrument *C.struct_CThostFtdcSpecificInstrumentField, pRspInfo *C.struct_CThostFtdcRspInfoField, nRequestID C.int, bIsLast C._Bool) {
api := cgo.Handle(v).Value().(*mdApi)
api.spi.OnRspSubMarketData((*thost.CThostFtdcSpecificInstrumentField)(unsafe.Pointer(pSpecificInstrument)), (*thost.CThostFtdcRspInfoField)(unsafe.Pointer(pRspInfo)), int(nRequestID), bool(bIsLast))
}
//export wrapMdOnRspUnSubMarketData
func wrapMdOnRspUnSubMarketData(v uintptr, pSpecificInstrument *C.struct_CThostFtdcSpecificInstrumentField, pRspInfo *C.struct_CThostFtdcRspInfoField, nRequestID C.int, bIsLast C._Bool) {
api := cgo.Handle(v).Value().(*mdApi)
api.spi.OnRspUnSubMarketData((*thost.CThostFtdcSpecificInstrumentField)(unsafe.Pointer(pSpecificInstrument)), (*thost.CThostFtdcRspInfoField)(unsafe.Pointer(pRspInfo)), int(nRequestID), bool(bIsLast))
}
//export wrapMdOnRspSubForQuoteRsp
func wrapMdOnRspSubForQuoteRsp(v uintptr, pSpecificInstrument *C.struct_CThostFtdcSpecificInstrumentField, pRspInfo *C.struct_CThostFtdcRspInfoField, nRequestID C.int, bIsLast C._Bool) {
api := cgo.Handle(v).Value().(*mdApi)
api.spi.OnRspSubForQuoteRsp((*thost.CThostFtdcSpecificInstrumentField)(unsafe.Pointer(pSpecificInstrument)), (*thost.CThostFtdcRspInfoField)(unsafe.Pointer(pRspInfo)), int(nRequestID), bool(bIsLast))
}
//export wrapMdOnRspUnSubForQuoteRsp
func wrapMdOnRspUnSubForQuoteRsp(v uintptr, pSpecificInstrument *C.struct_CThostFtdcSpecificInstrumentField, pRspInfo *C.struct_CThostFtdcRspInfoField, nRequestID C.int, bIsLast C._Bool) {
api := cgo.Handle(v).Value().(*mdApi)
api.spi.OnRspUnSubForQuoteRsp((*thost.CThostFtdcSpecificInstrumentField)(unsafe.Pointer(pSpecificInstrument)), (*thost.CThostFtdcRspInfoField)(unsafe.Pointer(pRspInfo)), int(nRequestID), bool(bIsLast))
}
//export wrapMdOnRtnDepthMarketData
func wrapMdOnRtnDepthMarketData(v uintptr, pDepthMarketData *C.struct_CThostFtdcDepthMarketDataField) {
api := cgo.Handle(v).Value().(*mdApi)
api.spi.OnRtnDepthMarketData((*thost.CThostFtdcDepthMarketDataField)(unsafe.Pointer(pDepthMarketData)))
}
//export wrapMdOnRtnForQuoteRsp
func wrapMdOnRtnForQuoteRsp(v uintptr, pForQuoteRsp *C.struct_CThostFtdcForQuoteRspField) {
api := cgo.Handle(v).Value().(*mdApi)
api.spi.OnRtnForQuoteRsp((*thost.CThostFtdcForQuoteRspField)(unsafe.Pointer(pForQuoteRsp)))
}
// -----------------------------------------------------
func MdFlowPath(path string) MdOption {
return func(api *mdApi) {
api.flowPath = path
}
}
func MdUsingUDP(usingudp bool) MdOption {
return func(api *mdApi) {
api.usingUDP = usingudp
}
}
func MdMultiCast(multicast bool) MdOption {
return func(api *mdApi) {
api.multicast = multicast
}
}