cctp.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. // Copyright 2013-2014 Fuzamei tech Ltd. All rights reserved.
  2. package gocctp
  3. /*
  4. #cgo CFLAGS: -I./include
  5. #cgo LDFLAGS: -L./lib -lm -lcctp -lthosttraderapi -lthostmduserapi
  6. #include <libcctp.h>
  7. #include <stdlib.h>
  8. ///行情登录请求响应
  9. extern void GoOnMdRspUserLogin(CThostFtdcRspUserLoginField *pRspUserLogin, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast, void* ctx);
  10. ///行情登出请求响应
  11. extern void GoOnMdRspUserLogout(CThostFtdcUserLogoutField *pUserLogout, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast, void* ctx);
  12. ///行情错误应答
  13. extern void GoOnMdRspError(CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast, void* ctx);
  14. ///交易登录请求响应
  15. extern void GoOnTdRspUserLogin(CThostFtdcRspUserLoginField *pRspUserLogin, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast, void* ctx);
  16. ///交易登出请求响应
  17. extern void GoOnTdRspUserLogout(CThostFtdcUserLogoutField *pUserLogout, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast, void* ctx);
  18. ///交易错误应答
  19. extern void GoOnTdRspError(CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast, void* ctx);
  20. ///深度行情通知
  21. extern void GoOnRtnDepthMarketData(CThostFtdcDepthMarketDataField *pDepthMarketData, void* ctx);
  22. ///请求查询交易所响应
  23. extern void GoOnRspQryExchange(CThostFtdcExchangeField *pExchange, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast, void* ctx);
  24. ///请求查询合约响应
  25. extern void GoOnRspQryInstrument(CThostFtdcInstrumentField *pInstrument, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast, void* ctx);
  26. ///行情登录请求响应
  27. void OnMdRspUserLogin(CThostFtdcRspUserLoginField *pRspUserLogin, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast, void* ctx) {
  28. GoOnMdRspUserLogin(pRspUserLogin, pRspInfo, nRequestID, bIsLast, ctx);
  29. }
  30. ///行情登出请求响应
  31. void OnMdRspUserLogout(CThostFtdcUserLogoutField *pUserLogout, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast, void* ctx) {
  32. GoOnMdRspUserLogout(pUserLogout, pRspInfo, nRequestID, bIsLast, ctx);
  33. }
  34. ///行情错误应答
  35. void OnMdRspError(CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast, void* ctx) {
  36. GoOnMdRspError(pRspInfo, nRequestID, bIsLast, ctx);
  37. }
  38. ///交易登录请求响应
  39. void OnTdRspUserLogin(CThostFtdcRspUserLoginField *pRspUserLogin, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast, void* ctx) {
  40. GoOnTdRspUserLogin(pRspUserLogin, pRspInfo, nRequestID, bIsLast, ctx);
  41. }
  42. ///交易登出请求响应
  43. void OnTdRspUserLogout(CThostFtdcUserLogoutField *pUserLogout, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast, void* ctx) {
  44. GoOnTdRspUserLogout(pUserLogout, pRspInfo, nRequestID, bIsLast, ctx);
  45. }
  46. ///交易错误应答
  47. void OnTdRspError(CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast, void* ctx) {
  48. GoOnTdRspError(pRspInfo, nRequestID, bIsLast, ctx);
  49. }
  50. ///深度行情通知
  51. void OnRtnDepthMarketData(CThostFtdcDepthMarketDataField *pDepthMarketData, void* ctx) {
  52. GoOnRtnDepthMarketData(pDepthMarketData, ctx);
  53. }
  54. ///请求查询交易所响应
  55. void OnRspQryExchange(CThostFtdcExchangeField *pExchange, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast, void* ctx) {
  56. GoOnRspQryExchange(pExchange, pRspInfo, nRequestID, bIsLast, ctx);
  57. }
  58. ///请求查询合约响应
  59. void OnRspQryInstrument(CThostFtdcInstrumentField *pInstrument, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast, void* ctx) {
  60. GoOnRspQryInstrument(pInstrument, pRspInfo, nRequestID, bIsLast, ctx);
  61. }
  62. */
  63. import "C"
  64. import (
  65. "unsafe"
  66. )
  67. type Spi interface {
  68. OnRspUserLogin(errMsg string)
  69. OnRspUserLogout(errMsg string)
  70. OnRspError(errMsg string)
  71. }
  72. type SpiBase struct{}
  73. func (sb *SpiBase) OnRspUserLogout(errMsg string) {}
  74. func (sb *SpiBase) OnRspError(errMsg string) {}
  75. type MdSpi interface {
  76. Spi
  77. OnRtnDepthMarketData(field *CThostFtdcDepthMarketDataField)
  78. }
  79. type TdSpi interface {
  80. Spi
  81. OnRspQryExchange(field *CThostFtdcExchangeField, errMsg string, isLast bool)
  82. OnRspQryInstrument(field *CThostFtdcInstrumentField, errMsg string, isLast bool)
  83. }
  84. type MdApi struct {
  85. MdSpi
  86. cmdApi unsafe.Pointer
  87. }
  88. func NewMdApi(spi MdSpi, dir string) *MdApi {
  89. csdir := C.CString(dir)
  90. defer C.free(unsafe.Pointer(csdir))
  91. cmdApi := C.CCTP_MdApiCreate(csdir)
  92. mdApi := &MdApi{spi, cmdApi}
  93. f1 := C.fnOnRspUserLogin(C.OnMdRspUserLogin)
  94. C.CCTP_RegOnMdRspUserLogin(cmdApi, f1, unsafe.Pointer(mdApi))
  95. f2 := C.fnOnRspUserLogout(C.OnMdRspUserLogout)
  96. C.CCTP_RegOnMdRspUserLogout(cmdApi, f2, unsafe.Pointer(mdApi))
  97. f3 := C.fnOnRspError(C.OnMdRspError)
  98. C.CCTP_RegOnMdRspError(cmdApi, f3, unsafe.Pointer(mdApi))
  99. f4 := C.fnOnRtnDepthMarketData(C.OnRtnDepthMarketData)
  100. C.CCTP_RegOnRtnDepthMarketData(cmdApi, f4, unsafe.Pointer(mdApi))
  101. return mdApi
  102. }
  103. func (md *MdApi) Release() {
  104. C.CCTP_MdRelease(unsafe.Pointer(md.cmdApi))
  105. }
  106. func (md *MdApi) Login(saddr, brokerId, userId, password string) {
  107. csaddr := C.CString(saddr)
  108. defer C.free(unsafe.Pointer(csaddr))
  109. cbrokerId := C.CString(brokerId)
  110. defer C.free(unsafe.Pointer(cbrokerId))
  111. cuserId := C.CString(userId)
  112. defer C.free(unsafe.Pointer(cuserId))
  113. cpassword := C.CString(password)
  114. defer C.free(unsafe.Pointer(cpassword))
  115. C.CCTP_MdLogin(md.cmdApi, csaddr, cbrokerId, cuserId, cpassword)
  116. }
  117. func (md *MdApi) Logout(brokerId, userId string) {
  118. cbrokerId := C.CString(brokerId)
  119. defer C.free(unsafe.Pointer(cbrokerId))
  120. cuserId := C.CString(userId)
  121. defer C.free(unsafe.Pointer(cuserId))
  122. C.CCTP_MdLogout(md.cmdApi, cbrokerId, cuserId)
  123. }
  124. func (md *MdApi) SubscribeMarketData(instrumentIds []string) {
  125. n := len(instrumentIds)
  126. if n == 0 {
  127. return
  128. }
  129. cinsIds := make([](*C.char), n)
  130. for i, id := range instrumentIds {
  131. cid := C.CString(id)
  132. cinsIds[i] = cid
  133. }
  134. C.CCTP_SubscribeMarketData(md.cmdApi, (**C.char)((unsafe.Pointer)(&cinsIds[0])), C.int(n))
  135. for _, x := range cinsIds {
  136. C.free(unsafe.Pointer(x))
  137. }
  138. }
  139. func (md *MdApi) UnSubscribeMarketData(instrumentIds []string) {
  140. n := len(instrumentIds)
  141. if n == 0 {
  142. return
  143. }
  144. cinsIds := make([](*C.char), n)
  145. for i, id := range instrumentIds {
  146. cid := C.CString(id)
  147. cinsIds[i] = cid
  148. }
  149. C.CCTP_UnSubscribeMarketData(md.cmdApi, (**C.char)((unsafe.Pointer)(&cinsIds[0])), C.int(n))
  150. for _, x := range cinsIds {
  151. C.free(unsafe.Pointer(x))
  152. }
  153. }
  154. type TdApi struct {
  155. TdSpi
  156. ctdApi unsafe.Pointer
  157. }
  158. func NewTdApi(spi TdSpi, dir string) *TdApi {
  159. csdir := C.CString(dir)
  160. defer C.free(unsafe.Pointer(csdir))
  161. ctdApi := C.CCTP_TdApiCreate(csdir)
  162. tdApi := &TdApi{spi, ctdApi}
  163. f1 := C.fnOnRspUserLogin(C.OnTdRspUserLogin)
  164. C.CCTP_RegOnTdRspUserLogin(ctdApi, f1, unsafe.Pointer(tdApi))
  165. f2 := C.fnOnRspUserLogout(C.OnTdRspUserLogout)
  166. C.CCTP_RegOnTdRspUserLogout(ctdApi, f2, unsafe.Pointer(tdApi))
  167. f3 := C.fnOnRspError(C.OnTdRspError)
  168. C.CCTP_RegOnTdRspError(ctdApi, f3, unsafe.Pointer(tdApi))
  169. f4 := C.fnOnRspQryExchange(C.OnRspQryExchange)
  170. C.CCTP_RegOnRspQryExchange(ctdApi, f4, unsafe.Pointer(tdApi))
  171. f5 := C.fnOnRspQryInstrument(C.OnRspQryInstrument)
  172. C.CCTP_RegOnRspQryInstrument(ctdApi, f5, unsafe.Pointer(tdApi))
  173. return tdApi
  174. }
  175. func (td *TdApi) Release() {
  176. C.CCTP_TdRelease(unsafe.Pointer(td.ctdApi))
  177. }
  178. func (td *TdApi) Login(saddr, brokerId, userId, password, authCode, userProductInfo string) {
  179. csaddr := C.CString(saddr)
  180. defer C.free(unsafe.Pointer(csaddr))
  181. cbrokerId := C.CString(brokerId)
  182. defer C.free(unsafe.Pointer(cbrokerId))
  183. cuserId := C.CString(userId)
  184. defer C.free(unsafe.Pointer(cuserId))
  185. cpassword := C.CString(password)
  186. defer C.free(unsafe.Pointer(cpassword))
  187. cauthCode := C.CString(authCode)
  188. defer C.free(unsafe.Pointer(cauthCode))
  189. cuserProductInfo := C.CString(userProductInfo)
  190. defer C.free(unsafe.Pointer(cuserProductInfo))
  191. C.CCTP_TdLogin(td.ctdApi, csaddr, cbrokerId, cuserId, cpassword, cauthCode, cuserProductInfo)
  192. }
  193. func (td *TdApi) Logout(brokerId, userId string) {
  194. cbrokerId := C.CString(brokerId)
  195. defer C.free(unsafe.Pointer(cbrokerId))
  196. cuserId := C.CString(userId)
  197. defer C.free(unsafe.Pointer(cuserId))
  198. C.CCTP_TdLogout(td.ctdApi, cbrokerId, cuserId)
  199. }
  200. func (td *TdApi) QryInstrument(instrumentId string) {
  201. cinsId := C.CString(instrumentId)
  202. if instrumentId == "" {
  203. cinsId = (*C.char)(unsafe.Pointer(nil))
  204. } else {
  205. defer C.free(unsafe.Pointer(cinsId))
  206. }
  207. C.CCTP_ReqQryInstrument(td.ctdApi, cinsId)
  208. }
  209. func (td *TdApi) QryExchange(exchangeId string) {
  210. cexid := C.CString(exchangeId)
  211. if exchangeId == "" {
  212. cexid = (*C.char)(unsafe.Pointer(nil))
  213. } else {
  214. defer C.free(unsafe.Pointer(cexid))
  215. }
  216. C.CCTP_ReqQryExchange(td.ctdApi, cexid)
  217. }