ReceiveCacheBuffer.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. using System.Threading;
  7. namespace HPSocketCS.Extended
  8. {
  9. /// <summary>
  10. /// 客户端接收数据缓存对象
  11. /// </summary>
  12. public class ReceiveCacheBuffer
  13. {
  14. public ReceiveCacheBuffer() {
  15. }
  16. public ReceiveCacheBuffer(Int64 _generateId)
  17. {
  18. this.generateId = _generateId;
  19. }
  20. Int64 generateId;
  21. /// <summary>
  22. /// 标识ID
  23. /// </summary>
  24. public Int64 GenerateId
  25. {
  26. get { return generateId; }
  27. set { generateId = value; }
  28. }
  29. string saveToFileName;
  30. /// <summary>
  31. /// 下载文件时,下载的文件要保存的路径文件名
  32. /// </summary>
  33. public string SaveToFileName
  34. {
  35. get { return saveToFileName; }
  36. set { saveToFileName = value; }
  37. }
  38. System.IO.MemoryStream dataBuffer = new System.IO.MemoryStream();
  39. /// <summary>
  40. /// 数据缓存
  41. /// </summary>
  42. public System.IO.MemoryStream DataBuffer
  43. {
  44. get { return dataBuffer; }
  45. set { dataBuffer = value; }
  46. }
  47. long length;
  48. /// <summary>
  49. /// 数据长度
  50. /// </summary>
  51. public long Length
  52. {
  53. get { return length; }
  54. set { length = value; }
  55. }
  56. /* long _dataBufferLength;
  57. /// <summary>
  58. /// 数据到缓存区和数据长度
  59. /// </summary>
  60. public long DataBufferLength
  61. {
  62. get { return _dataBufferLength; }
  63. }*/
  64. /// <summary>
  65. /// 添加数据到缓存区
  66. /// </summary>
  67. /// <param name="bytes"></param>
  68. public void AddDataToBuffer(byte[] bytes)
  69. {
  70. dataBuffer.Write(bytes,0,bytes.Length);
  71. //dataBuffer.Flush();
  72. // this._dataBufferLength = bytes.Length;
  73. // dataBuffer = Marshal.AllocHGlobal(bytes.Length);
  74. // Marshal.Copy(bytes, 0, dataBuffer, bytes.Length);
  75. }
  76. /// <summary>
  77. /// 获取缓存区中的数据
  78. /// </summary>
  79. /// <returns></returns>
  80. public byte[] GetBufferInData()
  81. {
  82. try
  83. {
  84. // byte[] retBytes = new byte[DataBufferLength];
  85. // Marshal.Copy(dataBuffer, retBytes, 0, retBytes.Length);
  86. return dataBuffer.GetBuffer();
  87. }
  88. catch {
  89. return null;
  90. }
  91. }
  92. /// <summary>
  93. /// 接收完成
  94. /// </summary>
  95. /// <param name="recBytes"></param>
  96. public bool ReceiveComplete(byte[] recBytes, bool isComplete)
  97. {
  98. try
  99. {
  100. this.IsReceiveComplete = isComplete;
  101. this.AddDataToBuffer(recBytes);
  102. if (this.IsReceiveComplete)
  103. {
  104. this.MEvent.Set();
  105. if (this.CallbackFunction != null)
  106. {
  107. this.CallbackFunction(new ReturnResultObject(generateId, dataBuffer.GetBuffer()));
  108. }
  109. }
  110. return true;
  111. }
  112. catch {
  113. return false;
  114. }
  115. }
  116. System.ComponentModel.BackgroundWorker backgroundWorker;
  117. /// <summary>
  118. /// 线程操作对象
  119. /// </summary>
  120. public System.ComponentModel.BackgroundWorker BackgroundWorker
  121. {
  122. get { return backgroundWorker; }
  123. set { backgroundWorker = value; }
  124. }
  125. DelegateExecuteThreadStart callbackFunction;
  126. /// <summary>
  127. /// 回调函数
  128. /// </summary>
  129. public DelegateExecuteThreadStart CallbackFunction
  130. {
  131. get { return callbackFunction; }
  132. set { callbackFunction = value; }
  133. }
  134. bool isReceiveComplete = false;
  135. /// <summary>
  136. /// 是否接收完成
  137. /// </summary>
  138. public bool IsReceiveComplete
  139. {
  140. get { return isReceiveComplete; }
  141. set { isReceiveComplete = value; }
  142. }
  143. AutoResetEvent mEvent = new AutoResetEvent(false);
  144. /// <summary>
  145. /// 线程通知
  146. /// </summary>
  147. public AutoResetEvent MEvent
  148. {
  149. get { return mEvent; }
  150. set { mEvent = value; }
  151. }
  152. /// <summary>
  153. /// 释放当前对象使用的资源
  154. /// </summary>
  155. public void Dispose()
  156. {
  157. try
  158. {
  159. /* if (dataBuffer != IntPtr.Zero)
  160. {
  161. Marshal.FreeHGlobal(dataBuffer);
  162. }*/
  163. this.dataBuffer.Close();
  164. this.dataBuffer.Dispose();
  165. this.dataBuffer = null;
  166. }
  167. catch {
  168. }
  169. try
  170. {
  171. mEvent.Set();
  172. mEvent.Close();
  173. mEvent.Dispose();
  174. mEvent = null;
  175. }
  176. catch { }
  177. }
  178. }
  179. }