1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-21 09:36:34 -05:00

simplify connection reuse settings

This commit is contained in:
v2ray 2016-06-02 20:52:52 +02:00
parent 9a8bc3fc3c
commit ff0cb89efa
8 changed files with 19 additions and 62 deletions

View File

@ -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 {

View File

@ -31,9 +31,6 @@
}
},
"transport": {
"streamType": "tcp",
"settings": {
"connectionReuse": true
}
"connectionReuse": true
}
}

View File

@ -40,9 +40,6 @@
}
],
"transport": {
"streamType": "tcp",
"settings": {
"connectionReuse": true
}
"connectionReuse": true
}
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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 {

View File

@ -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
}