Prechádzať zdrojové kódy

release all references in tcp hub after it is closed

v2ray 9 rokov pred
rodič
commit
6f7fff8173
1 zmenil súbory, kde vykonal 29 pridanie a 1 odobranie
  1. 29 1
      transport/hub/tcp.go

+ 29 - 1
transport/hub/tcp.go

@@ -1,6 +1,7 @@
 package hub
 
 import (
+	"errors"
 	"net"
 	"time"
 
@@ -8,6 +9,10 @@ import (
 	v2net "github.com/v2ray/v2ray-core/common/net"
 )
 
+var (
+	ErrorClosedConnection = errors.New("Connection already closed.")
+)
+
 type TCPConn struct {
 	conn     *net.TCPConn
 	listener *TCPHub
@@ -15,18 +20,34 @@ type TCPConn struct {
 }
 
 func (this *TCPConn) Read(b []byte) (int, error) {
+	if this == nil || this.conn == nil {
+		return 0, ErrorClosedConnection
+	}
 	return this.conn.Read(b)
 }
 
 func (this *TCPConn) Write(b []byte) (int, error) {
+	if this == nil || this.conn == nil {
+		return 0, ErrorClosedConnection
+	}
 	return this.conn.Write(b)
 }
 
 func (this *TCPConn) Close() error {
-	return this.conn.Close()
+	if this == nil || this.conn == nil {
+		return ErrorClosedConnection
+	}
+	err := this.conn.Close()
+	this.conn = nil
+	this.listener = nil
+	return err
 }
 
 func (this *TCPConn) Release() {
+	if this == nil || this.listener == nil {
+		return
+	}
+
 	if this.dirty {
 		this.Close()
 		return
@@ -55,10 +76,16 @@ func (this *TCPConn) SetWriteDeadline(t time.Time) error {
 }
 
 func (this *TCPConn) CloseRead() error {
+	if this == nil || this.conn == nil {
+		return nil
+	}
 	return this.conn.CloseRead()
 }
 
 func (this *TCPConn) CloseWrite() error {
+	if this == nil || this.conn == nil {
+		return nil
+	}
 	return this.conn.CloseWrite()
 }
 
@@ -88,6 +115,7 @@ func ListenTCP(port v2net.Port, callback func(*TCPConn)) (*TCPHub, error) {
 func (this *TCPHub) Close() {
 	this.accepting = false
 	this.listener.Close()
+	this.listener = nil
 }
 
 func (this *TCPHub) start() {