config.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package kcp
  2. import (
  3. v2net "v2ray.com/core/common/net"
  4. "v2ray.com/core/transport/internet"
  5. )
  6. func (v *MTU) GetValue() uint32 {
  7. if v == nil {
  8. return 1350
  9. }
  10. return v.Value
  11. }
  12. func (v *TTI) GetValue() uint32 {
  13. if v == nil {
  14. return 50
  15. }
  16. return v.Value
  17. }
  18. func (v *UplinkCapacity) GetValue() uint32 {
  19. if v == nil {
  20. return 5
  21. }
  22. return v.Value
  23. }
  24. func (v *DownlinkCapacity) GetValue() uint32 {
  25. if v == nil {
  26. return 20
  27. }
  28. return v.Value
  29. }
  30. func (v *WriteBuffer) GetSize() uint32 {
  31. if v == nil {
  32. return 2 * 1024 * 1024
  33. }
  34. return v.Size
  35. }
  36. func (v *ReadBuffer) GetSize() uint32 {
  37. if v == nil {
  38. return 2 * 1024 * 1024
  39. }
  40. return v.Size
  41. }
  42. func (v *Config) GetAuthenticator() (internet.Authenticator, error) {
  43. auth := NewSimpleAuthenticator()
  44. if v.HeaderConfig != nil {
  45. rawConfig, err := v.HeaderConfig.GetInstance()
  46. if err != nil {
  47. return nil, err
  48. }
  49. header, err := internet.CreateAuthenticator(v.HeaderConfig.Type, rawConfig)
  50. if err != nil {
  51. return nil, err
  52. }
  53. auth = internet.NewAuthenticatorChain(header, auth)
  54. }
  55. return auth, nil
  56. }
  57. func (v *Config) GetSendingInFlightSize() uint32 {
  58. size := v.UplinkCapacity.GetValue() * 1024 * 1024 / v.Mtu.GetValue() / (1000 / v.Tti.GetValue())
  59. if size < 8 {
  60. size = 8
  61. }
  62. return size
  63. }
  64. func (v *Config) GetSendingBufferSize() uint32 {
  65. return v.WriteBuffer.GetSize() / v.Mtu.GetValue()
  66. }
  67. func (v *Config) GetReceivingInFlightSize() uint32 {
  68. size := v.DownlinkCapacity.GetValue() * 1024 * 1024 / v.Mtu.GetValue() / (1000 / v.Tti.GetValue())
  69. if size < 8 {
  70. size = 8
  71. }
  72. return size
  73. }
  74. func (v *Config) GetReceivingBufferSize() uint32 {
  75. return v.ReadBuffer.GetSize() / v.Mtu.GetValue()
  76. }
  77. func (o *ConnectionReuse) IsEnabled() bool {
  78. if o == nil {
  79. return true
  80. }
  81. return o.Enable
  82. }
  83. func init() {
  84. internet.RegisterNetworkConfigCreator(v2net.Network_KCP, func() interface{} {
  85. return new(Config)
  86. })
  87. }