mirror of
https://github.com/v2fly/v2ray-core.git
synced 2026-01-01 23:15:21 -05:00
massive refactoring for kcp
This commit is contained in:
49
transport/internet/tcp/dialer.go
Normal file
49
transport/internet/tcp/dialer.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package tcp
|
||||
|
||||
import (
|
||||
"net"
|
||||
|
||||
"github.com/v2ray/v2ray-core/common/log"
|
||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||
"github.com/v2ray/v2ray-core/transport/internet"
|
||||
)
|
||||
|
||||
var (
|
||||
globalCache = NewConnectionCache()
|
||||
)
|
||||
|
||||
func Dial(src v2net.Address, dest v2net.Destination) (internet.Connection, error) {
|
||||
log.Info("Dailing TCP to ", dest)
|
||||
if src == nil {
|
||||
src = v2net.AnyIP
|
||||
}
|
||||
id := src.String() + "-" + dest.NetAddr()
|
||||
var conn net.Conn
|
||||
if dest.IsTCP() && effectiveConfig.ConnectionReuse {
|
||||
conn = globalCache.Get(id)
|
||||
}
|
||||
if conn == nil {
|
||||
var err error
|
||||
conn, err = internet.DialToDest(src, dest)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return NewConnection(id, conn, globalCache), nil
|
||||
}
|
||||
|
||||
func DialRaw(src v2net.Address, dest v2net.Destination) (internet.Connection, error) {
|
||||
log.Info("Dailing Raw TCP to ", dest)
|
||||
conn, err := internet.DialToDest(src, dest)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &RawConnection{
|
||||
TCPConn: *conn.(*net.TCPConn),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
internet.TCPDialer = Dial
|
||||
internet.RawTCPDialer = DialRaw
|
||||
}
|
||||
Reference in New Issue
Block a user