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