config.go 1.9 KB

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