|
@@ -4,6 +4,7 @@ import (
|
|
|
"crypto/tls"
|
|
"crypto/tls"
|
|
|
"errors"
|
|
"errors"
|
|
|
"net"
|
|
"net"
|
|
|
|
|
+ "sync"
|
|
|
|
|
|
|
|
"github.com/v2ray/v2ray-core/common/log"
|
|
"github.com/v2ray/v2ray-core/common/log"
|
|
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
|
@@ -14,6 +15,7 @@ var (
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
type TCPHub struct {
|
|
type TCPHub struct {
|
|
|
|
|
+ sync.Mutex
|
|
|
listener net.Listener
|
|
listener net.Listener
|
|
|
connCallback ConnectionHandler
|
|
connCallback ConnectionHandler
|
|
|
accepting bool
|
|
accepting bool
|
|
@@ -47,6 +49,9 @@ func ListenTCP(address v2net.Address, port v2net.Port, callback ConnectionHandle
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func (this *TCPHub) Close() {
|
|
func (this *TCPHub) Close() {
|
|
|
|
|
+ this.Lock()
|
|
|
|
|
+ defer this.Unlock()
|
|
|
|
|
+
|
|
|
this.accepting = false
|
|
this.accepting = false
|
|
|
this.listener.Close()
|
|
this.listener.Close()
|
|
|
this.listener = nil
|
|
this.listener = nil
|
|
@@ -55,7 +60,14 @@ func (this *TCPHub) Close() {
|
|
|
func (this *TCPHub) start() {
|
|
func (this *TCPHub) start() {
|
|
|
this.accepting = true
|
|
this.accepting = true
|
|
|
for this.accepting {
|
|
for this.accepting {
|
|
|
|
|
+ this.Lock()
|
|
|
|
|
+ if !this.accepting {
|
|
|
|
|
+ this.Unlock()
|
|
|
|
|
+ break
|
|
|
|
|
+ }
|
|
|
conn, err := this.listener.Accept()
|
|
conn, err := this.listener.Accept()
|
|
|
|
|
+ this.Unlock()
|
|
|
|
|
+
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
if this.accepting {
|
|
if this.accepting {
|
|
|
log.Warning("Listener: Failed to accept new TCP connection: ", err)
|
|
log.Warning("Listener: Failed to accept new TCP connection: ", err)
|
|
@@ -69,6 +81,12 @@ func (this *TCPHub) start() {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func (this *TCPHub) recycle(conn *net.TCPConn) {
|
|
|
|
|
-
|
|
|
|
|
|
|
+// @Private
|
|
|
|
|
+func (this *TCPHub) Recycle(conn net.Conn) {
|
|
|
|
|
+ if this.accepting {
|
|
|
|
|
+ go this.connCallback(&Connection{
|
|
|
|
|
+ conn: conn,
|
|
|
|
|
+ listener: this,
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|