From ff0cb89efab0ecbc6f0d3f003db90edfd86fddb0 Mon Sep 17 00:00:00 2001 From: v2ray Date: Thu, 2 Jun 2016 20:52:52 +0200 Subject: [PATCH] simplify connection reuse settings --- shell/point/point.go | 3 +-- testing/scenarios/data/test_4_client.json | 5 +--- testing/scenarios/data/test_4_server.json | 5 +--- transport/config.go | 14 +++++----- transport/config_json.go | 31 ++++------------------- transport/hub/connection.go | 2 +- transport/hub/dialer.go | 2 +- transport/transport.go | 19 +++----------- 8 files changed, 19 insertions(+), 62 deletions(-) diff --git a/shell/point/point.go b/shell/point/point.go index eb1b4e79b..17659bbb4 100644 --- a/shell/point/point.go +++ b/shell/point/point.go @@ -16,7 +16,6 @@ import ( "github.com/v2ray/v2ray-core/common/retry" "github.com/v2ray/v2ray-core/proxy" proxyrepo "github.com/v2ray/v2ray-core/proxy/repo" - "github.com/v2ray/v2ray-core/transport" ) // Point shell of V2Ray. @@ -40,7 +39,7 @@ func NewPoint(pConfig *Config) (*Point, error) { vpoint.listen = pConfig.ListenOn if pConfig.TransportConfig != nil { - transport.ApplyConfig(pConfig.TransportConfig) + pConfig.TransportConfig.Apply() } if pConfig.LogConfig != nil { diff --git a/testing/scenarios/data/test_4_client.json b/testing/scenarios/data/test_4_client.json index b56452b89..f2c44b61e 100644 --- a/testing/scenarios/data/test_4_client.json +++ b/testing/scenarios/data/test_4_client.json @@ -31,9 +31,6 @@ } }, "transport": { - "streamType": "tcp", - "settings": { - "connectionReuse": true - } + "connectionReuse": true } } diff --git a/testing/scenarios/data/test_4_server.json b/testing/scenarios/data/test_4_server.json index b7b7f7376..a06d0b2ec 100644 --- a/testing/scenarios/data/test_4_server.json +++ b/testing/scenarios/data/test_4_server.json @@ -40,9 +40,6 @@ } ], "transport": { - "streamType": "tcp", - "settings": { - "connectionReuse": true - } + "connectionReuse": true } } diff --git a/transport/config.go b/transport/config.go index 4dd04a87f..7442cb3c7 100644 --- a/transport/config.go +++ b/transport/config.go @@ -2,15 +2,13 @@ package transport type StreamType int -const ( - StreamTypeTCP = StreamType(0) -) - -type TCPConfig struct { +type Config struct { ConnectionReuse bool } -type Config struct { - StreamType StreamType - TCPConfig *TCPConfig +func (this *Config) Apply() error { + if this.ConnectionReuse { + connectionReuse = true + } + return nil } diff --git a/transport/config_json.go b/transport/config_json.go index 6198efec7..fb724bfc3 100644 --- a/transport/config_json.go +++ b/transport/config_json.go @@ -2,37 +2,16 @@ package transport -import ( - "encoding/json" - "strings" -) +import "encoding/json" func (this *Config) UnmarshalJSON(data []byte) error { - type TypeConfig struct { - StreamType string `json:"streamType"` - Settings json.RawMessage `json:"settings"` - } - type JsonTCPConfig struct { + type JsonConfig struct { ConnectionReuse bool `json:"connectionReuse"` } - - typeConfig := new(TypeConfig) - if err := json.Unmarshal(data, typeConfig); err != nil { + jsonConfig := new(JsonConfig) + if err := json.Unmarshal(data, jsonConfig); err != nil { return err } - - this.StreamType = StreamTypeTCP - - streamType := strings.ToLower(typeConfig.StreamType) - if streamType == "tcp" { - jsonTCPConfig := new(JsonTCPConfig) - if err := json.Unmarshal(typeConfig.Settings, jsonTCPConfig); err != nil { - return err - } - this.TCPConfig = &TCPConfig{ - ConnectionReuse: jsonTCPConfig.ConnectionReuse, - } - } - + this.ConnectionReuse = jsonConfig.ConnectionReuse return nil } diff --git a/transport/hub/connection.go b/transport/hub/connection.go index f00c8c987..0c7c0019e 100644 --- a/transport/hub/connection.go +++ b/transport/hub/connection.go @@ -39,7 +39,7 @@ func (this *Connection) Close() error { if this == nil || this.conn == nil { return ErrorClosedConnection } - if transport.TCPStreamConfig.ConnectionReuse && this.Reusable() { + if transport.IsConnectionReusable() && this.Reusable() { this.listener.Recycle(this.dest, this.conn) return nil } diff --git a/transport/hub/dialer.go b/transport/hub/dialer.go index 10c4278a7..6561d99b8 100644 --- a/transport/hub/dialer.go +++ b/transport/hub/dialer.go @@ -19,7 +19,7 @@ var ( func Dial(dest v2net.Destination) (*Connection, error) { destStr := dest.String() var conn net.Conn - if transport.TCPStreamConfig.ConnectionReuse { + if transport.IsConnectionReusable() { conn = globalCache.Get(destStr) } if conn == nil { diff --git a/transport/transport.go b/transport/transport.go index 478ee4f9d..c6bd97bf2 100644 --- a/transport/transport.go +++ b/transport/transport.go @@ -1,22 +1,9 @@ package transport -import "github.com/v2ray/v2ray-core/common/log" - var ( - TCPStreamConfig = TCPConfig{ - ConnectionReuse: false, - } + connectionReuse = false ) -func ApplyConfig(config *Config) error { - if config.StreamType == StreamTypeTCP { - if config.TCPConfig != nil { - TCPStreamConfig.ConnectionReuse = config.TCPConfig.ConnectionReuse - if TCPStreamConfig.ConnectionReuse { - log.Info("Transport: TCP connection reuse enabled.") - } - } - } - - return nil +func IsConnectionReusable() bool { + return connectionReuse }