message.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. package msq
  2. import "sync/atomic"
  3. import "errors"
  4. import "fmt"
  5. import "time"
  6. import "sync"
  7. const (
  8. MsgInit = iota
  9. MsgShutDown
  10. MsgClose
  11. MsgMsqBindPrivate
  12. MsgMsqBindPublic
  13. MsgTimeout
  14. MsgEcho
  15. MsgLog
  16. MsgMatch
  17. MsgMulti
  18. MsgCheckReqId
  19. MsgCancelOrder
  20. MsgPlaceOrder
  21. MsgNotify
  22. MsgConnect
  23. MsgMarketInfo
  24. MsgLoadAccount
  25. MsgDeposit
  26. MsgWithdraw
  27. MsgWalletInfo
  28. MsgActiveOrderList
  29. MsgHistoryOrderList
  30. MsgGetCount
  31. MsgTopAccount
  32. MsgEmpty
  33. MsgTKDown
  34. MsgTKGetTicks
  35. MsgTKSub
  36. MsgTKHis
  37. MsgTK
  38. MsgTKS
  39. MsgInss
  40. MsgIns
  41. MsgGetPage
  42. MsgGetPageRange
  43. MsgDelete
  44. MsgGetSnapRange
  45. MsgRecoverSnap
  46. MsgSetConfig
  47. MsgGetConfig
  48. MsgCount
  49. )
  50. var msgname = []string{
  51. "MsgInit",
  52. "MsgShutDown",
  53. "MsgClose",
  54. "MsgMsqBindPrivate",
  55. "MsgMsqBindPublic",
  56. "MsgTimeout",
  57. "MsgEcho",
  58. "MsgLog",
  59. "MsgMatch",
  60. "MsgMulti",
  61. "MsgCheckReqId",
  62. "MsgCancelOrder",
  63. "MsgPlaceOrder",
  64. "MsgNotify",
  65. "MsgConnect",
  66. "MsgMarketInfo",
  67. "MsgLoadAccount",
  68. "MsgDeposit",
  69. "MsgWithdraw",
  70. "MsgWalletInfo",
  71. "MsgActiveOrderList",
  72. "MsgHistoryOrderList",
  73. "MsgGetCount",
  74. "MsgTopAccount",
  75. "MsgEmpty",
  76. "MsgTKDown",
  77. "MsgTKGetTicks",
  78. "MsgTKSub",
  79. "MsgTKHis",
  80. "MsgTK",
  81. "MsgTKS",
  82. "MsgInss",
  83. "MsgIns",
  84. "MsgGetPage",
  85. "MsgGetPageRange",
  86. "MsgDelete",
  87. "MsgGetSnapRange",
  88. "MsgRecoverSnap",
  89. "MsgSetConfig",
  90. "MsgGetConfig",
  91. "MsgCount",
  92. }
  93. var custommsg = make(map[int]string)
  94. const (
  95. NeedLog = 1 << iota
  96. SendAsyn
  97. Public
  98. Private
  99. )
  100. var ErrTimeout = errors.New("timeout")
  101. var messageId int64
  102. func RegMessage(ty int, msgname string) error {
  103. if ty < MsgCount {
  104. return errors.New("message type less than msgcount error")
  105. }
  106. custommsg[ty] = msgname
  107. return nil
  108. }
  109. func getMsgName(ty int) string {
  110. if ty < MsgCount {
  111. return msgname[ty]
  112. }
  113. return custommsg[ty]
  114. }
  115. type Message struct {
  116. Type int
  117. Index int32
  118. Flag int32
  119. ClientId int64
  120. Id int64
  121. err error
  122. SendTime int64
  123. RecvTime int64
  124. mu sync.Mutex
  125. data interface{}
  126. Ch chan *Message
  127. }
  128. func NewMessage(mtype int, clientId int64, data interface{}, id int64) *Message {
  129. err := error(nil)
  130. if id == 0 {
  131. id = atomic.AddInt64(&messageId, 1)
  132. }
  133. if mtype == MsgTimeout {
  134. err = ErrTimeout
  135. }
  136. return &Message{mtype, 0, 0, clientId, id, err, 0, 0, sync.Mutex{}, data, nil}
  137. }
  138. func (msg *Message) SetErr(err error) {
  139. msg.mu.Lock()
  140. defer msg.mu.Unlock()
  141. msg.err = err
  142. }
  143. func (msg *Message) GetErr() (err error) {
  144. msg.mu.Lock()
  145. defer msg.mu.Unlock()
  146. return msg.err
  147. }
  148. func (msg *Message) SetData(data interface{}) {
  149. msg.mu.Lock()
  150. defer msg.mu.Unlock()
  151. msg.data = data
  152. }
  153. func (msg *Message) GetData() interface{} {
  154. msg.mu.Lock()
  155. defer msg.mu.Unlock()
  156. return msg.data
  157. }
  158. func (msg *Message) GetId() int64 {
  159. return atomic.LoadInt64(&msg.Id)
  160. }
  161. func (msg *Message) SetId(id int64) {
  162. atomic.StoreInt64(&msg.Id, id)
  163. }
  164. func (msg *Message) Name() string {
  165. return getMsgName(msg.Type)
  166. }
  167. func (msg *Message) String() string {
  168. return "[" + getMsgName(msg.Type) + "]" + fmt.Sprint(msg.Id, msg.GetErr(), msg.GetData(), "index = ", msg.GetIndex())
  169. }
  170. func (msg *Message) IncIndex() int32 {
  171. return atomic.AddInt32(&msg.Index, 1)
  172. }
  173. func (msg *Message) GetIndex() int32 {
  174. return atomic.LoadInt32(&msg.Index)
  175. }
  176. func getTime() int64 {
  177. return time.Now().UnixNano()
  178. }