123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Threading;
- namespace HPSocketCS.Extended
- {
- /// <summary>
- /// 客户端接收数据缓存对象
- /// </summary>
- public class ReceiveCacheBuffer
- {
- public ReceiveCacheBuffer() {
-
- }
- public ReceiveCacheBuffer(Int64 _generateId)
- {
- this.generateId = _generateId;
- }
- Int64 generateId;
- /// <summary>
- /// 标识ID
- /// </summary>
- public Int64 GenerateId
- {
- get { return generateId; }
- set { generateId = value; }
- }
- string saveToFileName;
- /// <summary>
- /// 下载文件时,下载的文件要保存的路径文件名
- /// </summary>
- public string SaveToFileName
- {
- get { return saveToFileName; }
- set { saveToFileName = value; }
- }
- System.IO.MemoryStream dataBuffer = new System.IO.MemoryStream();
- /// <summary>
- /// 数据缓存
- /// </summary>
- public System.IO.MemoryStream DataBuffer
- {
- get { return dataBuffer; }
- set { dataBuffer = value; }
- }
- long length;
- /// <summary>
- /// 数据长度
- /// </summary>
- public long Length
- {
- get { return length; }
- set { length = value; }
- }
- /* long _dataBufferLength;
- /// <summary>
- /// 数据到缓存区和数据长度
- /// </summary>
- public long DataBufferLength
- {
- get { return _dataBufferLength; }
- }*/
- /// <summary>
- /// 添加数据到缓存区
- /// </summary>
- /// <param name="bytes"></param>
- public void AddDataToBuffer(byte[] bytes)
- {
- dataBuffer.Write(bytes,0,bytes.Length);
- //dataBuffer.Flush();
- // this._dataBufferLength = bytes.Length;
- // dataBuffer = Marshal.AllocHGlobal(bytes.Length);
- // Marshal.Copy(bytes, 0, dataBuffer, bytes.Length);
- }
- /// <summary>
- /// 获取缓存区中的数据
- /// </summary>
- /// <returns></returns>
- public byte[] GetBufferInData()
- {
- try
- {
- // byte[] retBytes = new byte[DataBufferLength];
- // Marshal.Copy(dataBuffer, retBytes, 0, retBytes.Length);
- return dataBuffer.GetBuffer();
- }
- catch {
- return null;
- }
- }
- /// <summary>
- /// 接收完成
- /// </summary>
- /// <param name="recBytes"></param>
- public bool ReceiveComplete(byte[] recBytes, bool isComplete)
- {
- try
- {
- this.IsReceiveComplete = isComplete;
- this.AddDataToBuffer(recBytes);
- if (this.IsReceiveComplete)
- {
- this.MEvent.Set();
- if (this.CallbackFunction != null)
- {
- this.CallbackFunction(new ReturnResultObject(generateId, dataBuffer.GetBuffer()));
- }
- }
- return true;
- }
- catch {
- return false;
- }
- }
- System.ComponentModel.BackgroundWorker backgroundWorker;
- /// <summary>
- /// 线程操作对象
- /// </summary>
- public System.ComponentModel.BackgroundWorker BackgroundWorker
- {
- get { return backgroundWorker; }
- set { backgroundWorker = value; }
- }
- DelegateExecuteThreadStart callbackFunction;
- /// <summary>
- /// 回调函数
- /// </summary>
- public DelegateExecuteThreadStart CallbackFunction
- {
- get { return callbackFunction; }
- set { callbackFunction = value; }
- }
- bool isReceiveComplete = false;
- /// <summary>
- /// 是否接收完成
- /// </summary>
- public bool IsReceiveComplete
- {
- get { return isReceiveComplete; }
- set { isReceiveComplete = value; }
- }
- AutoResetEvent mEvent = new AutoResetEvent(false);
- /// <summary>
- /// 线程通知
- /// </summary>
- public AutoResetEvent MEvent
- {
- get { return mEvent; }
- set { mEvent = value; }
- }
- /// <summary>
- /// 释放当前对象使用的资源
- /// </summary>
- public void Dispose()
- {
- try
- {
- /* if (dataBuffer != IntPtr.Zero)
- {
- Marshal.FreeHGlobal(dataBuffer);
- }*/
- this.dataBuffer.Close();
- this.dataBuffer.Dispose();
- this.dataBuffer = null;
- }
- catch {
-
- }
- try
- {
- mEvent.Set();
- mEvent.Close();
- mEvent.Dispose();
- mEvent = null;
- }
- catch { }
- }
- }
- }
|