Kcp.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. using System;
  2. using System.Runtime.InteropServices;
  3. namespace ET
  4. {
  5. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  6. public delegate int KcpOutput(IntPtr buf, int len, IntPtr kcp, IntPtr user);
  7. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  8. public delegate void KcpLog(IntPtr buf, int len, IntPtr kcp, IntPtr user);
  9. public class Kcp
  10. {
  11. private IntPtr intPtr;
  12. public Kcp(uint conv, IntPtr user)
  13. {
  14. this.intPtr = ikcp_create(conv, user);
  15. }
  16. public long Id
  17. {
  18. get
  19. {
  20. return this.intPtr.ToInt64();
  21. }
  22. }
  23. public const int OneM = 1024 * 1024;
  24. public const int InnerMaxWaitSize = 1024 * 1024;
  25. public const int OuterMaxWaitSize = 1024 * 1024;
  26. private static KcpOutput KcpOutput;
  27. private static KcpLog KcpLog;
  28. #if UNITY_IPHONE && !UNITY_EDITOR
  29. const string KcpDLL = "__Internal";
  30. #else
  31. const string KcpDLL = "kcp";
  32. #endif
  33. [DllImport(KcpDLL, CallingConvention=CallingConvention.Cdecl)]
  34. private static extern uint ikcp_check(IntPtr kcp, uint current);
  35. [DllImport(KcpDLL, CallingConvention=CallingConvention.Cdecl)]
  36. private static extern IntPtr ikcp_create(uint conv, IntPtr user);
  37. [DllImport(KcpDLL, CallingConvention=CallingConvention.Cdecl)]
  38. private static extern void ikcp_flush(IntPtr kcp);
  39. [DllImport(KcpDLL, CallingConvention=CallingConvention.Cdecl)]
  40. private static extern uint ikcp_getconv(IntPtr ptr);
  41. [DllImport(KcpDLL, CallingConvention=CallingConvention.Cdecl)]
  42. private static extern int ikcp_input(IntPtr kcp, byte[] data, int offset, int size);
  43. [DllImport(KcpDLL, CallingConvention=CallingConvention.Cdecl)]
  44. private static extern int ikcp_nodelay(IntPtr kcp, int nodelay, int interval, int resend, int nc);
  45. [DllImport(KcpDLL, CallingConvention=CallingConvention.Cdecl)]
  46. private static extern int ikcp_peeksize(IntPtr kcp);
  47. [DllImport(KcpDLL, CallingConvention=CallingConvention.Cdecl)]
  48. private static extern int ikcp_recv(IntPtr kcp, byte[] buffer, int index, int len);
  49. [DllImport(KcpDLL, CallingConvention=CallingConvention.Cdecl)]
  50. private static extern void ikcp_release(IntPtr kcp);
  51. [DllImport(KcpDLL, CallingConvention=CallingConvention.Cdecl)]
  52. private static extern int ikcp_send(IntPtr kcp, byte[] buffer, int offset, int len);
  53. [DllImport(KcpDLL, CallingConvention=CallingConvention.Cdecl)]
  54. private static extern void ikcp_setminrto(IntPtr ptr, int minrto);
  55. [DllImport(KcpDLL, CallingConvention=CallingConvention.Cdecl)]
  56. private static extern int ikcp_setmtu(IntPtr kcp, int mtu);
  57. [DllImport(KcpDLL, CallingConvention=CallingConvention.Cdecl)]
  58. private static extern void ikcp_setoutput(KcpOutput output);
  59. [DllImport(KcpDLL, CallingConvention=CallingConvention.Cdecl)]
  60. private static extern void ikcp_setlog(KcpLog log);
  61. [DllImport(KcpDLL, CallingConvention=CallingConvention.Cdecl)]
  62. private static extern void ikcp_update(IntPtr kcp, uint current);
  63. [DllImport(KcpDLL, CallingConvention=CallingConvention.Cdecl)]
  64. private static extern int ikcp_waitsnd(IntPtr kcp);
  65. [DllImport(KcpDLL, CallingConvention=CallingConvention.Cdecl)]
  66. private static extern int ikcp_wndsize(IntPtr kcp, int sndwnd, int rcvwnd);
  67. public uint Check(uint current)
  68. {
  69. if (this.intPtr == IntPtr.Zero)
  70. {
  71. throw new Exception($"kcp error, kcp point is zero");
  72. }
  73. uint ret = ikcp_check(this.intPtr, current);
  74. return ret;
  75. }
  76. public void Flush()
  77. {
  78. if (this.intPtr == IntPtr.Zero)
  79. {
  80. throw new Exception($"kcp error, kcp point is zero");
  81. }
  82. ikcp_flush(this.intPtr);
  83. }
  84. public uint GetConv()
  85. {
  86. if (this.intPtr == IntPtr.Zero)
  87. {
  88. throw new Exception($"kcp error, kcp point is zero");
  89. }
  90. return ikcp_getconv(this.intPtr);
  91. }
  92. public int Input(byte[] buffer, int offset, int len)
  93. {
  94. if (this.intPtr == IntPtr.Zero)
  95. {
  96. throw new Exception($"kcp error, kcp point is zero");
  97. }
  98. if (offset + len > buffer.Length)
  99. {
  100. throw new Exception($"kcp error, KcpInput {buffer.Length} {offset} {len}");
  101. }
  102. int ret = ikcp_input(this.intPtr, buffer, offset, len);
  103. return ret;
  104. }
  105. public int Nodelay(int nodelay, int interval, int resend, int nc)
  106. {
  107. if (this.intPtr == IntPtr.Zero)
  108. {
  109. throw new Exception($"kcp error, kcp point is zero");
  110. }
  111. return ikcp_nodelay(this.intPtr, nodelay, interval, resend, nc);
  112. }
  113. public int Peeksize()
  114. {
  115. if (this.intPtr == IntPtr.Zero)
  116. {
  117. throw new Exception($"kcp error, kcp point is zero");
  118. }
  119. int ret = ikcp_peeksize(this.intPtr);
  120. return ret;
  121. }
  122. public int Recv(byte[] buffer, int index, int len)
  123. {
  124. if (this.intPtr == IntPtr.Zero)
  125. {
  126. throw new Exception($"kcp error, kcp point is zero");
  127. }
  128. if (buffer.Length < index + len)
  129. {
  130. throw new Exception($"kcp error, KcpRecv error: {index} {len}");
  131. }
  132. int ret = ikcp_recv(this.intPtr, buffer, index, len);
  133. return ret;
  134. }
  135. public void Release()
  136. {
  137. if (this.intPtr == IntPtr.Zero)
  138. {
  139. throw new Exception($"kcp error, kcp point is zero");
  140. }
  141. ikcp_release(this.intPtr);
  142. this.intPtr = IntPtr.Zero;
  143. }
  144. public int Send(byte[] buffer, int offset, int len)
  145. {
  146. if (this.intPtr == IntPtr.Zero)
  147. {
  148. throw new Exception($"kcp error, kcp point is zero");
  149. }
  150. if (offset + len > buffer.Length)
  151. {
  152. throw new Exception($"kcp error, KcpSend {buffer.Length} {offset} {len}");
  153. }
  154. int ret = ikcp_send(this.intPtr, buffer, offset, len);
  155. return ret;
  156. }
  157. public void SetMinrto(int minrto)
  158. {
  159. if (this.intPtr == IntPtr.Zero)
  160. {
  161. throw new Exception($"kcp error, kcp point is zero");
  162. }
  163. ikcp_setminrto(this.intPtr, minrto);
  164. }
  165. public int SetMtu(int mtu)
  166. {
  167. if (this.intPtr == IntPtr.Zero)
  168. {
  169. throw new Exception($"kcp error, kcp point is zero");
  170. }
  171. return ikcp_setmtu(this.intPtr, mtu);
  172. }
  173. public static void SetOutput(KcpOutput output)
  174. {
  175. KcpOutput = output;
  176. ikcp_setoutput(KcpOutput);
  177. }
  178. public static void SetLog(KcpLog kcpLog)
  179. {
  180. KcpLog = kcpLog;
  181. ikcp_setlog(KcpLog);
  182. }
  183. public void Update(uint current)
  184. {
  185. if (this.intPtr == IntPtr.Zero)
  186. {
  187. throw new Exception($"kcp error, kcp point is zero");
  188. }
  189. ikcp_update(this.intPtr, current);
  190. }
  191. public int Waitsnd()
  192. {
  193. if (this.intPtr == IntPtr.Zero)
  194. {
  195. throw new Exception($"kcp error, kcp point is zero");
  196. }
  197. int ret = ikcp_waitsnd(this.intPtr);
  198. return ret;
  199. }
  200. public int SetWndSize(int sndwnd, int rcvwnd)
  201. {
  202. if (this.intPtr == IntPtr.Zero)
  203. {
  204. throw new Exception($"kcp error, kcp point is zero");
  205. }
  206. return ikcp_wndsize(this.intPtr, sndwnd, rcvwnd);
  207. }
  208. }
  209. }