monitor.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package tcp_packet
  2. import (
  3. "fmt"
  4. "net"
  5. lift "lift-monitor/handler/v1"
  6. )
  7. func monitorDataHandel(src []byte) (res []byte, sn string, err error) {
  8. // 解析数据帧
  9. res, sn, err = lift.LiftFrameParse(src)
  10. if err != nil {
  11. fmt.Printf("LiftFrameParse:%v\n", err)
  12. }
  13. return res, sn, err
  14. }
  15. func HandleSocketClient(conn net.Conn) {
  16. // 处理完后关闭连接
  17. defer conn.Close()
  18. request := make([]byte, 1024)
  19. var gleft, result []byte
  20. var isComplete bool
  21. for {
  22. read_len, err := conn.Read(request)
  23. fmt.Printf("read read:%v,%v,%s\n", read_len, err, request[:read_len])
  24. if err != nil {
  25. break
  26. }
  27. gleft = append(gleft, request[:read_len]...)
  28. for {
  29. result, gleft, isComplete, err = lift.LiftParseTcp(gleft[:])
  30. fmt.Printf("is complete:%v, err:%v\n", isComplete, err)
  31. if err != nil {
  32. return
  33. }
  34. // 包没有读完继续读取数据包
  35. if isComplete == false {
  36. break
  37. }
  38. fmt.Printf("complete:%s\n", result)
  39. // 处理数据
  40. res, _, err := monitorDataHandel(result)
  41. if err != nil {
  42. return
  43. }
  44. conn.Write(res)
  45. }
  46. }
  47. }