13 Star 65 Fork 41

海风/goctp

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
quote_pro.go 1.68 KB
一键复制 编辑 原始数据 按行查看 历史
package goctp
import (
"time"
"golang.org/x/text/encoding/simplifiedchinese"
)
type QuotePro struct {
*QuoteExt
OnRtnTick func(pDepthMarketData *CThostFtdcDepthMarketDataField)
// 行情 key: InstrumentID
Ticks map[string]CThostFtdcDepthMarketDataField
// 响应事件
eventChan chan Event
// 错误
errorChan chan CThostFtdcRspInfoField
}
func NewQuotePro() *QuotePro {
q := &QuotePro{}
q.QuoteExt = NewQuoteExt()
q.eventChan = make(chan Event)
q.errorChan = make(chan CThostFtdcRspInfoField)
q.Ticks = make(map[string]CThostFtdcDepthMarketDataField)
q.Quote.OnRtnDepthMarketData = func(pDepthMarketData *CThostFtdcDepthMarketDataField) {
q.Ticks[pDepthMarketData.InstrumentID.String()] = *pDepthMarketData
if q.OnRtnTick != nil {
q.OnRtnTick(pDepthMarketData)
}
}
return q
}
func (q *QuotePro) Start(cfg LoginConfig) (logInfo CThostFtdcRspUserLoginField, rsp CThostFtdcRspInfoField) {
q.Quote.OnFrontConnected = func() {
q.eventChan <- onFrontConnected
}
q.Quote.OnRspUserLogin = func(pRspUserLogin *CThostFtdcRspUserLoginField, pRspInfo *CThostFtdcRspInfoField, nRequestID int, bIsLast bool) {
if bIsLast {
if pRspInfo.ErrorID == 0 {
logInfo = *pRspUserLogin
q.eventChan <- onRspUserLogin
} else {
q.errorChan <- *pRspInfo
}
}
}
q.Quote.RegisterFront(cfg.Front)
q.Quote.Init()
// 连接
select {
case <-q.eventChan: // 连接
q.ReqUserLogin(cfg.Broker, cfg.UserID, cfg.Password)
case <-time.NewTimer(5 * time.Second).C:
bs, _ := simplifiedchinese.GB18030.NewEncoder().Bytes([]byte("连接超时 5s"))
rsp.ErrorID = -1
copy(rsp.ErrorMsg[:], bs)
return
}
// 登录
select {
case <-q.eventChan:
case rsp = <-q.errorChan:
}
return
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/haifengat/goctp.git
git@gitee.com:haifengat/goctp.git
haifengat
goctp
goctp
v2.0.9

搜索帮助

0d507c66 1850385 C8b1a773 1850385