monitor.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Copyright 2019 autocareai.com. All rights reserved.
  2. // Use of this source code is governed by autocareai.com.
  3. package tcp_packet
  4. import (
  5. "context"
  6. dust "dust-monitor/handler/v1"
  7. "dust-monitor/util"
  8. "fmt"
  9. "net"
  10. )
  11. func monitorDataHandel(src []byte) (err error) {
  12. // 解析数据帧
  13. frame := util.DustFrameParse(src)
  14. err = dust.DustDataHandle(context.Background(), &frame)
  15. return err
  16. }
  17. func HandleSocketClient(conn net.Conn) {
  18. // 处理完后关闭连接
  19. defer conn.Close()
  20. request := make([]byte, 1024)
  21. var gleft, result []byte
  22. var isComplete bool
  23. for {
  24. read_len, err := conn.Read(request)
  25. fmt.Printf("read read:%v,%v,%s\n", read_len, err, request[:read_len])
  26. if err != nil {
  27. break
  28. }
  29. gleft = append(gleft, request[:read_len]...)
  30. for {
  31. result, gleft, isComplete, err = util.DustParse(gleft[:])
  32. if err != nil {
  33. return
  34. }
  35. // 包没有读完继续读取数据包
  36. if isComplete == false {
  37. break
  38. }
  39. fmt.Printf("complete:%s\n", result)
  40. // 处理数据
  41. monitorDataHandel(result)
  42. }
  43. }
  44. }