|
|
@@ -3,7 +3,6 @@ package hub
|
|
|
import (
|
|
|
"errors"
|
|
|
"net"
|
|
|
- "time"
|
|
|
|
|
|
"github.com/v2ray/v2ray-core/common/log"
|
|
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
|
|
@@ -13,84 +12,13 @@ var (
|
|
|
ErrorClosedConnection = errors.New("Connection already closed.")
|
|
|
)
|
|
|
|
|
|
-type TCPConn struct {
|
|
|
- conn *net.TCPConn
|
|
|
- listener *TCPHub
|
|
|
-}
|
|
|
-
|
|
|
-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 {
|
|
|
- if this == nil || this.conn == nil {
|
|
|
- return ErrorClosedConnection
|
|
|
- }
|
|
|
- err := this.conn.Close()
|
|
|
- return err
|
|
|
-}
|
|
|
-
|
|
|
-func (this *TCPConn) Release() {
|
|
|
- if this == nil || this.listener == nil {
|
|
|
- return
|
|
|
- }
|
|
|
-
|
|
|
- this.Close()
|
|
|
- this.conn = nil
|
|
|
- this.listener = nil
|
|
|
-}
|
|
|
-
|
|
|
-func (this *TCPConn) LocalAddr() net.Addr {
|
|
|
- return this.conn.LocalAddr()
|
|
|
-}
|
|
|
-
|
|
|
-func (this *TCPConn) RemoteAddr() net.Addr {
|
|
|
- return this.conn.RemoteAddr()
|
|
|
-}
|
|
|
-
|
|
|
-func (this *TCPConn) SetDeadline(t time.Time) error {
|
|
|
- return this.conn.SetDeadline(t)
|
|
|
-}
|
|
|
-
|
|
|
-func (this *TCPConn) SetReadDeadline(t time.Time) error {
|
|
|
- return this.conn.SetReadDeadline(t)
|
|
|
-}
|
|
|
-
|
|
|
-func (this *TCPConn) SetWriteDeadline(t time.Time) error {
|
|
|
- return this.conn.SetWriteDeadline(t)
|
|
|
-}
|
|
|
-
|
|
|
-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()
|
|
|
-}
|
|
|
-
|
|
|
type TCPHub struct {
|
|
|
listener *net.TCPListener
|
|
|
- connCallback func(*TCPConn)
|
|
|
+ connCallback ConnectionHandler
|
|
|
accepting bool
|
|
|
}
|
|
|
|
|
|
-func ListenTCP(port v2net.Port, callback func(*TCPConn)) (*TCPHub, error) {
|
|
|
+func ListenTCP(port v2net.Port, callback ConnectionHandler) (*TCPHub, error) {
|
|
|
listener, err := net.ListenTCP("tcp", &net.TCPAddr{
|
|
|
IP: []byte{0, 0, 0, 0},
|
|
|
Port: int(port),
|
|
|
@@ -123,7 +51,7 @@ func (this *TCPHub) start() {
|
|
|
}
|
|
|
continue
|
|
|
}
|
|
|
- go this.connCallback(&TCPConn{
|
|
|
+ go this.connCallback(&TCPConnection{
|
|
|
conn: conn,
|
|
|
listener: this,
|
|
|
})
|