KcpConfig.cs 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // common config struct, instead of passing 10 parameters manually every time.
  2. using System;
  3. namespace kcp2k
  4. {
  5. // [Serializable] to show it in Unity inspector.
  6. // 'class' so we can set defaults easily.
  7. [Serializable]
  8. public class KcpConfig
  9. {
  10. // socket configuration ////////////////////////////////////////////////
  11. // DualMode uses both IPv6 and IPv4. not all platforms support it.
  12. // (Nintendo Switch, etc.)
  13. public bool DualMode;
  14. // UDP servers use only one socket.
  15. // maximize buffer to handle as many connections as possible.
  16. //
  17. // M1 mac pro:
  18. // recv buffer default: 786896 (771 KB)
  19. // send buffer default: 9216 (9 KB)
  20. // max configurable: ~7 MB
  21. public int RecvBufferSize;
  22. public int SendBufferSize;
  23. // kcp configuration ///////////////////////////////////////////////////
  24. // configurable MTU in case kcp sits on top of other abstractions like
  25. // encrypted transports, relays, etc.
  26. public int Mtu;
  27. // NoDelay is recommended to reduce latency. This also scales better
  28. // without buffers getting full.
  29. public bool NoDelay;
  30. // KCP internal update interval. 100ms is KCP default, but a lower
  31. // interval is recommended to minimize latency and to scale to more
  32. // networked entities.
  33. public uint Interval;
  34. // KCP fastresend parameter. Faster resend for the cost of higher
  35. // bandwidth.
  36. public int FastResend;
  37. // KCP congestion window heavily limits messages flushed per update.
  38. // congestion window may actually be broken in kcp:
  39. // - sending max sized message @ M1 mac flushes 2-3 messages per update
  40. // - even with super large send/recv window, it requires thousands of
  41. // update calls
  42. // best to leave this disabled, as it may significantly increase latency.
  43. public bool CongestionWindow;
  44. // KCP window size can be modified to support higher loads.
  45. // for example, Mirror Benchmark requires:
  46. // 128, 128 for 4k monsters
  47. // 512, 512 for 10k monsters
  48. // 8192, 8192 for 20k monsters
  49. public uint SendWindowSize;
  50. public uint ReceiveWindowSize;
  51. // timeout in milliseconds
  52. public int Timeout;
  53. // maximum retransmission attempts until dead_link
  54. public uint MaxRetransmits;
  55. // constructor /////////////////////////////////////////////////////////
  56. // constructor with defaults for convenience.
  57. // makes it easy to define "new KcpConfig(DualMode=false)" etc.
  58. public KcpConfig(
  59. bool DualMode = true,
  60. int RecvBufferSize = 1024 * 1024 * 7,
  61. int SendBufferSize = 1024 * 1024 * 7,
  62. int Mtu = Kcp.MTU_DEF,
  63. bool NoDelay = true,
  64. uint Interval = 10,
  65. int FastResend = 0,
  66. bool CongestionWindow = false,
  67. uint SendWindowSize = Kcp.WND_SND,
  68. uint ReceiveWindowSize = Kcp.WND_RCV,
  69. int Timeout = KcpPeer.DEFAULT_TIMEOUT,
  70. uint MaxRetransmits = Kcp.DEADLINK)
  71. {
  72. this.DualMode = DualMode;
  73. this.RecvBufferSize = RecvBufferSize;
  74. this.SendBufferSize = SendBufferSize;
  75. this.Mtu = Mtu;
  76. this.NoDelay = NoDelay;
  77. this.Interval = Interval;
  78. this.FastResend = FastResend;
  79. this.CongestionWindow = CongestionWindow;
  80. this.SendWindowSize = SendWindowSize;
  81. this.ReceiveWindowSize = ReceiveWindowSize;
  82. this.Timeout = Timeout;
  83. this.MaxRetransmits = MaxRetransmits;
  84. }
  85. }
  86. }