package lmaxapi import "sync/atomic" import "errors" import "fmt" import "time" const ( MsgInit = iota MsgGetPosition MsgGetPositions MsgGetInstrument MsgGetInstruments MsgGetAccount MsgSetTick MsgSetOrder MsgGetOrders MsgSetRejected MsgSetAccount MsgSetAccountDetails MsgSetPosition MsgSetInstrument MsgSetExchangeRate MsgGetExchangeRate MsgGetTick MsgGetAllTick MsgGetInstument MsgGetAllInstrument MsgShutDown MsgMtfBindPrivate MsgMtfBindPublic MsgTimeout MsgEcho MsgClose MsgSetObStatus MsgSetExecution MsgSetOneExecution MsgCancelOrder MsgCloseOrder MsgAmendOrder MsgPlaceOrder MsgCloneAccount MsgLog MsgLoged MsgMatched MsgCount ) var msgname = []string{ "MsgInit", "MsgGetPosition", "MsgGetPositions", "MsgGetInstrument", "MsgGetInstruments", "MsgGetAccount", "MsgSetTick", "MsgSetOrder", "MsgGetOrders", "MsgSetRejected", "MsgSetAccount", "MsgSetAccountDetails", "MsgSetPosition", "MsgSetInstrument", "MsgSetExchangeRate", "MsgGetExchangeRate", "MsgGetTick", "MsgGetAllTick", "MsgGetInstument", "MsgGetAllInstrument", "MsgShutDown", "MsgMtfBindPrivate", "MsgMtfBindPublic", "MsgTimeout", "MsgEcho", "MsgClose", "MsgSetObStatus", "MsgSetExecution", "MsgSetOneExecution", "MsgCancelOrder", "MsgCloseOrder", "MsgAmendOrder", "MsgPlaceOrder", "MsgCloneAccount", "MsgLog", "MsgLoged", "MsgMatched", "MsgCount", } const ( NeedLog = 1 << iota SendAsyn Public Private ) var ErrTimeout = errors.New("timeout") var messageId int64 type Message struct { Type int Flag int ClientId int64 Id int64 Err error SendTime int64 RecvTime int64 Data interface{} Ch chan *Message } func NewMessage(mtype int, clientId int64, data interface{}, id int64) *Message { err := error(nil) if id == 0 { id = atomic.AddInt64(&messageId, 1) } if mtype == MsgTimeout { err = ErrTimeout } return &Message{mtype, 0, clientId, id, err, 0, 0, data, nil} } func (msg *Message) GetId() int64 { return msg.Id } func (msg *Message) Name() string { return msgname[msg.Type] } func (msg *Message) String() string { return "[" + msgname[msg.Type] + "]" + fmt.Sprint(msg.Id, msg.Err, msg.Data, msg.SendTime, msg.RecvTime) } func getTime() int64 { return time.Now().UnixNano() }