config.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package kcp
  2. type Config struct {
  3. Mtu uint32 // Maximum transmission unit
  4. Tti uint32
  5. UplinkCapacity uint32
  6. DownlinkCapacity uint32
  7. Congestion bool
  8. WriteBuffer uint32
  9. ReadBuffer uint32
  10. }
  11. func (this *Config) Apply() {
  12. effectiveConfig = *this
  13. }
  14. func (this *Config) GetSendingInFlightSize() uint32 {
  15. size := this.UplinkCapacity * 1024 * 1024 / this.Mtu / (1000 / this.Tti) / 2
  16. if size == 0 {
  17. size = 8
  18. }
  19. return size
  20. }
  21. func (this *Config) GetSendingWindowSize() uint32 {
  22. return this.GetSendingInFlightSize() * 4
  23. }
  24. func (this *Config) GetSendingQueueSize() uint32 {
  25. return this.WriteBuffer / this.Mtu
  26. }
  27. func (this *Config) GetReceivingWindowSize() uint32 {
  28. size := this.DownlinkCapacity * 1024 * 1024 / this.Mtu / (1000 / this.Tti) / 2
  29. if size == 0 {
  30. size = 8
  31. }
  32. return size
  33. }
  34. func (this *Config) GetReceivingQueueSize() uint32 {
  35. return this.ReadBuffer / this.Mtu
  36. }
  37. func DefaultConfig() Config {
  38. return Config{
  39. Mtu: 1350,
  40. Tti: 20,
  41. UplinkCapacity: 5,
  42. DownlinkCapacity: 20,
  43. Congestion: false,
  44. WriteBuffer: 8 * 1024 * 1024,
  45. ReadBuffer: 8 * 1024 * 1024,
  46. }
  47. }
  48. var (
  49. effectiveConfig = DefaultConfig()
  50. )