1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2026-06-04 18:19:12 -04:00

close method for point server

This commit is contained in:
v2ray
2016-01-04 00:33:25 +01:00
parent 201481a82c
commit 56c5302367
11 changed files with 80 additions and 49 deletions

View File

@@ -13,7 +13,8 @@ import (
)
type DokodemoDoor struct {
sync.Mutex
tcpMutex sync.RWMutex
udpMutex sync.RWMutex
config Config
accepting bool
address v2net.Address
@@ -35,16 +36,16 @@ func NewDokodemoDoor(space app.Space, config Config) *DokodemoDoor {
func (this *DokodemoDoor) Close() {
this.accepting = false
if this.tcpListener != nil {
this.Lock()
this.tcpListener.Close()
this.tcpMutex.Lock()
this.tcpListener = nil
this.Unlock()
this.tcpMutex.Unlock()
}
if this.udpConn != nil {
this.Lock()
this.udpConn.Close()
this.udpMutex.Lock()
this.udpConn = nil
this.Unlock()
this.udpMutex.Unlock()
}
}
@@ -84,13 +85,12 @@ func (this *DokodemoDoor) ListenUDP(port v2net.Port) error {
func (this *DokodemoDoor) handleUDPPackets() {
for this.accepting {
buffer := alloc.NewBuffer()
var udpConn *net.UDPConn
this.Lock()
if this.udpConn != nil {
udpConn = this.udpConn
this.udpMutex.RLock()
if !this.accepting {
return
}
this.Unlock()
nBytes, addr, err := udpConn.ReadFromUDP(buffer.Value)
nBytes, addr, err := this.udpConn.ReadFromUDP(buffer.Value)
this.udpMutex.RUnlock()
buffer.Slice(0, nBytes)
if err != nil {
buffer.Release()
@@ -103,7 +103,13 @@ func (this *DokodemoDoor) handleUDPPackets() {
close(ray.InboundInput())
for payload := range ray.InboundOutput() {
udpConn.WriteToUDP(payload.Value, addr)
this.udpMutex.RLock()
if !this.accepting {
this.udpMutex.RUnlock()
return
}
this.udpConn.WriteToUDP(payload.Value, addr)
this.udpMutex.RUnlock()
}
}
}
@@ -126,8 +132,11 @@ func (this *DokodemoDoor) ListenTCP(port v2net.Port) error {
func (this *DokodemoDoor) AcceptTCPConnections() {
for this.accepting {
retry.Timed(100, 100).On(func() error {
this.Lock()
defer this.Unlock()
if !this.accepting {
return nil
}
this.tcpMutex.RLock()
defer this.tcpMutex.RUnlock()
if this.tcpListener != nil {
connection, err := this.tcpListener.AcceptTCP()
if err != nil {

View File

@@ -35,8 +35,8 @@ func NewHttpProxyServer(space app.Space, config Config) *HttpProxyServer {
func (this *HttpProxyServer) Close() {
this.accepting = false
if this.tcpListener != nil {
this.Lock()
this.tcpListener.Close()
this.Lock()
this.tcpListener = nil
this.Unlock()
}

View File

@@ -23,7 +23,8 @@ var (
// SocksServer is a SOCKS 5 proxy server
type SocksServer struct {
sync.RWMutex
tcpMutex sync.RWMutex
udpMutex sync.RWMutex
accepting bool
space app.Space
config Config
@@ -42,20 +43,16 @@ func NewSocksServer(space app.Space, config Config) *SocksServer {
func (this *SocksServer) Close() {
this.accepting = false
if this.tcpListener != nil {
this.Lock()
if this.tcpListener != nil {
this.tcpListener.Close()
this.tcpListener = nil
}
this.Unlock()
this.tcpListener.Close()
this.tcpMutex.Lock()
this.tcpListener = nil
this.tcpMutex.Unlock()
}
if this.udpConn != nil {
this.Lock()
if this.udpConn != nil {
this.udpConn.Close()
this.udpConn = nil
}
this.Unlock()
this.udpConn.Close()
this.udpMutex.Lock()
this.udpConn = nil
this.udpMutex.Unlock()
}
}
@@ -81,12 +78,16 @@ func (this *SocksServer) Listen(port v2net.Port) error {
func (this *SocksServer) AcceptConnections() {
for this.accepting {
retry.Timed(100 /* times */, 100 /* ms */).On(func() error {
this.RLock()
defer this.RUnlock()
if !this.accepting {
return nil
}
this.tcpMutex.RLock()
if this.tcpListener == nil {
this.tcpMutex.RUnlock()
return nil
}
connection, err := this.tcpListener.AcceptTCP()
this.tcpMutex.RUnlock()
if err != nil {
log.Error("Socks failed to accept new connection %v", err)
return err

View File

@@ -30,13 +30,13 @@ func (this *SocksServer) ListenUDP(port v2net.Port) error {
func (this *SocksServer) AcceptPackets() error {
for this.accepting {
buffer := alloc.NewBuffer()
this.RLock()
this.udpMutex.RLock()
if !this.accepting {
this.RUnlock()
this.udpMutex.RUnlock()
return nil
}
nBytes, addr, err := this.udpConn.ReadFromUDP(buffer.Value)
this.RUnlock()
this.udpMutex.RUnlock()
if err != nil {
log.Error("Socks failed to read UDP packets: %v", err)
buffer.Release()
@@ -82,13 +82,13 @@ func (this *SocksServer) handlePacket(packet v2net.Packet, clientAddr *net.UDPAd
udpMessage := alloc.NewSmallBuffer().Clear()
response.Write(udpMessage)
this.RLock()
this.udpMutex.RLock()
if !this.accepting {
this.RUnlock()
this.udpMutex.RUnlock()
return
}
nBytes, err := this.udpConn.WriteToUDP(udpMessage.Value, clientAddr)
this.RUnlock()
this.udpMutex.RUnlock()
udpMessage.Release()
response.Data.Release()
if err != nil {

View File

@@ -38,11 +38,9 @@ func NewVMessInboundHandler(space app.Space, clients user.UserSet) *VMessInbound
func (this *VMessInboundHandler) Close() {
this.accepting = false
if this.listener != nil {
this.listener.Close()
this.Lock()
if this.listener != nil {
this.listener.Close()
this.listener = nil
}
this.listener = nil
this.Unlock()
}
}