SoftBuffer.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using MECF.Framework.RT.Core.IoProviders.Common.Net;
  7. using MECF.Framework.RT.Core.ThreadLock;
  8. namespace MECF.Framework.RT.Core.IoProviders.Common.Transfer
  9. {
  10. /// <summary>
  11. /// 一个线程安全的缓存数据块,支持批量动态修改,添加,并获取快照
  12. /// </summary>
  13. /// <remarks>
  14. /// 这个类可以实现什么功能呢,就是你有一个大的数组,作为你的应用程序的中间数据池,允许你往byte[]数组里存放指定长度的子byte[]数组,也允许从里面拿数据,
  15. /// 这些操作都是线程安全的,当然,本类扩展了一些额外的方法支持,也可以直接赋值或获取基本的数据类型对象。
  16. /// </remarks>
  17. /// <example>
  18. /// 此处举例一些数据的读写说明,可以此处的数据示例。
  19. /// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\BasicFramework\SoftBufferExample.cs" region="SoftBufferExample1" title="SoftBuffer示例" />
  20. /// </example>
  21. public class SoftBuffer
  22. {
  23. #region Constructor
  24. /// <summary>
  25. /// 使用默认的大小初始化缓存空间
  26. /// </summary>
  27. public SoftBuffer()
  28. {
  29. buffer = new byte[capacity];
  30. hybirdLock = new SimpleHybirdLock();
  31. byteTransform = new RegularByteTransform();
  32. }
  33. /// <summary>
  34. /// 使用指定的容量初始化缓存数据块
  35. /// </summary>
  36. /// <param name="capacity">初始化的容量</param>
  37. public SoftBuffer(int capacity)
  38. {
  39. buffer = new byte[capacity];
  40. this.capacity = capacity;
  41. hybirdLock = new SimpleHybirdLock();
  42. byteTransform = new RegularByteTransform();
  43. }
  44. #endregion
  45. #region Bool Operate Support
  46. /// <summary>
  47. /// 设置指定的位置的数据块,如果超出,则丢弃数据
  48. /// </summary>
  49. /// <param name="value">bool值</param>
  50. /// <param name="destIndex">目标存储的索引</param>
  51. /// <exception cref="IndexOutOfRangeException"></exception>
  52. public void SetBool(bool value, int destIndex)
  53. {
  54. SetBool(new bool[] { value }, destIndex);
  55. }
  56. /// <summary>
  57. /// 设置指定的位置的数据块,如果超出,则丢弃数据
  58. /// </summary>
  59. /// <param name="value">bool数组值</param>
  60. /// <param name="destIndex">目标存储的索引</param>
  61. /// <exception cref="IndexOutOfRangeException"></exception>
  62. public void SetBool(bool[] value, int destIndex)
  63. {
  64. if (value != null)
  65. {
  66. try
  67. {
  68. hybirdLock.Enter();
  69. for (int i = 0; i < value.Length; i++)
  70. {
  71. int byteIndex = (destIndex + i) / 8;
  72. int offect = (destIndex + i) % 8;
  73. if (value[i])
  74. {
  75. buffer[byteIndex] = (byte)(buffer[byteIndex] | getOrByte(offect));
  76. }
  77. else
  78. {
  79. buffer[byteIndex] = (byte)(buffer[byteIndex] & getAndByte(offect));
  80. }
  81. }
  82. hybirdLock.Leave();
  83. }
  84. catch
  85. {
  86. hybirdLock.Leave();
  87. throw;
  88. }
  89. }
  90. }
  91. /// <summary>
  92. /// 获取指定的位置的bool值,如果超出,则引发异常
  93. /// </summary>
  94. /// <param name="destIndex">目标存储的索引</param>
  95. /// <returns>获取索引位置的bool数据值</returns>
  96. /// <exception cref="IndexOutOfRangeException"></exception>
  97. public bool GetBool(int destIndex)
  98. {
  99. return GetBool(destIndex, 1)[0];
  100. }
  101. /// <summary>
  102. /// 获取指定位置的bool数组值,如果超过,则引发异常
  103. /// </summary>
  104. /// <param name="destIndex">目标存储的索引</param>
  105. /// <param name="length">读取的数组长度</param>
  106. /// <exception cref="IndexOutOfRangeException"></exception>
  107. /// <returns>bool数组值</returns>
  108. public bool[] GetBool(int destIndex, int length)
  109. {
  110. bool[] result = new bool[length];
  111. try
  112. {
  113. hybirdLock.Enter();
  114. for (int i = 0; i < length; i++)
  115. {
  116. int byteIndex = (destIndex + i) / 8;
  117. int offect = (destIndex + i) % 8;
  118. result[i] = (buffer[byteIndex] & getOrByte(offect)) == getOrByte(offect);
  119. }
  120. hybirdLock.Leave();
  121. }
  122. catch
  123. {
  124. hybirdLock.Leave();
  125. throw;
  126. }
  127. return result;
  128. }
  129. private byte getAndByte(int offect)
  130. {
  131. switch (offect)
  132. {
  133. case 0: return 0xFE;
  134. case 1: return 0xFD;
  135. case 2: return 0xFB;
  136. case 3: return 0xF7;
  137. case 4: return 0xEF;
  138. case 5: return 0xDF;
  139. case 6: return 0xBF;
  140. case 7: return 0x7F;
  141. default: return 0xFF;
  142. }
  143. }
  144. private byte getOrByte(int offect)
  145. {
  146. switch (offect)
  147. {
  148. case 0: return 0x01;
  149. case 1: return 0x02;
  150. case 2: return 0x04;
  151. case 3: return 0x08;
  152. case 4: return 0x10;
  153. case 5: return 0x20;
  154. case 6: return 0x40;
  155. case 7: return 0x80;
  156. default: return 0x00;
  157. }
  158. }
  159. #endregion
  160. #region Byte Operate Support
  161. /// <summary>
  162. /// 设置指定的位置的数据块,如果超出,则丢弃数据
  163. /// </summary>
  164. /// <param name="data">数据块信息</param>
  165. /// <param name="destIndex">目标存储的索引</param>
  166. public void SetBytes(byte[] data, int destIndex)
  167. {
  168. if (destIndex < capacity && destIndex >= 0 && data != null)
  169. {
  170. hybirdLock.Enter();
  171. if ((data.Length + destIndex) > buffer.Length)
  172. {
  173. Array.Copy(data, 0, buffer, destIndex, (buffer.Length - destIndex));
  174. }
  175. else
  176. {
  177. data.CopyTo(buffer, destIndex);
  178. }
  179. hybirdLock.Leave();
  180. }
  181. }
  182. /// <summary>
  183. /// 设置指定的位置的数据块,如果超出,则丢弃数据
  184. /// </summary>
  185. /// <param name="data">数据块信息</param>
  186. /// <param name="destIndex">目标存储的索引</param>
  187. /// <param name="length">准备拷贝的数据长度</param>
  188. public void SetBytes(byte[] data, int destIndex, int length)
  189. {
  190. if (destIndex < capacity && destIndex >= 0 && data != null)
  191. {
  192. if (length > data.Length) length = data.Length;
  193. hybirdLock.Enter();
  194. if ((length + destIndex) > buffer.Length)
  195. {
  196. Array.Copy(data, 0, buffer, destIndex, (buffer.Length - destIndex));
  197. }
  198. else
  199. {
  200. Array.Copy(data, 0, buffer, destIndex, length);
  201. }
  202. hybirdLock.Leave();
  203. }
  204. }
  205. /// <summary>
  206. /// 设置指定的位置的数据块,如果超出,则丢弃数据
  207. /// </summary>
  208. /// <param name="data">数据块信息</param>
  209. /// <param name="sourceIndex">Data中的起始位置</param>
  210. /// <param name="destIndex">目标存储的索引</param>
  211. /// <param name="length">准备拷贝的数据长度</param>
  212. /// <exception cref="IndexOutOfRangeException"></exception>
  213. public void SetBytes(byte[] data, int sourceIndex, int destIndex, int length)
  214. {
  215. if (destIndex < capacity && destIndex >= 0 && data != null)
  216. {
  217. if (length > data.Length) length = data.Length;
  218. hybirdLock.Enter();
  219. Array.Copy(data, sourceIndex, buffer, destIndex, length);
  220. hybirdLock.Leave();
  221. }
  222. }
  223. /// <summary>
  224. /// 获取内存指定长度的数据信息
  225. /// </summary>
  226. /// <param name="index">起始位置</param>
  227. /// <param name="length">数组长度</param>
  228. /// <returns>返回实际的数据信息</returns>
  229. public byte[] GetBytes(int index, int length)
  230. {
  231. byte[] result = new byte[length];
  232. if (length > 0)
  233. {
  234. hybirdLock.Enter();
  235. if (index >= 0 && (index + length) <= buffer.Length)
  236. {
  237. Array.Copy(buffer, index, result, 0, length);
  238. }
  239. hybirdLock.Leave();
  240. }
  241. return result;
  242. }
  243. /// <summary>
  244. /// 获取内存所有的数据信息
  245. /// </summary>
  246. /// <returns>实际的数据信息</returns>
  247. public byte[] GetBytes()
  248. {
  249. return GetBytes(0, capacity);
  250. }
  251. #endregion
  252. #region BCL Set Support
  253. /// <summary>
  254. /// 设置byte类型的数据到缓存区
  255. /// </summary>
  256. /// <param name="value">byte数值</param>
  257. /// <param name="index">索引位置</param>
  258. public void SetValue(byte value, int index)
  259. {
  260. SetBytes(new byte[] { value }, index);
  261. }
  262. /// <summary>
  263. /// 设置short类型的数据到缓存区
  264. /// </summary>
  265. /// <param name="values">short数组</param>
  266. /// <param name="index">索引位置</param>
  267. public void SetValue(short[] values, int index)
  268. {
  269. SetBytes(byteTransform.TransByte(values), index);
  270. }
  271. /// <summary>
  272. /// 设置short类型的数据到缓存区
  273. /// </summary>
  274. /// <param name="value">short数值</param>
  275. /// <param name="index">索引位置</param>
  276. public void SetValue(short value, int index)
  277. {
  278. SetValue(new short[] { value }, index);
  279. }
  280. /// <summary>
  281. /// 设置ushort类型的数据到缓存区
  282. /// </summary>
  283. /// <param name="values">ushort数组</param>
  284. /// <param name="index">索引位置</param>
  285. public void SetValue(ushort[] values, int index)
  286. {
  287. SetBytes(byteTransform.TransByte(values), index);
  288. }
  289. /// <summary>
  290. /// 设置ushort类型的数据到缓存区
  291. /// </summary>
  292. /// <param name="value">ushort数值</param>
  293. /// <param name="index">索引位置</param>
  294. public void SetValue(ushort value, int index)
  295. {
  296. SetValue(new ushort[] { value }, index);
  297. }
  298. /// <summary>
  299. /// 设置int类型的数据到缓存区
  300. /// </summary>
  301. /// <param name="values">int数组</param>
  302. /// <param name="index">索引位置</param>
  303. public void SetValue(int[] values, int index)
  304. {
  305. SetBytes(byteTransform.TransByte(values), index);
  306. }
  307. /// <summary>
  308. /// 设置int类型的数据到缓存区
  309. /// </summary>
  310. /// <param name="value">int数值</param>
  311. /// <param name="index">索引位置</param>
  312. public void SetValue(int value, int index)
  313. {
  314. SetValue(new int[] { value }, index);
  315. }
  316. /// <summary>
  317. /// 设置uint类型的数据到缓存区
  318. /// </summary>
  319. /// <param name="values">uint数组</param>
  320. /// <param name="index">索引位置</param>
  321. public void SetValue(uint[] values, int index)
  322. {
  323. SetBytes(byteTransform.TransByte(values), index);
  324. }
  325. /// <summary>
  326. /// 设置uint类型的数据到缓存区
  327. /// </summary>
  328. /// <param name="value">uint数值</param>
  329. /// <param name="index">索引位置</param>
  330. public void SetValue(uint value, int index)
  331. {
  332. SetValue(new uint[] { value }, index);
  333. }
  334. /// <summary>
  335. /// 设置float类型的数据到缓存区
  336. /// </summary>
  337. /// <param name="values">float数组</param>
  338. /// <param name="index">索引位置</param>
  339. public void SetValue(float[] values, int index)
  340. {
  341. SetBytes(byteTransform.TransByte(values), index);
  342. }
  343. /// <summary>
  344. /// 设置float类型的数据到缓存区
  345. /// </summary>
  346. /// <param name="value">float数值</param>
  347. /// <param name="index">索引位置</param>
  348. public void SetValue(float value, int index)
  349. {
  350. SetValue(new float[] { value }, index);
  351. }
  352. /// <summary>
  353. /// 设置long类型的数据到缓存区
  354. /// </summary>
  355. /// <param name="values">long数组</param>
  356. /// <param name="index">索引位置</param>
  357. public void SetValue(long[] values, int index)
  358. {
  359. SetBytes(byteTransform.TransByte(values), index);
  360. }
  361. /// <summary>
  362. /// 设置long类型的数据到缓存区
  363. /// </summary>
  364. /// <param name="value">long数值</param>
  365. /// <param name="index">索引位置</param>
  366. public void SetValue(long value, int index)
  367. {
  368. SetValue(new long[] { value }, index);
  369. }
  370. /// <summary>
  371. /// 设置ulong类型的数据到缓存区
  372. /// </summary>
  373. /// <param name="values">ulong数组</param>
  374. /// <param name="index">索引位置</param>
  375. public void SetValue(ulong[] values, int index)
  376. {
  377. SetBytes(byteTransform.TransByte(values), index);
  378. }
  379. /// <summary>
  380. /// 设置ulong类型的数据到缓存区
  381. /// </summary>
  382. /// <param name="value">ulong数值</param>
  383. /// <param name="index">索引位置</param>
  384. public void SetValue(ulong value, int index)
  385. {
  386. SetValue(new ulong[] { value }, index);
  387. }
  388. /// <summary>
  389. /// 设置double类型的数据到缓存区
  390. /// </summary>
  391. /// <param name="values">double数组</param>
  392. /// <param name="index">索引位置</param>
  393. public void SetValue(double[] values, int index)
  394. {
  395. SetBytes(byteTransform.TransByte(values), index);
  396. }
  397. /// <summary>
  398. /// 设置double类型的数据到缓存区
  399. /// </summary>
  400. /// <param name="value">double数值</param>
  401. /// <param name="index">索引位置</param>
  402. public void SetValue(double value, int index)
  403. {
  404. SetValue(new double[] { value }, index);
  405. }
  406. #endregion
  407. #region BCL Get Support
  408. /// <summary>
  409. /// 获取byte类型的数据
  410. /// </summary>
  411. /// <param name="index">索引位置</param>
  412. /// <returns>byte数值</returns>
  413. public byte GetByte(int index)
  414. {
  415. return GetBytes(index, 1)[0];
  416. }
  417. /// <summary>
  418. /// 获取short类型的数组到缓存区
  419. /// </summary>
  420. /// <param name="index">索引位置</param>
  421. /// <param name="length">数组长度</param>
  422. /// <returns>short数组</returns>
  423. public short[] GetInt16(int index, int length)
  424. {
  425. byte[] tmp = GetBytes(index, length * 2);
  426. return byteTransform.TransInt16(tmp, 0, length);
  427. }
  428. /// <summary>
  429. /// 获取short类型的数据到缓存区
  430. /// </summary>
  431. /// <param name="index">索引位置</param>
  432. /// <returns>short数据</returns>
  433. public short GetInt16(int index)
  434. {
  435. return GetInt16(index, 1)[0];
  436. }
  437. /// <summary>
  438. /// 获取ushort类型的数组到缓存区
  439. /// </summary>
  440. /// <param name="index">索引位置</param>
  441. /// <param name="length">数组长度</param>
  442. /// <returns>ushort数组</returns>
  443. public ushort[] GetUInt16(int index, int length)
  444. {
  445. byte[] tmp = GetBytes(index, length * 2);
  446. return byteTransform.TransUInt16(tmp, 0, length);
  447. }
  448. /// <summary>
  449. /// 获取ushort类型的数据到缓存区
  450. /// </summary>
  451. /// <param name="index">索引位置</param>
  452. /// <returns>ushort数据</returns>
  453. public ushort GetUInt16(int index)
  454. {
  455. return GetUInt16(index, 1)[0];
  456. }
  457. /// <summary>
  458. /// 获取int类型的数组到缓存区
  459. /// </summary>
  460. /// <param name="index">索引位置</param>
  461. /// <param name="length">数组长度</param>
  462. /// <returns>int数组</returns>
  463. public int[] GetInt32(int index, int length)
  464. {
  465. byte[] tmp = GetBytes(index, length * 4);
  466. return byteTransform.TransInt32(tmp, 0, length);
  467. }
  468. /// <summary>
  469. /// 获取int类型的数据到缓存区
  470. /// </summary>
  471. /// <param name="index">索引位置</param>
  472. /// <returns>int数据</returns>
  473. public int GetInt32(int index)
  474. {
  475. return GetInt32(index, 1)[0];
  476. }
  477. /// <summary>
  478. /// 获取uint类型的数组到缓存区
  479. /// </summary>
  480. /// <param name="index">索引位置</param>
  481. /// <param name="length">数组长度</param>
  482. /// <returns>uint数组</returns>
  483. public uint[] GetUInt32(int index, int length)
  484. {
  485. byte[] tmp = GetBytes(index, length * 4);
  486. return byteTransform.TransUInt32(tmp, 0, length);
  487. }
  488. /// <summary>
  489. /// 获取uint类型的数据到缓存区
  490. /// </summary>
  491. /// <param name="index">索引位置</param>
  492. /// <returns>uint数据</returns>
  493. public uint GetUInt32(int index)
  494. {
  495. return GetUInt32(index, 1)[0];
  496. }
  497. /// <summary>
  498. /// 获取float类型的数组到缓存区
  499. /// </summary>
  500. /// <param name="index">索引位置</param>
  501. /// <param name="length">数组长度</param>
  502. /// <returns>float数组</returns>
  503. public float[] GetSingle(int index, int length)
  504. {
  505. byte[] tmp = GetBytes(index, length * 4);
  506. return byteTransform.TransSingle(tmp, 0, length);
  507. }
  508. /// <summary>
  509. /// 获取float类型的数据到缓存区
  510. /// </summary>
  511. /// <param name="index">索引位置</param>
  512. /// <returns>float数据</returns>
  513. public float GetSingle(int index)
  514. {
  515. return GetSingle(index, 1)[0];
  516. }
  517. /// <summary>
  518. /// 获取long类型的数组到缓存区
  519. /// </summary>
  520. /// <param name="index">索引位置</param>
  521. /// <param name="length">数组长度</param>
  522. /// <returns>long数组</returns>
  523. public long[] GetInt64(int index, int length)
  524. {
  525. byte[] tmp = GetBytes(index, length * 8);
  526. return byteTransform.TransInt64(tmp, 0, length);
  527. }
  528. /// <summary>
  529. /// 获取long类型的数据到缓存区
  530. /// </summary>
  531. /// <param name="index">索引位置</param>
  532. /// <returns>long数据</returns>
  533. public long GetInt64(int index)
  534. {
  535. return GetInt64(index, 1)[0];
  536. }
  537. /// <summary>
  538. /// 获取ulong类型的数组到缓存区
  539. /// </summary>
  540. /// <param name="index">索引位置</param>
  541. /// <param name="length">数组长度</param>
  542. /// <returns>ulong数组</returns>
  543. public ulong[] GetUInt64(int index, int length)
  544. {
  545. byte[] tmp = GetBytes(index, length * 8);
  546. return byteTransform.TransUInt64(tmp, 0, length);
  547. }
  548. /// <summary>
  549. /// 获取ulong类型的数据到缓存区
  550. /// </summary>
  551. /// <param name="index">索引位置</param>
  552. /// <returns>ulong数据</returns>
  553. public ulong GetUInt64(int index)
  554. {
  555. return GetUInt64(index, 1)[0];
  556. }
  557. /// <summary>
  558. /// 获取double类型的数组到缓存区
  559. /// </summary>
  560. /// <param name="index">索引位置</param>
  561. /// <param name="length">数组长度</param>
  562. /// <returns>ulong数组</returns>
  563. public double[] GetDouble(int index, int length)
  564. {
  565. byte[] tmp = GetBytes(index, length * 8);
  566. return byteTransform.TransDouble(tmp, 0, length);
  567. }
  568. /// <summary>
  569. /// 获取double类型的数据到缓存区
  570. /// </summary>
  571. /// <param name="index">索引位置</param>
  572. /// <returns>double数据</returns>
  573. public double GetDouble(int index)
  574. {
  575. return GetDouble(index, 1)[0];
  576. }
  577. #endregion
  578. #region Customer Support
  579. /// <summary>
  580. /// 读取自定义类型的数据,需要规定解析规则
  581. /// </summary>
  582. /// <typeparam name="T">类型名称</typeparam>
  583. /// <param name="index">起始索引</param>
  584. /// <returns>自定义的数据类型</returns>
  585. public T GetCustomer<T>(int index) where T : IDataTransfer, new()
  586. {
  587. T Content = new T();
  588. byte[] read = GetBytes(index, Content.ReadCount);
  589. Content.ParseSource(read);
  590. return Content;
  591. }
  592. /// <summary>
  593. /// 写入自定义类型的数据到缓存中去,需要规定生成字节的方法
  594. /// </summary>
  595. /// <typeparam name="T">自定义类型</typeparam>
  596. /// <param name="data">实例对象</param>
  597. /// <param name="index">起始地址</param>
  598. public void SetCustomer<T>(T data, int index) where T : IDataTransfer, new()
  599. {
  600. SetBytes(data.ToSource(), index);
  601. }
  602. #endregion
  603. #region Public Properties
  604. /// <summary>
  605. /// 获取或设置当前的数据缓存类的解析规则
  606. /// </summary>
  607. public IByteTransform ByteTransform
  608. {
  609. get => byteTransform;
  610. set => byteTransform = value;
  611. }
  612. #endregion
  613. #region Private Member
  614. private int capacity = 10; // 缓存的容量
  615. private byte[] buffer; // 缓存的数据
  616. private SimpleHybirdLock hybirdLock; // 高效的混合锁
  617. private IByteTransform byteTransform; // 数据转换类
  618. #endregion
  619. }
  620. }