From 6f7fff8173b60674e4d52d745c53adc4e5803d23 Mon Sep 17 00:00:00 2001 From: v2ray Date: Wed, 9 Mar 2016 11:34:27 +0100 Subject: [PATCH] release all references in tcp hub after it is closed --- transport/hub/tcp.go | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/transport/hub/tcp.go b/transport/hub/tcp.go index e6a495aa3..2f71a2348 100644 --- a/transport/hub/tcp.go +++ b/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() {