mirror of https://github.com/v2ray/v2ray-core
simplify tcp connection
parent
406360ed3e
commit
f594f5b606
|
@ -118,7 +118,7 @@ func (this *DokodemoDoor) ListenTCP(port v2net.Port) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *DokodemoDoor) HandleTCPConnection(conn hub.Connection) {
|
func (this *DokodemoDoor) HandleTCPConnection(conn *hub.Connection) {
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
|
|
||||||
ray := this.packetDispatcher.DispatchToOutbound(v2net.TCPDestination(this.address, this.port))
|
ray := this.packetDispatcher.DispatchToOutbound(v2net.TCPDestination(this.address, this.port))
|
||||||
|
|
|
@ -95,7 +95,7 @@ func parseHost(rawHost string, defaultPort v2net.Port) (v2net.Destination, error
|
||||||
return v2net.TCPDestination(v2net.DomainAddress(host), port), nil
|
return v2net.TCPDestination(v2net.DomainAddress(host), port), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *HttpProxyServer) handleConnection(conn hub.Connection) {
|
func (this *HttpProxyServer) handleConnection(conn *hub.Connection) {
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
reader := bufio.NewReader(conn)
|
reader := bufio.NewReader(conn)
|
||||||
|
|
||||||
|
|
|
@ -157,7 +157,7 @@ func (this *Shadowsocks) handlerUDPPayload(payload *alloc.Buffer, source v2net.D
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *Shadowsocks) handleConnection(conn hub.Connection) {
|
func (this *Shadowsocks) handleConnection(conn *hub.Connection) {
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
|
|
||||||
buffer := alloc.NewSmallBuffer()
|
buffer := alloc.NewSmallBuffer()
|
||||||
|
|
|
@ -92,7 +92,7 @@ func (this *Server) Listen(port v2net.Port) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *Server) handleConnection(connection hub.Connection) {
|
func (this *Server) handleConnection(connection *hub.Connection) {
|
||||||
defer connection.Close()
|
defer connection.Close()
|
||||||
|
|
||||||
timedReader := v2net.NewTimeOutReader(120, connection)
|
timedReader := v2net.NewTimeOutReader(120, connection)
|
||||||
|
|
|
@ -115,7 +115,7 @@ func (this *VMessInboundHandler) Listen(port v2net.Port) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *VMessInboundHandler) HandleConnection(connection hub.Connection) {
|
func (this *VMessInboundHandler) HandleConnection(connection *hub.Connection) {
|
||||||
defer connection.Close()
|
defer connection.Close()
|
||||||
|
|
||||||
connReader := v2net.NewTimeOutReader(16, connection)
|
connReader := v2net.NewTimeOutReader(16, connection)
|
||||||
|
|
|
@ -1,36 +1,18 @@
|
||||||
package hub
|
package hub
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/tls"
|
|
||||||
"net"
|
"net"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/v2ray/v2ray-core/common"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type ConnectionHandler func(Connection)
|
type ConnectionHandler func(*Connection)
|
||||||
|
|
||||||
type Connection interface {
|
type Connection struct {
|
||||||
common.Releasable
|
conn net.Conn
|
||||||
|
|
||||||
Read([]byte) (int, error)
|
|
||||||
Write([]byte) (int, error)
|
|
||||||
Close() error
|
|
||||||
LocalAddr() net.Addr
|
|
||||||
RemoteAddr() net.Addr
|
|
||||||
SetDeadline(t time.Time) error
|
|
||||||
SetReadDeadline(t time.Time) error
|
|
||||||
SetWriteDeadline(t time.Time) error
|
|
||||||
CloseRead() error
|
|
||||||
CloseWrite() error
|
|
||||||
}
|
|
||||||
|
|
||||||
type TCPConnection struct {
|
|
||||||
conn *net.TCPConn
|
|
||||||
listener *TCPHub
|
listener *TCPHub
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *TCPConnection) Read(b []byte) (int, error) {
|
func (this *Connection) Read(b []byte) (int, error) {
|
||||||
if this == nil || this.conn == nil {
|
if this == nil || this.conn == nil {
|
||||||
return 0, ErrorClosedConnection
|
return 0, ErrorClosedConnection
|
||||||
}
|
}
|
||||||
|
@ -38,14 +20,14 @@ func (this *TCPConnection) Read(b []byte) (int, error) {
|
||||||
return this.conn.Read(b)
|
return this.conn.Read(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *TCPConnection) Write(b []byte) (int, error) {
|
func (this *Connection) Write(b []byte) (int, error) {
|
||||||
if this == nil || this.conn == nil {
|
if this == nil || this.conn == nil {
|
||||||
return 0, ErrorClosedConnection
|
return 0, ErrorClosedConnection
|
||||||
}
|
}
|
||||||
return this.conn.Write(b)
|
return this.conn.Write(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *TCPConnection) Close() error {
|
func (this *Connection) Close() error {
|
||||||
if this == nil || this.conn == nil {
|
if this == nil || this.conn == nil {
|
||||||
return ErrorClosedConnection
|
return ErrorClosedConnection
|
||||||
}
|
}
|
||||||
|
@ -53,7 +35,7 @@ func (this *TCPConnection) Close() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *TCPConnection) Release() {
|
func (this *Connection) Release() {
|
||||||
if this == nil || this.listener == nil {
|
if this == nil || this.listener == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -63,101 +45,22 @@ func (this *TCPConnection) Release() {
|
||||||
this.listener = nil
|
this.listener = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *TCPConnection) LocalAddr() net.Addr {
|
func (this *Connection) LocalAddr() net.Addr {
|
||||||
return this.conn.LocalAddr()
|
return this.conn.LocalAddr()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *TCPConnection) RemoteAddr() net.Addr {
|
func (this *Connection) RemoteAddr() net.Addr {
|
||||||
return this.conn.RemoteAddr()
|
return this.conn.RemoteAddr()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *TCPConnection) SetDeadline(t time.Time) error {
|
func (this *Connection) SetDeadline(t time.Time) error {
|
||||||
return this.conn.SetDeadline(t)
|
return this.conn.SetDeadline(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *TCPConnection) SetReadDeadline(t time.Time) error {
|
func (this *Connection) SetReadDeadline(t time.Time) error {
|
||||||
return this.conn.SetReadDeadline(t)
|
return this.conn.SetReadDeadline(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *TCPConnection) SetWriteDeadline(t time.Time) error {
|
func (this *Connection) SetWriteDeadline(t time.Time) error {
|
||||||
return this.conn.SetWriteDeadline(t)
|
return this.conn.SetWriteDeadline(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *TCPConnection) CloseRead() error {
|
|
||||||
if this == nil || this.conn == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return this.conn.CloseRead()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *TCPConnection) CloseWrite() error {
|
|
||||||
if this == nil || this.conn == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return this.conn.CloseWrite()
|
|
||||||
}
|
|
||||||
|
|
||||||
type TLSConnection struct {
|
|
||||||
conn *tls.Conn
|
|
||||||
listener *TCPHub
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *TLSConnection) Read(b []byte) (int, error) {
|
|
||||||
if this == nil || this.conn == nil {
|
|
||||||
return 0, ErrorClosedConnection
|
|
||||||
}
|
|
||||||
return this.conn.Read(b)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *TLSConnection) Write(b []byte) (int, error) {
|
|
||||||
if this == nil || this.conn == nil {
|
|
||||||
return this.conn.Write(b)
|
|
||||||
}
|
|
||||||
return this.conn.Write(b)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *TLSConnection) Close() error {
|
|
||||||
if this == nil || this.conn == nil {
|
|
||||||
return ErrorClosedConnection
|
|
||||||
}
|
|
||||||
err := this.conn.Close()
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *TLSConnection) Release() {
|
|
||||||
if this == nil || this.listener == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
this.Close()
|
|
||||||
this.conn = nil
|
|
||||||
this.listener = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *TLSConnection) LocalAddr() net.Addr {
|
|
||||||
return this.conn.LocalAddr()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *TLSConnection) RemoteAddr() net.Addr {
|
|
||||||
return this.conn.RemoteAddr()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *TLSConnection) SetDeadline(t time.Time) error {
|
|
||||||
return this.conn.SetDeadline(t)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *TLSConnection) SetReadDeadline(t time.Time) error {
|
|
||||||
return this.conn.SetReadDeadline(t)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *TLSConnection) SetWriteDeadline(t time.Time) error {
|
|
||||||
return this.conn.SetWriteDeadline(t)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *TLSConnection) CloseRead() error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *TLSConnection) CloseWrite() error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
|
@ -51,7 +51,7 @@ func (this *TCPHub) start() {
|
||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
go this.connCallback(&TCPConnection{
|
go this.connCallback(&Connection{
|
||||||
conn: conn,
|
conn: conn,
|
||||||
listener: this,
|
listener: this,
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue