1
This commit is contained in:
parent
f1771dec9f
commit
cee0dec313
@ -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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user