106 lines
2.4 KiB
Go
Raw Normal View History

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