mtf_client.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. package lmaxapi
  2. import "unsafe"
  3. import "time"
  4. import "sync/atomic"
  5. import "log"
  6. import "tickserver/api/lmaxapi/request"
  7. import "tickserver/api/lmaxapi/response"
  8. import "errors"
  9. var debug = true
  10. type MtfClient struct {
  11. mtf *Mtf
  12. clientId int64
  13. chrecv chan *Message
  14. isFullClose bool
  15. timeout time.Duration
  16. refCount int64
  17. stoptick chan int
  18. count int64
  19. }
  20. var ErrChanFull = errors.New("chan is full")
  21. //fullClose 这个参数很重要,如果设置为 true,那么send 函数完全异步,否则就是有可能
  22. //发生等待的情况。
  23. func NewMtfClient(mtf *Mtf, timeout time.Duration, isFullClose bool, bufferCount int) *MtfClient {
  24. client := &MtfClient{}
  25. client.mtf = mtf
  26. client.chrecv = make(chan *Message, bufferCount)
  27. client.isFullClose = isFullClose
  28. client.timeout = timeout
  29. client.clientId = int64(uintptr(unsafe.Pointer(client)))
  30. if debug {
  31. client.stoptick = make(chan int, 1)
  32. //在调试状态下可以打开,看看是否有timeout
  33. go client.countStat()
  34. }
  35. return client
  36. }
  37. func (this *MtfClient) countStat() {
  38. ticker := time.NewTicker(time.Second)
  39. lastcount := int64(0)
  40. for {
  41. select {
  42. case <-ticker.C:
  43. if lastcount > 0 {
  44. count := atomic.LoadInt64(&this.count) - lastcount
  45. tracelog.Println("send count:", count)
  46. }
  47. lastcount = atomic.LoadInt64(&this.count)
  48. case <-this.stoptick:
  49. return
  50. }
  51. }
  52. log.Println("stop count")
  53. }
  54. func (this *MtfClient) IncRef() int64 {
  55. return atomic.AddInt64(&this.refCount, 1)
  56. }
  57. func (this *MtfClient) DecRef() int64 {
  58. return atomic.AddInt64(&this.refCount, -1)
  59. }
  60. func (this *MtfClient) GetRef() int64 {
  61. return atomic.LoadInt64(&this.refCount)
  62. }
  63. func (this *MtfClient) GetMtf() *Mtf {
  64. return this.mtf
  65. }
  66. func (this *MtfClient) SetMtf(mtf *Mtf) {
  67. this.mtf = mtf
  68. }
  69. func (this *MtfClient) State() {
  70. }
  71. func (this *MtfClient) Recv(cb func(msg *Message)) {
  72. //log.Println("client is ready", this.GetId())
  73. for {
  74. msg, ok := <-this.chrecv
  75. if !ok { //chan is closed
  76. break
  77. }
  78. if cb != nil {
  79. cb(msg)
  80. }
  81. }
  82. // log.Println("client is stop", this.GetId())
  83. }
  84. func (this *MtfClient) RecvMulti(cb func(msg []*Message)) {
  85. //log.Println("client is ready, multi", this.GetId())
  86. msgs := make([]*Message, 1024)
  87. for {
  88. n, err := this.read(msgs)
  89. if err != nil {
  90. if n > 0 {
  91. if cb != nil {
  92. cb(msgs[:n])
  93. }
  94. }
  95. break
  96. }
  97. if cb != nil {
  98. cb(msgs[:n])
  99. }
  100. }
  101. // log.Println("client is stop, multi", this.GetId())
  102. }
  103. func (s *MtfClient) read(buf []*Message) (int, error) {
  104. var i = 1
  105. var ok bool
  106. buf[0], ok = <-s.chrecv
  107. if !ok {
  108. return 0, errors.New("chan is closed")
  109. }
  110. for {
  111. if i == len(buf) {
  112. return i, nil
  113. }
  114. select {
  115. case data, ok := <-s.chrecv:
  116. if !ok {
  117. return i, errors.New("chan is closed")
  118. }
  119. buf[i] = data
  120. i++
  121. default:
  122. return i, nil
  123. }
  124. }
  125. panic("nerver reach")
  126. }
  127. func (this *MtfClient) GetId() int64 {
  128. return this.clientId
  129. }
  130. func (this *MtfClient) SetId(id int64) {
  131. this.clientId = id
  132. }
  133. func (this *MtfClient) Send(data interface{}) error {
  134. msg := data.(*Message)
  135. select {
  136. case this.chrecv <- msg:
  137. default:
  138. // log.Println("send message block")
  139. if this.isFullClose {
  140. return ErrChanFull
  141. } else {
  142. this.chrecv <- msg
  143. }
  144. }
  145. return nil
  146. }
  147. func (this *MtfClient) Close() error {
  148. if this.GetRef() != 0 {
  149. return errors.New("ref count > 0")
  150. }
  151. msg := this.SendMessage(MsgClose, nil)
  152. if msg.Err != nil {
  153. return msg.Err
  154. }
  155. close(this.chrecv)
  156. this.stoptick <- 1
  157. // log.Println("mtf client close success.", this.GetId())
  158. return nil
  159. }
  160. //连接到mtf
  161. func (this *MtfClient) ConnectPrivate() error {
  162. _, err := this.sendMessage(MsgMtfBindPrivate, this)
  163. return err
  164. }
  165. func (this *MtfClient) ConnectPublic() error {
  166. _, err := this.sendMessage(MsgMtfBindPublic, this)
  167. return err
  168. }
  169. func (this *MtfClient) SetAccount(account *response.AccountStateEvent) error {
  170. _, err := this.sendMessage(MsgSetAccount, account)
  171. return err
  172. }
  173. func (this *MtfClient) SetAccountDetails(account *response.AccountDetails) error {
  174. _, err := this.sendMessage(MsgSetAccountDetails, account)
  175. return err
  176. }
  177. func (this *MtfClient) CancelOrder(req *request.CancelOrderRequest) error {
  178. _, err := this.sendMessage(MsgCancelOrder, req)
  179. return err
  180. }
  181. func (this *MtfClient) CloseOrder(req *request.ClosingOrderRequest) error {
  182. _, err := this.sendMessage(MsgCloseOrder, req)
  183. return err
  184. }
  185. func (this *MtfClient) AmendOrder(req *request.AmendStopsOrderRequest) error {
  186. _, err := this.sendMessage(MsgAmendOrder, req)
  187. return err
  188. }
  189. func (this *MtfClient) PlaceOrder(req *request.OrderRequest) error {
  190. _, err := this.sendMessage(MsgPlaceOrder, req)
  191. return err
  192. }
  193. func (this *MtfClient) SendMessage(msgType int, data interface{}) *Message {
  194. //准备
  195. clientid := this.GetId()
  196. msg := NewMessage(msgType, clientid, data, 0)
  197. if msgType == MsgLog {
  198. msg.Flag |= NeedLog
  199. }
  200. msg.Ch = make(chan *Message, 5)
  201. return this.SendMessage2(msg)
  202. }
  203. func (this *MtfClient) SendMessage2(msg *Message) *Message {
  204. this.mtf.Send(msg)
  205. //接受
  206. recvmsg := <-msg.Ch
  207. if debug {
  208. atomic.AddInt64(&this.count, 1)
  209. recvmsg.RecvTime = getTime()
  210. if recvmsg.RecvTime-recvmsg.SendTime > int64(this.timeout) {
  211. log.Println("msg timeout", msg)
  212. }
  213. }
  214. return recvmsg
  215. }
  216. func (this *MtfClient) SendMessageAsyn2(msg *Message) *Message {
  217. msg.Flag |= SendAsyn
  218. this.mtf.Send(msg)
  219. return msg
  220. }
  221. func (this *MtfClient) SendMessageAsyn(msgType int, data interface{}) *Message {
  222. //准备
  223. clientid := this.GetId()
  224. msg := NewMessage(msgType, clientid, data, 0)
  225. return this.SendMessageAsyn2(msg)
  226. }
  227. func (this *MtfClient) sendMessage(msgType int, data interface{}) (interface{}, error) {
  228. msg := this.SendMessage(msgType, data)
  229. if msg.Err != nil {
  230. return msg.Data, msg.Err
  231. }
  232. return msg.Data, nil
  233. }
  234. //更新tick, 这个操作是最频繁的
  235. func (this *MtfClient) UpdateTick(event *TickEvent) *AccountUpdated {
  236. updated, err := this.sendMessage(MsgSetTick, event)
  237. if err != nil {
  238. return nil
  239. }
  240. return updated.(*AccountUpdated)
  241. }
  242. func (this *MtfClient) CloneAccount(copyInstinfo bool) *AccountInfo {
  243. data, err := this.sendMessage(MsgCloneAccount, copyInstinfo)
  244. if err != nil {
  245. return nil
  246. }
  247. return data.(*AccountInfo)
  248. }
  249. func (this *MtfClient) GetOb2(id int64) *response.OrderBookEvent {
  250. data, err := this.sendMessage(MsgGetTick, id)
  251. if err != nil {
  252. return nil
  253. }
  254. return data.(*response.OrderBookEvent)
  255. }
  256. func (this *MtfClient) GetAllOb2() []*response.OrderBookEvent {
  257. data, err := this.sendMessage(MsgGetAllTick, nil)
  258. if err != nil {
  259. return nil
  260. }
  261. return data.([]*response.OrderBookEvent)
  262. }
  263. func (this *MtfClient) SetPosition(event *response.PositionEvent) error {
  264. _, err := this.sendMessage(MsgSetPosition, event)
  265. return err
  266. }
  267. func (this *MtfClient) SetOrder(event *response.OrderEvent) error {
  268. _, err := this.sendMessage(MsgSetOrder, event)
  269. return err
  270. }
  271. func (this *MtfClient) SetExecution(event *response.ExecutionEvent) error {
  272. _, err := this.sendMessage(MsgSetExecution, event)
  273. return err
  274. }
  275. func (this *MtfClient) Init(event *InitEvent) *AccountUpdated {
  276. updated, err := this.sendMessage(MsgInit, event)
  277. if err != nil {
  278. return nil
  279. }
  280. return updated.(*AccountUpdated)
  281. }
  282. func (this *MtfClient) SetOneExecution(event *OneExecutionEvent) *AccountUpdated {
  283. updated, err := this.sendMessage(MsgSetOneExecution, event)
  284. if err != nil {
  285. return nil
  286. }
  287. return updated.(*AccountUpdated)
  288. }
  289. func (this *MtfClient) GetPosition(id int64) *response.PositionEvent {
  290. data, err := this.sendMessage(MsgGetPosition, id)
  291. if err != nil {
  292. return nil
  293. }
  294. return data.(*response.PositionEvent)
  295. }
  296. func (this *MtfClient) GetPositions(iscopy bool) []*response.PositionEvent {
  297. data, err := this.sendMessage(MsgGetPositions, iscopy)
  298. if err != nil {
  299. return nil
  300. }
  301. return data.([]*response.PositionEvent)
  302. }
  303. func (this *MtfClient) GetOrders(iscopy bool) []*response.OrderEvent {
  304. data, err := this.sendMessage(MsgGetOrders, iscopy)
  305. if err != nil {
  306. return nil
  307. }
  308. return data.([]*response.OrderEvent)
  309. }
  310. func (this *MtfClient) SetRejected(event *response.InstructionRejectedEvent) error {
  311. _, err := this.sendMessage(MsgSetRejected, event)
  312. return err
  313. }
  314. //货币对列表
  315. //暂时不做更新
  316. func (this *MtfClient) SetInstrument(event *response.Instrument) error {
  317. _, err := this.sendMessage(MsgSetInstrument, event)
  318. return err
  319. }
  320. func (this *MtfClient) SetObStatus(event *response.OrderBookStatusEvent) error {
  321. _, err := this.sendMessage(MsgSetObStatus, event)
  322. return err
  323. }
  324. //暂时不做更新
  325. func (this *MtfClient) SetExchangeRate(event *RateEvent) *AccountUpdated {
  326. updated, err := this.sendMessage(MsgSetExchangeRate, event)
  327. if err != nil {
  328. return nil
  329. }
  330. return updated.(*AccountUpdated)
  331. }
  332. func (this *MtfClient) GetInstrument(id int64) *response.Instrument {
  333. data, err := this.sendMessage(MsgGetInstrument, id)
  334. if err != nil {
  335. return nil
  336. }
  337. return data.(*response.Instrument)
  338. }
  339. func (this *MtfClient) GetInstruments(iscopy bool) []*response.Instrument {
  340. data, err := this.sendMessage(MsgGetInstruments, iscopy)
  341. if err != nil {
  342. return nil
  343. }
  344. return data.([]*response.Instrument)
  345. }
  346. func (this *MtfClient) GetAccount() *response.AccountStateEvent {
  347. data, err := this.sendMessage(MsgGetAccount, nil)
  348. if err != nil {
  349. return nil
  350. }
  351. return data.(*response.AccountStateEvent)
  352. }