config.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package kcp
  2. import (
  3. "github.com/v2ray/v2ray-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 == 0 {
  33. size = 8
  34. }
  35. return size
  36. }
  37. func (this *Config) GetSendingWindowSize() uint32 {
  38. return this.GetSendingInFlightSize() * 4
  39. }
  40. func (this *Config) GetSendingQueueSize() uint32 {
  41. return this.WriteBuffer / this.Mtu
  42. }
  43. func (this *Config) GetReceivingWindowSize() uint32 {
  44. size := this.DownlinkCapacity * 1024 * 1024 / this.Mtu / (1000 / this.Tti) / 2
  45. if size == 0 {
  46. size = 8
  47. }
  48. return size
  49. }
  50. func (this *Config) GetReceivingQueueSize() uint32 {
  51. return this.ReadBuffer / this.Mtu
  52. }
  53. func DefaultConfig() Config {
  54. return Config{
  55. Mtu: 1350,
  56. Tti: 20,
  57. UplinkCapacity: 5,
  58. DownlinkCapacity: 20,
  59. Congestion: false,
  60. WriteBuffer: 1 * 1024 * 1024,
  61. ReadBuffer: 1 * 1024 * 1024,
  62. }
  63. }
  64. var (
  65. effectiveConfig = DefaultConfig()
  66. )