msgRealQuote.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /**
  2. * @Author : zou.yingbin
  3. * @Create : 2022/3/26 14:52
  4. * @Modify : 2022/3/26 14:52
  5. * @note :
  6. */
  7. package client
  8. import (
  9. "mtp20access/packet"
  10. "strconv"
  11. )
  12. // DispatchRealQuote 分发行情
  13. func DispatchRealQuote(p *packet.MiQuotePacket, clinet *Client) {
  14. // TODO: 目前只实现了订阅发送模式, 未支持全部发送模式
  15. pending := make([]byte, 0)
  16. // 解析接收到的商品
  17. ware := parseWareInfo(p)
  18. // 获取已订阅行情商品列表
  19. quoteSubs := clinet.GetQuoteSubs()
  20. for _, sub := range quoteSubs {
  21. for i := range ware {
  22. w := &ware[i]
  23. if w.exchId == strconv.Itoa(sub.ExchangeId) && w.goodsCode == sub.GoodsCode {
  24. pending = append(pending, w.buf...)
  25. }
  26. }
  27. }
  28. if len(pending) > 0 {
  29. // 按商品重新打包
  30. quote := packet.MiQuotePacket{
  31. BigType: p.BigType,
  32. SmallType: p.SmallType,
  33. SerialNum: p.SerialNum,
  34. Mode: p.Mode,
  35. }
  36. quote.Msg = pending
  37. sendBuf := quote.EnPack()
  38. // 发送给客户端
  39. clinet.quoteChan <- sendBuf
  40. }
  41. }
  42. // parseWareInfo 从报文中解析出所有报价商品
  43. func parseWareInfo(p *packet.MiQuotePacket) []wareInfo {
  44. data := p.OriMsg[14:]
  45. ware := make([]wareInfo, 0)
  46. nPos1, nPos2 := -1, -1
  47. for i := 0; i < len(data); i++ {
  48. // 报价包开始
  49. if data[i] == 0x10 {
  50. nPos1 = i
  51. }
  52. // 报价包结束
  53. if data[i] == 0x11 {
  54. nPos2 = i + 1
  55. }
  56. if nPos1 >= 0 && nPos2 > 0 {
  57. v := wareInfo{buf: data[nPos1:nPos2]}
  58. v.parseField()
  59. //v.printInfo()
  60. //v.debugPrintAllField()
  61. ware = append(ware, v)
  62. // 重置nPos1, nPos2 继续查找下一个报价包
  63. nPos1, nPos2 = -1, -1
  64. }
  65. }
  66. return ware
  67. }