msgRealQuote.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. if clinet.quoteWriteChan != nil {
  40. clinet.quoteWriteChan <- sendBuf
  41. }
  42. }
  43. }
  44. // parseWareInfo 从报文中解析出所有报价商品
  45. func parseWareInfo(p *packet.MiQuotePacket) []wareInfo {
  46. data := p.OriMsg[14:]
  47. ware := make([]wareInfo, 0)
  48. nPos1, nPos2 := -1, -1
  49. for i := 0; i < len(data); i++ {
  50. // 报价包开始
  51. if data[i] == 0x10 {
  52. nPos1 = i
  53. }
  54. // 报价包结束
  55. if data[i] == 0x11 {
  56. nPos2 = i + 1
  57. }
  58. if nPos1 >= 0 && nPos2 > 0 {
  59. v := wareInfo{buf: data[nPos1:nPos2]}
  60. v.parseField()
  61. //v.printInfo()
  62. //v.debugPrintAllField()
  63. ware = append(ware, v)
  64. // 重置nPos1, nPos2 继续查找下一个报价包
  65. nPos1, nPos2 = -1, -1
  66. }
  67. }
  68. return ware
  69. }