BufferQueue.cs 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189
  1. using System;
  2. namespace IFramework.Net.KCP
  3. {
  4. public class BufferQueue : ICloneable
  5. {
  6. //字节缓存区
  7. private byte[] _buffer;
  8. //读取索引
  9. private int _read = 0;
  10. //写入索引
  11. private int _write = 0;
  12. //读取索引标记
  13. private int markReadIndex = 0;
  14. //写入索引标记
  15. private int markWirteIndex = 0;
  16. //缓存区字节数组的长度
  17. private int _capacity;
  18. private class Pool : ObjectPool<BufferQueue>
  19. {
  20. protected override BufferQueue CreateNew(IEventArgs arg)
  21. {
  22. return null;
  23. }
  24. }
  25. //对象池
  26. private static Pool pool =new Pool();
  27. //此对象是否池化
  28. private bool _pool = false;
  29. /// <summary>
  30. /// 构造方法
  31. /// </summary>
  32. /// <param name="capacity">初始容量</param>
  33. private BufferQueue(int capacity)
  34. {
  35. this._buffer = new byte[capacity];
  36. this._capacity = capacity;
  37. this._read = 0;
  38. this._write = 0;
  39. }
  40. /// <summary>
  41. /// 构造方法
  42. /// </summary>
  43. /// <param name="bytes">初始字节数组</param>
  44. private BufferQueue(byte[] bytes)
  45. {
  46. this._buffer = new byte[bytes.Length];
  47. Array.Copy(bytes, 0, _buffer, 0, _buffer.Length);
  48. this._capacity = _buffer.Length;
  49. this._read = 0;
  50. this._write = bytes.Length + 1;
  51. }
  52. /// <summary>
  53. /// 构建一个capacity长度的字节缓存区ByteBuffer对象
  54. /// </summary>
  55. /// <param name="capacity">初始容量</param>
  56. /// <param name="fromPool">
  57. /// true表示获取一个池化的ByteBuffer对象,池化的对象必须在调用Dispose后才会推入池中,此方法为线程安全的。
  58. /// 当为true时,从池中获取的对象的实际capacity值。
  59. /// </param>
  60. /// <returns>ByteBuffer对象</returns>
  61. public static BufferQueue Allocate(int capacity, bool fromPool = false)
  62. {
  63. if (!fromPool)
  64. {
  65. return new BufferQueue(capacity);
  66. }
  67. BufferQueue bbuf;
  68. if (pool.count == 0)
  69. {
  70. bbuf = new BufferQueue(capacity)
  71. {
  72. _pool = true
  73. };
  74. return bbuf;
  75. }
  76. bbuf = pool.Get();
  77. if (!bbuf._pool)
  78. {
  79. bbuf._pool = true;
  80. }
  81. return bbuf;
  82. }
  83. /// <summary>
  84. /// 构建一个以bytes为字节缓存区的ByteBuffer对象,一般不推荐使用
  85. /// </summary>
  86. /// <param name="bytes">初始字节数组</param>
  87. /// <param name="fromPool">
  88. /// true表示获取一个池化的ByteBuffer对象,池化的对象必须在调用Dispose后才会推入池中,此方法为线程安全的。
  89. /// </param>
  90. /// <returns>ByteBuffer对象</returns>
  91. public static BufferQueue Allocate(byte[] bytes, bool fromPool = false)
  92. {
  93. if (!fromPool)
  94. {
  95. return new BufferQueue(bytes);
  96. }
  97. BufferQueue bbuf;
  98. if (pool.count == 0)
  99. {
  100. bbuf = new BufferQueue(bytes)
  101. {
  102. _pool = true
  103. };
  104. return bbuf;
  105. }
  106. bbuf = pool.Get();
  107. bbuf.WriteBytes(bytes);
  108. if (!bbuf._pool)
  109. {
  110. bbuf._pool = true;
  111. }
  112. return bbuf;
  113. }
  114. /// <summary>
  115. /// 根据value,确定大于此length的最近的2次方数,如length=7,则返回值为8;length=12,则返回16
  116. /// </summary>
  117. /// <param name="value">参考容量</param>
  118. /// <returns>比参考容量大的最接近的2次方数</returns>
  119. private int FixLength(int value)
  120. {
  121. if (value == 0)
  122. {
  123. return 1;
  124. }
  125. value--;
  126. value |= value >> 1;
  127. value |= value >> 2;
  128. value |= value >> 4;
  129. value |= value >> 8;
  130. value |= value >> 16;
  131. return value + 1;
  132. //int n = 2;
  133. //int b = 2;
  134. //while (b < length)
  135. //{
  136. // b = 2 << n;
  137. // n++;
  138. //}
  139. //return b;
  140. }
  141. /// <summary>
  142. /// 翻转字节数组,如果本地字节序列为高字节序列,则进行翻转以转换为低字节序列
  143. /// </summary>
  144. /// <param name="bytes">待转为高字节序的字节数组</param>
  145. /// <returns>低字节序列的字节数组</returns>
  146. private byte[] Flip(byte[] bytes)
  147. {
  148. //if (BitConverter.IsLittleEndian)
  149. //{
  150. // Array.Reverse(bytes);
  151. //}
  152. return bytes;
  153. }
  154. /// <summary>
  155. /// 确定内部字节缓存数组的大小
  156. /// </summary>
  157. /// <param name="currLen">当前容量</param>
  158. /// <param name="futureLen">将来的容量</param>
  159. /// <returns>当前缓冲区的最大容量</returns>
  160. private int FixSizeAndReset(int currLen, int futureLen)
  161. {
  162. if (futureLen > currLen)
  163. {
  164. //以原大小的2次方数的两倍确定内部字节缓存区大小
  165. int size = FixLength(currLen) * 2;
  166. if (futureLen > size)
  167. {
  168. //以将来的大小的2次方的两倍确定内部字节缓存区大小
  169. size = FixLength(futureLen) * 2;
  170. }
  171. byte[] newbuf = new byte[size];
  172. Array.Copy(_buffer, 0, newbuf, 0, currLen);
  173. _buffer = newbuf;
  174. _capacity = size;
  175. }
  176. return futureLen;
  177. }
  178. /// <summary>
  179. /// 确保有这么多字节可以用来写入
  180. /// </summary>
  181. /// <param name="minBytes"></param>
  182. public void EnsureWritableBytes(int minBytes)
  183. {
  184. // 如果没有足够的空间进行写入了
  185. if (canWrite < minBytes)
  186. {
  187. // 优先整理空间
  188. if (reader >= minBytes)
  189. {
  190. // 整理出来可用空间
  191. TrimReadedBytes();
  192. }
  193. else
  194. {
  195. // 空间不足时,重新分配内存
  196. FixSizeAndReset(_buffer.Length, _buffer.Length + minBytes);
  197. }
  198. }
  199. }
  200. public void TrimReadedBytes()
  201. {
  202. Buffer.BlockCopy(_buffer, _read, _buffer, 0, _write - _read);
  203. _write -= _read;
  204. _read = 0;
  205. }
  206. /// <summary>
  207. /// 将bytes字节数组从startIndex开始的length字节写入到此缓存区
  208. /// </summary>
  209. /// <param name="bytes">待写入的字节数据</param>
  210. /// <param name="startIndex">写入的开始位置</param>
  211. /// <param name="length">写入的长度</param>
  212. public void WriteBytes(byte[] bytes, int startIndex, int length)
  213. {
  214. if (length <= 0 || startIndex < 0) return;
  215. int total = length + _write;
  216. int len = _buffer.Length;
  217. FixSizeAndReset(len, total);
  218. Array.Copy(bytes, startIndex, _buffer, _write, length);
  219. _write = total;
  220. }
  221. /// <summary>
  222. /// 将字节数组中从0到length的元素写入缓存区
  223. /// </summary>
  224. /// <param name="bytes">待写入的字节数据</param>
  225. /// <param name="length">写入的长度</param>
  226. public void WriteBytes(byte[] bytes, int length)
  227. {
  228. WriteBytes(bytes, 0, length);
  229. }
  230. /// <summary>
  231. /// 将字节数组全部写入缓存区
  232. /// </summary>
  233. /// <param name="bytes">待写入的字节数据</param>
  234. public void WriteBytes(byte[] bytes)
  235. {
  236. WriteBytes(bytes, bytes.Length);
  237. }
  238. /// <summary>
  239. /// 将一个ByteBuffer的有效字节区写入此缓存区中
  240. /// </summary>
  241. /// <param name="buffer">待写入的字节缓存区</param>
  242. public void Write(BufferQueue buffer)
  243. {
  244. if (buffer == null) return;
  245. if (buffer.canRead <= 0) return;
  246. WriteBytes(buffer.ToArray());
  247. }
  248. /// <summary>
  249. /// 写入一个int16数据
  250. /// </summary>
  251. /// <param name="value">short数据</param>
  252. public void WriteShort(short value)
  253. {
  254. WriteBytes(Flip(BitConverter.GetBytes(value)));
  255. }
  256. /// <summary>
  257. /// 写入一个uint16数据
  258. /// </summary>
  259. /// <param name="value">ushort数据</param>
  260. public void WriteUshort(ushort value)
  261. {
  262. WriteBytes(Flip(BitConverter.GetBytes(value)));
  263. }
  264. /// <summary>
  265. /// 写入一个int32数据
  266. /// </summary>
  267. /// <param name="value">int数据</param>
  268. public void WriteInt(int value)
  269. {
  270. //byte[] array = new byte[4];
  271. //for (int i = 3; i >= 0; i--)
  272. //{
  273. // array[i] = (byte)(value & 0xff);
  274. // value = value >> 8;
  275. //}
  276. //Array.Reverse(array);
  277. //Write(array);
  278. WriteBytes(Flip(BitConverter.GetBytes(value)));
  279. }
  280. /// <summary>
  281. /// 写入一个uint32数据
  282. /// </summary>
  283. /// <param name="value">uint数据</param>
  284. public void WriteUint(uint value)
  285. {
  286. WriteBytes(Flip(BitConverter.GetBytes(value)));
  287. }
  288. /// <summary>
  289. /// 写入一个int64数据
  290. /// </summary>
  291. /// <param name="value">long数据</param>
  292. public void WriteLong(long value)
  293. {
  294. WriteBytes(Flip(BitConverter.GetBytes(value)));
  295. }
  296. /// <summary>
  297. /// 写入一个uint64数据
  298. /// </summary>
  299. /// <param name="value">ulong数据</param>
  300. public void WriteUlong(ulong value)
  301. {
  302. WriteBytes(Flip(BitConverter.GetBytes(value)));
  303. }
  304. /// <summary>
  305. /// 写入一个float数据
  306. /// </summary>
  307. /// <param name="value">float数据</param>
  308. public void WriteFloat(float value)
  309. {
  310. WriteBytes(Flip(BitConverter.GetBytes(value)));
  311. }
  312. /// <summary>
  313. /// 写入一个byte数据
  314. /// </summary>
  315. /// <param name="value">byte数据</param>
  316. public void WriteByte(byte value)
  317. {
  318. int afterLen = _write + 1;
  319. int len = _buffer.Length;
  320. FixSizeAndReset(len, afterLen);
  321. _buffer[_write] = value;
  322. _write = afterLen;
  323. }
  324. /// <summary>
  325. /// 写入一个byte数据
  326. /// </summary>
  327. /// <param name="value">byte数据</param>
  328. public void WriteByte(int value)
  329. {
  330. byte b = (byte)value;
  331. WriteByte(b);
  332. }
  333. /// <summary>
  334. /// 写入一个double类型数据
  335. /// </summary>
  336. /// <param name="value">double数据</param>
  337. public void WriteDouble(double value)
  338. {
  339. WriteBytes(Flip(BitConverter.GetBytes(value)));
  340. }
  341. /// <summary>
  342. /// 写入一个字符
  343. /// </summary>
  344. /// <param name="value"></param>
  345. public void WriteChar(char value)
  346. {
  347. WriteBytes(Flip(BitConverter.GetBytes(value)));
  348. }
  349. /// <summary>
  350. /// 写入一个布尔型数据
  351. /// </summary>
  352. /// <param name="value"></param>
  353. public void WriteBoolean(bool value)
  354. {
  355. WriteBytes(Flip(BitConverter.GetBytes(value)));
  356. }
  357. /// <summary>
  358. /// 读取一个字节
  359. /// </summary>
  360. /// <returns>字节数据</returns>
  361. public byte ReadByte()
  362. {
  363. byte b = _buffer[_read];
  364. _read++;
  365. return b;
  366. }
  367. /// <summary>
  368. /// 获取从index索引处开始len长度的字节
  369. /// </summary>
  370. /// <param name="index"></param>
  371. /// <param name="len"></param>
  372. /// <returns></returns>
  373. private byte[] Get(int index, int len)
  374. {
  375. byte[] bytes = new byte[len];
  376. Array.Copy(_buffer, index, bytes, 0, len);
  377. return Flip(bytes);
  378. }
  379. /// <summary>
  380. /// 从读取索引位置开始读取len长度的字节数组
  381. /// </summary>
  382. /// <param name="len">待读取的字节长度</param>
  383. /// <returns>字节数组</returns>
  384. private byte[] Read(int len)
  385. {
  386. byte[] bytes = Get(_read, len);
  387. _read += len;
  388. return bytes;
  389. }
  390. /// <summary>
  391. /// 读取一个uint16数据
  392. /// </summary>
  393. /// <returns>ushort数据</returns>
  394. public ushort ReadUshort()
  395. {
  396. return BitConverter.ToUInt16(Read(2), 0);
  397. }
  398. /// <summary>
  399. /// 读取一个int16数据
  400. /// </summary>
  401. /// <returns>short数据</returns>
  402. public short ReadShort()
  403. {
  404. return BitConverter.ToInt16(Read(2), 0);
  405. }
  406. /// <summary>
  407. /// 读取一个uint32数据
  408. /// </summary>
  409. /// <returns>uint数据</returns>
  410. public uint ReadUint()
  411. {
  412. return BitConverter.ToUInt32(Read(4), 0);
  413. }
  414. /// <summary>
  415. /// 读取一个int32数据
  416. /// </summary>
  417. /// <returns>int数据</returns>
  418. public int ReadInt()
  419. {
  420. return BitConverter.ToInt32(Read(4), 0);
  421. }
  422. /// <summary>
  423. /// 读取一个uint64数据
  424. /// </summary>
  425. /// <returns>ulong数据</returns>
  426. public ulong ReadUlong()
  427. {
  428. return BitConverter.ToUInt64(Read(8), 0);
  429. }
  430. /// <summary>
  431. /// 读取一个long数据
  432. /// </summary>
  433. /// <returns>long数据</returns>
  434. public long ReadLong()
  435. {
  436. return BitConverter.ToInt64(Read(8), 0);
  437. }
  438. /// <summary>
  439. /// 读取一个float数据
  440. /// </summary>
  441. /// <returns>float数据</returns>
  442. public float ReadFloat()
  443. {
  444. return BitConverter.ToSingle(Read(4), 0);
  445. }
  446. /// <summary>
  447. /// 读取一个double数据
  448. /// </summary>
  449. /// <returns>double数据</returns>
  450. public double ReadDouble()
  451. {
  452. return BitConverter.ToDouble(Read(8), 0);
  453. }
  454. /// <summary>
  455. /// 读取一个字符
  456. /// </summary>
  457. /// <returns></returns>
  458. public char ReadChar()
  459. {
  460. return BitConverter.ToChar(Read(2), 0);
  461. }
  462. /// <summary>
  463. /// 读取布尔型数据
  464. /// </summary>
  465. /// <returns></returns>
  466. public bool ReadBoolean()
  467. {
  468. return BitConverter.ToBoolean(Read(1), 0);
  469. }
  470. /// <summary>
  471. /// 从读取索引位置开始读取len长度的字节到disbytes目标字节数组中
  472. /// </summary>
  473. /// <param name="disbytes">读取的字节将存入此字节数组</param>
  474. /// <param name="disstart">目标字节数组的写入索引</param>
  475. /// <param name="len">读取的长度</param>
  476. public void ReadBytes(byte[] disbytes, int disstart, int len)
  477. {
  478. int size = disstart + len;
  479. for (int i = disstart; i < size; i++)
  480. {
  481. disbytes[i] = this.ReadByte();
  482. }
  483. }
  484. public byte[] ReadBytes(int len)
  485. {
  486. return ReadBytes(_read, len);
  487. }
  488. public byte[] ReadBytes(int index, int len)
  489. {
  490. if (canRead < len)
  491. throw new Exception("no more readable bytes");
  492. var buffer = new byte[len];
  493. Array.Copy(_buffer, index, buffer, 0, len);
  494. _read += len;
  495. return buffer;
  496. }
  497. /// <summary>
  498. /// 获取一个字节
  499. /// </summary>
  500. /// <param name="index"></param>
  501. /// <returns></returns>
  502. public byte GetByte(int index)
  503. {
  504. return _buffer[index];
  505. }
  506. /// <summary>
  507. /// 获取一个字节
  508. /// </summary>
  509. /// <returns></returns>
  510. public byte GetByte()
  511. {
  512. return GetByte(_read);
  513. }
  514. /// <summary>
  515. /// 获取一个双精度浮点数据,不改变数据内容
  516. /// </summary>
  517. /// <param name="index">字节索引</param>
  518. /// <returns></returns>
  519. public double GetDouble(int index)
  520. {
  521. return BitConverter.ToDouble(Get(index, 8), 0);
  522. }
  523. /// <summary>
  524. /// 获取一个双精度浮点数据,不改变数据内容
  525. /// </summary>
  526. /// <returns></returns>
  527. public double GetDouble()
  528. {
  529. return GetDouble(_read);
  530. }
  531. /// <summary>
  532. /// 获取一个浮点数据,不改变数据内容
  533. /// </summary>
  534. /// <param name="index">字节索引</param>
  535. /// <returns></returns>
  536. public float GetFloat(int index)
  537. {
  538. return BitConverter.ToSingle(Get(index, 4), 0);
  539. }
  540. /// <summary>
  541. /// 获取一个浮点数据,不改变数据内容
  542. /// </summary>
  543. /// <returns></returns>
  544. public float GetFloat()
  545. {
  546. return GetFloat(_read);
  547. }
  548. /// <summary>
  549. /// 获取一个长整形数据,不改变数据内容
  550. /// </summary>
  551. /// <param name="index">字节索引</param>
  552. /// <returns></returns>
  553. public long GetLong(int index)
  554. {
  555. return BitConverter.ToInt64(Get(index, 8), 0);
  556. }
  557. /// <summary>
  558. /// 获取一个长整形数据,不改变数据内容
  559. /// </summary>
  560. /// <returns></returns>
  561. public long GetLong()
  562. {
  563. return GetLong(_read);
  564. }
  565. /// <summary>
  566. /// 获取一个长整形数据,不改变数据内容
  567. /// </summary>
  568. /// <param name="index">字节索引</param>
  569. /// <returns></returns>
  570. public ulong GetUlong(int index)
  571. {
  572. return BitConverter.ToUInt64(Get(index, 8), 0);
  573. }
  574. /// <summary>
  575. /// 获取一个长整形数据,不改变数据内容
  576. /// </summary>
  577. /// <returns></returns>
  578. public ulong GetUlong()
  579. {
  580. return GetUlong(_read);
  581. }
  582. /// <summary>
  583. /// 获取一个整形数据,不改变数据内容
  584. /// </summary>
  585. /// <param name="index">字节索引</param>
  586. /// <returns></returns>
  587. public int GetInt(int index)
  588. {
  589. return BitConverter.ToInt32(Get(index, 4), 0);
  590. }
  591. /// <summary>
  592. /// 获取一个整形数据,不改变数据内容
  593. /// </summary>
  594. /// <returns></returns>
  595. public int GetInt()
  596. {
  597. return GetInt(_read);
  598. }
  599. /// <summary>
  600. /// 获取一个整形数据,不改变数据内容
  601. /// </summary>
  602. /// <param name="index">字节索引</param>
  603. /// <returns></returns>
  604. public uint GetUint(int index)
  605. {
  606. return BitConverter.ToUInt32(Get(index, 4), 0);
  607. }
  608. /// <summary>
  609. /// 获取一个整形数据,不改变数据内容
  610. /// </summary>
  611. /// <returns></returns>
  612. public uint GetUint()
  613. {
  614. return GetUint(_read);
  615. }
  616. /// <summary>
  617. /// 获取一个短整形数据,不改变数据内容
  618. /// </summary>
  619. /// <param name="index">字节索引</param>
  620. /// <returns></returns>
  621. public int GetShort(int index)
  622. {
  623. return BitConverter.ToInt16(Get(index, 2), 0);
  624. }
  625. /// <summary>
  626. /// 获取一个短整形数据,不改变数据内容
  627. /// </summary>
  628. /// <returns></returns>
  629. public int GetShort()
  630. {
  631. return GetShort(_read);
  632. }
  633. /// <summary>
  634. /// 获取一个短整形数据,不改变数据内容
  635. /// </summary>
  636. /// <param name="index">字节索引</param>
  637. /// <returns></returns>
  638. public int GetUshort(int index)
  639. {
  640. return BitConverter.ToUInt16(Get(index, 2), 0);
  641. }
  642. /// <summary>
  643. /// 获取一个短整形数据,不改变数据内容
  644. /// </summary>
  645. /// <returns></returns>
  646. public int GetUshort()
  647. {
  648. return GetUshort(_read);
  649. }
  650. /// <summary>
  651. /// 获取一个char数据,不改变数据内容
  652. /// </summary>
  653. /// <param name="index">字节索引</param>
  654. /// <returns></returns>
  655. public char GetChar(int index)
  656. {
  657. return BitConverter.ToChar(Get(index, 2), 0);
  658. }
  659. /// <summary>
  660. /// 获取一个char数据,不改变数据内容
  661. /// </summary>
  662. /// <returns></returns>
  663. public char GetChar()
  664. {
  665. return GetChar(_read);
  666. }
  667. /// <summary>
  668. /// 获取一个布尔数据,不改变数据内容
  669. /// </summary>
  670. /// <param name="index">字节索引</param>
  671. /// <returns></returns>
  672. public bool GetBoolean(int index)
  673. {
  674. return BitConverter.ToBoolean(Get(index, 1), 0);
  675. }
  676. /// <summary>
  677. /// 获取一个布尔数据,不改变数据内容
  678. /// </summary>
  679. /// <returns></returns>
  680. public bool GetBoolean()
  681. {
  682. return GetBoolean(_read);
  683. }
  684. /// <summary>
  685. /// 清除已读字节并重建缓存区
  686. /// </summary>
  687. public void DiscardReadBytes()
  688. {
  689. if (_read <= 0) return;
  690. int len = _buffer.Length - _read;
  691. byte[] newbuf = new byte[len];
  692. Array.Copy(_buffer, _read, newbuf, 0, len);
  693. _buffer = newbuf;
  694. _write -= _read;
  695. markReadIndex -= _read;
  696. if (markReadIndex < 0)
  697. {
  698. //markReadIndex = readIndex;
  699. markReadIndex = 0;
  700. }
  701. markWirteIndex -= _read;
  702. if (markWirteIndex < 0 || markWirteIndex < _read || markWirteIndex < markReadIndex)
  703. {
  704. markWirteIndex = _write;
  705. }
  706. _read = 0;
  707. }
  708. /// <summary>
  709. /// 设置/获取读指针位置
  710. /// </summary>
  711. public int reader
  712. {
  713. get
  714. {
  715. return _read;
  716. }
  717. set
  718. {
  719. if (value < 0) return;
  720. _read = value;
  721. }
  722. }
  723. /// <summary>
  724. /// 设置/获取写指针位置
  725. /// </summary>
  726. public int writer
  727. {
  728. get
  729. {
  730. return _write;
  731. }
  732. set
  733. {
  734. if (value < 0) return;
  735. _write = value;
  736. }
  737. }
  738. /// <summary>
  739. /// 标记读取的索引位置
  740. /// </summary>
  741. public void MarkReaderIndex()
  742. {
  743. markReadIndex = _read;
  744. }
  745. /// <summary>
  746. /// 标记写入的索引位置
  747. /// </summary>
  748. public void MarkWriterIndex()
  749. {
  750. markWirteIndex = _write;
  751. }
  752. /// <summary>
  753. /// 将读取的索引位置重置为标记的读取索引位置
  754. /// </summary>
  755. public void ResetReaderIndex()
  756. {
  757. _read = markReadIndex;
  758. }
  759. /// <summary>
  760. /// 将写入的索引位置重置为标记的写入索引位置
  761. /// </summary>
  762. public void ResetWriterIndex()
  763. {
  764. _write = markWirteIndex;
  765. }
  766. /// <summary>
  767. /// 可读的有效字节数
  768. /// </summary>
  769. /// <returns>可读的字节数</returns>
  770. public int canRead
  771. {
  772. get
  773. {
  774. return _write - _read;
  775. }
  776. }
  777. /// <summary>
  778. /// 可写的剩余空间数
  779. /// </summary>
  780. /// <returns>可写的字节数</returns>
  781. public int canWrite
  782. {
  783. get
  784. {
  785. return _capacity - _write;
  786. }
  787. }
  788. /// <summary>
  789. /// 获取缓存区容量大小
  790. /// </summary>
  791. /// <returns>缓存区容量</returns>
  792. public int capacity
  793. {
  794. get
  795. {
  796. return this._capacity;
  797. }
  798. }
  799. public byte[] buffer
  800. {
  801. get
  802. {
  803. return _buffer;
  804. }
  805. }
  806. /// <summary>
  807. /// 获取可读的字节数组
  808. /// </summary>
  809. /// <returns>字节数据</returns>
  810. public byte[] ToArray()
  811. {
  812. byte[] bytes = new byte[_write - _read];
  813. Array.Copy(_buffer, _read, bytes, 0, bytes.Length);
  814. return bytes;
  815. }
  816. /// <summary>
  817. /// 简单的数据类型
  818. /// </summary>
  819. public enum DataType
  820. {
  821. //byte类型
  822. BYTE = 1,
  823. //short类型
  824. SHORT = 2,
  825. //int类型
  826. INT = 3,
  827. //long类型
  828. LONG = 4
  829. }
  830. /// <summary>
  831. /// 写入一个数据
  832. /// </summary>
  833. /// <param name="value">待写入的数据</param>
  834. /// <param name="type">待写入的数据类型</param>
  835. private void WriteValue(int value, DataType type)
  836. {
  837. switch (type)
  838. {
  839. case DataType.BYTE:
  840. this.WriteByte(value);
  841. break;
  842. case DataType.SHORT:
  843. this.WriteShort((short)value);
  844. break;
  845. case DataType.LONG:
  846. this.WriteLong((long)value);
  847. break;
  848. default:
  849. this.WriteInt(value);
  850. break;
  851. }
  852. }
  853. /// <summary>
  854. /// 读取一个值,值类型根据type决定,int或short或byte
  855. /// </summary>
  856. /// <param name="type">值类型</param>
  857. /// <returns>int数据</returns>
  858. private int ReadValue(DataType type)
  859. {
  860. switch (type)
  861. {
  862. case DataType.BYTE:
  863. return (int)ReadByte();
  864. case DataType.SHORT:
  865. return (int)ReadShort();
  866. case DataType.INT:
  867. return (int)ReadInt();
  868. case DataType.LONG:
  869. return (int)ReadLong();
  870. default:
  871. return -1;
  872. }
  873. }
  874. /// <summary>
  875. /// 写入可变长的UTF-8字符串
  876. /// <para>以长度类型(byte:1, short:2, int:3) + 长度(根据长度类型写入到字节缓冲区) + 字节数组表示一个字符串</para>
  877. /// </summary>
  878. /// <param name="content"></param>
  879. //public void WriteUTF8VarString(string content)
  880. //{
  881. // byte[] bytes = System.Text.Encoding.UTF8.GetBytes(content);
  882. // ValueType lenType;
  883. // if (bytes.Length <= byte.MaxValue)
  884. // {
  885. // lenType = ValueType.BYTE;
  886. // }
  887. // else if (bytes.Length <= short.MaxValue)
  888. // {
  889. // lenType = ValueType.SHORT;
  890. // }
  891. // else
  892. // {
  893. // lenType = ValueType.INT;
  894. // }
  895. // WriteByte((int)lenType);
  896. // if (lenType == ValueType.BYTE)
  897. // {
  898. // WriteByte(bytes.Length);
  899. // }
  900. // else if (lenType == ValueType.SHORT)
  901. // {
  902. // WriteShort((short)bytes.Length);
  903. // }
  904. // else
  905. // {
  906. // WriteInt(bytes.Length);
  907. // }
  908. // WriteBytes(bytes);
  909. //}
  910. /// <summary>
  911. /// 读取可变长的UTF-8字符串
  912. /// <para>以长度类型(byte:1, short:2, int:3) + 长度(根据长度类型从字节缓冲区读取) + 字节数组表示一个字符串</para>
  913. /// </summary>
  914. /// <returns></returns>
  915. //public string ReadUTF8VarString()
  916. //{
  917. // int lenTypeValue = ReadByte();
  918. // int len = 0;
  919. // if (lenTypeValue == (int)ValueType.BYTE)
  920. // {
  921. // len = ReadByte();
  922. // }
  923. // else if (lenTypeValue == (int)ValueType.SHORT)
  924. // {
  925. // len = ReadShort();
  926. // }
  927. // else if (lenTypeValue == (int)ValueType.INT)
  928. // {
  929. // len = ReadInt();
  930. // }
  931. // if (len > 0)
  932. // {
  933. // byte[] bytes = new byte[len];
  934. // ReadBytes(bytes, 0, len);
  935. // return System.Text.Encoding.UTF8.GetString(bytes);
  936. // }
  937. // return "";
  938. //}
  939. /// <summary>
  940. /// 写入一个UTF-8字符串,UTF-8字符串无高低字节序问题
  941. /// <para>写入缓冲区的结构为字符串字节长度(类型由lenType指定) + 字符串字节数组</para>
  942. /// </summary>
  943. /// <param name="content">待写入的字符串</param>
  944. /// <param name="lenType">写入的字符串长度类型</param>
  945. public void WriteUTF8String(string content, DataType lenType)
  946. {
  947. byte[] bytes = System.Text.Encoding.UTF8.GetBytes(content);
  948. int max;
  949. if (lenType == DataType.BYTE)
  950. {
  951. WriteByte(bytes.Length);
  952. max = byte.MaxValue;
  953. }
  954. else if (lenType == DataType.SHORT)
  955. {
  956. WriteShort((short)bytes.Length);
  957. max = short.MaxValue;
  958. }
  959. else
  960. {
  961. WriteInt(bytes.Length);
  962. max = int.MaxValue;
  963. }
  964. if (bytes.Length > max)
  965. {
  966. WriteBytes(bytes, 0, max);
  967. }
  968. else
  969. {
  970. WriteBytes(bytes, 0, bytes.Length);
  971. }
  972. }
  973. /// <summary>
  974. /// 写入以short表示的字符串字节长度和字符串字节数据
  975. /// </summary>
  976. /// <param name="content"></param>
  977. public void WriteUTF(string content)
  978. {
  979. this.WriteUTF8String(content, DataType.SHORT);
  980. }
  981. /// <summary>
  982. /// 读取一个UTF-8字符串,UTF-8字符串无高低字节序问题
  983. /// </summary>
  984. /// <param name="len">需读取的字符串长度</param>
  985. /// <returns>字符串</returns>
  986. public string ReadUTF8String(int len)
  987. {
  988. byte[] bytes = new byte[len];
  989. this.ReadBytes(bytes, 0, len);
  990. return System.Text.Encoding.UTF8.GetString(bytes);
  991. }
  992. /// <summary>
  993. /// 读取一个UTF-8字符串,UTF-8字符串无高低字节序问题
  994. /// </summary>
  995. /// <param name="lenType">字符串长度类型</param>
  996. /// <returns>字符串</returns>
  997. public string ReadUTF8String(DataType lenType)
  998. {
  999. int len = ReadValue(lenType);
  1000. return ReadUTF8String(len);
  1001. }
  1002. /// <summary>
  1003. /// 读取short类型的字符串字节长度,然后根据此长度读取对应数量的字节数据后转为字符串
  1004. /// </summary>
  1005. /// <returns>UTF-8字符串</returns>
  1006. public string ReadUTF()
  1007. {
  1008. return this.ReadUTF8String(DataType.SHORT);
  1009. }
  1010. /// <summary>
  1011. /// 复制一个对象,具有与原对象相同的数据,不改变原对象的数据,不包括已读数据
  1012. /// </summary>
  1013. /// <returns></returns>
  1014. public BufferQueue Copy()
  1015. {
  1016. if (_buffer == null)
  1017. {
  1018. return new BufferQueue(16);
  1019. }
  1020. if (_read < _write)
  1021. {
  1022. byte[] newbytes = new byte[_write - _read];
  1023. Array.Copy(_buffer, _read, newbytes, 0, newbytes.Length);
  1024. BufferQueue buffer = new BufferQueue(newbytes.Length);
  1025. buffer.WriteBytes(newbytes);
  1026. buffer._pool = this._pool;
  1027. return buffer;
  1028. }
  1029. return new BufferQueue(16);
  1030. }
  1031. /// <summary>
  1032. /// 深度复制,具有与原对象相同的数据,不改变原对象的数据,包括已读数据
  1033. /// </summary>
  1034. /// <returns></returns>
  1035. public object Clone()
  1036. {
  1037. if (_buffer == null)
  1038. {
  1039. return new BufferQueue(16);
  1040. }
  1041. BufferQueue newBuf = new BufferQueue(_buffer)
  1042. {
  1043. _capacity = this._capacity,
  1044. _read = this._read,
  1045. _write = this._write,
  1046. markReadIndex = this.markReadIndex,
  1047. markWirteIndex = this.markWirteIndex,
  1048. _pool = this._pool
  1049. };
  1050. return newBuf;
  1051. }
  1052. /// <summary>
  1053. /// 遍历所有的字节数据
  1054. /// </summary>
  1055. /// <param name="action"></param>
  1056. public void ForEach(Action<byte> action)
  1057. {
  1058. for (int i = 0; i < this.canRead; i++)
  1059. {
  1060. action.Invoke(this._buffer[i]);
  1061. }
  1062. }
  1063. /// <summary>
  1064. /// 清空此对象,但保留字节缓存数组(空数组)
  1065. /// </summary>
  1066. public void Clear()
  1067. {
  1068. _read = 0;
  1069. _write = 0;
  1070. markReadIndex = 0;
  1071. markWirteIndex = 0;
  1072. _capacity = _buffer.Length;
  1073. }
  1074. /// <summary>
  1075. /// 释放对象,清除字节缓存数组,如果此对象为可池化,那么调用此方法将会把此对象推入到池中等待下次调用
  1076. /// </summary>
  1077. public void Dispose()
  1078. {
  1079. if (_pool)
  1080. {
  1081. this.Clear();
  1082. pool.Set(this);
  1083. }
  1084. _read = 0;
  1085. _write = 0;
  1086. markReadIndex = 0;
  1087. markWirteIndex = 0;
  1088. _capacity = 0;
  1089. _buffer = null;
  1090. }
  1091. }
  1092. }