config.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. return this.GetSendingInFlightSize() + this.WriteBuffer/this.Mtu
  39. }
  40. func (this *Config) GetReceivingInFlightSize() uint32 {
  41. size := this.DownlinkCapacity * 1024 * 1024 / this.Mtu / (1000 / this.Tti) / 2
  42. if size < 8 {
  43. size = 8
  44. }
  45. return size
  46. }
  47. func (this *Config) GetReceivingBufferSize() uint32 {
  48. return this.GetReceivingInFlightSize() + this.ReadBuffer/this.Mtu
  49. }
  50. func DefaultConfig() Config {
  51. return Config{
  52. Mtu: 1350,
  53. Tti: 50,
  54. UplinkCapacity: 5,
  55. DownlinkCapacity: 20,
  56. Congestion: false,
  57. WriteBuffer: 1 * 1024 * 1024,
  58. ReadBuffer: 1 * 1024 * 1024,
  59. }
  60. }
  61. var (
  62. effectiveConfig = DefaultConfig()
  63. )