github.com/Uhtred009/v2ray-core-1@v4.31.2+incompatible/transport/internet/kcp/config.go (about)

     1  // +build !confonly
     2  
     3  package kcp
     4  
     5  import (
     6  	"crypto/cipher"
     7  	"v2ray.com/core/common"
     8  	"v2ray.com/core/transport/internet"
     9  )
    10  
    11  const protocolName = "mkcp"
    12  
    13  // GetMTUValue returns the value of MTU settings.
    14  func (c *Config) GetMTUValue() uint32 {
    15  	if c == nil || c.Mtu == nil {
    16  		return 1350
    17  	}
    18  	return c.Mtu.Value
    19  }
    20  
    21  // GetTTIValue returns the value of TTI settings.
    22  func (c *Config) GetTTIValue() uint32 {
    23  	if c == nil || c.Tti == nil {
    24  		return 50
    25  	}
    26  	return c.Tti.Value
    27  }
    28  
    29  // GetUplinkCapacityValue returns the value of UplinkCapacity settings.
    30  func (c *Config) GetUplinkCapacityValue() uint32 {
    31  	if c == nil || c.UplinkCapacity == nil {
    32  		return 5
    33  	}
    34  	return c.UplinkCapacity.Value
    35  }
    36  
    37  // GetDownlinkCapacityValue returns the value of DownlinkCapacity settings.
    38  func (c *Config) GetDownlinkCapacityValue() uint32 {
    39  	if c == nil || c.DownlinkCapacity == nil {
    40  		return 20
    41  	}
    42  	return c.DownlinkCapacity.Value
    43  }
    44  
    45  // GetWriteBufferSize returns the size of WriterBuffer in bytes.
    46  func (c *Config) GetWriteBufferSize() uint32 {
    47  	if c == nil || c.WriteBuffer == nil {
    48  		return 2 * 1024 * 1024
    49  	}
    50  	return c.WriteBuffer.Size
    51  }
    52  
    53  // GetReadBufferSize returns the size of ReadBuffer in bytes.
    54  func (c *Config) GetReadBufferSize() uint32 {
    55  	if c == nil || c.ReadBuffer == nil {
    56  		return 2 * 1024 * 1024
    57  	}
    58  	return c.ReadBuffer.Size
    59  }
    60  
    61  // GetSecurity returns the security settings.
    62  func (c *Config) GetSecurity() (cipher.AEAD, error) {
    63  	if c.Seed != nil {
    64  		return NewAEADAESGCMBasedOnSeed(c.Seed.Seed), nil
    65  	}
    66  	return NewSimpleAuthenticator(), nil
    67  }
    68  
    69  func (c *Config) GetPackerHeader() (internet.PacketHeader, error) {
    70  	if c.HeaderConfig != nil {
    71  		rawConfig, err := c.HeaderConfig.GetInstance()
    72  		if err != nil {
    73  			return nil, err
    74  		}
    75  
    76  		return internet.CreatePacketHeader(rawConfig)
    77  	}
    78  	return nil, nil
    79  }
    80  
    81  func (c *Config) GetSendingInFlightSize() uint32 {
    82  	size := c.GetUplinkCapacityValue() * 1024 * 1024 / c.GetMTUValue() / (1000 / c.GetTTIValue())
    83  	if size < 8 {
    84  		size = 8
    85  	}
    86  	return size
    87  }
    88  
    89  func (c *Config) GetSendingBufferSize() uint32 {
    90  	return c.GetWriteBufferSize() / c.GetMTUValue()
    91  }
    92  
    93  func (c *Config) GetReceivingInFlightSize() uint32 {
    94  	size := c.GetDownlinkCapacityValue() * 1024 * 1024 / c.GetMTUValue() / (1000 / c.GetTTIValue())
    95  	if size < 8 {
    96  		size = 8
    97  	}
    98  	return size
    99  }
   100  
   101  func (c *Config) GetReceivingBufferSize() uint32 {
   102  	return c.GetReadBufferSize() / c.GetMTUValue()
   103  }
   104  
   105  func init() {
   106  	common.Must(internet.RegisterProtocolConfigCreator(protocolName, func() interface{} {
   107  		return new(Config)
   108  	}))
   109  }