mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-01-03 07:56:42 -05:00
lock in udp hub
This commit is contained in:
parent
fc69c77369
commit
c7b0264f9a
@ -2,6 +2,7 @@ package udp
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net"
|
"net"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/v2ray/v2ray-core/common/alloc"
|
"github.com/v2ray/v2ray-core/common/alloc"
|
||||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||||
@ -10,6 +11,7 @@ import (
|
|||||||
type UDPPayloadHandler func(*alloc.Buffer, v2net.Destination)
|
type UDPPayloadHandler func(*alloc.Buffer, v2net.Destination)
|
||||||
|
|
||||||
type UDPHub struct {
|
type UDPHub struct {
|
||||||
|
sync.RWMutex
|
||||||
conn *net.UDPConn
|
conn *net.UDPConn
|
||||||
callback UDPPayloadHandler
|
callback UDPPayloadHandler
|
||||||
accepting bool
|
accepting bool
|
||||||
@ -32,6 +34,9 @@ func ListenUDP(address v2net.Address, port v2net.Port, callback UDPPayloadHandle
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (this *UDPHub) Close() {
|
func (this *UDPHub) Close() {
|
||||||
|
this.Lock()
|
||||||
|
defer this.Unlock()
|
||||||
|
|
||||||
this.accepting = false
|
this.accepting = false
|
||||||
this.conn.Close()
|
this.conn.Close()
|
||||||
}
|
}
|
||||||
@ -44,8 +49,11 @@ func (this *UDPHub) WriteTo(payload []byte, dest v2net.Destination) (int, error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (this *UDPHub) start() {
|
func (this *UDPHub) start() {
|
||||||
|
this.Lock()
|
||||||
this.accepting = true
|
this.accepting = true
|
||||||
for this.accepting {
|
this.Unlock()
|
||||||
|
|
||||||
|
for this.Running() {
|
||||||
buffer := alloc.NewBuffer()
|
buffer := alloc.NewBuffer()
|
||||||
nBytes, addr, err := this.conn.ReadFromUDP(buffer.Value)
|
nBytes, addr, err := this.conn.ReadFromUDP(buffer.Value)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -57,3 +65,10 @@ func (this *UDPHub) start() {
|
|||||||
go this.callback(buffer, dest)
|
go this.callback(buffer, dest)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (this *UDPHub) Running() bool {
|
||||||
|
this.RLock()
|
||||||
|
defer this.RUnlock()
|
||||||
|
|
||||||
|
return this.accepting
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user