KcpTool.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System;
  2. namespace IFramework.Net.KCP
  3. {
  4. class KcpTool
  5. {
  6. public const int IKCP_RTO_NDL = 30; // no delay min rto
  7. public const int IKCP_RTO_MIN = 100; // normal min rto
  8. public const int IKCP_RTO_DEF = 200;
  9. public const int IKCP_RTO_MAX = 60000;
  10. public const int IKCP_CMD_PUSH = 81; // cmd: push data
  11. public const int IKCP_CMD_ACK = 82; // cmd: ack
  12. public const int IKCP_CMD_WASK = 83; // cmd: window probe (ask)
  13. public const int IKCP_CMD_WINS = 84; // cmd: window size (tell)
  14. public const int IKCP_ASK_SEND = 1; // need to send IKCP_CMD_WASK
  15. public const int IKCP_ASK_TELL = 2; // need to send IKCP_CMD_WINS
  16. public const int IKCP_WND_SND = 32;
  17. public const int IKCP_WND_RCV = 32;
  18. //public const int IKCP_MTU_DEF = 1400;
  19. public const int IKCP_MTU_DEF = 1024;
  20. public const int IKCP_ACK_FAST = 3;
  21. public const int IKCP_INTERVAL = 100;
  22. public const int IKCP_OVERHEAD = 24;
  23. public const int IKCP_DEADLINK = 20;
  24. public const int IKCP_THRESH_INIT = 2;
  25. public const int IKCP_THRESH_MIN = 2;
  26. public const int IKCP_PROBE_INIT = 7000; // 7 secs to probe window size
  27. public const int IKCP_PROBE_LIMIT = 120000; // up to 120 secs to probe window
  28. public const int IKCP_SN_OFFSET = 12;
  29. // encode 8 bits unsigned int
  30. public static int Encode(byte[] p, int offset, byte c)
  31. {
  32. p[0 + offset] = c;
  33. return 1;
  34. }
  35. // decode 8 bits unsigned int
  36. public static int Decode(byte[] p, int offset, ref byte c)
  37. {
  38. c = p[0 + offset];
  39. return 1;
  40. }
  41. /* encode 16 bits unsigned int (lsb) */
  42. public static int Encode(byte[] p, int offset, UInt16 w)
  43. {
  44. p[0 + offset] = (byte)(w >> 0);
  45. p[1 + offset] = (byte)(w >> 8);
  46. return 2;
  47. }
  48. /* decode 16 bits unsigned int (lsb) */
  49. public static int Decode(byte[] p, int offset, ref UInt16 c)
  50. {
  51. UInt16 result = 0;
  52. result |= (UInt16)p[0 + offset];
  53. result |= (UInt16)(p[1 + offset] << 8);
  54. c = result;
  55. return 2;
  56. }
  57. /* encode 32 bits unsigned int (lsb) */
  58. public static int Encode(byte[] p, int offset, UInt32 l)
  59. {
  60. p[0 + offset] = (byte)(l >> 0);
  61. p[1 + offset] = (byte)(l >> 8);
  62. p[2 + offset] = (byte)(l >> 16);
  63. p[3 + offset] = (byte)(l >> 24);
  64. return 4;
  65. }
  66. /* decode 32 bits unsigned int (lsb) */
  67. public static int Decode(byte[] p, int offset, ref UInt32 c)
  68. {
  69. UInt32 result = 0;
  70. result |= (UInt32)p[0 + offset];
  71. result |= (UInt32)(p[1 + offset] << 8);
  72. result |= (UInt32)(p[2 + offset] << 16);
  73. result |= (UInt32)(p[3 + offset] << 24);
  74. c = result;
  75. return 4;
  76. }
  77. public static UInt32 Min(UInt32 a, UInt32 b) => a <= b ? a : b;
  78. public static UInt32 Max(UInt32 a, UInt32 b) => a >= b ? a : b;
  79. public static UInt32 Clamp(UInt32 lower, UInt32 middle, UInt32 upper) => Min(Max(lower, middle), upper);
  80. public static Int32 Subtract(UInt32 later, UInt32 earlier) => ((Int32)(later - earlier));
  81. }
  82. }