config.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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) GetSendingWindowSize() 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) GetSendingQueueSize() uint32 {
  22. return this.WriteBuffer / this.Mtu
  23. }
  24. func (this *Config) GetReceivingWindowSize() uint32 {
  25. size := this.DownlinkCapacity * 1024 * 1024 / this.Mtu / (1000 / this.Tti) / 2
  26. if size == 0 {
  27. size = 8
  28. }
  29. return size
  30. }
  31. func (this *Config) GetReceivingQueueSize() uint32 {
  32. return this.ReadBuffer / this.Mtu
  33. }
  34. func DefaultConfig() Config {
  35. return Config{
  36. Mtu: 1350,
  37. Tti: 20,
  38. UplinkCapacity: 5,
  39. DownlinkCapacity: 20,
  40. Congestion: false,
  41. WriteBuffer: 8 * 1024 * 1024,
  42. ReadBuffer: 8 * 1024 * 1024,
  43. }
  44. }
  45. var (
  46. effectiveConfig = DefaultConfig()
  47. )