Skip to content

Commit

Permalink
lock in udp hub
Browse files Browse the repository at this point in the history
  • Loading branch information
DarienRaymond committed Jul 13, 2016
1 parent fc69c77 commit c7b0264
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion transport/internet/udp/udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package udp

import (
"net"
"sync"

"github.com/v2ray/v2ray-core/common/alloc"
v2net "github.com/v2ray/v2ray-core/common/net"
Expand All @@ -10,6 +11,7 @@ import (
type UDPPayloadHandler func(*alloc.Buffer, v2net.Destination)

type UDPHub struct {
sync.RWMutex
conn *net.UDPConn
callback UDPPayloadHandler
accepting bool
Expand All @@ -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()
}
Expand All @@ -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 {
Expand All @@ -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
}

0 comments on commit c7b0264

Please sign in to comment.