msgRealQuote.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. "fmt"
  10. "mtp20access/packet"
  11. "strconv"
  12. )
  13. // DispatchRealQuote 分发行情
  14. func DispatchRealQuote(p *packet.MiQuotePacket, clinet *Client) {
  15. // TODO: 目前只实现了订阅发送模式, 未支持全部发送模式
  16. pending := make([]byte, 0)
  17. // 解析接收到的商品
  18. ware := parseWareInfo(p)
  19. // 获取已订阅行情商品列表
  20. quoteSubs := clinet.GetQuoteSubs()
  21. for _, sub := range quoteSubs {
  22. for i := range ware {
  23. w := &ware[i]
  24. if w.exchId == strconv.Itoa(sub.ExchangeId) && w.goodsCode == sub.GoodsCode {
  25. pending = append(pending, w.buf...)
  26. }
  27. }
  28. }
  29. if len(pending) > 0 {
  30. // 按商品重新打包
  31. quote := packet.MiQuotePacket{
  32. BigType: p.BigType,
  33. SmallType: p.SmallType,
  34. SerialNum: p.SerialNum,
  35. Mode: p.Mode,
  36. }
  37. quote.Msg = pending
  38. sendBuf := quote.EnPack()
  39. // 发送给客户端
  40. if clinet.quoteWriteChan != nil {
  41. clinet.quoteWriteChan <- sendBuf
  42. }
  43. }
  44. }
  45. // parseWareInfo 从报文中解析出所有报价商品
  46. func parseWareInfo(p *packet.MiQuotePacket) []wareInfo {
  47. ware := make([]wareInfo, 0)
  48. // 0x42(66)是控制信息,要过滤掉
  49. if p.OriMsg[5] == 0x42 {
  50. return ware
  51. }
  52. data := p.OriMsg[14:]
  53. nPos1, nPos2 := -1, -1
  54. for i := 0; i < len(data); i++ {
  55. // 报价包开始
  56. if data[i] == 0x10 {
  57. nPos1 = i
  58. }
  59. // 报价包结束
  60. if data[i] == 0x11 {
  61. nPos2 = i + 1
  62. }
  63. if nPos1 >= 0 && nPos2 > 0 {
  64. // 处理闪退问题
  65. if nPos1 > nPos2 {
  66. fmt.Printf("接收到错误的行情记录(nPos1>nPos2): %v \n", p.OriMsg)
  67. // 重置nPos1, nPos2 继续查找下一个报价包
  68. nPos1, nPos2 = -1, -1
  69. }
  70. v := wareInfo{buf: data[nPos1:nPos2]}
  71. v.parseField()
  72. //v.printInfo()
  73. //v.debugPrintAllField()
  74. ware = append(ware, v)
  75. // 重置nPos1, nPos2 继续查找下一个报价包
  76. nPos1, nPos2 = -1, -1
  77. }
  78. }
  79. return ware
  80. }