This commit is contained in:
yangduo 2024-09-12 16:54:23 +08:00
parent f1771dec9f
commit cee0dec313

View File

@ -14,7 +14,6 @@ import (
)
type UDPListener struct {
// ss.MsgHandlerImpl
udpconn *net.UDPConn
ch chan bool
}
@ -28,8 +27,7 @@ func (this *UDPListener) Init() {
panic(err)
} else {
this.udpconn = conn
// go this.serverRead()
go this.serverRead2()
go this.serverRead()
}
}
@ -41,38 +39,7 @@ func (this *UDPListener) UnInit() {
close(this.ch)
}
var UDPPACKHEAD = 2
func (this *UDPListener) serverRead() {
for {
this.ch <- true
go func(conn *net.UDPConn, ch chan bool) {
buf := make([]byte, jccommon.MAX_PACKET_LEN+UDPPACKHEAD)
bufLen, _, err := this.udpconn.ReadFrom(buf)
if err == nil && bufLen > UDPPACKHEAD {
dataLen := q5.MkUInt16(buf[0], buf[1])
if bufLen >= int(UDPPACKHEAD)+int(dataLen) {
obj := struct {
Topic string `json:"topic"`
Expire int32 `json:"expire"`
Msg string `json:"msg"`
}{}
data := buf[UDPPACKHEAD : UDPPACKHEAD+int(dataLen)]
if json.Unmarshal(data, &obj) == nil {
expire := obj.Expire
if expire > 86400*7 {
expire = 86400 * 7
}
service.MqManager.PublishTopic(obj.Topic, obj.Msg, expire)
}
}
}
<-ch
}(this.udpconn, this.ch)
}
}
func (this *UDPListener) serverRead2() {
for {
buf := make([]byte, jccommon.MAX_PACKET_LEN)
bufLen, peer, err := this.udpconn.ReadFromUDP(buf)