config.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package kcp
  2. import (
  3. "crypto/cipher"
  4. "v2ray.com/core/common"
  5. "v2ray.com/core/transport/internet"
  6. )
  7. // GetMTUValue returns the value of MTU settings.
  8. func (v *Config) GetMTUValue() uint32 {
  9. if v == nil || v.Mtu == nil {
  10. return 1350
  11. }
  12. return v.Mtu.Value
  13. }
  14. // GetTTIValue returns the value of TTI settings.
  15. func (v *Config) GetTTIValue() uint32 {
  16. if v == nil || v.Tti == nil {
  17. return 50
  18. }
  19. return v.Tti.Value
  20. }
  21. // GetUplinkCapacityValue returns the value of UplinkCapacity settings.
  22. func (v *Config) GetUplinkCapacityValue() uint32 {
  23. if v == nil || v.UplinkCapacity == nil {
  24. return 5
  25. }
  26. return v.UplinkCapacity.Value
  27. }
  28. // GetDownlinkCapacityValue returns the value of DownlinkCapacity settings.
  29. func (v *Config) GetDownlinkCapacityValue() uint32 {
  30. if v == nil || v.DownlinkCapacity == nil {
  31. return 20
  32. }
  33. return v.DownlinkCapacity.Value
  34. }
  35. // GetWriteBufferSize returns the size of WriterBuffer in bytes.
  36. func (v *Config) GetWriteBufferSize() uint32 {
  37. if v == nil || v.WriteBuffer == nil {
  38. return 2 * 1024 * 1024
  39. }
  40. return v.WriteBuffer.Size
  41. }
  42. // GetReadBufferSize returns the size of ReadBuffer in bytes.
  43. func (v *Config) GetReadBufferSize() uint32 {
  44. if v == nil || v.ReadBuffer == nil {
  45. return 2 * 1024 * 1024
  46. }
  47. return v.ReadBuffer.Size
  48. }
  49. // GetSecurity returns the security settings.
  50. func (v *Config) GetSecurity() (cipher.AEAD, error) {
  51. return NewSimpleAuthenticator(), nil
  52. }
  53. func (v *Config) GetPackerHeader() (internet.PacketHeader, error) {
  54. if v.HeaderConfig != nil {
  55. rawConfig, err := v.HeaderConfig.GetInstance()
  56. if err != nil {
  57. return nil, err
  58. }
  59. return internet.CreatePacketHeader(rawConfig)
  60. }
  61. return nil, nil
  62. }
  63. func (v *Config) GetSendingInFlightSize() uint32 {
  64. size := v.GetUplinkCapacityValue() * 1024 * 1024 / v.GetMTUValue() / (1000 / v.GetTTIValue())
  65. if size < 8 {
  66. size = 8
  67. }
  68. return size
  69. }
  70. func (v *Config) GetSendingBufferSize() uint32 {
  71. return v.GetWriteBufferSize() / v.GetMTUValue()
  72. }
  73. func (v *Config) GetReceivingInFlightSize() uint32 {
  74. size := v.GetDownlinkCapacityValue() * 1024 * 1024 / v.GetMTUValue() / (1000 / v.GetTTIValue())
  75. if size < 8 {
  76. size = 8
  77. }
  78. return size
  79. }
  80. func (v *Config) GetReceivingBufferSize() uint32 {
  81. return v.GetReadBufferSize() / v.GetMTUValue()
  82. }
  83. func init() {
  84. common.Must(internet.RegisterProtocolConfigCreator(internet.TransportProtocol_MKCP, func() interface{} {
  85. return new(Config)
  86. }))
  87. }