ByteBuf.cs 45 KB

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