1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-09-07 04:24:23 -04:00
v2fly/transport/hub/udp_server.go

144 lines
3.2 KiB
Go
Raw Normal View History

2016-02-01 10:36:33 -05:00
package hub
import (
"sync"
2016-05-10 18:43:02 -04:00
"time"
2016-02-01 10:36:33 -05:00
"github.com/v2ray/v2ray-core/app/dispatcher"
2016-04-25 18:13:26 -04:00
"github.com/v2ray/v2ray-core/common/alloc"
2016-02-01 10:36:33 -05:00
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/transport/ray"
)
2016-04-25 18:13:26 -04:00
type UDPResponseCallback func(destination v2net.Destination, payload *alloc.Buffer)
2016-02-01 10:36:33 -05:00
2016-05-10 18:43:02 -04:00
type TimedInboundRay struct {
name string
2016-02-01 10:36:33 -05:00
inboundRay ray.InboundRay
2016-05-10 18:43:02 -04:00
accessed chan bool
server *UDPServer
sync.RWMutex
}
func NewTimedInboundRay(name string, inboundRay ray.InboundRay) *TimedInboundRay {
r := &TimedInboundRay{
name: name,
inboundRay: inboundRay,
accessed: make(chan bool),
}
go r.Monitor()
return r
}
func (this *TimedInboundRay) Monitor() {
for {
time.Sleep(16 * time.Second)
select {
case <-this.accessed:
default:
// Ray not accessed for a while, assuming communication is dead.
this.Release()
return
}
}
}
func (this *TimedInboundRay) InboundInput() ray.OutputStream {
this.RLock()
defer this.RUnlock()
if this.inboundRay == nil {
return nil
}
select {
case this.accessed <- true:
default:
}
return this.inboundRay.InboundInput()
}
func (this *TimedInboundRay) InboundOutput() ray.InputStream {
this.RLock()
this.RUnlock()
if this.inboundRay == nil {
return nil
}
select {
case this.accessed <- true:
default:
}
return this.inboundRay.InboundOutput()
}
func (this *TimedInboundRay) Release() {
this.Lock()
defer this.Unlock()
if this.server == nil {
return
}
this.server.RemoveRay(this.name)
this.server = nil
this.inboundRay.InboundInput().Close()
this.inboundRay.InboundOutput().Release()
this.inboundRay = nil
2016-02-01 10:36:33 -05:00
}
type UDPServer struct {
sync.RWMutex
2016-05-10 18:43:02 -04:00
conns map[string]*TimedInboundRay
2016-02-01 10:36:33 -05:00
packetDispatcher dispatcher.PacketDispatcher
}
func NewUDPServer(packetDispatcher dispatcher.PacketDispatcher) *UDPServer {
return &UDPServer{
2016-05-10 18:43:02 -04:00
conns: make(map[string]*TimedInboundRay),
2016-02-01 10:36:33 -05:00
packetDispatcher: packetDispatcher,
}
}
2016-05-10 18:43:02 -04:00
func (this *UDPServer) RemoveRay(name string) {
this.Lock()
defer this.Unlock()
delete(this.conns, name)
}
func (this *UDPServer) locateExistingAndDispatch(name string, payload *alloc.Buffer) bool {
2016-02-01 10:36:33 -05:00
this.RLock()
defer this.RUnlock()
2016-05-10 18:43:02 -04:00
if entry, found := this.conns[name]; found {
err := entry.InboundInput().Write(payload)
if err != nil {
this.RemoveRay(name)
return false
}
2016-02-01 10:36:33 -05:00
return true
}
return false
}
2016-04-25 18:13:26 -04:00
func (this *UDPServer) Dispatch(source v2net.Destination, destination v2net.Destination, payload *alloc.Buffer, callback UDPResponseCallback) {
2016-05-10 18:43:02 -04:00
destString := source.Address().String() + "-" + destination.Address().String()
2016-04-25 18:13:26 -04:00
if this.locateExistingAndDispatch(destString, payload) {
2016-02-01 10:36:33 -05:00
return
}
this.Lock()
2016-04-25 18:13:26 -04:00
inboundRay := this.packetDispatcher.DispatchToOutbound(destination)
inboundRay.InboundInput().Write(payload)
2016-05-10 18:43:02 -04:00
timedInboundRay := NewTimedInboundRay(destString, inboundRay)
this.conns[destString] = timedInboundRay
2016-02-01 10:36:33 -05:00
this.Unlock()
2016-05-10 18:43:02 -04:00
go this.handleConnection(timedInboundRay, source, callback)
2016-02-01 10:36:33 -05:00
}
2016-05-10 18:43:02 -04:00
func (this *UDPServer) handleConnection(inboundRay *TimedInboundRay, source v2net.Destination, callback UDPResponseCallback) {
2016-04-18 12:44:10 -04:00
for {
data, err := inboundRay.InboundOutput().Read()
if err != nil {
break
}
2016-04-25 18:13:26 -04:00
callback(source, data)
2016-02-01 10:36:33 -05:00
}
2016-05-10 18:43:02 -04:00
inboundRay.Release()
2016-02-01 10:36:33 -05:00
}