github.com/xtls/xray-core@v1.8.12-0.20240518155711-3168d27b0bdb/transport/internet/kcp/config.go (about)

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