msgRealQuote.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. data := p.OriMsg[14:]
  48. ware := make([]wareInfo, 0)
  49. nPos1, nPos2 := -1, -1
  50. for i := 0; i < len(data); i++ {
  51. // 报价包开始
  52. if data[i] == 0x10 {
  53. nPos1 = i
  54. }
  55. // 报价包结束
  56. if data[i] == 0x11 {
  57. nPos2 = i + 1
  58. }
  59. if nPos1 >= 0 && nPos2 > 0 {
  60. // 处理闪退问题
  61. if nPos1 > nPos2 {
  62. fmt.Printf("接收到错误的行情记录(nPos1>nPos2): %v \n", p.OriMsg)
  63. // 重置nPos1, nPos2 继续查找下一个报价包
  64. nPos1, nPos2 = -1, -1
  65. }
  66. v := wareInfo{buf: data[nPos1:nPos2]}
  67. v.parseField()
  68. //v.printInfo()
  69. //v.debugPrintAllField()
  70. ware = append(ware, v)
  71. // 重置nPos1, nPos2 继续查找下一个报价包
  72. nPos1, nPos2 = -1, -1
  73. }
  74. }
  75. return ware
  76. }