StreamSendWrapper 类(NetworkComms 2.3.1源码了解和学习)

 networkComms.net2.3.1开源版本,基于gpl V3协议。因为不能公开3.x版本的源码,所以基于此版本进行学习。3.X版本进行了诸多改进和Bug修复,使用方法上两者相差不大。
/*请注意使用以下代码,需遵循GplV3协议*/


using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace DPSBase
{
     
    /// 发送全部或者部分数据流
   
    public class StreamSendWrapper : IDisposable
    {
        object streamLocker = new object();

       
        public ThreadSafeStream ThreadSafeStream { get; set; }
      
        public long Start { get; private set; }
      
        public long Length { get; private set; }

      
        public StreamSendWrapper(ThreadSafeStream stream)
        {
            this.ThreadSafeStream = stream;
            this.Start = 0;
            this.Length = stream.Length;
        }

       
        public StreamSendWrapper(ThreadSafeStream stream, long start, long length)
        {
            if (start < 0)
                throw new Exception("Provided start value cannot be less than 0.");

            if (length < 0)
                throw new Exception("Provided length value cannot be less than 0.");

            this.ThreadSafeStream = stream;
            this.Start = start;
            this.Length = length;
        }

        /// <summary>
        /// Return the MD5 for the specific part of the stream only.
        /// 返回数据流指定部分的MD5值
        /// </summary>
        /// <returns></returns>
        public string MD5CheckSum()
        {
            using (MemoryStream ms = new MemoryStream())
            {
                ThreadSafeStream.CopyTo(ms, Start, Length, 8000);

#if WINDOWS_PHONE
                using(var md5 = new DPSBase.MD5Managed())
                {
#else
                using (var md5 = System.Security.Cryptography.MD5.Create())
                {
#endif
                    return BitConverter.ToString(md5.ComputeHash(ms)).Replace("-", "");
                }
            }
        }
 
        public void Dispose()
        {
            ThreadSafeStream.Dispose();
        }
    }
}
 http://www.cnblogs.com/networkcomms
http://www.networkcoms.cn 编辑

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。