From 98e472c57c5ee74a142dfee9990e864cadaaf4ec Mon Sep 17 00:00:00 2001 From: lixxxww <941403820@qq.com> Date: Mon, 22 Jan 2024 14:26:46 +0000 Subject: [PATCH] =?UTF-8?q?tcp=E6=8E=A5=E5=8F=A3=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: lixxxww <941403820@qq.com> --- iothub/server/tcpserver/hook.go | 90 ++++++++++++++++----------------- 1 file changed, 44 insertions(+), 46 deletions(-) diff --git a/iothub/server/tcpserver/hook.go b/iothub/server/tcpserver/hook.go index 9048173..a80960d 100644 --- a/iothub/server/tcpserver/hook.go +++ b/iothub/server/tcpserver/hook.go @@ -25,77 +25,75 @@ func InitTcpHook(addr string, hs *hook_message_work.HookService) { if err != nil { global.Log.Error("IOTHUB TCP服务启动错误", err) return - } else { - global.Log.Infof("TCP IOTHUB HOOK Start SUCCESS, Server listen: %s", addr) } - go func() { - for { - conn, err := server.listener.AcceptTCP() - if err != nil { - global.Log.Error("Error accepting connection:", err) - continue - } - //conn.SetReadDeadline(time.Now().Add(2 * time.Minute)) - hhs := &HookTcpService{ - HookService: hs, - conn: conn, - } - go hhs.hook() - - } - }() + global.Log.Infof("TCP IOTHUB HOOK Start SUCCESS, Server listen: %s", addr) + go acceptConnections(server.listener, hs) } -func (hhs *HookTcpService) hook() { +func acceptConnections(listener *net.TCPListener, hs *hook_message_work.HookService) { + for { + conn, err := listener.AcceptTCP() + if err != nil { + global.Log.Error("Error accepting connection:", err) + continue + } + go handleConnection(conn, hs) + } +} + +func handleConnection(conn *net.TCPConn, hs *hook_message_work.HookService) { isAuth := false etoken := &model.DeviceAuth{} + defer func() { + _ = conn.Close() + if isAuth { + data := netbase.CreateConnectionInfo(message.DisConnectMes, "tcp", conn.RemoteAddr().String(), conn.RemoteAddr().String(), etoken) + go hs.Queue.Queue(data) + } + tcpclient.TcpClient.Delete(etoken.DeviceId) + }() + for { buf := make([]byte, 128) - n := 0 - n, err := hhs.conn.Read(buf) + n, err := conn.Read(buf) if err != nil { - _ = hhs.conn.Close() - //设置断开连接 - if isAuth { - data := netbase.CreateConnectionInfo(message.DisConnectMes, "tcp", hhs.conn.RemoteAddr().String(), hhs.conn.RemoteAddr().String(), etoken) - go hhs.HookService.Queue.Queue(data) - } - tcpclient.TcpClient.Delete(etoken.DeviceId) - isAuth = false return } + if !isAuth { token := string(buf[:n]) etoken.GetDeviceToken(token) auth := netbase.Auth(token) - // 认证成功,创建连接记录 if auth { global.Log.Infof("TCP协议 设备%s,认证成功", etoken.DeviceId) - data := netbase.CreateConnectionInfo(message.ConnectMes, "tcp", hhs.conn.RemoteAddr().String(), hhs.conn.RemoteAddr().String(), etoken) - go hhs.HookService.Queue.Queue(data) + data := netbase.CreateConnectionInfo(message.ConnectMes, "tcp", conn.RemoteAddr().String(), conn.RemoteAddr().String(), etoken) + go hs.Queue.Queue(data) isAuth = true - tcpclient.TcpClient.Store(etoken.DeviceId, hhs.conn) - hhs.Send("success") + tcpclient.TcpClient.Store(etoken.DeviceId, conn) + sendResponse(conn, "success") } else { - hhs.Send("fail") + sendResponse(conn, "fail") } } else { - data := &netbase.DeviceEventInfo{ - DeviceId: etoken.DeviceId, - DeviceAuth: etoken, - } - hexData := hex.EncodeToString(buf[:n]) global.Log.Infof("TCP协议 设备%s, 接受消息%s", etoken.DeviceId, hexData) ts := time.Now().Format("2006-01-02 15:04:05.000") - data.Type = message.RowMes - data.Datas = fmt.Sprintf(`{"ts": "%s","rowdata": "%s"}`, ts, hexData) - - // etoken中添加设备标识 - go hhs.HookService.Queue.Queue(data) + data := &netbase.DeviceEventInfo{ + DeviceId: etoken.DeviceId, + DeviceAuth: etoken, + Type: message.RowMes, + Datas: fmt.Sprintf(`{"ts": "%s","rowdata": "%s"}`, ts, hexData), + } + go hs.Queue.Queue(data) } } +} +func sendResponse(conn *net.TCPConn, message string) { + _, err := conn.Write([]byte(message)) + if err != nil { + conn.Close() + } } func (hhs *HookTcpService) Send(message string) error { @@ -127,4 +125,4 @@ func (hhs *HookTcpService) SendBytes(msg []byte) error { func isHex(str string) bool { _, err := hex.DecodeString(str) return err == nil -} +} \ No newline at end of file