From 59a5f832f6a312c300acc34e55a0ccc929dc4666 Mon Sep 17 00:00:00 2001 From: v2ray Date: Mon, 4 Jan 2016 01:19:27 +0100 Subject: [PATCH] fixes for sync logic --- proxy/dokodemo/dokodemo.go | 17 ++++++++--------- proxy/http/http.go | 16 +++++++--------- proxy/socks/socks.go | 8 ++------ proxy/vmess/inbound/inbound.go | 16 +++++++--------- 4 files changed, 24 insertions(+), 33 deletions(-) diff --git a/proxy/dokodemo/dokodemo.go b/proxy/dokodemo/dokodemo.go index 908c3bafa..8276dc4f9 100644 --- a/proxy/dokodemo/dokodemo.go +++ b/proxy/dokodemo/dokodemo.go @@ -87,6 +87,7 @@ func (this *DokodemoDoor) handleUDPPackets() { buffer := alloc.NewBuffer() this.udpMutex.RLock() if !this.accepting { + this.udpMutex.RUnlock() return } nBytes, addr, err := this.udpConn.ReadFromUDP(buffer.Value) @@ -132,19 +133,17 @@ func (this *DokodemoDoor) ListenTCP(port v2net.Port) error { func (this *DokodemoDoor) AcceptTCPConnections() { for this.accepting { retry.Timed(100, 100).On(func() error { + this.tcpMutex.RLock() + defer this.tcpMutex.RUnlock() if !this.accepting { return nil } - this.tcpMutex.RLock() - defer this.tcpMutex.RUnlock() - if this.tcpListener != nil { - connection, err := this.tcpListener.AcceptTCP() - if err != nil { - log.Error("Dokodemo failed to accept new connections: %v", err) - return err - } - go this.HandleTCPConnection(connection) + connection, err := this.tcpListener.AcceptTCP() + if err != nil { + log.Error("Dokodemo failed to accept new connections: %v", err) + return err } + go this.HandleTCPConnection(connection) return nil }) } diff --git a/proxy/http/http.go b/proxy/http/http.go index 119001746..3c3fa35de 100644 --- a/proxy/http/http.go +++ b/proxy/http/http.go @@ -59,19 +59,17 @@ func (this *HttpProxyServer) Listen(port v2net.Port) error { func (this *HttpProxyServer) accept() { for this.accepting { retry.Timed(100 /* times */, 100 /* ms */).On(func() error { + this.Lock() + defer this.Unlock() if !this.accepting { return nil } - this.Lock() - defer this.Unlock() - if this.tcpListener != nil { - tcpConn, err := this.tcpListener.AcceptTCP() - if err != nil { - log.Error("Failed to accept HTTP connection: %v", err) - return err - } - go this.handleConnection(tcpConn) + tcpConn, err := this.tcpListener.AcceptTCP() + if err != nil { + log.Error("Failed to accept HTTP connection: %v", err) + return err } + go this.handleConnection(tcpConn) return nil }) } diff --git a/proxy/socks/socks.go b/proxy/socks/socks.go index c304eab2e..83c3167fe 100644 --- a/proxy/socks/socks.go +++ b/proxy/socks/socks.go @@ -78,16 +78,12 @@ 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.tcpMutex.RLock() + defer this.tcpMutex.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 diff --git a/proxy/vmess/inbound/inbound.go b/proxy/vmess/inbound/inbound.go index 4affb9619..40ec829fe 100644 --- a/proxy/vmess/inbound/inbound.go +++ b/proxy/vmess/inbound/inbound.go @@ -64,19 +64,17 @@ func (this *VMessInboundHandler) Listen(port v2net.Port) error { func (this *VMessInboundHandler) AcceptConnections() error { for this.accepting { retry.Timed(100 /* times */, 100 /* ms */).On(func() error { + this.Lock() + defer this.Unlock() if !this.accepting { return nil } - this.Lock() - defer this.Unlock() - if this.listener != nil { - connection, err := this.listener.AcceptTCP() - if err != nil { - log.Error("Failed to accpet connection: %s", err.Error()) - return err - } - go this.HandleConnection(connection) + connection, err := this.listener.AcceptTCP() + if err != nil { + log.Error("Failed to accpet connection: %s", err.Error()) + return err } + go this.HandleConnection(connection) return nil })