2016-06-14 20:54:08 +00:00
|
|
|
package kcp
|
2016-06-11 09:30:38 +00:00
|
|
|
|
2016-08-06 19:59:22 +00:00
|
|
|
import (
|
2016-12-08 15:27:41 +00:00
|
|
|
"crypto/cipher"
|
|
|
|
|
2017-01-12 11:54:34 +00:00
|
|
|
"v2ray.com/core/common"
|
2016-08-20 18:55:45 +00:00
|
|
|
"v2ray.com/core/transport/internet"
|
2016-08-06 19:59:22 +00:00
|
|
|
)
|
|
|
|
|
2016-12-23 11:42:25 +00:00
|
|
|
// GetMTUValue returns the value of MTU settings.
|
|
|
|
func (v *Config) GetMTUValue() uint32 {
|
|
|
|
if v == nil || v.Mtu == nil {
|
2016-09-21 19:08:05 +00:00
|
|
|
return 1350
|
|
|
|
}
|
2016-12-23 11:42:25 +00:00
|
|
|
return v.Mtu.Value
|
2016-09-21 19:08:05 +00:00
|
|
|
}
|
|
|
|
|
2016-12-23 11:42:25 +00:00
|
|
|
// GetTTIValue returns the value of TTI settings.
|
|
|
|
func (v *Config) GetTTIValue() uint32 {
|
|
|
|
if v == nil || v.Tti == nil {
|
2016-09-21 19:08:05 +00:00
|
|
|
return 50
|
|
|
|
}
|
2016-12-23 11:42:25 +00:00
|
|
|
return v.Tti.Value
|
2016-09-21 19:08:05 +00:00
|
|
|
}
|
|
|
|
|
2016-12-23 11:42:25 +00:00
|
|
|
// GetUplinkCapacityValue returns the value of UplinkCapacity settings.
|
|
|
|
func (v *Config) GetUplinkCapacityValue() uint32 {
|
|
|
|
if v == nil || v.UplinkCapacity == nil {
|
2016-09-21 19:08:05 +00:00
|
|
|
return 5
|
|
|
|
}
|
2016-12-23 11:42:25 +00:00
|
|
|
return v.UplinkCapacity.Value
|
2016-09-21 19:08:05 +00:00
|
|
|
}
|
|
|
|
|
2016-12-23 11:42:25 +00:00
|
|
|
// GetDownlinkCapacityValue returns the value of DownlinkCapacity settings.
|
|
|
|
func (v *Config) GetDownlinkCapacityValue() uint32 {
|
|
|
|
if v == nil || v.DownlinkCapacity == nil {
|
2016-09-21 19:08:05 +00:00
|
|
|
return 20
|
|
|
|
}
|
2016-12-23 11:42:25 +00:00
|
|
|
return v.DownlinkCapacity.Value
|
2016-09-21 19:08:05 +00:00
|
|
|
}
|
|
|
|
|
2016-12-23 11:42:25 +00:00
|
|
|
// GetWriteBufferSize returns the size of WriterBuffer in bytes.
|
|
|
|
func (v *Config) GetWriteBufferSize() uint32 {
|
|
|
|
if v == nil || v.WriteBuffer == nil {
|
2016-11-30 20:36:40 +00:00
|
|
|
return 2 * 1024 * 1024
|
2016-09-21 19:08:05 +00:00
|
|
|
}
|
2016-12-23 11:42:25 +00:00
|
|
|
return v.WriteBuffer.Size
|
2016-09-21 19:08:05 +00:00
|
|
|
}
|
|
|
|
|
2016-12-23 11:42:25 +00:00
|
|
|
// GetReadBufferSize returns the size of ReadBuffer in bytes.
|
|
|
|
func (v *Config) GetReadBufferSize() uint32 {
|
|
|
|
if v == nil || v.ReadBuffer == nil {
|
2016-11-30 20:36:40 +00:00
|
|
|
return 2 * 1024 * 1024
|
2016-09-21 19:08:05 +00:00
|
|
|
}
|
2016-12-23 11:42:25 +00:00
|
|
|
return v.ReadBuffer.Size
|
2016-09-21 19:08:05 +00:00
|
|
|
}
|
|
|
|
|
2016-12-08 15:37:04 +00:00
|
|
|
// GetSecurity returns the security settings.
|
2016-12-08 15:27:41 +00:00
|
|
|
func (v *Config) GetSecurity() (cipher.AEAD, error) {
|
|
|
|
return NewSimpleAuthenticator(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Config) GetPackerHeader() (internet.PacketHeader, error) {
|
2016-11-27 20:39:09 +00:00
|
|
|
if v.HeaderConfig != nil {
|
|
|
|
rawConfig, err := v.HeaderConfig.GetInstance()
|
2016-10-16 12:22:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-01-12 21:47:10 +00:00
|
|
|
return internet.CreatePacketHeader(rawConfig)
|
2016-08-06 19:59:22 +00:00
|
|
|
}
|
2016-12-08 15:27:41 +00:00
|
|
|
return nil, nil
|
2016-08-06 19:59:22 +00:00
|
|
|
}
|
|
|
|
|
2016-11-27 20:39:09 +00:00
|
|
|
func (v *Config) GetSendingInFlightSize() uint32 {
|
2016-12-23 11:42:25 +00:00
|
|
|
size := v.GetUplinkCapacityValue() * 1024 * 1024 / v.GetMTUValue() / (1000 / v.GetTTIValue())
|
2016-08-24 05:50:33 +00:00
|
|
|
if size < 8 {
|
2016-06-29 14:46:26 +00:00
|
|
|
size = 8
|
|
|
|
}
|
|
|
|
return size
|
2016-06-20 14:10:47 +00:00
|
|
|
}
|
|
|
|
|
2016-11-27 20:39:09 +00:00
|
|
|
func (v *Config) GetSendingBufferSize() uint32 {
|
2016-12-23 11:42:25 +00:00
|
|
|
return v.GetWriteBufferSize() / v.GetMTUValue()
|
2016-07-02 09:19:32 +00:00
|
|
|
}
|
|
|
|
|
2016-11-27 20:39:09 +00:00
|
|
|
func (v *Config) GetReceivingInFlightSize() uint32 {
|
2016-12-23 11:42:25 +00:00
|
|
|
size := v.GetDownlinkCapacityValue() * 1024 * 1024 / v.GetMTUValue() / (1000 / v.GetTTIValue())
|
2016-08-24 05:50:33 +00:00
|
|
|
if size < 8 {
|
2016-06-29 14:46:26 +00:00
|
|
|
size = 8
|
|
|
|
}
|
|
|
|
return size
|
2016-06-20 14:10:47 +00:00
|
|
|
}
|
|
|
|
|
2016-11-27 20:39:09 +00:00
|
|
|
func (v *Config) GetReceivingBufferSize() uint32 {
|
2016-12-23 11:42:25 +00:00
|
|
|
return v.GetReadBufferSize() / v.GetMTUValue()
|
2016-06-17 14:57:48 +00:00
|
|
|
}
|
|
|
|
|
2016-12-23 11:42:25 +00:00
|
|
|
func (v *Config) IsConnectionReuse() bool {
|
|
|
|
if v == nil || v.ConnectionReuse == nil {
|
2016-11-27 07:58:31 +00:00
|
|
|
return true
|
|
|
|
}
|
2016-12-23 11:42:25 +00:00
|
|
|
return v.ConnectionReuse.Enable
|
2016-11-27 07:58:31 +00:00
|
|
|
}
|
|
|
|
|
2016-10-02 21:43:58 +00:00
|
|
|
func init() {
|
2017-01-12 11:54:34 +00:00
|
|
|
common.Must(internet.RegisterProtocolConfigCreator(internet.TransportProtocol_MKCP, func() interface{} {
|
2016-10-02 21:43:58 +00:00
|
|
|
return new(Config)
|
2017-01-12 11:54:34 +00:00
|
|
|
}))
|
2016-10-02 21:43:58 +00:00
|
|
|
}
|