message.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package lmaxapi
  2. import "sync/atomic"
  3. import "errors"
  4. import "fmt"
  5. import "time"
  6. const (
  7. MsgInit = iota
  8. MsgGetPosition
  9. MsgGetPositions
  10. MsgGetInstrument
  11. MsgGetInstruments
  12. MsgGetAccount
  13. MsgSetTick
  14. MsgSetOrder
  15. MsgGetOrders
  16. MsgSetRejected
  17. MsgSetAccount
  18. MsgSetAccountDetails
  19. MsgSetPosition
  20. MsgSetInstrument
  21. MsgSetExchangeRate
  22. MsgGetExchangeRate
  23. MsgGetTick
  24. MsgGetAllTick
  25. MsgGetInstument
  26. MsgGetAllInstrument
  27. MsgShutDown
  28. MsgMtfBindPrivate
  29. MsgMtfBindPublic
  30. MsgTimeout
  31. MsgEcho
  32. MsgClose
  33. MsgSetObStatus
  34. MsgSetExecution
  35. MsgSetOneExecution
  36. MsgCancelOrder
  37. MsgCloseOrder
  38. MsgAmendOrder
  39. MsgPlaceOrder
  40. MsgCloneAccount
  41. MsgLog
  42. MsgLoged
  43. MsgMatched
  44. MsgCount
  45. )
  46. var msgname = []string{
  47. "MsgInit",
  48. "MsgGetPosition",
  49. "MsgGetPositions",
  50. "MsgGetInstrument",
  51. "MsgGetInstruments",
  52. "MsgGetAccount",
  53. "MsgSetTick",
  54. "MsgSetOrder",
  55. "MsgGetOrders",
  56. "MsgSetRejected",
  57. "MsgSetAccount",
  58. "MsgSetAccountDetails",
  59. "MsgSetPosition",
  60. "MsgSetInstrument",
  61. "MsgSetExchangeRate",
  62. "MsgGetExchangeRate",
  63. "MsgGetTick",
  64. "MsgGetAllTick",
  65. "MsgGetInstument",
  66. "MsgGetAllInstrument",
  67. "MsgShutDown",
  68. "MsgMtfBindPrivate",
  69. "MsgMtfBindPublic",
  70. "MsgTimeout",
  71. "MsgEcho",
  72. "MsgClose",
  73. "MsgSetObStatus",
  74. "MsgSetExecution",
  75. "MsgSetOneExecution",
  76. "MsgCancelOrder",
  77. "MsgCloseOrder",
  78. "MsgAmendOrder",
  79. "MsgPlaceOrder",
  80. "MsgCloneAccount",
  81. "MsgLog",
  82. "MsgLoged",
  83. "MsgMatched",
  84. "MsgCount",
  85. }
  86. const (
  87. NeedLog = 1 << iota
  88. SendAsyn
  89. Public
  90. Private
  91. )
  92. var ErrTimeout = errors.New("timeout")
  93. var messageId int64
  94. type Message struct {
  95. Type int
  96. Flag int
  97. ClientId int64
  98. Id int64
  99. Err error
  100. SendTime int64
  101. RecvTime int64
  102. Data interface{}
  103. Ch chan *Message
  104. }
  105. func NewMessage(mtype int, clientId int64, data interface{}, id int64) *Message {
  106. err := error(nil)
  107. if id == 0 {
  108. id = atomic.AddInt64(&messageId, 1)
  109. }
  110. if mtype == MsgTimeout {
  111. err = ErrTimeout
  112. }
  113. return &Message{mtype, 0, clientId, id, err, 0, 0, data, nil}
  114. }
  115. func (msg *Message) GetId() int64 {
  116. return msg.Id
  117. }
  118. func (msg *Message) Name() string {
  119. return msgname[msg.Type]
  120. }
  121. func (msg *Message) String() string {
  122. return "[" + msgname[msg.Type] + "]" + fmt.Sprint(msg.Id, msg.Err, msg.Data, msg.SendTime, msg.RecvTime)
  123. }
  124. func getTime() int64 {
  125. return time.Now().UnixNano()
  126. }