1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- // Copyright 2013-2014 Fuzamei tech Ltd. All rights reserved.
- package gofix
- /*
- #cgo CFLAGS: -I./include
- #cgo LDFLAGS: -L/usr/lib
- #cgo linux LDFLAGS: -lboost_system -lquickfix -lufx_security -lcfix -pthread -lm
- #include <libcfix.h>
- #include <stdlib.h>
- extern void GoOnMdMessage(TickFull* tick, void* ctx);
- void OnMdMessage(TickFull* tick, void* ctx) {
- GoOnMdMessage(tick, ctx);
- }
- */
- import "C"
- import (
- "errors"
- "log"
- "unsafe"
- )
- const MAX_BID_ASK_COUNT = 25
- type FixTick struct {
- AskPrice [MAX_BID_ASK_COUNT]float64
- BidPrice [MAX_BID_ASK_COUNT]float64
- AskVolume [MAX_BID_ASK_COUNT]float64
- BidVolume [MAX_BID_ASK_COUNT]float64
- Symbol [8]byte
- Time int32
- Millisecond int32
- AskCount int32
- BidCount int32
- }
- func Symbol(symbol [8]byte) string {
- for i, v := range symbol {
- if v == 0 {
- return string(symbol[:i])
- }
- }
- return string(symbol[:])
- }
- type App struct {
- symbolMap map[string]string
- account, password string
- Ch chan *FixTick
- }
- func NewApp(symbolMap map[string]string, account, password string) *App {
- return &App{
- symbolMap: symbolMap,
- account: account,
- password: password,
- Ch: make(chan *FixTick, 1024),
- }
- }
- func (app *App) Run(fname string) error {
- cacc := C.CString(app.account)
- defer C.free(unsafe.Pointer(cacc))
- cpswd := C.CString(app.password)
- defer C.free(unsafe.Pointer(cpswd))
- capp := C.CreateApp(cacc, cpswd)
- if capp == nil {
- return errors.New(" C.CreateApp() error")
- }
- for key, val := range app.symbolMap {
- csym := C.CString(key)
- cexchange := C.CString(val)
- C.AddSymbol(capp, csym, cexchange)
- C.free(unsafe.Pointer(csym))
- C.free(unsafe.Pointer(cexchange))
- }
- fn := C.fnOnMdMessage(C.OnMdMessage)
- C.Reg_OnMdMessage(capp, fn, unsafe.Pointer(app))
- cfname := C.CString(fname)
- defer C.free(unsafe.Pointer(cfname))
- C.AppRun(capp, cfname)
- return nil
- }
- func (app *App) onMdMessage(tick *FixTick) {
- log.Println("App.onMdMessage", string(tick.Symbol[:]), tick.Time)
- select {
- case app.Ch <- tick:
- default:
- }
- }
|