KcpSegment.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. using System.Buffers.Binary;
  2. using System.Runtime.InteropServices;
  3. namespace System.Net.Sockets.Kcp
  4. {
  5. /// <summary>
  6. /// 调整了没存布局,直接拷贝块提升性能。
  7. /// <para>结构体保存内容只有一个指针,不用担心参数传递过程中的性能</para>
  8. /// https://github.com/skywind3000/kcp/issues/118#issuecomment-338133930
  9. /// <para>不要对没有初始化的KcpSegment(内部指针为0,所有属性都将指向位置区域) 进行任何赋值操作,可能导致内存损坏。
  10. /// 出于性能考虑,没有对此项进行安全检查。</para>
  11. /// </summary>
  12. public struct KcpSegment : IKcpSegment
  13. {
  14. internal readonly unsafe byte* ptr;
  15. public unsafe KcpSegment(byte* intPtr, uint appendDateSize)
  16. {
  17. this.ptr = intPtr;
  18. len = appendDateSize;
  19. }
  20. /// <summary>
  21. /// 使用完必须显示释放,否则内存泄漏
  22. /// </summary>
  23. /// <param name="appendDateSize"></param>
  24. /// <returns></returns>
  25. public static KcpSegment AllocHGlobal(int appendDateSize)
  26. {
  27. var total = LocalOffset + HeadOffset + appendDateSize;
  28. IntPtr intPtr = Marshal.AllocHGlobal(total);
  29. unsafe
  30. {
  31. ///清零 不知道是不是有更快的清0方法?
  32. Span<byte> span = new Span<byte>(intPtr.ToPointer(), total);
  33. span.Clear();
  34. return new KcpSegment((byte*)intPtr.ToPointer(), (uint)appendDateSize);
  35. }
  36. }
  37. /// <summary>
  38. /// 释放非托管内存
  39. /// </summary>
  40. /// <param name="seg"></param>
  41. public static void FreeHGlobal(KcpSegment seg)
  42. {
  43. unsafe
  44. {
  45. Marshal.FreeHGlobal((IntPtr)seg.ptr);
  46. }
  47. }
  48. /// 以下为本机使用的参数
  49. /// <summary>
  50. /// offset = 0
  51. /// </summary>
  52. public uint resendts
  53. {
  54. get
  55. {
  56. unsafe
  57. {
  58. return *(uint*)(ptr + 0);
  59. }
  60. }
  61. set
  62. {
  63. unsafe
  64. {
  65. *(uint*)(ptr + 0) = value;
  66. }
  67. }
  68. }
  69. /// <summary>
  70. /// offset = 4
  71. /// </summary>
  72. public uint rto
  73. {
  74. get
  75. {
  76. unsafe
  77. {
  78. return *(uint*)(ptr + 4);
  79. }
  80. }
  81. set
  82. {
  83. unsafe
  84. {
  85. *(uint*)(ptr + 4) = value;
  86. }
  87. }
  88. }
  89. /// <summary>
  90. /// offset = 8
  91. /// </summary>
  92. public uint fastack
  93. {
  94. get
  95. {
  96. unsafe
  97. {
  98. return *(uint*)(ptr + 8);
  99. }
  100. }
  101. set
  102. {
  103. unsafe
  104. {
  105. *(uint*)(ptr + 8) = value;
  106. }
  107. }
  108. }
  109. /// <summary>
  110. /// offset = 12
  111. /// </summary>
  112. public uint xmit
  113. {
  114. get
  115. {
  116. unsafe
  117. {
  118. return *(uint*)(ptr + 12);
  119. }
  120. }
  121. set
  122. {
  123. unsafe
  124. {
  125. *(uint*)(ptr + 12) = value;
  126. }
  127. }
  128. }
  129. ///以下为需要网络传输的参数
  130. public const int LocalOffset = 4 * 4;
  131. public const int HeadOffset = KcpConst.IKCP_OVERHEAD;
  132. /// <summary>
  133. /// offset = <see cref="LocalOffset"/>
  134. /// </summary>
  135. /// https://github.com/skywind3000/kcp/issues/134
  136. public uint conv
  137. {
  138. get
  139. {
  140. unsafe
  141. {
  142. return *(uint*)(LocalOffset + 0 + ptr);
  143. }
  144. }
  145. set
  146. {
  147. unsafe
  148. {
  149. *(uint*)(LocalOffset + 0 + ptr) = value;
  150. }
  151. }
  152. }
  153. /// <summary>
  154. /// offset = <see cref="LocalOffset"/> + 4
  155. /// </summary>
  156. public byte cmd
  157. {
  158. get
  159. {
  160. unsafe
  161. {
  162. return *(LocalOffset + 4 + ptr);
  163. }
  164. }
  165. set
  166. {
  167. unsafe
  168. {
  169. *(LocalOffset + 4 + ptr) = value;
  170. }
  171. }
  172. }
  173. /// <summary>
  174. /// offset = <see cref="LocalOffset"/> + 5
  175. /// </summary>
  176. public byte frg
  177. {
  178. get
  179. {
  180. unsafe
  181. {
  182. return *(LocalOffset + 5 + ptr);
  183. }
  184. }
  185. set
  186. {
  187. unsafe
  188. {
  189. *(LocalOffset + 5 + ptr) = value;
  190. }
  191. }
  192. }
  193. /// <summary>
  194. /// offset = <see cref="LocalOffset"/> + 6
  195. /// </summary>
  196. public ushort wnd
  197. {
  198. get
  199. {
  200. unsafe
  201. {
  202. return *(ushort*)(LocalOffset + 6 + ptr);
  203. }
  204. }
  205. set
  206. {
  207. unsafe
  208. {
  209. *(ushort*)(LocalOffset + 6 + ptr) = value;
  210. }
  211. }
  212. }
  213. /// <summary>
  214. /// offset = <see cref="LocalOffset"/> + 8
  215. /// </summary>
  216. public uint ts
  217. {
  218. get
  219. {
  220. unsafe
  221. {
  222. return *(uint*)(LocalOffset + 8 + ptr);
  223. }
  224. }
  225. set
  226. {
  227. unsafe
  228. {
  229. *(uint*)(LocalOffset + 8 + ptr) = value;
  230. }
  231. }
  232. }
  233. /// <summary>
  234. /// <para> SendNumber? </para>
  235. /// offset = <see cref="LocalOffset"/> + 12
  236. /// </summary>
  237. public uint sn
  238. {
  239. get
  240. {
  241. unsafe
  242. {
  243. return *(uint*)(LocalOffset + 12 + ptr);
  244. }
  245. }
  246. set
  247. {
  248. unsafe
  249. {
  250. *(uint*)(LocalOffset + 12 + ptr) = value;
  251. }
  252. }
  253. }
  254. /// <summary>
  255. /// offset = <see cref="LocalOffset"/> + 16
  256. /// </summary>
  257. public uint una
  258. {
  259. get
  260. {
  261. unsafe
  262. {
  263. return *(uint*)(LocalOffset + 16 + ptr);
  264. }
  265. }
  266. set
  267. {
  268. unsafe
  269. {
  270. *(uint*)(LocalOffset + 16 + ptr) = value;
  271. }
  272. }
  273. }
  274. /// <summary>
  275. /// <para> AppendDateSize </para>
  276. /// offset = <see cref="LocalOffset"/> + 20
  277. /// </summary>
  278. public uint len
  279. {
  280. get
  281. {
  282. unsafe
  283. {
  284. return *(uint*)(LocalOffset + 20 + ptr);
  285. }
  286. }
  287. private set
  288. {
  289. unsafe
  290. {
  291. *(uint*)(LocalOffset + 20 + ptr) = value;
  292. }
  293. }
  294. }
  295. /// <summary>
  296. ///
  297. /// </summary>
  298. /// https://github.com/skywind3000/kcp/issues/35#issuecomment-263770736
  299. public Span<byte> data
  300. {
  301. get
  302. {
  303. unsafe
  304. {
  305. return new Span<byte>(LocalOffset + HeadOffset + ptr, (int)len);
  306. }
  307. }
  308. }
  309. /// <summary>
  310. /// 将片段中的要发送的数据拷贝到指定缓冲区
  311. /// </summary>
  312. /// <param name="buffer"></param>
  313. /// <returns></returns>
  314. public int Encode(Span<byte> buffer)
  315. {
  316. var datelen = (int)(HeadOffset + len);
  317. ///备用偏移值 现阶段没有使用
  318. const int offset = 0;
  319. if (KcpConst.IsLittleEndian)
  320. {
  321. if (BitConverter.IsLittleEndian)
  322. {
  323. ///小端可以一次拷贝
  324. unsafe
  325. {
  326. ///要发送的数据从LocalOffset开始。
  327. ///本结构体调整了要发送字段和单机使用字段的位置,让报头数据和数据连续,节约一次拷贝。
  328. Span<byte> sendDate = new Span<byte>(ptr + LocalOffset, datelen);
  329. sendDate.CopyTo(buffer);
  330. }
  331. }
  332. else
  333. {
  334. BinaryPrimitives.WriteUInt32LittleEndian(buffer.Slice(offset), conv);
  335. buffer[offset + 4] = cmd;
  336. buffer[offset + 5] = frg;
  337. BinaryPrimitives.WriteUInt16LittleEndian(buffer.Slice(offset + 6), wnd);
  338. BinaryPrimitives.WriteUInt32LittleEndian(buffer.Slice(offset + 8), ts);
  339. BinaryPrimitives.WriteUInt32LittleEndian(buffer.Slice(offset + 12), sn);
  340. BinaryPrimitives.WriteUInt32LittleEndian(buffer.Slice(offset + 16), una);
  341. BinaryPrimitives.WriteUInt32LittleEndian(buffer.Slice(offset + 20), len);
  342. data.CopyTo(buffer.Slice(HeadOffset));
  343. }
  344. }
  345. else
  346. {
  347. if (BitConverter.IsLittleEndian)
  348. {
  349. BinaryPrimitives.WriteUInt32BigEndian(buffer.Slice(offset), conv);
  350. buffer[offset + 4] = cmd;
  351. buffer[offset + 5] = frg;
  352. BinaryPrimitives.WriteUInt16BigEndian(buffer.Slice(offset + 6), wnd);
  353. BinaryPrimitives.WriteUInt32BigEndian(buffer.Slice(offset + 8), ts);
  354. BinaryPrimitives.WriteUInt32BigEndian(buffer.Slice(offset + 12), sn);
  355. BinaryPrimitives.WriteUInt32BigEndian(buffer.Slice(offset + 16), una);
  356. BinaryPrimitives.WriteUInt32BigEndian(buffer.Slice(offset + 20), len);
  357. data.CopyTo(buffer.Slice(HeadOffset));
  358. }
  359. else
  360. {
  361. ///大端可以一次拷贝
  362. unsafe
  363. {
  364. ///要发送的数据从LocalOffset开始。
  365. ///本结构体调整了要发送字段和单机使用字段的位置,让报头数据和数据连续,节约一次拷贝。
  366. Span<byte> sendDate = new Span<byte>(ptr + LocalOffset, datelen);
  367. sendDate.CopyTo(buffer);
  368. }
  369. }
  370. }
  371. return datelen;
  372. }
  373. }
  374. }