CommBuffer.cs 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625
  1. using System;
  2. using System.Net;
  3. using System.Diagnostics;
  4. using System.Runtime.InteropServices;
  5. public class CommWriteBuf
  6. {
  7. private byte[] beginPtr;
  8. private Int32 position;
  9. private Int32 length; //Buff的大小
  10. private void _set(ref byte[] ptr, Int32 len)
  11. {
  12. _set(ref ptr, len, 0);
  13. }
  14. private void _set(ref byte[] ptr, Int32 len, Int32 offset)
  15. {
  16. beginPtr = ptr;
  17. position = 0;
  18. length = 0;
  19. if (null != beginPtr)
  20. {
  21. length = len;
  22. position = offset;
  23. }
  24. }
  25. private void _reset()
  26. {
  27. position = 0;
  28. length = 0;
  29. beginPtr = null;
  30. }
  31. public CommWriteBuf()
  32. {
  33. beginPtr = null;
  34. position = 0;
  35. length = 0;
  36. }
  37. public CommWriteBuf(ref byte[] ptr, Int32 len)
  38. {
  39. _set(ref ptr, len);
  40. }
  41. public CommWriteBuf(ref byte[] ptr, Int32 len, Int32 offset)
  42. {
  43. _set(ref ptr, len, offset);
  44. }
  45. public CommWriteBuf(Int32 len)
  46. {
  47. beginPtr = new byte[len];
  48. position = 0;
  49. length = 0;
  50. if (null != beginPtr)
  51. {
  52. length = len;
  53. }
  54. }
  55. /* public function */
  56. public void reset()
  57. {
  58. _reset();
  59. }
  60. public void set(ref byte[] ptr, int len)
  61. {
  62. _set(ref ptr ,len);
  63. }
  64. public Int32 getUsedSize()
  65. {
  66. return position;
  67. }
  68. public Int32 getTotalSize()
  69. {
  70. return length;
  71. }
  72. public Int32 getLeftSize()
  73. {
  74. return (length - position);
  75. }
  76. public byte[] getBeginPtr()
  77. {
  78. return beginPtr;
  79. }
  80. public CommError.Type reserve(int gap)
  81. {
  82. if (position > length)
  83. {
  84. #if (DEBUG)
  85. StackTrace st = new StackTrace(true);
  86. for (int i = 0; i < st.FrameCount; i++)
  87. {
  88. if (null != st.GetFrame(i).GetFileName())
  89. {
  90. Console.WriteLine(st.GetFrame(i).ToString());
  91. }
  92. }
  93. #endif
  94. return CommError.Type.COMM_ERR_SHORT_BUF_FOR_WRITE;
  95. }
  96. if (gap > (length - position))
  97. {
  98. #if (DEBUG)
  99. StackTrace st = new StackTrace(true);
  100. for (int i = 0; i < st.FrameCount; i++)
  101. {
  102. if (null != st.GetFrame(i).GetFileName())
  103. {
  104. Console.WriteLine(st.GetFrame(i).ToString());
  105. }
  106. }
  107. #endif
  108. return CommError.Type.COMM_ERR_SHORT_BUF_FOR_WRITE;
  109. }
  110. position += gap;
  111. return CommError.Type.COMM_NO_ERROR;
  112. }
  113. /* write successively */
  114. public CommError.Type writeInt8(sbyte src)
  115. {
  116. return writeUInt8((byte)src);
  117. }
  118. public CommError.Type writeUInt8(byte src)
  119. {
  120. if(null == beginPtr)
  121. {
  122. #if (DEBUG)
  123. StackTrace st = new StackTrace(true);
  124. for (int i = 0; i < st.FrameCount; i++)
  125. {
  126. if (null != st.GetFrame(i).GetFileName())
  127. {
  128. Console.WriteLine(st.GetFrame(i).ToString());
  129. }
  130. }
  131. #endif
  132. return CommError.Type.COMM_ERR_ARG_IS_NULL;
  133. }
  134. if (sizeof(byte) > (length - position))
  135. {
  136. #if (DEBUG)
  137. StackTrace st = new StackTrace(true);
  138. for (int i = 0; i < st.FrameCount; i++)
  139. {
  140. if (null != st.GetFrame(i).GetFileName())
  141. {
  142. Console.WriteLine(st.GetFrame(i).ToString());
  143. }
  144. }
  145. #endif
  146. return CommError.Type.COMM_ERR_SHORT_BUF_FOR_WRITE;
  147. }
  148. beginPtr[position++] = src;
  149. return CommError.Type.COMM_NO_ERROR;
  150. }
  151. public CommError.Type writeUInt16(UInt16 src)
  152. {
  153. return writeInt16((Int16)src);
  154. }
  155. public CommError.Type writeInt16(Int16 src)
  156. {
  157. if(null == beginPtr)
  158. {
  159. #if (DEBUG)
  160. StackTrace st = new StackTrace(true);
  161. for (int i = 0; i < st.FrameCount; i++)
  162. {
  163. if (null != st.GetFrame(i).GetFileName())
  164. {
  165. Console.WriteLine(st.GetFrame(i).ToString());
  166. }
  167. }
  168. #endif
  169. return CommError.Type.COMM_ERR_ARG_IS_NULL;
  170. }
  171. if (sizeof(Int16) > (length - position))
  172. {
  173. #if (DEBUG)
  174. StackTrace st = new StackTrace(true);
  175. for (int i = 0; i < st.FrameCount; i++)
  176. {
  177. if (null != st.GetFrame(i).GetFileName())
  178. {
  179. Console.WriteLine(st.GetFrame(i).ToString());
  180. }
  181. }
  182. #endif
  183. return CommError.Type.COMM_ERR_SHORT_BUF_FOR_WRITE;
  184. }
  185. byte[] buffer = MiscUtil.Conversion.EndianBitConverter.Big.GetBytes(src);
  186. for (int i =0; i < buffer.GetLength(0); i++)
  187. {
  188. beginPtr[position++] = buffer[i];
  189. }
  190. return CommError.Type.COMM_NO_ERROR;
  191. }
  192. public CommError.Type writeUInt32(UInt32 src)
  193. {
  194. return writeInt32((Int32)src);
  195. }
  196. public CommError.Type writeInt32(Int32 src)
  197. {
  198. if(null == beginPtr)
  199. {
  200. #if (DEBUG)
  201. StackTrace st = new StackTrace(true);
  202. for (int i = 0; i < st.FrameCount; i++)
  203. {
  204. if (null != st.GetFrame(i).GetFileName())
  205. {
  206. Console.WriteLine(st.GetFrame(i).ToString());
  207. }
  208. }
  209. #endif
  210. return CommError.Type.COMM_ERR_ARG_IS_NULL;
  211. }
  212. if (sizeof(Int32) > (length - position))
  213. {
  214. #if (DEBUG)
  215. StackTrace st = new StackTrace(true);
  216. for (int i = 0; i < st.FrameCount; i++)
  217. {
  218. if (null != st.GetFrame(i).GetFileName())
  219. {
  220. Console.WriteLine(st.GetFrame(i).ToString());
  221. }
  222. }
  223. #endif
  224. return CommError.Type.COMM_ERR_SHORT_BUF_FOR_WRITE;
  225. }
  226. byte[] buffer = MiscUtil.Conversion.EndianBitConverter.Big.GetBytes(src);
  227. for (int i =0; i < buffer.GetLength(0); i++)
  228. {
  229. beginPtr[position++] = buffer[i];
  230. }
  231. return CommError.Type.COMM_NO_ERROR;
  232. }
  233. public CommError.Type writeUInt64(UInt64 src)
  234. {
  235. return writeInt64((Int64)src);
  236. }
  237. public CommError.Type writeInt64(Int64 src)
  238. {
  239. if(null == beginPtr)
  240. {
  241. #if (DEBUG)
  242. StackTrace st = new StackTrace(true);
  243. for (int i = 0; i < st.FrameCount; i++)
  244. {
  245. if (null != st.GetFrame(i).GetFileName())
  246. {
  247. Console.WriteLine(st.GetFrame(i).ToString());
  248. }
  249. }
  250. #endif
  251. return CommError.Type.COMM_ERR_ARG_IS_NULL;
  252. }
  253. if (sizeof(Int64) > (length - position))
  254. {
  255. #if (DEBUG)
  256. StackTrace st = new StackTrace(true);
  257. for (int i = 0; i < st.FrameCount; i++)
  258. {
  259. if (null != st.GetFrame(i).GetFileName())
  260. {
  261. Console.WriteLine(st.GetFrame(i).ToString());
  262. }
  263. }
  264. #endif
  265. return CommError.Type.COMM_ERR_SHORT_BUF_FOR_WRITE;
  266. }
  267. byte[] buffer = MiscUtil.Conversion.EndianBitConverter.Big.GetBytes(src);
  268. for (int i =0; i < buffer.GetLength(0); i++)
  269. {
  270. beginPtr[position++] = buffer[i];
  271. }
  272. return CommError.Type.COMM_NO_ERROR;
  273. }
  274. public CommError.Type writeFloat(float src)
  275. {
  276. Int32 tmp = MiscUtil.Conversion.EndianBitConverter.Big.ToInt32(MiscUtil.Conversion.EndianBitConverter.Big.GetBytes(src), 0);
  277. return writeInt32(tmp);
  278. }
  279. public CommError.Type writeDouble(double src)
  280. {
  281. Int64 tmp = BitConverter.DoubleToInt64Bits(src);
  282. return writeInt64(tmp);
  283. }
  284. public CommError.Type writeCString(byte [] src, Int32 count)
  285. {
  286. if(null == beginPtr)
  287. {
  288. #if (DEBUG)
  289. StackTrace st = new StackTrace(true);
  290. for (int i = 0; i < st.FrameCount; i++)
  291. {
  292. if (null != st.GetFrame(i).GetFileName())
  293. {
  294. Console.WriteLine(st.GetFrame(i).ToString());
  295. }
  296. }
  297. #endif
  298. return CommError.Type.COMM_ERR_ARG_IS_NULL;
  299. }
  300. if (count > (length - position))
  301. {
  302. #if (DEBUG)
  303. StackTrace st = new StackTrace(true);
  304. for (int i = 0; i < st.FrameCount; i++)
  305. {
  306. if (null != st.GetFrame(i).GetFileName())
  307. {
  308. Console.WriteLine(st.GetFrame(i).ToString());
  309. }
  310. }
  311. #endif
  312. return CommError.Type.COMM_ERR_SHORT_BUF_FOR_WRITE;
  313. }
  314. for (int i = 0; i < count; i++)
  315. {
  316. beginPtr[position++] = src[i];
  317. }
  318. return CommError.Type.COMM_NO_ERROR;
  319. }
  320. public CommError.Type writeWString(Int16 [] src, Int32 count)
  321. {
  322. if(null == beginPtr)
  323. {
  324. #if (DEBUG)
  325. StackTrace st = new StackTrace(true);
  326. for (int i = 0; i < st.FrameCount; i++)
  327. {
  328. if (null != st.GetFrame(i).GetFileName())
  329. {
  330. Console.WriteLine(st.GetFrame(i).ToString());
  331. }
  332. }
  333. #endif
  334. return CommError.Type.COMM_ERR_ARG_IS_NULL;
  335. }
  336. if ((2 * count) > (length - position))
  337. {
  338. return CommError.Type.COMM_ERR_SHORT_BUF_FOR_WRITE;
  339. }
  340. for (int i = 0; i < count; i++)
  341. {
  342. byte[] buffer = MiscUtil.Conversion.EndianBitConverter.Big.GetBytes(src[i]);
  343. for (int j = 0; j < buffer.GetLength(0); j++ )
  344. {
  345. beginPtr[position++] = buffer[j];
  346. }
  347. }
  348. return CommError.Type.COMM_NO_ERROR;
  349. }
  350. /* write directly */
  351. public CommError.Type writeInt8(sbyte src, int pos)
  352. {
  353. return writeUInt8((byte)src, pos);
  354. }
  355. public CommError.Type writeUInt8(byte src, int pos)
  356. {
  357. if(null == beginPtr)
  358. {
  359. #if (DEBUG)
  360. StackTrace st = new StackTrace(true);
  361. for (int i = 0; i < st.FrameCount; i++)
  362. {
  363. if (null != st.GetFrame(i).GetFileName())
  364. {
  365. Console.WriteLine(st.GetFrame(i).ToString());
  366. }
  367. }
  368. #endif
  369. return CommError.Type.COMM_ERR_ARG_IS_NULL;
  370. }
  371. if (sizeof(byte) > (length - pos))
  372. {
  373. #if (DEBUG)
  374. StackTrace st = new StackTrace(true);
  375. for (int i = 0; i < st.FrameCount; i++)
  376. {
  377. if (null != st.GetFrame(i).GetFileName())
  378. {
  379. Console.WriteLine(st.GetFrame(i).ToString());
  380. }
  381. }
  382. #endif
  383. return CommError.Type.COMM_ERR_SHORT_BUF_FOR_WRITE;
  384. }
  385. beginPtr[pos] = src;
  386. return CommError.Type.COMM_NO_ERROR;
  387. }
  388. public CommError.Type writeUInt16(UInt16 src, int pos)
  389. {
  390. return writeInt16((Int16)src, pos);
  391. }
  392. public CommError.Type writeInt16(Int16 src, int pos)
  393. {
  394. if(null == beginPtr)
  395. {
  396. #if (DEBUG)
  397. StackTrace st = new StackTrace(true);
  398. for (int i = 0; i < st.FrameCount; i++)
  399. {
  400. if (null != st.GetFrame(i).GetFileName())
  401. {
  402. Console.WriteLine(st.GetFrame(i).ToString());
  403. }
  404. }
  405. #endif
  406. return CommError.Type.COMM_ERR_ARG_IS_NULL;
  407. }
  408. if (sizeof(Int16) > (length - pos))
  409. {
  410. #if (DEBUG)
  411. StackTrace st = new StackTrace(true);
  412. for (int i = 0; i < st.FrameCount; i++)
  413. {
  414. if (null != st.GetFrame(i).GetFileName())
  415. {
  416. Console.WriteLine(st.GetFrame(i).ToString());
  417. }
  418. }
  419. #endif
  420. return CommError.Type.COMM_ERR_SHORT_BUF_FOR_WRITE;
  421. }
  422. byte[] buffer = MiscUtil.Conversion.EndianBitConverter.Big.GetBytes(src);
  423. for (int i =0; i < buffer.GetLength(0); i++)
  424. {
  425. beginPtr[pos + i] = buffer[i];
  426. }
  427. return CommError.Type.COMM_NO_ERROR;
  428. }
  429. public CommError.Type writeUInt32(UInt32 src, int pos)
  430. {
  431. return writeInt32((Int32)src, pos);
  432. }
  433. public CommError.Type writeInt32(Int32 src, int pos)
  434. {
  435. if(null == beginPtr)
  436. {
  437. #if (DEBUG)
  438. StackTrace st = new StackTrace(true);
  439. for (int i = 0; i < st.FrameCount; i++)
  440. {
  441. if (null != st.GetFrame(i).GetFileName())
  442. {
  443. Console.WriteLine(st.GetFrame(i).ToString());
  444. }
  445. }
  446. #endif
  447. return CommError.Type.COMM_ERR_ARG_IS_NULL;
  448. }
  449. if (sizeof(Int32) > (length - pos))
  450. {
  451. #if (DEBUG)
  452. StackTrace st = new StackTrace(true);
  453. for (int i = 0; i < st.FrameCount; i++)
  454. {
  455. if (null != st.GetFrame(i).GetFileName())
  456. {
  457. Console.WriteLine(st.GetFrame(i).ToString());
  458. }
  459. }
  460. #endif
  461. return CommError.Type.COMM_ERR_SHORT_BUF_FOR_WRITE;
  462. }
  463. byte[] buffer = MiscUtil.Conversion.EndianBitConverter.Big.GetBytes(src);
  464. for (int i =0; i < buffer.GetLength(0); i++)
  465. {
  466. beginPtr[pos + i] = buffer[i];
  467. }
  468. return CommError.Type.COMM_NO_ERROR;
  469. }
  470. public CommError.Type writeUInt64(UInt64 src, int pos)
  471. {
  472. return writeInt64((Int64)src, pos);
  473. }
  474. public CommError.Type writeInt64(Int64 src, int pos)
  475. {
  476. if(null == beginPtr)
  477. {
  478. #if (DEBUG)
  479. StackTrace st = new StackTrace(true);
  480. for (int i = 0; i < st.FrameCount; i++)
  481. {
  482. if (null != st.GetFrame(i).GetFileName())
  483. {
  484. Console.WriteLine(st.GetFrame(i).ToString());
  485. }
  486. }
  487. #endif
  488. return CommError.Type.COMM_ERR_ARG_IS_NULL;
  489. }
  490. if (sizeof(Int64) > (length - pos))
  491. {
  492. #if (DEBUG)
  493. StackTrace st = new StackTrace(true);
  494. for (int i = 0; i < st.FrameCount; i++)
  495. {
  496. if (null != st.GetFrame(i).GetFileName())
  497. {
  498. Console.WriteLine(st.GetFrame(i).ToString());
  499. }
  500. }
  501. #endif
  502. return CommError.Type.COMM_ERR_SHORT_BUF_FOR_WRITE;
  503. }
  504. byte[] buffer = MiscUtil.Conversion.EndianBitConverter.Big.GetBytes(src);
  505. for (int i =0; i < buffer.GetLength(0); i++)
  506. {
  507. beginPtr[pos + i] = buffer[i];
  508. }
  509. return CommError.Type.COMM_NO_ERROR;
  510. }
  511. public CommError.Type writeFloat(float src, int pos)
  512. {
  513. Int32 tmp = MiscUtil.Conversion.EndianBitConverter.Big.ToInt32(MiscUtil.Conversion.EndianBitConverter.Big.GetBytes(src), 0);
  514. return writeInt32(tmp, pos);
  515. }
  516. public CommError.Type writeDouble(double src, int pos)
  517. {
  518. Int64 tmp = BitConverter.DoubleToInt64Bits(src);
  519. return writeInt64(tmp,pos);
  520. }
  521. public CommError.Type writeByteArrar(byte[] src,Int32 len)
  522. {
  523. if (null == beginPtr || null == src)
  524. {
  525. #if (DEBUG)
  526. StackTrace st = new StackTrace(true);
  527. for (int i = 0; i < st.FrameCount; i++)
  528. {
  529. if (null != st.GetFrame(i).GetFileName())
  530. {
  531. Console.WriteLine(st.GetFrame(i).ToString());
  532. }
  533. }
  534. #endif
  535. return CommError.Type.COMM_ERR_ARG_IS_NULL;
  536. }
  537. if(len > getLeftSize())
  538. {
  539. #if (DEBUG)
  540. StackTrace st = new StackTrace(true);
  541. for (int i = 0; i < st.FrameCount; i++)
  542. {
  543. if (null != st.GetFrame(i).GetFileName())
  544. {
  545. Console.WriteLine(st.GetFrame(i).ToString());
  546. }
  547. }
  548. #endif
  549. return CommError.Type.COMM_ERR_SHORT_BUF_FOR_WRITE;
  550. }
  551. Array.Copy(src, 0, beginPtr, position, len);
  552. position += len;
  553. return CommError.Type.COMM_NO_ERROR;
  554. }
  555. public CommError.Type writeCString(byte [] src, Int32 count, Int32 pos)
  556. {
  557. if(null == beginPtr || count > src.GetLength(0))
  558. {
  559. #if (DEBUG)
  560. StackTrace st = new StackTrace(true);
  561. for (int i = 0; i < st.FrameCount; i++)
  562. {
  563. if (null != st.GetFrame(i).GetFileName())
  564. {
  565. Console.WriteLine(st.GetFrame(i).ToString());
  566. }
  567. }
  568. #endif
  569. return CommError.Type.COMM_ERR_ARG_IS_NULL;
  570. }
  571. if (count > (length - pos))
  572. {
  573. #if (DEBUG)
  574. StackTrace st = new StackTrace(true);
  575. for (int i = 0; i < st.FrameCount; i++)
  576. {
  577. if (null != st.GetFrame(i).GetFileName())
  578. {
  579. Console.WriteLine(st.GetFrame(i).ToString());
  580. }
  581. }
  582. #endif
  583. return CommError.Type.COMM_ERR_SHORT_BUF_FOR_WRITE;
  584. }
  585. for (int i = 0; i < count; i++)
  586. {
  587. beginPtr[pos + i] = src[i];
  588. }
  589. return CommError.Type.COMM_NO_ERROR;
  590. }
  591. public CommError.Type writeWString(Int16 [] src, Int32 count ,Int32 pos)
  592. {
  593. if(null == beginPtr || count > src.GetLength(0))
  594. {
  595. #if (DEBUG)
  596. StackTrace st = new StackTrace(true);
  597. for (int i = 0; i < st.FrameCount; i++)
  598. {
  599. if (null != st.GetFrame(i).GetFileName())
  600. {
  601. Console.WriteLine(st.GetFrame(i).ToString());
  602. }
  603. }
  604. #endif
  605. return CommError.Type.COMM_ERR_ARG_IS_NULL;
  606. }
  607. if ((2 * count) > (length - pos))
  608. {
  609. #if (DEBUG)
  610. StackTrace st = new StackTrace(true);
  611. for (int i = 0; i < st.FrameCount; i++)
  612. {
  613. if (null != st.GetFrame(i).GetFileName())
  614. {
  615. Console.WriteLine(st.GetFrame(i).ToString());
  616. }
  617. }
  618. #endif
  619. return CommError.Type.COMM_ERR_SHORT_BUF_FOR_WRITE;
  620. }
  621. for (int i = 0; i < count; i++)
  622. {
  623. byte[] buffer = MiscUtil.Conversion.EndianBitConverter.Big.GetBytes(src[i]);
  624. for (int j = 0; j < buffer.GetLength(0); j++)
  625. {
  626. beginPtr[pos + (2*i +j)] = buffer[j];
  627. }
  628. }
  629. return CommError.Type.COMM_NO_ERROR;
  630. }
  631. }
  632. public class CommReadBuf
  633. {
  634. private byte[] beginPtr;
  635. private Int32 position;
  636. private Int32 length;
  637. private bool IsNetEndian;
  638. public CommReadBuf()
  639. {
  640. length = 0;
  641. position = 0;
  642. beginPtr = null;
  643. IsNetEndian = true;
  644. }
  645. public CommReadBuf(ref CommWriteBuf CommWriteBuf)
  646. {
  647. byte[] ptr = CommWriteBuf.getBeginPtr();
  648. set(ref ptr, CommWriteBuf.getUsedSize());
  649. }
  650. public CommReadBuf(ref byte[] ptr , Int32 len)
  651. {
  652. set(ref ptr, len);
  653. }
  654. public CommReadBuf(ref byte[] ptr, Int32 len, Int32 offset)
  655. {
  656. set(ref ptr, len, offset);
  657. }
  658. public void reset()
  659. {
  660. length = 0;
  661. position = 0;
  662. beginPtr = null;
  663. IsNetEndian = true;
  664. }
  665. public void set(ref byte[] ptr, Int32 len)
  666. {
  667. set(ref ptr,len,0);
  668. }
  669. public void set(ref byte[] ptr, Int32 len,Int32 offset)
  670. {
  671. beginPtr = ptr;
  672. position = 0;
  673. length = 0;
  674. IsNetEndian = true;
  675. if (null != beginPtr)
  676. {
  677. position = offset;
  678. length = len;
  679. }
  680. }
  681. public Int32 getUsedSize()
  682. {
  683. return position;
  684. }
  685. public Int32 getTotalSize()
  686. {
  687. return length;
  688. }
  689. public Int32 getLeftSize()
  690. {
  691. return length - position;
  692. }
  693. public void disableEndian()
  694. {
  695. IsNetEndian = false;
  696. }
  697. /* read successively */
  698. public CommError.Type readInt8(ref sbyte dest)
  699. {
  700. CommError.Type ret = CommError.Type.COMM_NO_ERROR;
  701. byte tmp = 0;
  702. ret = readUInt8(ref tmp);
  703. dest = (sbyte)tmp;
  704. return ret;
  705. }
  706. public CommError.Type readUInt8(ref byte dest)
  707. {
  708. if(null == beginPtr)
  709. {
  710. #if (DEBUG)
  711. StackTrace st = new StackTrace(true);
  712. for (int i = 0; i < st.FrameCount; i++)
  713. {
  714. if (null != st.GetFrame(i).GetFileName())
  715. {
  716. Console.WriteLine(st.GetFrame(i).ToString());
  717. }
  718. }
  719. #endif
  720. return CommError.Type.COMM_ERR_ARG_IS_NULL;
  721. }
  722. if (sizeof(byte) > (length - position))
  723. {
  724. #if (DEBUG)
  725. StackTrace st = new StackTrace(true);
  726. for (int i = 0; i < st.FrameCount; i++)
  727. {
  728. if (null != st.GetFrame(i).GetFileName())
  729. {
  730. Console.WriteLine(st.GetFrame(i).ToString());
  731. }
  732. }
  733. #endif
  734. return CommError.Type.COMM_ERR_SHORT_BUF_FOR_READ;
  735. }
  736. dest = beginPtr[position++];
  737. return CommError.Type.COMM_NO_ERROR;
  738. }
  739. public CommError.Type readInt16(ref Int16 dest)
  740. {
  741. if(null == beginPtr)
  742. {
  743. #if (DEBUG)
  744. StackTrace st = new StackTrace(true);
  745. for (int i = 0; i < st.FrameCount; i++)
  746. {
  747. if (null != st.GetFrame(i).GetFileName())
  748. {
  749. Console.WriteLine(st.GetFrame(i).ToString());
  750. }
  751. }
  752. #endif
  753. return CommError.Type.COMM_ERR_ARG_IS_NULL;
  754. }
  755. if (sizeof(Int16) > (length - position))
  756. {
  757. #if (DEBUG)
  758. StackTrace st = new StackTrace(true);
  759. for (int i = 0; i < st.FrameCount; i++)
  760. {
  761. if (null != st.GetFrame(i).GetFileName())
  762. {
  763. Console.WriteLine(st.GetFrame(i).ToString());
  764. }
  765. }
  766. #endif
  767. return CommError.Type.COMM_ERR_SHORT_BUF_FOR_READ;
  768. }
  769. dest = MiscUtil.Conversion.EndianBitConverter.Big.ToInt16(beginPtr,position);
  770. position += sizeof(Int16);
  771. return CommError.Type.COMM_NO_ERROR;
  772. }
  773. public CommError.Type readUInt16(ref UInt16 dest)
  774. {
  775. CommError.Type ret = CommError.Type.COMM_NO_ERROR;
  776. Int16 tmp = 0;
  777. ret = readInt16(ref tmp);
  778. dest = (UInt16)tmp;
  779. return ret;
  780. }
  781. public CommError.Type readInt32(ref Int32 dest)
  782. {
  783. if(null == beginPtr)
  784. {
  785. #if (DEBUG)
  786. StackTrace st = new StackTrace(true);
  787. for (int i = 0; i < st.FrameCount; i++)
  788. {
  789. if (null != st.GetFrame(i).GetFileName())
  790. {
  791. Console.WriteLine(st.GetFrame(i).ToString());
  792. }
  793. }
  794. #endif
  795. return CommError.Type.COMM_ERR_ARG_IS_NULL;
  796. }
  797. if (sizeof(Int32) > (length - position))
  798. {
  799. #if (DEBUG)
  800. StackTrace st = new StackTrace(true);
  801. for (int i = 0; i < st.FrameCount; i++)
  802. {
  803. if (null != st.GetFrame(i).GetFileName())
  804. {
  805. Console.WriteLine(st.GetFrame(i).ToString());
  806. }
  807. }
  808. #endif
  809. return CommError.Type.COMM_ERR_SHORT_BUF_FOR_READ;
  810. }
  811. dest = MiscUtil.Conversion.EndianBitConverter.Big.ToInt32(beginPtr,position);
  812. position += sizeof(Int32);
  813. return CommError.Type.COMM_NO_ERROR;
  814. }
  815. public CommError.Type readUInt32(ref UInt32 dest)
  816. {
  817. CommError.Type ret = CommError.Type.COMM_NO_ERROR;
  818. Int32 tmp = 0;
  819. ret = readInt32(ref tmp);
  820. dest = (UInt32)tmp;
  821. return ret;
  822. }
  823. public CommError.Type readInt64(ref Int64 dest)
  824. {
  825. if(null == beginPtr)
  826. {
  827. #if (DEBUG)
  828. StackTrace st = new StackTrace(true);
  829. for (int i = 0; i < st.FrameCount; i++)
  830. {
  831. if (null != st.GetFrame(i).GetFileName())
  832. {
  833. Console.WriteLine(st.GetFrame(i).ToString());
  834. }
  835. }
  836. #endif
  837. return CommError.Type.COMM_ERR_ARG_IS_NULL;
  838. }
  839. if (sizeof(Int64) > (length - position))
  840. {
  841. #if (DEBUG)
  842. StackTrace st = new StackTrace(true);
  843. for (int i = 0; i < st.FrameCount; i++)
  844. {
  845. if (null != st.GetFrame(i).GetFileName())
  846. {
  847. Console.WriteLine(st.GetFrame(i).ToString());
  848. }
  849. }
  850. #endif
  851. return CommError.Type.COMM_ERR_SHORT_BUF_FOR_READ;
  852. }
  853. dest = MiscUtil.Conversion.EndianBitConverter.Big.ToInt64(beginPtr,position);
  854. position += sizeof(Int64);
  855. return CommError.Type.COMM_NO_ERROR;
  856. }
  857. public CommError.Type readUInt64(ref UInt64 dest)
  858. {
  859. CommError.Type ret = CommError.Type.COMM_NO_ERROR;
  860. Int64 tmp = 0;
  861. ret = readInt64(ref tmp);
  862. dest = (UInt64)tmp;
  863. return ret;
  864. }
  865. public CommError.Type readFloat(ref float dest)
  866. {
  867. CommError.Type ret = CommError.Type.COMM_NO_ERROR;
  868. Int32 tmp = 0;
  869. ret = readInt32(ref tmp);
  870. dest = MiscUtil.Conversion.EndianBitConverter.Big.ToSingle(MiscUtil.Conversion.EndianBitConverter.Big.GetBytes(tmp), 0);
  871. return ret;
  872. }
  873. public CommError.Type readDouble(ref double dest)
  874. {
  875. CommError.Type ret = CommError.Type.COMM_NO_ERROR;
  876. Int64 tmp = 0;
  877. ret = readInt64(ref tmp);
  878. dest =BitConverter.Int64BitsToDouble(tmp);
  879. return ret;
  880. }
  881. public CommError.Type readByteArray(ref byte[] dest, Int32 count)
  882. {
  883. if (null == beginPtr || count > dest.GetLength(0))
  884. {
  885. #if (DEBUG)
  886. StackTrace st = new StackTrace(true);
  887. for (int i = 0; i < st.FrameCount; i++)
  888. {
  889. if (null != st.GetFrame(i).GetFileName())
  890. {
  891. Console.WriteLine(st.GetFrame(i).ToString());
  892. }
  893. }
  894. #endif
  895. return CommError.Type.COMM_ERR_ARG_IS_NULL;
  896. }
  897. /*if (null == dest || 0 == dest.GetLength(0))
  898. {
  899. #if (DEBUG)
  900. StackTrace st = new StackTrace(true);
  901. for (int i = 0; i < st.FrameCount; i++)
  902. {
  903. if (null != st.GetFrame(i).GetFileName())
  904. {
  905. Console.WriteLine(st.GetFrame(i).ToString());
  906. }
  907. }
  908. #endif
  909. return CommError.Type.COMM_ERR_ARG_IS_NULL;
  910. }*/
  911. if (count > (length - position))
  912. {
  913. #if (DEBUG)
  914. StackTrace st = new StackTrace(true);
  915. for (int i = 0; i < st.FrameCount; i++)
  916. {
  917. if (null != st.GetFrame(i).GetFileName())
  918. {
  919. Console.WriteLine(st.GetFrame(i).ToString());
  920. }
  921. }
  922. #endif
  923. return CommError.Type.COMM_ERR_SHORT_BUF_FOR_READ;
  924. }
  925. for (int i = 0; i < count; i++)
  926. {
  927. dest[i] = beginPtr[position++];
  928. }
  929. return CommError.Type.COMM_NO_ERROR;
  930. }
  931. public CommError.Type readCString(ref byte[] dest, Int32 count)
  932. {
  933. if(null == beginPtr || count > dest.GetLength(0))
  934. {
  935. #if (DEBUG)
  936. StackTrace st = new StackTrace(true);
  937. for (int i = 0; i < st.FrameCount; i++)
  938. {
  939. if (null != st.GetFrame(i).GetFileName())
  940. {
  941. Console.WriteLine(st.GetFrame(i).ToString());
  942. }
  943. }
  944. #endif
  945. return CommError.Type.COMM_ERR_ARG_IS_NULL;
  946. }
  947. if (null == dest || 0 == dest.GetLength(0))
  948. {
  949. #if (DEBUG)
  950. StackTrace st = new StackTrace(true);
  951. for (int i = 0; i < st.FrameCount; i++)
  952. {
  953. if (null != st.GetFrame(i).GetFileName())
  954. {
  955. Console.WriteLine(st.GetFrame(i).ToString());
  956. }
  957. }
  958. #endif
  959. return CommError.Type.COMM_ERR_ARG_IS_NULL;
  960. }
  961. if (count > (length - position))
  962. {
  963. #if (DEBUG)
  964. StackTrace st = new StackTrace(true);
  965. for (int i = 0; i < st.FrameCount; i++)
  966. {
  967. if (null != st.GetFrame(i).GetFileName())
  968. {
  969. Console.WriteLine(st.GetFrame(i).ToString());
  970. }
  971. }
  972. #endif
  973. return CommError.Type.COMM_ERR_SHORT_BUF_FOR_READ;
  974. }
  975. for (int i = 0; i < count; i++)
  976. {
  977. dest[i] = beginPtr[position++];
  978. }
  979. return CommError.Type.COMM_NO_ERROR;
  980. }
  981. public CommError.Type readWString(ref Int16[] dest, Int32 count)
  982. {
  983. if(null == beginPtr || count > dest.GetLength(0))
  984. {
  985. #if (DEBUG)
  986. StackTrace st = new StackTrace(true);
  987. for (int i = 0; i < st.FrameCount; i++)
  988. {
  989. if (null != st.GetFrame(i).GetFileName())
  990. {
  991. Console.WriteLine(st.GetFrame(i).ToString());
  992. }
  993. }
  994. #endif
  995. return CommError.Type.COMM_ERR_ARG_IS_NULL;
  996. }
  997. if (null == dest || 0 == dest.GetLength(0))
  998. {
  999. #if (DEBUG)
  1000. StackTrace st = new StackTrace(true);
  1001. for (int i = 0; i < st.FrameCount; i++)
  1002. {
  1003. if (null != st.GetFrame(i).GetFileName())
  1004. {
  1005. Console.WriteLine(st.GetFrame(i).ToString());
  1006. }
  1007. }
  1008. #endif
  1009. return CommError.Type.COMM_ERR_ARG_IS_NULL;
  1010. }
  1011. if ((2 * count) > (length - position))
  1012. {
  1013. #if (DEBUG)
  1014. StackTrace st = new StackTrace(true);
  1015. for (int i = 0; i < st.FrameCount; i++)
  1016. {
  1017. if (null != st.GetFrame(i).GetFileName())
  1018. {
  1019. Console.WriteLine(st.GetFrame(i).ToString());
  1020. }
  1021. }
  1022. #endif
  1023. return CommError.Type.COMM_ERR_SHORT_BUF_FOR_READ;
  1024. }
  1025. for (int i = 0; i < count; i++)
  1026. {
  1027. dest[i] = MiscUtil.Conversion.EndianBitConverter.Big.ToInt16(beginPtr,position);
  1028. position += sizeof(Int16);
  1029. }
  1030. return CommError.Type.COMM_NO_ERROR;
  1031. }
  1032. /* read directly */
  1033. public CommError.Type readInt8(ref sbyte dest, Int32 pos)
  1034. {
  1035. CommError.Type ret = CommError.Type.COMM_NO_ERROR;
  1036. byte tmp = 0;
  1037. ret = readUInt8(ref tmp,pos);
  1038. dest = (sbyte)tmp;
  1039. return ret;
  1040. }
  1041. public CommError.Type readUInt8(ref byte dest ,Int32 pos)
  1042. {
  1043. if(null == beginPtr)
  1044. {
  1045. #if (DEBUG)
  1046. StackTrace st = new StackTrace(true);
  1047. for (int i = 0; i < st.FrameCount; i++)
  1048. {
  1049. if (null != st.GetFrame(i).GetFileName())
  1050. {
  1051. Console.WriteLine(st.GetFrame(i).ToString());
  1052. }
  1053. }
  1054. #endif
  1055. return CommError.Type.COMM_ERR_ARG_IS_NULL;
  1056. }
  1057. if (sizeof(byte) > (length - pos))
  1058. {
  1059. #if (DEBUG)
  1060. StackTrace st = new StackTrace(true);
  1061. for (int i = 0; i < st.FrameCount; i++)
  1062. {
  1063. if (null != st.GetFrame(i).GetFileName())
  1064. {
  1065. Console.WriteLine(st.GetFrame(i).ToString());
  1066. }
  1067. }
  1068. #endif
  1069. return CommError.Type.COMM_ERR_SHORT_BUF_FOR_READ;
  1070. }
  1071. dest = beginPtr[pos];
  1072. return CommError.Type.COMM_NO_ERROR;
  1073. }
  1074. public CommError.Type readInt16(ref Int16 dest, Int32 pos)
  1075. {
  1076. if(null == beginPtr)
  1077. {
  1078. #if (DEBUG)
  1079. StackTrace st = new StackTrace(true);
  1080. for (int i = 0; i < st.FrameCount; i++)
  1081. {
  1082. if (null != st.GetFrame(i).GetFileName())
  1083. {
  1084. Console.WriteLine(st.GetFrame(i).ToString());
  1085. }
  1086. }
  1087. #endif
  1088. return CommError.Type.COMM_ERR_ARG_IS_NULL;
  1089. }
  1090. if (sizeof(Int16) > (length - pos))
  1091. {
  1092. #if (DEBUG)
  1093. StackTrace st = new StackTrace(true);
  1094. for (int i = 0; i < st.FrameCount; i++)
  1095. {
  1096. if (null != st.GetFrame(i).GetFileName())
  1097. {
  1098. Console.WriteLine(st.GetFrame(i).ToString());
  1099. }
  1100. }
  1101. #endif
  1102. return CommError.Type.COMM_ERR_SHORT_BUF_FOR_READ;
  1103. }
  1104. dest = MiscUtil.Conversion.EndianBitConverter.Big.ToInt16(beginPtr,pos);
  1105. return CommError.Type.COMM_NO_ERROR;
  1106. }
  1107. public CommError.Type readUInt16(ref UInt16 dest, Int32 pos)
  1108. {
  1109. CommError.Type ret = CommError.Type.COMM_NO_ERROR;
  1110. Int16 tmp = 0;
  1111. ret = readInt16(ref tmp,pos);
  1112. dest = (UInt16)tmp;
  1113. return ret;
  1114. }
  1115. public CommError.Type readInt32(ref Int32 dest, Int32 pos)
  1116. {
  1117. if(null == beginPtr)
  1118. {
  1119. #if (DEBUG)
  1120. StackTrace st = new StackTrace(true);
  1121. for (int i = 0; i < st.FrameCount; i++)
  1122. {
  1123. if (null != st.GetFrame(i).GetFileName())
  1124. {
  1125. Console.WriteLine(st.GetFrame(i).ToString());
  1126. }
  1127. }
  1128. #endif
  1129. return CommError.Type.COMM_ERR_ARG_IS_NULL;
  1130. }
  1131. if (sizeof(Int32) > (length - pos))
  1132. {
  1133. #if (DEBUG)
  1134. StackTrace st = new StackTrace(true);
  1135. for (int i = 0; i < st.FrameCount; i++)
  1136. {
  1137. if (null != st.GetFrame(i).GetFileName())
  1138. {
  1139. Console.WriteLine(st.GetFrame(i).ToString());
  1140. }
  1141. }
  1142. #endif
  1143. return CommError.Type.COMM_ERR_SHORT_BUF_FOR_READ;
  1144. }
  1145. dest = MiscUtil.Conversion.EndianBitConverter.Big.ToInt32(beginPtr,pos);
  1146. return CommError.Type.COMM_NO_ERROR;
  1147. }
  1148. public CommError.Type readUInt32(ref UInt32 dest, Int32 pos)
  1149. {
  1150. CommError.Type ret = CommError.Type.COMM_NO_ERROR;
  1151. Int32 tmp = 0;
  1152. ret = readInt32(ref tmp,pos);
  1153. dest = (UInt32)tmp;
  1154. return ret;
  1155. }
  1156. public CommError.Type readInt64(ref Int64 dest, Int32 pos)
  1157. {
  1158. if(null == beginPtr)
  1159. {
  1160. #if (DEBUG)
  1161. StackTrace st = new StackTrace(true);
  1162. for (int i = 0; i < st.FrameCount; i++)
  1163. {
  1164. if (null != st.GetFrame(i).GetFileName())
  1165. {
  1166. Console.WriteLine(st.GetFrame(i).ToString());
  1167. }
  1168. }
  1169. #endif
  1170. return CommError.Type.COMM_ERR_ARG_IS_NULL;
  1171. }
  1172. if (sizeof(Int64) > (length - pos))
  1173. {
  1174. #if (DEBUG)
  1175. StackTrace st = new StackTrace(true);
  1176. for (int i = 0; i < st.FrameCount; i++)
  1177. {
  1178. if (null != st.GetFrame(i).GetFileName())
  1179. {
  1180. Console.WriteLine(st.GetFrame(i).ToString());
  1181. }
  1182. }
  1183. #endif
  1184. return CommError.Type.COMM_ERR_SHORT_BUF_FOR_READ;
  1185. }
  1186. dest = MiscUtil.Conversion.EndianBitConverter.Big.ToInt64(beginPtr, pos);
  1187. return CommError.Type.COMM_NO_ERROR;
  1188. }
  1189. public CommError.Type readUInt64(ref UInt64 dest, Int32 pos)
  1190. {
  1191. CommError.Type ret = CommError.Type.COMM_NO_ERROR;
  1192. Int64 tmp = 0;
  1193. ret = readInt64(ref tmp,pos);
  1194. dest = (UInt64)tmp;
  1195. return ret;
  1196. }
  1197. public CommError.Type readFloat(ref float dest, Int32 pos)
  1198. {
  1199. CommError.Type ret = CommError.Type.COMM_NO_ERROR;
  1200. Int32 tmp = 0;
  1201. ret = readInt32(ref tmp,pos);
  1202. dest = MiscUtil.Conversion.EndianBitConverter.Big.ToSingle(MiscUtil.Conversion.EndianBitConverter.Big.GetBytes(tmp), 0);
  1203. return ret;
  1204. }
  1205. public CommError.Type readDouble(ref double dest, Int32 pos)
  1206. {
  1207. CommError.Type ret = CommError.Type.COMM_NO_ERROR;
  1208. Int64 tmp = 0;
  1209. ret = readInt64(ref tmp,pos);
  1210. dest =BitConverter.Int64BitsToDouble(tmp);
  1211. return ret;
  1212. }
  1213. public CommError.Type readCString(ref byte[] dest, Int32 count, Int32 pos)
  1214. {
  1215. if(null == beginPtr || count > dest.GetLength(0))
  1216. {
  1217. #if (DEBUG)
  1218. StackTrace st = new StackTrace(true);
  1219. for (int i = 0; i < st.FrameCount; i++)
  1220. {
  1221. if (null != st.GetFrame(i).GetFileName())
  1222. {
  1223. Console.WriteLine(st.GetFrame(i).ToString());
  1224. }
  1225. }
  1226. #endif
  1227. return CommError.Type.COMM_ERR_ARG_IS_NULL;
  1228. }
  1229. if (null == dest || 0 == dest.GetLength(0))
  1230. {
  1231. #if (DEBUG)
  1232. StackTrace st = new StackTrace(true);
  1233. for (int i = 0; i < st.FrameCount; i++)
  1234. {
  1235. if (null != st.GetFrame(i).GetFileName())
  1236. {
  1237. Console.WriteLine(st.GetFrame(i).ToString());
  1238. }
  1239. }
  1240. #endif
  1241. return CommError.Type.COMM_ERR_ARG_IS_NULL;
  1242. }
  1243. if (count > (length - pos))
  1244. {
  1245. #if (DEBUG)
  1246. StackTrace st = new StackTrace(true);
  1247. for (int i = 0; i < st.FrameCount; i++)
  1248. {
  1249. if (null != st.GetFrame(i).GetFileName())
  1250. {
  1251. Console.WriteLine(st.GetFrame(i).ToString());
  1252. }
  1253. }
  1254. #endif
  1255. return CommError.Type.COMM_ERR_SHORT_BUF_FOR_READ;
  1256. }
  1257. for (int i = 0; i < count; i++)
  1258. {
  1259. dest[i] = beginPtr[pos + count];
  1260. }
  1261. return CommError.Type.COMM_NO_ERROR;
  1262. }
  1263. public CommError.Type readWString(ref Int16[] dest, Int32 count, Int32 pos)
  1264. {
  1265. if(null == beginPtr || count > dest.GetLength(0))
  1266. {
  1267. #if (DEBUG)
  1268. StackTrace st = new StackTrace(true);
  1269. for (int i = 0; i < st.FrameCount; i++)
  1270. {
  1271. if (null != st.GetFrame(i).GetFileName())
  1272. {
  1273. Console.WriteLine(st.GetFrame(i).ToString());
  1274. }
  1275. }
  1276. #endif
  1277. return CommError.Type.COMM_ERR_ARG_IS_NULL;
  1278. }
  1279. if (null == dest || 0 == dest.GetLength(0))
  1280. {
  1281. #if (DEBUG)
  1282. StackTrace st = new StackTrace(true);
  1283. for (int i = 0; i < st.FrameCount; i++)
  1284. {
  1285. if (null != st.GetFrame(i).GetFileName())
  1286. {
  1287. Console.WriteLine(st.GetFrame(i).ToString());
  1288. }
  1289. }
  1290. #endif
  1291. return CommError.Type.COMM_ERR_ARG_IS_NULL;
  1292. }
  1293. if ((2 * count) > (length - pos))
  1294. {
  1295. #if (DEBUG)
  1296. StackTrace st = new StackTrace(true);
  1297. for (int i = 0; i < st.FrameCount; i++)
  1298. {
  1299. if (null != st.GetFrame(i).GetFileName())
  1300. {
  1301. Console.WriteLine(st.GetFrame(i).ToString());
  1302. }
  1303. }
  1304. #endif
  1305. return CommError.Type.COMM_ERR_SHORT_BUF_FOR_READ;
  1306. }
  1307. for (int i = 0; i < count; i++)
  1308. {
  1309. dest[i] = MiscUtil.Conversion.EndianBitConverter.Big.ToInt16(beginPtr,pos + 2 * count);
  1310. }
  1311. return CommError.Type.COMM_NO_ERROR;
  1312. }
  1313. public CommError.Type toHexStr(ref char[] buffer,out int usedsize)
  1314. {
  1315. CommError.Type ret = CommError.Type.COMM_NO_ERROR;
  1316. int dataLen = length - position;
  1317. int hexDatLen = dataLen * 2 + 1;
  1318. if(buffer.GetLength(0) < hexDatLen)
  1319. {
  1320. #if (DEBUG)
  1321. StackTrace st = new StackTrace(true);
  1322. for (int i = 0; i < st.FrameCount; i++)
  1323. {
  1324. if (null != st.GetFrame(i).GetFileName())
  1325. {
  1326. Console.WriteLine(st.GetFrame(i).ToString());
  1327. }
  1328. }
  1329. #endif
  1330. usedsize = 0;
  1331. return CommError.Type.COMM_ERR_SHORT_BUF_FOR_WRITE;
  1332. }
  1333. string s = "";
  1334. byte[] bArray = new byte[length - position];
  1335. for(int i =0;i<length - position;i++)
  1336. {
  1337. ret = readUInt8(ref bArray[i],position + i);
  1338. if(ret != CommError.Type.COMM_NO_ERROR)
  1339. {
  1340. usedsize = 0;
  1341. return ret ;
  1342. }
  1343. s += string.Format("{0:x2}",bArray[i]);
  1344. }
  1345. s += string.Format("{0:x}",0x00);
  1346. buffer = s.ToCharArray();
  1347. usedsize = hexDatLen;
  1348. return ret;
  1349. }
  1350. public CommError.Type toHexStr(ref string buffer)
  1351. {
  1352. CommError.Type ret = CommError.Type.COMM_NO_ERROR;
  1353. byte[] bArray = new byte[length - position];
  1354. for(int i =0;i<length - position;i++)
  1355. {
  1356. ret = readUInt8(ref bArray[i],position + i);
  1357. if(ret != CommError.Type.COMM_NO_ERROR)
  1358. {
  1359. return ret;
  1360. }
  1361. buffer += string.Format("{0:x2}",bArray[i]);
  1362. }
  1363. buffer += string.Format("{0:x}",0x00);
  1364. return ret;
  1365. }
  1366. }
  1367. public class VisualBuf
  1368. {
  1369. private string visualBuf;
  1370. public VisualBuf()
  1371. {
  1372. visualBuf = "";
  1373. }
  1374. public CommError.Type sprintf(string format , params object[]args)
  1375. {
  1376. CommError.Type ret = CommError.Type.COMM_NO_ERROR;
  1377. string str = "";
  1378. try
  1379. {
  1380. str = string.Format(format, args);
  1381. }
  1382. catch (System.ArgumentNullException ex)
  1383. {
  1384. #if (DEBUG)
  1385. StackTrace st = new StackTrace(true);
  1386. for (int i = 0; i < st.FrameCount; i++)
  1387. {
  1388. if (null != st.GetFrame(i).GetFileName())
  1389. {
  1390. Console.WriteLine(st.GetFrame(i).ToString());
  1391. }
  1392. }
  1393. #endif
  1394. Console.WriteLine("Error: " + ex.Message);
  1395. ret = CommError.Type.COMM_ERR_ARGUMENT_NULL_EXCEPTION;
  1396. }
  1397. catch(System.FormatException ex)
  1398. {
  1399. #if (DEBUG)
  1400. StackTrace st = new StackTrace(true);
  1401. for (int i = 0; i < st.FrameCount; i++)
  1402. {
  1403. if (null != st.GetFrame(i).GetFileName())
  1404. {
  1405. Console.WriteLine(st.GetFrame(i).ToString());
  1406. }
  1407. }
  1408. #endif
  1409. Console.WriteLine("Error: " + ex.Message);
  1410. ret = CommError.Type.COMM_ERR_INVALID_FORMAT;
  1411. }
  1412. if(CommError.Type.COMM_NO_ERROR == ret)
  1413. {
  1414. visualBuf += str;
  1415. }
  1416. return ret;
  1417. }
  1418. public string getVisualBuf()
  1419. {
  1420. return visualBuf;
  1421. }
  1422. }