gitee.com/haifengat/gotap_dipper@v0.0.4-0.20231212021028-041a6fa876e5/quote/quote_lnx.go (about)

     1  package quote
     2  
     3  /*
     4  #cgo CPPFLAGS: -fPIC -I./
     5  #cgo linux LDFLAGS: -fPIC -L${SRCDIR}/../lib -Wl,-rpath ${SRCDIR}/../lib -ltapquote -lstdc++
     6  
     7  #include "struct_cgo.h"
     8  
     9  void* CreateQuoteAPINotify();
    10  void* CreateQuoteAPI(struct TapAPIApplicationInfo *appInfo);
    11  void FreeQuoteAPI(void* api);
    12  void* GetQuoteAPIVersion();
    13  int SetQuoteAPIDataPath(char* path);
    14  int SetQuoteAPILogLevel(char level);
    15  
    16  // ********************** 调用函数 ******************
    17  int qSetAPINotify(void* api,void  *apiNotify);
    18  int qSetHostAddress(void* api,char *IP,unsigned short port);
    19  int qLogin(void* api,struct TapAPIQuoteLoginAuth *loginAuth);
    20  int qDisconnect(void* api);
    21  int qQryCommodity(void* api,unsigned int *sessionID);
    22  int qQryContract(void* api,unsigned int *sessionID,struct TapAPICommodity *qryReq);
    23  int qSubscribeQuote(void* api,unsigned int *sessionID,struct TapAPIContract *contract);
    24  int qUnSubscribeQuote(void* api,unsigned int *sessionID,struct TapAPIContract *contract);
    25  
    26  
    27  
    28  // ********************** 响应函数 ******************
    29  void qSetOnRspLogin(void*, void*);
    30  void qSetOnAPIReady(void*, void*);
    31  void qSetOnDisconnect(void*, void*);
    32  void qSetOnRspQryCommodity(void*, void*);
    33  void qSetOnRspQryContract(void*, void*);
    34  void qSetOnRspSubscribeQuote(void*, void*);
    35  void qSetOnRspUnSubscribeQuote(void*, void*);
    36  void qSetOnRtnQuote(void*, void*);
    37  
    38  
    39  // Onxxx 为go 的 export所用
    40  void OnRspLogin( int errorCode,  struct TapAPIQuotLoginRspInfo *info);
    41  void OnAPIReady();
    42  void OnDisconnect( int reasonCode);
    43  void OnRspQryCommodity( unsigned int sessionID,  int errorCode,  char isLast,  struct TapAPIQuoteCommodityInfo *info);
    44  void OnRspQryContract( unsigned int sessionID,  int errorCode,  char isLast,  struct TapAPIQuoteContractInfo *info);
    45  void OnRspSubscribeQuote( unsigned int sessionID,  int errorCode,  char isLast,  struct TapAPIQuoteWhole *info);
    46  void OnRspUnSubscribeQuote( unsigned int sessionID,  int errorCode,  char isLast,  struct TapAPIContract *info);
    47  void OnRtnQuote( struct TapAPIQuoteWhole *info);
    48  
    49  
    50  #include <stdlib.h>
    51  #include <stdint.h>
    52  */
    53  import "C"
    54  
    55  import (
    56  	"fmt"
    57  	"unsafe"
    58  )
    59  
    60  var q *Quote
    61  
    62  func NewQuote() *Quote {
    63  	if q == nil {
    64  		q = &Quote{}
    65  	}
    66  	return q
    67  }
    68  
    69  // Quote 行情接口
    70  type Quote struct {
    71  	api, spi unsafe.Pointer
    72  	// 定义响应函数变量
    73  	OnRspLogin            func(errorCode TAPIINT32, info *TapAPIQuotLoginRspInfo)
    74  	OnAPIReady            func()
    75  	OnDisconnect          func(reasonCode TAPIINT32)
    76  	OnRspQryCommodity     func(sessionID TAPIUINT32, errorCode TAPIINT32, isLast TAPIYNFLAG, info *TapAPIQuoteCommodityInfo)
    77  	OnRspQryContract      func(sessionID TAPIUINT32, errorCode TAPIINT32, isLast TAPIYNFLAG, info *TapAPIQuoteContractInfo)
    78  	OnRspSubscribeQuote   func(sessionID TAPIUINT32, errorCode TAPIINT32, isLast TAPIYNFLAG, info *TapAPIQuoteWhole)
    79  	OnRspUnSubscribeQuote func(sessionID TAPIUINT32, errorCode TAPIINT32, isLast TAPIYNFLAG, info *TapAPIContract)
    80  	OnRtnQuote            func(info *TapAPIQuoteWhole)
    81  }
    82  
    83  func (q *Quote) CreateTapQuoteAPINotify() {
    84  	q.spi = C.CreateQuoteAPINotify()
    85  }
    86  func (q *Quote) CreateTapQuoteAPI(appInfo *TapAPIApplicationInfo) {
    87  	q.api = C.CreateQuoteAPI((*C.struct_TapAPIApplicationInfo)(unsafe.Pointer(appInfo)))
    88  }
    89  func (q *Quote) FreeTapQuoteAPI(api unsafe.Pointer) {
    90  	C.FreeQuoteAPI(api)
    91  }
    92  func (q *Quote) GetTapQuoteAPIVersion() string {
    93  	return C.GoString((*C.char)(C.GetQuoteAPIVersion()))
    94  }
    95  func (q *Quote) SetTapQuoteAPIDataPath(path string) C.int {
    96  	return C.SetQuoteAPIDataPath(C.CString(path))
    97  }
    98  func (q *Quote) SetTapQuoteAPILogLevel(level byte) C.int {
    99  	return C.SetQuoteAPILogLevel(C.char(level))
   100  }
   101  
   102  // 替代 SetAPINotify
   103  func (q *Quote) SetSpi() { C.qSetAPINotify(q.api, q.spi) }
   104  
   105  // ********************** 调用函数 ******************
   106  
   107  func (q *Quote) SetAPINotify(apiNotify unsafe.Pointer) C.int {
   108  	return C.qSetAPINotify(q.api, apiNotify)
   109  }
   110  
   111  func (q *Quote) SetHostAddress(IP string, port TAPIUINT16) C.int {
   112  	return C.qSetHostAddress(q.api, C.CString(IP), C.ushort(port))
   113  }
   114  
   115  func (q *Quote) Login(loginAuth *TapAPIQuoteLoginAuth) C.int {
   116  	return C.qLogin(q.api, (*C.struct_TapAPIQuoteLoginAuth)(unsafe.Pointer(loginAuth)))
   117  }
   118  
   119  func (q *Quote) Disconnect() C.int { return C.qDisconnect(q.api) }
   120  
   121  func (q *Quote) QryCommodity(sessionID *TAPIUINT32) C.int {
   122  	return C.qQryCommodity(q.api, (*C.uint)(unsafe.Pointer(sessionID)))
   123  }
   124  
   125  func (q *Quote) QryContract(sessionID *TAPIUINT32, qryReq *TapAPICommodity) C.int {
   126  	return C.qQryContract(q.api, (*C.uint)(unsafe.Pointer(sessionID)), (*C.struct_TapAPICommodity)(unsafe.Pointer(qryReq)))
   127  }
   128  
   129  func (q *Quote) SubscribeQuote(sessionID *TAPIUINT32, contract *TapAPIContract) C.int {
   130  	return C.qSubscribeQuote(q.api, (*C.uint)(unsafe.Pointer(sessionID)), (*C.struct_TapAPIContract)(unsafe.Pointer(contract)))
   131  }
   132  
   133  func (q *Quote) UnSubscribeQuote(sessionID *TAPIUINT32, contract *TapAPIContract) C.int {
   134  	return C.qUnSubscribeQuote(q.api, (*C.uint)(unsafe.Pointer(sessionID)), (*C.struct_TapAPIContract)(unsafe.Pointer(contract)))
   135  }
   136  
   137  // ********************** 响应函数 ******************
   138  func (q *Quote) RegCallBack() {
   139  	C.qSetOnRspLogin(q.spi, C.OnRspLogin)
   140  	C.qSetOnAPIReady(q.spi, C.OnAPIReady)
   141  	C.qSetOnDisconnect(q.spi, C.OnDisconnect)
   142  	C.qSetOnRspQryCommodity(q.spi, C.OnRspQryCommodity)
   143  	C.qSetOnRspQryContract(q.spi, C.OnRspQryContract)
   144  	C.qSetOnRspSubscribeQuote(q.spi, C.OnRspSubscribeQuote)
   145  	C.qSetOnRspUnSubscribeQuote(q.spi, C.OnRspUnSubscribeQuote)
   146  	C.qSetOnRtnQuote(q.spi, C.OnRtnQuote)
   147  
   148  }
   149  
   150  //export OnRspLogin
   151  func OnRspLogin(errorCode C.int, info *C.struct_TapAPIQuotLoginRspInfo) {
   152  	if q.OnRspLogin != nil {
   153  		q.OnRspLogin(TAPIINT32(errorCode), (*TapAPIQuotLoginRspInfo)(unsafe.Pointer(info)))
   154  	} else {
   155  		fmt.Println("OnRspLogin")
   156  	}
   157  }
   158  
   159  //export OnAPIReady
   160  func OnAPIReady() {
   161  	if q.OnAPIReady != nil {
   162  		q.OnAPIReady()
   163  	} else {
   164  		fmt.Println("OnAPIReady")
   165  	}
   166  }
   167  
   168  //export OnDisconnect
   169  func OnDisconnect(reasonCode C.int) {
   170  	if q.OnDisconnect != nil {
   171  		q.OnDisconnect(TAPIINT32(reasonCode))
   172  	} else {
   173  		fmt.Println("OnDisconnect")
   174  	}
   175  }
   176  
   177  //export OnRspQryCommodity
   178  func OnRspQryCommodity(sessionID C.uint, errorCode C.int, isLast C.char, info *C.struct_TapAPIQuoteCommodityInfo) {
   179  	if q.OnRspQryCommodity != nil {
   180  		q.OnRspQryCommodity(TAPIUINT32(sessionID), TAPIINT32(errorCode), TAPIYNFLAG(isLast), (*TapAPIQuoteCommodityInfo)(unsafe.Pointer(info)))
   181  	} else {
   182  		fmt.Println("OnRspQryCommodity")
   183  	}
   184  }
   185  
   186  //export OnRspQryContract
   187  func OnRspQryContract(sessionID C.uint, errorCode C.int, isLast C.char, info *C.struct_TapAPIQuoteContractInfo) {
   188  	if q.OnRspQryContract != nil {
   189  		q.OnRspQryContract(TAPIUINT32(sessionID), TAPIINT32(errorCode), TAPIYNFLAG(isLast), (*TapAPIQuoteContractInfo)(unsafe.Pointer(info)))
   190  	} else {
   191  		fmt.Println("OnRspQryContract")
   192  	}
   193  }
   194  
   195  //export OnRspSubscribeQuote
   196  func OnRspSubscribeQuote(sessionID C.uint, errorCode C.int, isLast C.char, info *C.struct_TapAPIQuoteWhole) {
   197  	if q.OnRspSubscribeQuote != nil {
   198  		q.OnRspSubscribeQuote(TAPIUINT32(sessionID), TAPIINT32(errorCode), TAPIYNFLAG(isLast), (*TapAPIQuoteWhole)(unsafe.Pointer(info)))
   199  	} else {
   200  		fmt.Println("OnRspSubscribeQuote")
   201  	}
   202  }
   203  
   204  //export OnRspUnSubscribeQuote
   205  func OnRspUnSubscribeQuote(sessionID C.uint, errorCode C.int, isLast C.char, info *C.struct_TapAPIContract) {
   206  	if q.OnRspUnSubscribeQuote != nil {
   207  		q.OnRspUnSubscribeQuote(TAPIUINT32(sessionID), TAPIINT32(errorCode), TAPIYNFLAG(isLast), (*TapAPIContract)(unsafe.Pointer(info)))
   208  	} else {
   209  		fmt.Println("OnRspUnSubscribeQuote")
   210  	}
   211  }
   212  
   213  //export OnRtnQuote
   214  func OnRtnQuote(info *C.struct_TapAPIQuoteWhole) {
   215  	if q.OnRtnQuote != nil {
   216  		q.OnRtnQuote((*TapAPIQuoteWhole)(unsafe.Pointer(info)))
   217  	} else {
   218  		fmt.Println("OnRtnQuote")
   219  	}
   220  }