1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-09-29 23:36:25 -04:00

lock in udp hub

This commit is contained in:
v2ray 2016-07-13 21:39:18 +02:00
parent fc69c77369
commit c7b0264f9a
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169

View File

@ -2,6 +2,7 @@ package udp
import (
"net"
"sync"
"github.com/v2ray/v2ray-core/common/alloc"
v2net "github.com/v2ray/v2ray-core/common/net"
@ -10,6 +11,7 @@ import (
type UDPPayloadHandler func(*alloc.Buffer, v2net.Destination)
type UDPHub struct {
sync.RWMutex
conn *net.UDPConn
callback UDPPayloadHandler
accepting bool
@ -32,6 +34,9 @@ func ListenUDP(address v2net.Address, port v2net.Port, callback UDPPayloadHandle
}
func (this *UDPHub) Close() {
this.Lock()
defer this.Unlock()
this.accepting = false
this.conn.Close()
}
@ -44,8 +49,11 @@ func (this *UDPHub) WriteTo(payload []byte, dest v2net.Destination) (int, error)
}
func (this *UDPHub) start() {
this.Lock()
this.accepting = true
for this.accepting {
this.Unlock()
for this.Running() {
buffer := alloc.NewBuffer()
nBytes, addr, err := this.conn.ReadFromUDP(buffer.Value)
if err != nil {
@ -57,3 +65,10 @@ func (this *UDPHub) start() {
go this.callback(buffer, dest)
}
}
func (this *UDPHub) Running() bool {
this.RLock()
defer this.RUnlock()
return this.accepting
}