123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- // Copyright 2019 autocareai.com. All rights reserved.
- // Use of this source code is governed by autocareai.com.
- package tcp_packet
- import (
- "context"
- dust "dust-monitor/handler/v1"
- "dust-monitor/util"
- "fmt"
- "go.uber.org/zap"
- "net"
- "github.com/jaryhe/gopkgs/logger"
- "time"
- )
- func monitorDataHandel(src []byte) (err error) {
- // 解析数据帧
- frame := util.DustFrameParse(src)
- err = dust.DustDataHandle(context.Background(), &frame)
- return err
- }
- func HandleSocketClient(conn net.Conn) {
- // 处理完后关闭连接
- defer conn.Close()
- request := make([]byte, 1024)
- var gleft, result []byte
- var isComplete bool
- for {
- read_len, err := conn.Read(request)
- fmt.Printf("dust once read:length:%v, err:%v, bytes:%s\n", read_len, err, request[:read_len])
- start := time.Now().UnixNano()
- if err != nil {
- logger.Error("dust read start error",
- zap.String("client", conn.RemoteAddr().String()),
- zap.String("err", err.Error()),
- zap.String("msg", string(request[:read_len])),
- zap.String("timestamp", fmt.Sprintf("%d", start)))
- break
- } else {
- logger.Info("dust read start",
- zap.String("client", conn.RemoteAddr().String()),
- zap.String("msg", string(request[:read_len])),
- zap.String("timestamp", fmt.Sprintf("%d", start)))
- }
- gleft = append(gleft, request[:read_len]...)
- for {
- result, gleft, isComplete, err = util.DustParse(gleft[:])
- if err != nil {
- return
- }
- // 包没有读完继续读取数据包
- if isComplete == false {
- break
- }
- fmt.Printf("complete bytes:%s\n", result)
- // 处理数据
- err = monitorDataHandel(result)
- end := time.Now().UnixNano()
- if err != nil {
- logger.Error("dust handle over error",
- zap.String("client", conn.RemoteAddr().String()),
- zap.String("error", err.Error()),
- zap.String("bytes", string(result)),
- zap.String("timediff",fmt.Sprintf("%d", end-start)),
- zap.String("timestamp", fmt.Sprintf("%d", end)))
- } else {
- logger.Info("dust handle over",
- zap.String("client", conn.RemoteAddr().String()),
- zap.String("timediff",fmt.Sprintf("%d", end-start)),
- zap.String("timestamp", fmt.Sprintf("%d", end)))
- }
- }
- }
- }
|