udp接口代码优化

Signed-off-by: lixxxww <941403820@qq.com>
This commit is contained in:
lixxxww
2024-01-22 14:27:27 +00:00
committed by Gitee
parent 98e472c57c
commit 327d57d9b0

View File

@@ -5,13 +5,14 @@ import (
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"net" "net"
"time"
udpclient "pandax/iothub/client/updclient" udpclient "pandax/iothub/client/updclient"
"pandax/iothub/hook_message_work" "pandax/iothub/hook_message_work"
"pandax/iothub/netbase" "pandax/iothub/netbase"
"pandax/pkg/global" "pandax/pkg/global"
"pandax/pkg/global/model" "pandax/pkg/global/model"
"pandax/pkg/rule_engine/message" "pandax/pkg/rule_engine/message"
"time"
) )
type HookUdpService struct { type HookUdpService struct {
@@ -25,9 +26,9 @@ func InitUdpHook(addr string, hs *hook_message_work.HookService) {
if err != nil { if err != nil {
global.Log.Error("IOTHUB UDP服务启动错误", err) global.Log.Error("IOTHUB UDP服务启动错误", err)
return return
} else {
global.Log.Infof("UDP IOTHUB HOOK Start SUCCESS, Server listen: %s", addr)
} }
global.Log.Infof("UDP IOTHUB HOOK Start SUCCESS, Server listen: %s", addr)
buffer := make([]byte, 1024) buffer := make([]byte, 1024)
authMap := make(map[string]bool) authMap := make(map[string]bool)
etoken := &model.DeviceAuth{} etoken := &model.DeviceAuth{}
@@ -35,12 +36,13 @@ func InitUdpHook(addr string, hs *hook_message_work.HookService) {
HookService: hs, HookService: hs,
conn: server.listener, conn: server.listener,
} }
for { for {
n, client, err := server.listener.ReadFromUDP(buffer) n, client, err := server.listener.ReadFromUDP(buffer)
if err != nil { if err != nil {
global.Log.Error("Error accepting connection:", err) global.Log.Error("Error accepting connection:", err)
_ = server.listener.Close() _ = server.listener.Close()
//设置断开连接
if isAuth, ok := authMap[client.AddrPort().String()]; ok && isAuth { if isAuth, ok := authMap[client.AddrPort().String()]; ok && isAuth {
data := netbase.CreateConnectionInfo(message.DisConnectMes, "udp", client.IP.String(), client.AddrPort().String(), etoken) data := netbase.CreateConnectionInfo(message.DisConnectMes, "udp", client.IP.String(), client.AddrPort().String(), etoken)
go hhs.HookService.Queue.Queue(data) go hhs.HookService.Queue.Queue(data)
@@ -49,6 +51,7 @@ func InitUdpHook(addr string, hs *hook_message_work.HookService) {
delete(authMap, client.AddrPort().String()) delete(authMap, client.AddrPort().String())
continue continue
} }
if isAuth, ok := authMap[client.AddrPort().String()]; ok && isAuth { if isAuth, ok := authMap[client.AddrPort().String()]; ok && isAuth {
data := &netbase.DeviceEventInfo{ data := &netbase.DeviceEventInfo{
DeviceId: etoken.DeviceId, DeviceId: etoken.DeviceId,
@@ -61,13 +64,12 @@ func InitUdpHook(addr string, hs *hook_message_work.HookService) {
data.Type = message.RowMes data.Type = message.RowMes
data.Datas = fmt.Sprintf(`{"ts": "%s","rowdata": "%s"}`, ts, hexData) data.Datas = fmt.Sprintf(`{"ts": "%s","rowdata": "%s"}`, ts, hexData)
// etoken中添加设备标识
go hhs.HookService.Queue.Queue(data) go hhs.HookService.Queue.Queue(data)
} else { } else {
token := string(buffer[:n]) token := string(buffer[:n])
etoken.GetDeviceToken(token) etoken.GetDeviceToken(token)
auth := netbase.Auth(token) auth := netbase.Auth(token)
// 认证成功,创建连接记录
if auth { if auth {
global.Log.Infof("UDP协议 设备%s,认证成功", etoken.DeviceId) global.Log.Infof("UDP协议 设备%s,认证成功", etoken.DeviceId)
data := netbase.CreateConnectionInfo(message.ConnectMes, "udp", client.IP.String(), client.AddrPort().String(), etoken) data := netbase.CreateConnectionInfo(message.ConnectMes, "udp", client.IP.String(), client.AddrPort().String(), etoken)
@@ -110,4 +112,4 @@ func (hhs *HookUdpService) SendBytes(addr *net.UDPAddr, msg []byte) error {
hhs.HookService.MessageCh <- data hhs.HookService.MessageCh <- data
} }
return err return err
} }