config.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package kcp
  2. import (
  3. "v2ray.com/core/transport/internet"
  4. )
  5. type Config struct {
  6. Mtu uint32 // Maximum transmission unit
  7. Tti uint32
  8. UplinkCapacity uint32
  9. DownlinkCapacity uint32
  10. Congestion bool
  11. WriteBuffer uint32
  12. ReadBuffer uint32
  13. HeaderType string
  14. HeaderConfig internet.AuthenticatorConfig
  15. }
  16. func (this *Config) Apply() {
  17. effectiveConfig = *this
  18. }
  19. func (this *Config) GetAuthenticator() (internet.Authenticator, error) {
  20. auth := NewSimpleAuthenticator()
  21. if this.HeaderConfig != nil {
  22. header, err := internet.CreateAuthenticator(this.HeaderType, this.HeaderConfig)
  23. if err != nil {
  24. return nil, err
  25. }
  26. auth = internet.NewAuthenticatorChain(header, auth)
  27. }
  28. return auth, nil
  29. }
  30. func (this *Config) GetSendingInFlightSize() uint32 {
  31. size := this.UplinkCapacity * 1024 * 1024 / this.Mtu / (1000 / this.Tti) / 2
  32. if size < 8 {
  33. size = 8
  34. }
  35. return size
  36. }
  37. func (this *Config) GetSendingBufferSize() uint32 {
  38. size := this.WriteBuffer / this.Mtu
  39. if size < this.GetSendingInFlightSize() {
  40. size = this.GetSendingInFlightSize()
  41. }
  42. return size
  43. }
  44. func (this *Config) GetReceivingWindowSize() uint32 {
  45. size := this.DownlinkCapacity * 1024 * 1024 / this.Mtu / (1000 / this.Tti) / 2
  46. if size < 8 {
  47. size = 8
  48. }
  49. return size
  50. }
  51. func (this *Config) GetReceivingBufferSize() uint32 {
  52. bufferSize := this.ReadBuffer / this.Mtu
  53. windowSize := this.DownlinkCapacity * 1024 * 1024 / this.Mtu / (1000 / this.Tti) / 2
  54. if windowSize < 8 {
  55. windowSize = 8
  56. }
  57. if bufferSize < windowSize {
  58. bufferSize = windowSize
  59. }
  60. return bufferSize
  61. }
  62. func DefaultConfig() Config {
  63. return Config{
  64. Mtu: 1350,
  65. Tti: 50,
  66. UplinkCapacity: 5,
  67. DownlinkCapacity: 20,
  68. Congestion: false,
  69. WriteBuffer: 1 * 1024 * 1024,
  70. ReadBuffer: 1 * 1024 * 1024,
  71. }
  72. }
  73. var (
  74. effectiveConfig = DefaultConfig()
  75. )