config.go 1.9 KB

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