1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-09-28 23:06:14 -04:00

release all references in tcp hub after it is closed

This commit is contained in:
v2ray 2016-03-09 11:34:27 +01:00
parent 50ef8e0465
commit 6f7fff8173

View File

@ -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() {