Skip to content

Commit

Permalink
cleanup internet connection
Browse files Browse the repository at this point in the history
  • Loading branch information
DarienRaymond committed Jan 4, 2017
1 parent e678000 commit e35e271
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 66 deletions.
Original file line number Diff line number Diff line change
@@ -1,30 +1,57 @@
package tcp
package internal

import (
"io"
"net"
"sync"
"time"

"v2ray.com/core/transport/internet/internal"
v2net "v2ray.com/core/common/net"
)

// ConnectionID is the ID of a connection.
type ConnectionID struct {
Local v2net.Address
Remote v2net.Address
RemotePort v2net.Port
}

// NewConnectionID creates a new ConnectionId.
func NewConnectionID(source v2net.Address, dest v2net.Destination) ConnectionID {
return ConnectionID{
Local: source,
Remote: dest.Address,
RemotePort: dest.Port,
}
}

type Reuser struct {
userEnabled bool
appEnable bool
}

func ReuseConnection(reuse bool) *Reuser {
return &Reuser{
userEnabled: reuse,
appEnable: reuse,
}
}

// Connection is an implementation of net.Conn with re-usability.
type Connection struct {
sync.RWMutex
id internal.ConnectionID
reusable bool
id ConnectionID
conn net.Conn
listener internal.ConnectionRecyler
config *Config
listener ConnectionRecyler
reuser *Reuser
}

func NewConnection(id internal.ConnectionID, conn net.Conn, manager internal.ConnectionRecyler, config *Config) *Connection {
func NewConnection(id ConnectionID, conn net.Conn, manager ConnectionRecyler, reuser *Reuser) *Connection {
return &Connection{
id: id,
conn: conn,
listener: manager,
reusable: config.IsConnectionReuse(),
config: config,
reuser: reuser,
}
}

Expand All @@ -45,6 +72,7 @@ func (v *Connection) Write(b []byte) (int, error) {
return conn.Write(b)
}

// Close implements net.Conn.Close(). If the connection is reusable, the underlying connection will be recycled.
func (v *Connection) Close() error {
if v == nil {
return io.ErrClosedPipe
Expand Down Expand Up @@ -108,22 +136,22 @@ func (v *Connection) SetReusable(reusable bool) {
if v == nil {
return
}
v.reusable = reusable
v.reuser.appEnable = reusable
}

func (v *Connection) Reusable() bool {
if v == nil {
return false
}
return v.config.IsConnectionReuse() && v.reusable
return v.reuser.userEnabled && v.reuser.appEnable
}

func (v *Connection) SysFd() (int, error) {
conn := v.underlyingConn()
if conn == nil {
return 0, io.ErrClosedPipe
}
return internal.GetSysFd(conn)
return GetSysFd(conn)
}

func (v *Connection) underlyingConn() net.Conn {
Expand Down
17 changes: 0 additions & 17 deletions transport/internet/internal/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"sync"
"time"

v2net "v2ray.com/core/common/net"
"v2ray.com/core/common/signal"
)

Expand All @@ -15,22 +14,6 @@ type ConnectionRecyler interface {
Put(ConnectionID, net.Conn)
}

// ConnectionID is the ID of a connection.
type ConnectionID struct {
Local v2net.Address
Remote v2net.Address
RemotePort v2net.Port
}

// NewConnectionID creates a new ConnectionId.
func NewConnectionID(source v2net.Address, dest v2net.Destination) ConnectionID {
return ConnectionID{
Local: source,
Remote: dest.Address,
RemotePort: dest.Port,
}
}

// ExpiringConnection is a connection that will expire in certain time.
type ExpiringConnection struct {
conn net.Conn
Expand Down
2 changes: 1 addition & 1 deletion transport/internet/kcp/dialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func DialKCP(src v2net.Address, dest v2net.Destination, options internet.DialerO
config.ServerName = dest.Address.Domain()
}
tlsConn := tls.Client(iConn, config)
iConn = v2tls.NewConnection(tlsConn)
iConn = UnreusableConnection{Conn: tlsConn}
}
}

Expand Down
2 changes: 1 addition & 1 deletion transport/internet/kcp/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ func (v *Listener) Accept() (internet.Connection, error) {
}
if v.tlsConfig != nil {
tlsConn := tls.Server(conn, v.tlsConfig)
return v2tls.NewConnection(tlsConn), nil
return UnreusableConnection{Conn: tlsConn}, nil
}
return conn, nil
case <-time.After(time.Second):
Expand Down
13 changes: 13 additions & 0 deletions transport/internet/kcp/tls.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package kcp

import "net"

type UnreusableConnection struct {
net.Conn
}

func (c UnreusableConnection) Reusable() bool {
return false
}

func (c UnreusableConnection) SetReusable(bool) {}
2 changes: 1 addition & 1 deletion transport/internet/tcp/dialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func Dial(src v2net.Address, dest v2net.Destination, options internet.DialerOpti
conn = auth.Client(conn)
}
}
return NewConnection(id, conn, globalCache, tcpSettings), nil
return internal.NewConnection(id, conn, globalCache, internal.ReuseConnection(tcpSettings.IsConnectionReuse())), nil
}

func init() {
Expand Down
2 changes: 1 addition & 1 deletion transport/internet/tcp/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (v *TCPListener) Accept() (internet.Connection, error) {
return nil, connErr.err
}
conn := connErr.conn
return NewConnection(internal.ConnectionID{}, conn, v, v.config), nil
return internal.NewConnection(internal.ConnectionID{}, conn, v, internal.ReuseConnection(v.config.IsConnectionReuse())), nil
case <-time.After(time.Second * 2):
}
}
Expand Down
21 changes: 0 additions & 21 deletions transport/internet/tls/connection.go

This file was deleted.

16 changes: 4 additions & 12 deletions transport/internet/websocket/wsconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,18 +121,10 @@ func (ws *wsconn) RemoteAddr() net.Addr {
return ws.wsc.RemoteAddr()
}
func (ws *wsconn) SetDeadline(t time.Time) error {
return func() error {
errr := ws.SetReadDeadline(t)
errw := ws.SetWriteDeadline(t)
if errr == nil || errw == nil {
return nil
}
if errr != nil {
return errr
}

return errw
}()
if err := ws.SetReadDeadline(t); err != nil {
return err
}
return ws.SetWriteDeadline(t)
}
func (ws *wsconn) SetReadDeadline(t time.Time) error {
return ws.wsc.SetReadDeadline(t)
Expand Down

0 comments on commit e35e271

Please sign in to comment.