Browse Source

fixes for sync logic

v2ray 9 years ago
parent
commit
59a5f832f6
4 changed files with 24 additions and 33 deletions
  1. 8 9
      proxy/dokodemo/dokodemo.go
  2. 7 9
      proxy/http/http.go
  3. 2 6
      proxy/socks/socks.go
  4. 7 9
      proxy/vmess/inbound/inbound.go

+ 8 - 9
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
 		})
 	}

+ 7 - 9
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
 		})
 	}

+ 2 - 6
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 {
-			if !this.accepting {
-				return nil
-			}
 			this.tcpMutex.RLock()
-			if this.tcpListener == nil {
-				this.tcpMutex.RUnlock()
+			defer this.tcpMutex.RUnlock()
+			if !this.accepting {
 				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

+ 7 - 9
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
 		})