123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- package tcp_packet
- import (
- "fmt"
- "net"
- tower "tower-monitor/handler/v1"
- )
- func monitorDataHandel(src []byte) (res []byte, sn string, err error) {
- // 解析数据帧
- res, sn, err = tower.TowerFrameParse(src)
- if err != nil {
- fmt.Printf("TowerFrameParse:%v\n", err)
- }
- return res, sn, 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("read read:%v,%v,%s\n", read_len, err, request[:read_len])
- if err != nil {
- break
- }
- gleft = append(gleft, request[:read_len]...)
- for {
- result, gleft, isComplete, err = tower.TowerParseTcp(gleft[:])
- fmt.Printf("is complete:%v, err:%v\n", isComplete, err)
- if err != nil {
- return
- }
- // 包没有读完继续读取数据包
- if isComplete == false {
- break
- }
- fmt.Printf("complete:%s\n", result)
- // 处理数据
- res, _, err := monitorDataHandel(result)
- if err != nil {
- return
- }
- conn.Write(res)
- }
- }
- }
|