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