config.go 1.7 KB

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