github.com/v2fly/v2ray-core/v4@v4.45.2/transport/internet/kcp/config.go (about)

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