ByteBuf.cs 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572
  1. using ET;
  2. using System;
  3. using System.Diagnostics.CodeAnalysis;
  4. using System.Numerics;
  5. using System.Runtime.CompilerServices;
  6. using System.Text;
  7. namespace Luban
  8. {
  9. public enum EDeserializeError
  10. {
  11. OK,
  12. NOT_ENOUGH,
  13. EXCEED_SIZE,
  14. // UNMARSHAL_ERR,
  15. }
  16. [EnableClass]
  17. public class SerializationException : Exception
  18. {
  19. public SerializationException() { }
  20. public SerializationException(string msg) : base(msg) { }
  21. public SerializationException(string message, Exception innerException) : base(message, innerException)
  22. {
  23. }
  24. }
  25. public readonly struct SegmentSaveState
  26. {
  27. public SegmentSaveState(int readerIndex, int writerIndex)
  28. {
  29. ReaderIndex = readerIndex;
  30. WriterIndex = writerIndex;
  31. }
  32. public int ReaderIndex { get; }
  33. public int WriterIndex { get; }
  34. }
  35. [EnableClass]
  36. public sealed class ByteBuf : ICloneable, IEquatable<ByteBuf>
  37. {
  38. public ByteBuf()
  39. {
  40. Bytes = Array.Empty<byte>();
  41. ReaderIndex = WriterIndex = 0;
  42. }
  43. public ByteBuf(int capacity)
  44. {
  45. Bytes = capacity > 0 ? new byte[capacity] : Array.Empty<byte>();
  46. ReaderIndex = 0;
  47. WriterIndex = 0;
  48. }
  49. public ByteBuf(byte[] bytes)
  50. {
  51. Bytes = bytes;
  52. ReaderIndex = 0;
  53. WriterIndex = Capacity;
  54. }
  55. public ByteBuf(byte[] bytes, int readIndex, int writeIndex)
  56. {
  57. Bytes = bytes;
  58. ReaderIndex = readIndex;
  59. WriterIndex = writeIndex;
  60. }
  61. public ByteBuf(int capacity, Action<ByteBuf> releaser) : this(capacity)
  62. {
  63. _releaser = releaser;
  64. }
  65. public static ByteBuf Wrap(byte[] bytes)
  66. {
  67. return new ByteBuf(bytes, 0, bytes.Length);
  68. }
  69. public void Replace(byte[] bytes)
  70. {
  71. Bytes = bytes;
  72. ReaderIndex = 0;
  73. WriterIndex = Capacity;
  74. }
  75. public void Replace(byte[] bytes, int beginPos, int endPos)
  76. {
  77. Bytes = bytes;
  78. ReaderIndex = beginPos;
  79. WriterIndex = endPos;
  80. }
  81. public int ReaderIndex { get; set; }
  82. public int WriterIndex { get; set; }
  83. private readonly Action<ByteBuf> _releaser;
  84. public int Capacity => Bytes.Length;
  85. public int Size { get { return WriterIndex - ReaderIndex; } }
  86. public bool Empty => WriterIndex <= ReaderIndex;
  87. public bool NotEmpty => WriterIndex > ReaderIndex;
  88. public void AddWriteIndex(int add)
  89. {
  90. WriterIndex += add;
  91. }
  92. public void AddReadIndex(int add)
  93. {
  94. ReaderIndex += add;
  95. }
  96. #pragma warning disable CA1819 // 属性不应返回数组
  97. public byte[] Bytes { get; private set; }
  98. #pragma warning restore CA1819 // 属性不应返回数组
  99. public byte[] CopyData()
  100. {
  101. var n = Remaining;
  102. if (n > 0)
  103. {
  104. var arr = new byte[n];
  105. Buffer.BlockCopy(Bytes, ReaderIndex, arr, 0, n);
  106. return arr;
  107. }
  108. else
  109. {
  110. return Array.Empty<byte>();
  111. }
  112. }
  113. public int Remaining { get { return WriterIndex - ReaderIndex; } }
  114. public void DiscardReadBytes()
  115. {
  116. WriterIndex -= ReaderIndex;
  117. Array.Copy(Bytes, ReaderIndex, Bytes, 0, WriterIndex);
  118. ReaderIndex = 0;
  119. }
  120. public int NotCompactWritable { get { return Capacity - WriterIndex; } }
  121. public void WriteBytesWithoutSize(byte[] bs)
  122. {
  123. WriteBytesWithoutSize(bs, 0, bs.Length);
  124. }
  125. public void WriteBytesWithoutSize(byte[] bs, int offset, int len)
  126. {
  127. EnsureWrite(len);
  128. Buffer.BlockCopy(bs, offset, Bytes, WriterIndex, len);
  129. WriterIndex += len;
  130. }
  131. public void Clear()
  132. {
  133. ReaderIndex = WriterIndex = 0;
  134. }
  135. private const int MIN_CAPACITY = 16;
  136. private static int PropSize(int initSize, int needSize)
  137. {
  138. for (int i = Math.Max(initSize, MIN_CAPACITY); ; i <<= 1)
  139. {
  140. if (i >= needSize)
  141. {
  142. return i;
  143. }
  144. }
  145. }
  146. private void EnsureWrite0(int size)
  147. {
  148. var needSize = WriterIndex + size - ReaderIndex;
  149. if (needSize < Capacity)
  150. {
  151. WriterIndex -= ReaderIndex;
  152. Array.Copy(Bytes, ReaderIndex, Bytes, 0, WriterIndex);
  153. ReaderIndex = 0;
  154. }
  155. else
  156. {
  157. int newCapacity = PropSize(Capacity, needSize);
  158. var newBytes = new byte[newCapacity];
  159. WriterIndex -= ReaderIndex;
  160. Buffer.BlockCopy(Bytes, ReaderIndex, newBytes, 0, WriterIndex);
  161. ReaderIndex = 0;
  162. Bytes = newBytes;
  163. }
  164. }
  165. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  166. public void EnsureWrite(int size)
  167. {
  168. if (WriterIndex + size > Capacity)
  169. {
  170. EnsureWrite0(size);
  171. }
  172. }
  173. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  174. private void EnsureRead(int size)
  175. {
  176. if (ReaderIndex + size > WriterIndex)
  177. {
  178. throw new SerializationException();
  179. }
  180. }
  181. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  182. private bool CanRead(int size)
  183. {
  184. return (ReaderIndex + size <= WriterIndex);
  185. }
  186. public void Append(byte x)
  187. {
  188. EnsureWrite(1);
  189. Bytes[WriterIndex++] = x;
  190. }
  191. public void WriteBool(bool b)
  192. {
  193. EnsureWrite(1);
  194. Bytes[WriterIndex++] = (byte)(b ? 1 : 0);
  195. }
  196. public bool ReadBool()
  197. {
  198. EnsureRead(1);
  199. return Bytes[ReaderIndex++] != 0;
  200. }
  201. public void WriteByte(byte x)
  202. {
  203. EnsureWrite(1);
  204. Bytes[WriterIndex++] = x;
  205. }
  206. public byte ReadByte()
  207. {
  208. EnsureRead(1);
  209. return Bytes[ReaderIndex++];
  210. }
  211. public void WriteShort(short x)
  212. {
  213. if (x >= 0)
  214. {
  215. if (x < 0x80)
  216. {
  217. EnsureWrite(1);
  218. Bytes[WriterIndex++] = (byte)x;
  219. return;
  220. }
  221. else if (x < 0x4000)
  222. {
  223. EnsureWrite(2);
  224. Bytes[WriterIndex + 1] = (byte)x;
  225. Bytes[WriterIndex] = (byte)((x >> 8) | 0x80);
  226. WriterIndex += 2;
  227. return;
  228. }
  229. }
  230. EnsureWrite(3);
  231. Bytes[WriterIndex] = 0xff;
  232. Bytes[WriterIndex + 2] = (byte)x;
  233. Bytes[WriterIndex + 1] = (byte)(x >> 8);
  234. WriterIndex += 3;
  235. }
  236. public short ReadShort()
  237. {
  238. EnsureRead(1);
  239. int h = Bytes[ReaderIndex];
  240. if (h < 0x80)
  241. {
  242. ReaderIndex++;
  243. return (short)h;
  244. }
  245. else if (h < 0xc0)
  246. {
  247. EnsureRead(2);
  248. int x = ((h & 0x3f) << 8) | Bytes[ReaderIndex + 1];
  249. ReaderIndex += 2;
  250. return (short)x;
  251. }
  252. else if ((h == 0xff))
  253. {
  254. EnsureRead(3);
  255. int x = (Bytes[ReaderIndex + 1] << 8) | Bytes[ReaderIndex + 2];
  256. ReaderIndex += 3;
  257. return (short)x;
  258. }
  259. else
  260. {
  261. throw new SerializationException();
  262. }
  263. }
  264. public short ReadFshort()
  265. {
  266. EnsureRead(2);
  267. short x;
  268. #if CPU_SUPPORT_MEMORY_NOT_ALIGN
  269. unsafe
  270. {
  271. fixed (byte* b = &Bytes[ReaderIndex])
  272. {
  273. x = *(short*)b;
  274. }
  275. }
  276. #else
  277. x = (short)((Bytes[ReaderIndex + 1] << 8) | Bytes[ReaderIndex]);
  278. #endif
  279. ReaderIndex += 2;
  280. return x;
  281. }
  282. public void WriteFshort(short x)
  283. {
  284. EnsureWrite(2);
  285. #if CPU_SUPPORT_MEMORY_NOT_ALIGN
  286. unsafe
  287. {
  288. fixed (byte* b = &Bytes[WriterIndex])
  289. {
  290. *(short*)b = x;
  291. }
  292. }
  293. #else
  294. Bytes[WriterIndex] = (byte)x;
  295. Bytes[WriterIndex + 1] = (byte)(x >> 8);
  296. #endif
  297. WriterIndex += 2;
  298. }
  299. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  300. public void WriteInt(int x)
  301. {
  302. WriteUint((uint)x);
  303. }
  304. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  305. public int ReadInt()
  306. {
  307. return (int)ReadUint();
  308. }
  309. public void WriteUint(uint x)
  310. {
  311. // 如果有修改,记得也把 EndWriteSegment改了
  312. // 0 111 1111
  313. if (x < 0x80)
  314. {
  315. EnsureWrite(1);
  316. Bytes[WriterIndex++] = (byte)x;
  317. }
  318. else if (x < 0x4000) // 10 11 1111, -
  319. {
  320. EnsureWrite(2);
  321. Bytes[WriterIndex + 1] = (byte)x;
  322. Bytes[WriterIndex] = (byte)((x >> 8) | 0x80);
  323. WriterIndex += 2;
  324. }
  325. else if (x < 0x200000) // 110 1 1111, -,-
  326. {
  327. EnsureWrite(3);
  328. Bytes[WriterIndex + 2] = (byte)x;
  329. Bytes[WriterIndex + 1] = (byte)(x >> 8);
  330. Bytes[WriterIndex] = (byte)((x >> 16) | 0xc0);
  331. WriterIndex += 3;
  332. }
  333. else if (x < 0x10000000) // 1110 1111,-,-,-
  334. {
  335. EnsureWrite(4);
  336. Bytes[WriterIndex + 3] = (byte)x;
  337. Bytes[WriterIndex + 2] = (byte)(x >> 8);
  338. Bytes[WriterIndex + 1] = (byte)(x >> 16);
  339. Bytes[WriterIndex] = (byte)((x >> 24) | 0xe0);
  340. WriterIndex += 4;
  341. }
  342. else
  343. {
  344. EnsureWrite(5);
  345. Bytes[WriterIndex] = 0xf0;
  346. Bytes[WriterIndex + 4] = (byte)x;
  347. Bytes[WriterIndex + 3] = (byte)(x >> 8);
  348. Bytes[WriterIndex + 2] = (byte)(x >> 16);
  349. Bytes[WriterIndex + 1] = (byte)(x >> 24);
  350. WriterIndex += 5;
  351. }
  352. }
  353. public uint ReadUint()
  354. {
  355. ///
  356. /// 警告! 如有修改,记得调整 TryDeserializeInplaceOctets
  357. EnsureRead(1);
  358. uint h = Bytes[ReaderIndex];
  359. if (h < 0x80)
  360. {
  361. ReaderIndex++;
  362. return h;
  363. }
  364. else if (h < 0xc0)
  365. {
  366. EnsureRead(2);
  367. uint x = ((h & 0x3f) << 8) | Bytes[ReaderIndex + 1];
  368. ReaderIndex += 2;
  369. return x;
  370. }
  371. else if (h < 0xe0)
  372. {
  373. EnsureRead(3);
  374. uint x = ((h & 0x1f) << 16) | ((uint)Bytes[ReaderIndex + 1] << 8) | Bytes[ReaderIndex + 2];
  375. ReaderIndex += 3;
  376. return x;
  377. }
  378. else if (h < 0xf0)
  379. {
  380. EnsureRead(4);
  381. uint x = ((h & 0x0f) << 24) | ((uint)Bytes[ReaderIndex + 1] << 16) | ((uint)Bytes[ReaderIndex + 2] << 8) | Bytes[ReaderIndex + 3];
  382. ReaderIndex += 4;
  383. return x;
  384. }
  385. else
  386. {
  387. EnsureRead(5);
  388. uint x = ((uint)Bytes[ReaderIndex + 1] << 24) | ((uint)(Bytes[ReaderIndex + 2] << 16)) | ((uint)Bytes[ReaderIndex + 3] << 8) | Bytes[ReaderIndex + 4];
  389. ReaderIndex += 5;
  390. return x;
  391. }
  392. }
  393. public unsafe void WriteUint_Unsafe(uint x)
  394. {
  395. // 0 111 1111
  396. if (x < 0x80)
  397. {
  398. EnsureWrite(1);
  399. Bytes[WriterIndex++] = (byte)(x << 1);
  400. }
  401. else if (x < 0x4000)// 10 11 1111, -
  402. {
  403. EnsureWrite(2);
  404. fixed (byte* wb = &Bytes[WriterIndex])
  405. {
  406. *(uint*)(wb) = (x << 2 | 0b01);
  407. }
  408. WriterIndex += 2;
  409. }
  410. else if (x < 0x200000) // 110 1 1111, -,-
  411. {
  412. EnsureWrite(3);
  413. fixed (byte* wb = &Bytes[WriterIndex])
  414. {
  415. *(uint*)(wb) = (x << 3 | 0b011);
  416. }
  417. WriterIndex += 3;
  418. }
  419. else if (x < 0x10000000) // 1110 1111,-,-,-
  420. {
  421. EnsureWrite(4);
  422. fixed (byte* wb = &Bytes[WriterIndex])
  423. {
  424. *(uint*)(wb) = (x << 4 | 0b0111);
  425. }
  426. WriterIndex += 4;
  427. }
  428. else
  429. {
  430. EnsureWrite(5);
  431. fixed (byte* wb = &Bytes[WriterIndex])
  432. {
  433. *(uint*)(wb) = (x << 5 | 0b01111);
  434. }
  435. WriterIndex += 5;
  436. }
  437. }
  438. public unsafe uint ReadUint_Unsafe()
  439. {
  440. ///
  441. /// 警告! 如有修改,记得调整 TryDeserializeInplaceOctets
  442. EnsureRead(1);
  443. uint h = Bytes[ReaderIndex];
  444. if ((h & 0b1) == 0b0)
  445. {
  446. ReaderIndex++;
  447. return (h >> 1);
  448. }
  449. else if ((h & 0b11) == 0b01)
  450. {
  451. EnsureRead(2);
  452. fixed (byte* rb = &Bytes[ReaderIndex])
  453. {
  454. ReaderIndex += 2;
  455. return (*(uint*)rb) >> 2;
  456. }
  457. }
  458. else if ((h & 0b111) == 0b011)
  459. {
  460. EnsureRead(3);
  461. fixed (byte* rb = &Bytes[ReaderIndex])
  462. {
  463. ReaderIndex += 3;
  464. return (*(uint*)rb) >> 3;
  465. }
  466. }
  467. else if ((h & 0b1111) == 0b0111)
  468. {
  469. EnsureRead(4);
  470. fixed (byte* rb = &Bytes[ReaderIndex])
  471. {
  472. ReaderIndex += 4;
  473. return (*(uint*)rb) >> 4;
  474. }
  475. }
  476. else
  477. {
  478. EnsureRead(5);
  479. fixed (byte* rb = &Bytes[ReaderIndex])
  480. {
  481. ReaderIndex += 5;
  482. return (*(uint*)rb) >> 5;
  483. }
  484. }
  485. }
  486. public int ReadFint()
  487. {
  488. EnsureRead(4);
  489. int x;
  490. #if CPU_SUPPORT_MEMORY_NOT_ALIGN
  491. unsafe
  492. {
  493. fixed (byte* b = &Bytes[ReaderIndex])
  494. {
  495. x = *(int*)b;
  496. }
  497. }
  498. #else
  499. x = (Bytes[ReaderIndex + 3] << 24) | (Bytes[ReaderIndex + 2] << 16) | (Bytes[ReaderIndex + 1] << 8) | (Bytes[ReaderIndex]);
  500. #endif
  501. ReaderIndex += 4;
  502. return x;
  503. }
  504. public void WriteFint(int x)
  505. {
  506. EnsureWrite(4);
  507. #if CPU_SUPPORT_MEMORY_NOT_ALIGN
  508. unsafe
  509. {
  510. fixed (byte* b = &Bytes[WriterIndex])
  511. {
  512. *(int*)b = x;
  513. }
  514. }
  515. #else
  516. Bytes[WriterIndex] = (byte)x;
  517. Bytes[WriterIndex + 1] = (byte)(x >> 8);
  518. Bytes[WriterIndex + 2] = (byte)(x >> 16);
  519. Bytes[WriterIndex + 3] = (byte)(x >> 24);
  520. #endif
  521. WriterIndex += 4;
  522. }
  523. public int ReadFint_Safe()
  524. {
  525. EnsureRead(4);
  526. int x;
  527. x = (Bytes[ReaderIndex + 3] << 24) | (Bytes[ReaderIndex + 2] << 16) | (Bytes[ReaderIndex + 1] << 8) | (Bytes[ReaderIndex]);
  528. ReaderIndex += 4;
  529. return x;
  530. }
  531. public void WriteFint_Safe(int x)
  532. {
  533. EnsureWrite(4);
  534. Bytes[WriterIndex] = (byte)x;
  535. Bytes[WriterIndex + 1] = (byte)(x >> 8);
  536. Bytes[WriterIndex + 2] = (byte)(x >> 16);
  537. Bytes[WriterIndex + 3] = (byte)(x >> 24);
  538. WriterIndex += 4;
  539. }
  540. public void WriteLong(long x)
  541. {
  542. WriteUlong((ulong)x);
  543. }
  544. public long ReadLong()
  545. {
  546. return (long)ReadUlong();
  547. }
  548. public void WriteNumberAsLong(double x)
  549. {
  550. WriteLong((long)x);
  551. }
  552. public double ReadLongAsNumber()
  553. {
  554. return ReadLong();
  555. }
  556. private void WriteUlong(ulong x)
  557. {
  558. // 0 111 1111
  559. if (x < 0x80)
  560. {
  561. EnsureWrite(1);
  562. Bytes[WriterIndex++] = (byte)x;
  563. }
  564. else if (x < 0x4000) // 10 11 1111, -
  565. {
  566. EnsureWrite(2);
  567. Bytes[WriterIndex + 1] = (byte)x;
  568. Bytes[WriterIndex] = (byte)((x >> 8) | 0x80);
  569. WriterIndex += 2;
  570. }
  571. else if (x < 0x200000) // 110 1 1111, -,-
  572. {
  573. EnsureWrite(3);
  574. Bytes[WriterIndex + 2] = (byte)x;
  575. Bytes[WriterIndex + 1] = (byte)(x >> 8);
  576. Bytes[WriterIndex] = (byte)((x >> 16) | 0xc0);
  577. WriterIndex += 3;
  578. }
  579. else if (x < 0x10000000) // 1110 1111,-,-,-
  580. {
  581. EnsureWrite(4);
  582. Bytes[WriterIndex + 3] = (byte)x;
  583. Bytes[WriterIndex + 2] = (byte)(x >> 8);
  584. Bytes[WriterIndex + 1] = (byte)(x >> 16);
  585. Bytes[WriterIndex] = (byte)((x >> 24) | 0xe0);
  586. WriterIndex += 4;
  587. }
  588. else if (x < 0x800000000L) // 1111 0xxx,-,-,-,-
  589. {
  590. EnsureWrite(5);
  591. Bytes[WriterIndex + 4] = (byte)x;
  592. Bytes[WriterIndex + 3] = (byte)(x >> 8);
  593. Bytes[WriterIndex + 2] = (byte)(x >> 16);
  594. Bytes[WriterIndex + 1] = (byte)(x >> 24);
  595. Bytes[WriterIndex] = (byte)((x >> 32) | 0xf0);
  596. WriterIndex += 5;
  597. }
  598. else if (x < 0x40000000000L) // 1111 10xx,
  599. {
  600. EnsureWrite(6);
  601. Bytes[WriterIndex + 5] = (byte)x;
  602. Bytes[WriterIndex + 4] = (byte)(x >> 8);
  603. Bytes[WriterIndex + 3] = (byte)(x >> 16);
  604. Bytes[WriterIndex + 2] = (byte)(x >> 24);
  605. Bytes[WriterIndex + 1] = (byte)(x >> 32);
  606. Bytes[WriterIndex] = (byte)((x >> 40) | 0xf8);
  607. WriterIndex += 6;
  608. }
  609. else if (x < 0x200000000000L) // 1111 110x,
  610. {
  611. EnsureWrite(7);
  612. Bytes[WriterIndex + 6] = (byte)x;
  613. Bytes[WriterIndex + 5] = (byte)(x >> 8);
  614. Bytes[WriterIndex + 4] = (byte)(x >> 16);
  615. Bytes[WriterIndex + 3] = (byte)(x >> 24);
  616. Bytes[WriterIndex + 2] = (byte)(x >> 32);
  617. Bytes[WriterIndex + 1] = (byte)(x >> 40);
  618. Bytes[WriterIndex] = (byte)((x >> 48) | 0xfc);
  619. WriterIndex += 7;
  620. }
  621. else if (x < 0x100000000000000L) // 1111 1110
  622. {
  623. EnsureWrite(8);
  624. Bytes[WriterIndex + 7] = (byte)x;
  625. Bytes[WriterIndex + 6] = (byte)(x >> 8);
  626. Bytes[WriterIndex + 5] = (byte)(x >> 16);
  627. Bytes[WriterIndex + 4] = (byte)(x >> 24);
  628. Bytes[WriterIndex + 3] = (byte)(x >> 32);
  629. Bytes[WriterIndex + 2] = (byte)(x >> 40);
  630. Bytes[WriterIndex + 1] = (byte)(x >> 48);
  631. Bytes[WriterIndex] = 0xfe;
  632. WriterIndex += 8;
  633. }
  634. else // 1111 1111
  635. {
  636. EnsureWrite(9);
  637. Bytes[WriterIndex] = 0xff;
  638. Bytes[WriterIndex + 8] = (byte)x;
  639. Bytes[WriterIndex + 7] = (byte)(x >> 8);
  640. Bytes[WriterIndex + 6] = (byte)(x >> 16);
  641. Bytes[WriterIndex + 5] = (byte)(x >> 24);
  642. Bytes[WriterIndex + 4] = (byte)(x >> 32);
  643. Bytes[WriterIndex + 3] = (byte)(x >> 40);
  644. Bytes[WriterIndex + 2] = (byte)(x >> 48);
  645. Bytes[WriterIndex + 1] = (byte)(x >> 56);
  646. WriterIndex += 9;
  647. }
  648. }
  649. public ulong ReadUlong()
  650. {
  651. EnsureRead(1);
  652. uint h = Bytes[ReaderIndex];
  653. if (h < 0x80)
  654. {
  655. ReaderIndex++;
  656. return h;
  657. }
  658. else if (h < 0xc0)
  659. {
  660. EnsureRead(2);
  661. uint x = ((h & 0x3f) << 8) | Bytes[ReaderIndex + 1];
  662. ReaderIndex += 2;
  663. return x;
  664. }
  665. else if (h < 0xe0)
  666. {
  667. EnsureRead(3);
  668. uint x = ((h & 0x1f) << 16) | ((uint)Bytes[ReaderIndex + 1] << 8) | Bytes[ReaderIndex + 2];
  669. ReaderIndex += 3;
  670. return x;
  671. }
  672. else if (h < 0xf0)
  673. {
  674. EnsureRead(4);
  675. uint x = ((h & 0x0f) << 24) | ((uint)Bytes[ReaderIndex + 1] << 16) | ((uint)Bytes[ReaderIndex + 2] << 8) | Bytes[ReaderIndex + 3];
  676. ReaderIndex += 4;
  677. return x;
  678. }
  679. else if (h < 0xf8)
  680. {
  681. EnsureRead(5);
  682. uint xl = ((uint)Bytes[ReaderIndex + 1] << 24) | ((uint)(Bytes[ReaderIndex + 2] << 16)) | ((uint)Bytes[ReaderIndex + 3] << 8) | (Bytes[ReaderIndex + 4]);
  683. uint xh = h & 0x07;
  684. ReaderIndex += 5;
  685. return ((ulong)xh << 32) | xl;
  686. }
  687. else if (h < 0xfc)
  688. {
  689. EnsureRead(6);
  690. uint xl = ((uint)Bytes[ReaderIndex + 2] << 24) | ((uint)(Bytes[ReaderIndex + 3] << 16)) | ((uint)Bytes[ReaderIndex + 4] << 8) | (Bytes[ReaderIndex + 5]);
  691. uint xh = ((h & 0x03) << 8) | Bytes[ReaderIndex + 1];
  692. ReaderIndex += 6;
  693. return ((ulong)xh << 32) | xl;
  694. }
  695. else if (h < 0xfe)
  696. {
  697. EnsureRead(7);
  698. uint xl = ((uint)Bytes[ReaderIndex + 3] << 24) | ((uint)(Bytes[ReaderIndex + 4] << 16)) | ((uint)Bytes[ReaderIndex + 5] << 8) | (Bytes[ReaderIndex + 6]);
  699. uint xh = ((h & 0x01) << 16) | ((uint)Bytes[ReaderIndex + 1] << 8) | Bytes[ReaderIndex + 2];
  700. ReaderIndex += 7;
  701. return ((ulong)xh << 32) | xl;
  702. }
  703. else if (h < 0xff)
  704. {
  705. EnsureRead(8);
  706. uint xl = ((uint)Bytes[ReaderIndex + 4] << 24) | ((uint)(Bytes[ReaderIndex + 5] << 16)) | ((uint)Bytes[ReaderIndex + 6] << 8) | (Bytes[ReaderIndex + 7]);
  707. uint xh = /*((h & 0x01) << 24) |*/ ((uint)Bytes[ReaderIndex + 1] << 16) | ((uint)Bytes[ReaderIndex + 2] << 8) | Bytes[ReaderIndex + 3];
  708. ReaderIndex += 8;
  709. return ((ulong)xh << 32) | xl;
  710. }
  711. else
  712. {
  713. EnsureRead(9);
  714. uint xl = ((uint)Bytes[ReaderIndex + 5] << 24) | ((uint)(Bytes[ReaderIndex + 6] << 16)) | ((uint)Bytes[ReaderIndex + 7] << 8) | (Bytes[ReaderIndex + 8]);
  715. uint xh = ((uint)Bytes[ReaderIndex + 1] << 24) | ((uint)Bytes[ReaderIndex + 2] << 16) | ((uint)Bytes[ReaderIndex + 3] << 8) | Bytes[ReaderIndex + 4];
  716. ReaderIndex += 9;
  717. return ((ulong)xh << 32) | xl;
  718. }
  719. }
  720. public void WriteFlong(long x)
  721. {
  722. EnsureWrite(8);
  723. #if CPU_SUPPORT_MEMORY_NOT_ALIGN
  724. unsafe
  725. {
  726. fixed (byte* b = &Bytes[WriterIndex])
  727. {
  728. *(long*)b = x;
  729. }
  730. }
  731. #else
  732. Bytes[WriterIndex] = (byte)x;
  733. Bytes[WriterIndex + 1] = (byte)(x >> 8);
  734. Bytes[WriterIndex + 2] = (byte)(x >> 16);
  735. Bytes[WriterIndex + 3] = (byte)(x >> 24);
  736. Bytes[WriterIndex + 4] = (byte)(x >> 32);
  737. Bytes[WriterIndex + 5] = (byte)(x >> 40);
  738. Bytes[WriterIndex + 6] = (byte)(x >> 48);
  739. Bytes[WriterIndex + 7] = (byte)(x >> 56);
  740. #endif
  741. WriterIndex += 8;
  742. }
  743. public long ReadFlong()
  744. {
  745. EnsureRead(8);
  746. long x;
  747. #if CPU_SUPPORT_MEMORY_NOT_ALIGN
  748. unsafe
  749. {
  750. fixed (byte* b = &Bytes[ReaderIndex])
  751. {
  752. x = *(long*)b;
  753. }
  754. }
  755. #else
  756. int xl = (Bytes[ReaderIndex + 3] << 24) | ((Bytes[ReaderIndex + 2] << 16)) | (Bytes[ReaderIndex + 1] << 8) | (Bytes[ReaderIndex]);
  757. int xh = (Bytes[ReaderIndex + 7] << 24) | (Bytes[ReaderIndex + 6] << 16) | (Bytes[ReaderIndex + 5] << 8) | Bytes[ReaderIndex + 4];
  758. x = ((long)xh << 32) | (long)xl;
  759. #endif
  760. ReaderIndex += 8;
  761. return x;
  762. }
  763. private static unsafe void Copy8(byte* dst, byte* src)
  764. {
  765. dst[0] = src[0];
  766. dst[1] = src[1];
  767. dst[2] = src[2];
  768. dst[3] = src[3];
  769. dst[4] = src[4];
  770. dst[5] = src[5];
  771. dst[6] = src[6];
  772. dst[7] = src[7];
  773. }
  774. private static unsafe void Copy4(byte* dst, byte* src)
  775. {
  776. dst[0] = src[0];
  777. dst[1] = src[1];
  778. dst[2] = src[2];
  779. dst[3] = src[3];
  780. }
  781. //const bool isLittleEndian = true;
  782. public void WriteFloat(float x)
  783. {
  784. EnsureWrite(4);
  785. unsafe
  786. {
  787. fixed (byte* b = &Bytes[WriterIndex])
  788. {
  789. #if !CPU_SUPPORT_MEMORY_NOT_ALIGN
  790. if ((long)b % 4 == 0)
  791. {
  792. *(float*)b = x;
  793. }
  794. else
  795. {
  796. Copy4(b, (byte*)&x);
  797. }
  798. #else
  799. *(float*)b = x;
  800. #endif
  801. }
  802. }
  803. //if (!BitConverter.IsLittleEndian)
  804. //{
  805. // Array.Reverse(data, endPos, 4);
  806. //}
  807. WriterIndex += 4;
  808. }
  809. public float ReadFloat()
  810. {
  811. EnsureRead(4);
  812. //if (!BitConverter.IsLittleEndian)
  813. //{
  814. // Array.Reverse(data, beginPos, 4);
  815. //}
  816. float x;
  817. unsafe
  818. {
  819. fixed (byte* b = &Bytes[ReaderIndex])
  820. {
  821. #if !CPU_SUPPORT_MEMORY_NOT_ALIGN
  822. if ((long)b % 4 == 0)
  823. {
  824. x = *(float*)b;
  825. }
  826. else
  827. {
  828. *((int*)&x) = (b[0]) | (b[1] << 8) | (b[2] << 16) | (b[3] << 24);
  829. }
  830. #else
  831. x = *(float*)b;
  832. #endif
  833. }
  834. }
  835. ReaderIndex += 4;
  836. return x;
  837. }
  838. public void WriteDouble(double x)
  839. {
  840. EnsureWrite(8);
  841. unsafe
  842. {
  843. fixed (byte* b = &Bytes[WriterIndex])
  844. {
  845. #if !CPU_SUPPORT_MEMORY_NOT_ALIGN
  846. if ((long)b % 8 == 0)
  847. {
  848. *(double*)b = x;
  849. }
  850. else
  851. {
  852. Copy8(b, (byte*)&x);
  853. }
  854. #else
  855. *(double*)b = x;
  856. #endif
  857. }
  858. //if (!BitConverter.IsLittleEndian)
  859. //{
  860. // Array.Reverse(data, endPos, 8);
  861. //}
  862. }
  863. WriterIndex += 8;
  864. }
  865. public double ReadDouble()
  866. {
  867. EnsureRead(8);
  868. //if (!BitConverter.IsLittleEndian)
  869. //{
  870. // Array.Reverse(data, beginPos, 8);
  871. //}
  872. double x;
  873. unsafe
  874. {
  875. fixed (byte* b = &Bytes[ReaderIndex])
  876. {
  877. #if !CPU_SUPPORT_MEMORY_NOT_ALIGN
  878. if ((long)b % 8 == 0)
  879. {
  880. x = *(double*)b;
  881. }
  882. else
  883. {
  884. int low = (b[0]) | (b[1] << 8) | (b[2] << 16) | (b[3] << 24);
  885. int high = (b[4]) | (b[5] << 8) | (b[6] << 16) | (b[7] << 24);
  886. *((long*)&x) = ((long)high << 32) | (uint)low;
  887. }
  888. #else
  889. x = *(double*)b;
  890. #endif
  891. }
  892. }
  893. ReaderIndex += 8;
  894. return x;
  895. }
  896. public void WriteSize(int n)
  897. {
  898. WriteUint((uint)n);
  899. }
  900. public int ReadSize()
  901. {
  902. return (int)ReadUint();
  903. }
  904. // marshal int
  905. // n -> (n << 1) ^ (n >> 31)
  906. // Read
  907. // (x >>> 1) ^ ((x << 31) >> 31)
  908. // (x >>> 1) ^ -(n&1)
  909. public void WriteSint(int x)
  910. {
  911. WriteUint(((uint)x << 1) ^ ((uint)x >> 31));
  912. }
  913. public int ReadSint()
  914. {
  915. uint x = ReadUint();
  916. return (int)((x >> 1) ^ ((x & 1) << 31));
  917. }
  918. // marshal long
  919. // n -> (n << 1) ^ (n >> 63)
  920. // Read
  921. // (x >>> 1) ^((x << 63) >> 63)
  922. // (x >>> 1) ^ -(n&1L)
  923. public void WriteSlong(long x)
  924. {
  925. WriteUlong(((ulong)x << 1) ^ ((ulong)x >> 63));
  926. }
  927. public long ReadSlong()
  928. {
  929. long x = ReadLong();
  930. return ((long)((ulong)x >> 1) ^ ((x & 1) << 63));
  931. }
  932. public void WriteString(string x)
  933. {
  934. var n = x != null ? Encoding.UTF8.GetByteCount(x) : 0;
  935. WriteSize(n);
  936. if (n > 0)
  937. {
  938. EnsureWrite(n);
  939. Encoding.UTF8.GetBytes(x, 0, x.Length, Bytes, WriterIndex);
  940. WriterIndex += n;
  941. }
  942. }
  943. // byte[], [start, end)
  944. [StaticField]
  945. public static Func<byte[], int, int, string> StringCacheFinder { get; set; }
  946. public string ReadString()
  947. {
  948. var n = ReadSize();
  949. if (n > 0)
  950. {
  951. EnsureRead(n);
  952. string s;
  953. if (StringCacheFinder == null)
  954. {
  955. s = Encoding.UTF8.GetString(Bytes, ReaderIndex, n);
  956. }
  957. else
  958. {
  959. // 只缓存比较小的字符串
  960. s = StringCacheFinder(Bytes, ReaderIndex, n);
  961. }
  962. ReaderIndex += n;
  963. return s;
  964. }
  965. else
  966. {
  967. return string.Empty;
  968. }
  969. }
  970. public void WriteBytes(byte[] x)
  971. {
  972. var n = x != null ? x.Length : 0;
  973. WriteSize(n);
  974. if (n > 0)
  975. {
  976. EnsureWrite(n);
  977. x.CopyTo(Bytes, WriterIndex);
  978. WriterIndex += n;
  979. }
  980. }
  981. public byte[] ReadBytes()
  982. {
  983. var n = ReadSize();
  984. if (n > 0)
  985. {
  986. EnsureRead(n);
  987. var x = new byte[n];
  988. Buffer.BlockCopy(Bytes, ReaderIndex, x, 0, n);
  989. ReaderIndex += n;
  990. return x;
  991. }
  992. else
  993. {
  994. return Array.Empty<byte>();
  995. }
  996. }
  997. // 以下是一些特殊类型
  998. public void WriteComplex(Complex x)
  999. {
  1000. WriteDouble(x.Real);
  1001. WriteDouble(x.Imaginary);
  1002. }
  1003. public Complex ReadComplex()
  1004. {
  1005. var x = ReadDouble();
  1006. var y = ReadDouble();
  1007. return new Complex(x, y);
  1008. }
  1009. public void WriteVector2(Vector2 x)
  1010. {
  1011. WriteFloat(x.X);
  1012. WriteFloat(x.Y);
  1013. }
  1014. public Vector2 ReadVector2()
  1015. {
  1016. float x = ReadFloat();
  1017. float y = ReadFloat();
  1018. return new Vector2(x, y);
  1019. }
  1020. public void WriteVector3(Vector3 x)
  1021. {
  1022. WriteFloat(x.X);
  1023. WriteFloat(x.Y);
  1024. WriteFloat(x.Z);
  1025. }
  1026. public Vector3 ReadVector3()
  1027. {
  1028. float x = ReadFloat();
  1029. float y = ReadFloat();
  1030. float z = ReadFloat();
  1031. return new Vector3(x, y, z);
  1032. }
  1033. public void WriteVector4(Vector4 x)
  1034. {
  1035. WriteFloat(x.X);
  1036. WriteFloat(x.Y);
  1037. WriteFloat(x.Z);
  1038. WriteFloat(x.W);
  1039. }
  1040. public Vector4 ReadVector4()
  1041. {
  1042. float x = ReadFloat();
  1043. float y = ReadFloat();
  1044. float z = ReadFloat();
  1045. float w = ReadFloat();
  1046. return new Vector4(x, y, z, w);
  1047. }
  1048. public void WriteQuaternion(Quaternion x)
  1049. {
  1050. WriteFloat(x.X);
  1051. WriteFloat(x.Y);
  1052. WriteFloat(x.Z);
  1053. WriteFloat(x.W);
  1054. }
  1055. public Quaternion ReadQuaternion()
  1056. {
  1057. float x = ReadFloat();
  1058. float y = ReadFloat();
  1059. float z = ReadFloat();
  1060. float w = ReadFloat();
  1061. return new Quaternion(x, y, z, w);
  1062. }
  1063. public void WriteMatrix4x4(Matrix4x4 x)
  1064. {
  1065. WriteFloat(x.M11);
  1066. WriteFloat(x.M12);
  1067. WriteFloat(x.M13);
  1068. WriteFloat(x.M14);
  1069. WriteFloat(x.M21);
  1070. WriteFloat(x.M22);
  1071. WriteFloat(x.M23);
  1072. WriteFloat(x.M24);
  1073. WriteFloat(x.M31);
  1074. WriteFloat(x.M32);
  1075. WriteFloat(x.M33);
  1076. WriteFloat(x.M34);
  1077. WriteFloat(x.M41);
  1078. WriteFloat(x.M42);
  1079. WriteFloat(x.M43);
  1080. WriteFloat(x.M44);
  1081. }
  1082. public Matrix4x4 ReadMatrix4x4()
  1083. {
  1084. float m11 = ReadFloat();
  1085. float m12 = ReadFloat();
  1086. float m13 = ReadFloat();
  1087. float m14 = ReadFloat();
  1088. float m21 = ReadFloat();
  1089. float m22 = ReadFloat();
  1090. float m23 = ReadFloat();
  1091. float m24 = ReadFloat();
  1092. float m31 = ReadFloat();
  1093. float m32 = ReadFloat();
  1094. float m33 = ReadFloat();
  1095. float m34 = ReadFloat();
  1096. float m41 = ReadFloat();
  1097. float m42 = ReadFloat();
  1098. float m43 = ReadFloat();
  1099. float m44 = ReadFloat();
  1100. return new Matrix4x4(m11, m12, m13, m14,
  1101. m21, m22, m23, m24,
  1102. m31, m32, m33, m34,
  1103. m41, m42, m43, m44);
  1104. }
  1105. internal void SkipBytes()
  1106. {
  1107. int n = ReadSize();
  1108. EnsureRead(n);
  1109. ReaderIndex += n;
  1110. }
  1111. public void WriteByteBufWithSize(ByteBuf o)
  1112. {
  1113. int n = o.Size;
  1114. if (n > 0)
  1115. {
  1116. WriteSize(n);
  1117. WriteBytesWithoutSize(o.Bytes, o.ReaderIndex, n);
  1118. }
  1119. else
  1120. {
  1121. WriteByte(0);
  1122. }
  1123. }
  1124. public void WriteByteBufWithoutSize(ByteBuf o)
  1125. {
  1126. int n = o.Size;
  1127. if (n > 0)
  1128. {
  1129. WriteBytesWithoutSize(o.Bytes, o.ReaderIndex, n);
  1130. }
  1131. }
  1132. public bool TryReadByte(out byte x)
  1133. {
  1134. if (CanRead(1))
  1135. {
  1136. x = Bytes[ReaderIndex++];
  1137. return true;
  1138. }
  1139. else
  1140. {
  1141. x = 0;
  1142. return false;
  1143. }
  1144. }
  1145. public EDeserializeError TryDeserializeInplaceByteBuf(int maxSize, ByteBuf inplaceTempBody)
  1146. {
  1147. //if (!CanRead(1)) { return EDeserializeError.NOT_ENOUGH; }
  1148. int oldReadIndex = ReaderIndex;
  1149. bool commit = false;
  1150. try
  1151. {
  1152. int n;
  1153. int h = Bytes[ReaderIndex];
  1154. if (h < 0x80)
  1155. {
  1156. ReaderIndex++;
  1157. n = h;
  1158. }
  1159. else if (h < 0xc0)
  1160. {
  1161. if (!CanRead(2)) { return EDeserializeError.NOT_ENOUGH; }
  1162. n = ((h & 0x3f) << 8) | Bytes[ReaderIndex + 1];
  1163. ReaderIndex += 2;
  1164. }
  1165. else if (h < 0xe0)
  1166. {
  1167. if (!CanRead(3)) { return EDeserializeError.NOT_ENOUGH; }
  1168. n = ((h & 0x1f) << 16) | (Bytes[ReaderIndex + 1] << 8) | Bytes[ReaderIndex + 2];
  1169. ReaderIndex += 3;
  1170. }
  1171. else if (h < 0xf0)
  1172. {
  1173. if (!CanRead(4)) { return EDeserializeError.NOT_ENOUGH; }
  1174. n = ((h & 0x0f) << 24) | (Bytes[ReaderIndex + 1] << 16) | (Bytes[ReaderIndex + 2] << 8) | Bytes[ReaderIndex + 3];
  1175. ReaderIndex += 4;
  1176. }
  1177. else
  1178. {
  1179. return EDeserializeError.EXCEED_SIZE;
  1180. }
  1181. if (n > maxSize)
  1182. {
  1183. return EDeserializeError.EXCEED_SIZE;
  1184. }
  1185. if (Remaining < n)
  1186. {
  1187. return EDeserializeError.NOT_ENOUGH;
  1188. }
  1189. int inplaceReadIndex = ReaderIndex;
  1190. ReaderIndex += n;
  1191. inplaceTempBody.Replace(Bytes, inplaceReadIndex, ReaderIndex);
  1192. commit = true;
  1193. }
  1194. finally
  1195. {
  1196. if (!commit)
  1197. {
  1198. ReaderIndex = oldReadIndex;
  1199. }
  1200. }
  1201. return EDeserializeError.OK;
  1202. }
  1203. public void WriteRawTag(byte b1)
  1204. {
  1205. EnsureWrite(1);
  1206. Bytes[WriterIndex++] = b1;
  1207. }
  1208. public void WriteRawTag(byte b1, byte b2)
  1209. {
  1210. EnsureWrite(2);
  1211. Bytes[WriterIndex] = b1;
  1212. Bytes[WriterIndex + 1] = b2;
  1213. WriterIndex += 2;
  1214. }
  1215. public void WriteRawTag(byte b1, byte b2, byte b3)
  1216. {
  1217. EnsureWrite(3);
  1218. Bytes[WriterIndex] = b1;
  1219. Bytes[WriterIndex + 1] = b2;
  1220. Bytes[WriterIndex + 2] = b3;
  1221. WriterIndex += 3;
  1222. }
  1223. #region segment
  1224. public void BeginWriteSegment(out int oldSize)
  1225. {
  1226. oldSize = Size;
  1227. EnsureWrite(1);
  1228. WriterIndex += 1;
  1229. }
  1230. public void EndWriteSegment(int oldSize)
  1231. {
  1232. int startPos = ReaderIndex + oldSize;
  1233. int segmentSize = WriterIndex - startPos - 1;
  1234. // 0 111 1111
  1235. if (segmentSize < 0x80)
  1236. {
  1237. Bytes[startPos] = (byte)segmentSize;
  1238. }
  1239. else if (segmentSize < 0x4000) // 10 11 1111, -
  1240. {
  1241. EnsureWrite(1);
  1242. Bytes[WriterIndex] = Bytes[startPos + 1];
  1243. Bytes[startPos + 1] = (byte)segmentSize;
  1244. Bytes[startPos] = (byte)((segmentSize >> 8) | 0x80);
  1245. WriterIndex += 1;
  1246. }
  1247. else if (segmentSize < 0x200000) // 110 1 1111, -,-
  1248. {
  1249. EnsureWrite(2);
  1250. Bytes[WriterIndex + 1] = Bytes[startPos + 2];
  1251. Bytes[startPos + 2] = (byte)segmentSize;
  1252. Bytes[WriterIndex] = Bytes[startPos + 1];
  1253. Bytes[startPos + 1] = (byte)(segmentSize >> 8);
  1254. Bytes[startPos] = (byte)((segmentSize >> 16) | 0xc0);
  1255. WriterIndex += 2;
  1256. }
  1257. else if (segmentSize < 0x10000000) // 1110 1111,-,-,-
  1258. {
  1259. EnsureWrite(3);
  1260. Bytes[WriterIndex + 2] = Bytes[startPos + 3];
  1261. Bytes[startPos + 3] = (byte)segmentSize;
  1262. Bytes[WriterIndex + 1] = Bytes[startPos + 2];
  1263. Bytes[startPos + 2] = (byte)(segmentSize >> 8);
  1264. Bytes[WriterIndex] = Bytes[startPos + 1];
  1265. Bytes[startPos + 1] = (byte)(segmentSize >> 16);
  1266. Bytes[startPos] = (byte)((segmentSize >> 24) | 0xe0);
  1267. WriterIndex += 3;
  1268. }
  1269. else
  1270. {
  1271. throw new SerializationException("exceed max segment size");
  1272. }
  1273. }
  1274. public void ReadSegment(out int startIndex, out int segmentSize)
  1275. {
  1276. EnsureRead(1);
  1277. int h = Bytes[ReaderIndex++];
  1278. startIndex = ReaderIndex;
  1279. if (h < 0x80)
  1280. {
  1281. segmentSize = h;
  1282. ReaderIndex += segmentSize;
  1283. }
  1284. else if (h < 0xc0)
  1285. {
  1286. EnsureRead(1);
  1287. segmentSize = ((h & 0x3f) << 8) | Bytes[ReaderIndex];
  1288. int endPos = ReaderIndex + segmentSize;
  1289. Bytes[ReaderIndex] = Bytes[endPos];
  1290. ReaderIndex += segmentSize + 1;
  1291. }
  1292. else if (h < 0xe0)
  1293. {
  1294. EnsureRead(2);
  1295. segmentSize = ((h & 0x1f) << 16) | ((int)Bytes[ReaderIndex] << 8) | Bytes[ReaderIndex + 1];
  1296. int endPos = ReaderIndex + segmentSize;
  1297. Bytes[ReaderIndex] = Bytes[endPos];
  1298. Bytes[ReaderIndex + 1] = Bytes[endPos + 1];
  1299. ReaderIndex += segmentSize + 2;
  1300. }
  1301. else if (h < 0xf0)
  1302. {
  1303. EnsureRead(3);
  1304. segmentSize = ((h & 0x0f) << 24) | ((int)Bytes[ReaderIndex] << 16) | ((int)Bytes[ReaderIndex + 1] << 8) | Bytes[ReaderIndex + 2];
  1305. int endPos = ReaderIndex + segmentSize;
  1306. Bytes[ReaderIndex] = Bytes[endPos];
  1307. Bytes[ReaderIndex + 1] = Bytes[endPos + 1];
  1308. Bytes[ReaderIndex + 2] = Bytes[endPos + 2];
  1309. ReaderIndex += segmentSize + 3;
  1310. }
  1311. else
  1312. {
  1313. throw new SerializationException("exceed max size");
  1314. }
  1315. if (ReaderIndex > WriterIndex)
  1316. {
  1317. throw new SerializationException("segment data not enough");
  1318. }
  1319. }
  1320. public void ReadSegment(ByteBuf buf)
  1321. {
  1322. ReadSegment(out int startPos, out var size);
  1323. buf.Bytes = Bytes;
  1324. buf.ReaderIndex = startPos;
  1325. buf.WriterIndex = startPos + size;
  1326. }
  1327. public void EnterSegment(out SegmentSaveState saveState)
  1328. {
  1329. ReadSegment(out int startPos, out int size);
  1330. saveState = new SegmentSaveState(ReaderIndex, WriterIndex);
  1331. ReaderIndex = startPos;
  1332. WriterIndex = startPos + size;
  1333. }
  1334. public void LeaveSegment(SegmentSaveState saveState)
  1335. {
  1336. ReaderIndex = saveState.ReaderIndex;
  1337. WriterIndex = saveState.WriterIndex;
  1338. }
  1339. #endregion
  1340. public override string ToString()
  1341. {
  1342. string[] datas = new string[WriterIndex - ReaderIndex];
  1343. for (var i = ReaderIndex; i < WriterIndex; i++)
  1344. {
  1345. datas[i - ReaderIndex] = Bytes[i].ToString("X2");
  1346. }
  1347. return string.Join(".", datas);
  1348. }
  1349. public override bool Equals(object obj)
  1350. {
  1351. return (obj is ByteBuf other) && Equals(other);
  1352. }
  1353. public bool Equals(ByteBuf other)
  1354. {
  1355. if (other == null)
  1356. {
  1357. return false;
  1358. }
  1359. if (Size != other.Size)
  1360. {
  1361. return false;
  1362. }
  1363. for (int i = 0, n = Size; i < n; i++)
  1364. {
  1365. if (Bytes[ReaderIndex + i] != other.Bytes[other.ReaderIndex + i])
  1366. {
  1367. return false;
  1368. }
  1369. }
  1370. return true;
  1371. }
  1372. public object Clone()
  1373. {
  1374. return new ByteBuf(CopyData());
  1375. }
  1376. public static ByteBuf FromString(string value)
  1377. {
  1378. var ss = value.Split(',');
  1379. byte[] data = new byte[ss.Length];
  1380. for (int i = 0; i < data.Length; i++)
  1381. {
  1382. data[i] = byte.Parse(ss[i]);
  1383. }
  1384. return new ByteBuf(data);
  1385. }
  1386. public override int GetHashCode()
  1387. {
  1388. int hash = 17;
  1389. for (int i = ReaderIndex; i < WriterIndex; i++)
  1390. {
  1391. hash = hash * 23 + Bytes[i];
  1392. }
  1393. return hash;
  1394. }
  1395. public void Release()
  1396. {
  1397. _releaser?.Invoke(this);
  1398. }
  1399. #if SUPPORT_PUERTS_ARRAYBUF
  1400. // -- add for puerts
  1401. public Puerts.ArrayBuffer ReadArrayBuffer()
  1402. {
  1403. return new Puerts.ArrayBuffer(ReadBytes());
  1404. }
  1405. public void WriteArrayBuffer(Puerts.ArrayBuffer bytes)
  1406. {
  1407. WriteBytes(bytes.Bytes);
  1408. }
  1409. #endif
  1410. }
  1411. }