⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 download.cs

📁 该源代码用 C# 写成
💻 CS
字号:
using System;
using System.Collections;
using System.IO;
using Org.InteliIM.Configurations;

namespace Org.InteliIM.Activities.FileSharing
{
	/// <summary>
	/// download, used for data receiver.
	/// </summary>
	public class Download
	{
		#region constructors

		public Download(IMClient client)
		{
			this.Client = client;

			this.Client.MessageReceived += new MessageReceivedOrSentEventHandler(Client_MessageReceived);
		}

		#endregion

		#region private fields

		private IMClient client;
		private string to;
		private Asset asset;
		private DownloadState state = DownloadState.Idle;
		private Stream m_ReceivedStream = null;

		private BinaryWriter m_ReceivedBw = null;

		private string saveFileName = null;
		private ArrayList receivedBlocks = new ArrayList();

		#endregion

		#region private methods

		/// <summary>
		/// handle client message received event
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void Client_MessageReceived(object sender, MessageReceivedOrSentEventArgs e)
		{
			InstantMessage message = e.MessageInfo.Message;

			if (message is MessageErrorOccurred)
			{
				//                this.NotifyUser(NotificationType.Error,
				//                    (message as MessageErrorOccurred).Reason);
			}
			else if (message is MessageDownloadData)
			{
				if ((message as MessageDownloadData).Data.Cancel)
				{
					//                    string notifyStr = message.From + " 取消了浏览共享给您";
					//
					//                    this.NotifyUser(notifyStr);

					this.Stop();
				}
				else
				{
					byte[] buffer = (message as MessageDownloadData).Data.Buffer;

					if ((message as MessageDownloadData).Data.Begin)
					{
						this.m_ReceivedStream = File.OpenWrite(this.SaveFileName);

						this.m_ReceivedBw = new BinaryWriter(this.m_ReceivedStream);

						this.receivedBlocks.Clear();
					}

					if (buffer != null && !receivedBlocks.Contains((message as MessageDownloadData).Data.No))
					{
						this.m_ReceivedBw.Write(buffer, 0, buffer.Length);
						this.m_ReceivedBw.Flush();

						float progress = (float)((message as MessageDownloadData).Data.No * this.BufferSize)
							/ this.Asset.Size;

						this.OnProgressing(new DownloadProgressingEventArgs(
							progress));

						receivedBlocks.Add((message as MessageDownloadData).Data.No);
					}

					if ((message as MessageDownloadData).Data.End)
					{
						this.m_ReceivedStream.Close();
						this.m_ReceivedBw.Close();

						this.OnProgressing(new DownloadProgressingEventArgs(1f));

						this.State = DownloadState.Complete;
					}
				}
			}
		}

		#endregion

		#region public properties

		/// <summary>
		/// instant messenger client
		/// </summary>
		/// <value></value>
		public IMClient Client
		{
			get
			{
				return this.client;
			}
			set
			{
				this.client = value;
			}
		}

		/// <summary>
		/// to
		/// </summary>
		/// <value></value>
		public string To
		{
			get
			{
				return this.to;
			}
			set
			{
				this.to = value;
			}
		}

		/// <summary>
		/// asset
		/// </summary>
		/// <value></value>
		public Asset Asset
		{
			get
			{
				if (this.asset == null)
					this.asset = new Asset();

				return this.asset;
			}
			set
			{
				this.asset = value;
			}
		}

		/// <summary>
		///  The size of the buffer (sending or receiving)
		/// </summary>
		/// <state></state>
		public long BufferSize
		{
			get
			{
				return 16384;
			}
		}

		/// <summary>
		/// 状态
		/// </summary>
		public DownloadState State
		{
			get
			{
				return this.state;
			}
			set
			{
				this.state = value;

				if (value == DownloadState.Complete)
				{
					this.OnCompleted(new EventArgs());
				}

				if (this.StateChanged != null)
					this.StateChanged(this, new DownloadStateChangedEventArgs(value));
			}
		}

		/// <summary>
		/// save file name
		/// </summary>
		/// <value></value>
		public string SaveFileName
		{
			get
			{
				return this.saveFileName;
			}
			set
			{
				this.saveFileName = value;
			}
		}

		#endregion

		#region public methods

		public void OnStateChanged(DownloadStateChangedEventArgs e)
		{
			if (this.StateChanged != null)
				this.StateChanged(this, e);
		}

		public void OnProgressing(DownloadProgressingEventArgs e)
		{
			if (this.Progressing != null)
				this.Progressing(this, e);
		}

		public void OnCompleted(EventArgs e)
		{
			if (this.Completed != null)
				this.Completed(this, e);
		}

		/// <summary>
		/// 停止发送
		/// </summary>
		public void Stop()
		{
			if (this.State != DownloadState.Idle)
			{
				MessageDownloadData responseMessage = new MessageDownloadData();
				responseMessage.From = this.Client.User.Id;
				responseMessage.To = this.To;
				responseMessage.Data.Cancel = true;
				responseMessage.Data.Buffer = null;

				this.Client.Connection.SendMessage(responseMessage);

				this.Client.MessageReceived -= new MessageReceivedOrSentEventHandler(Client_MessageReceived);

				if(this.State != DownloadState.Complete)
					this.State = DownloadState.Idle;
			}
		}

		/// <summary>
		/// 开始接收
		/// </summary>
		public void Receive()
		{
			Stop();

			this.State = DownloadState.Receiving;
		}

		/// <summary>
		/// 接受或拒绝邀请
		/// </summary>
		public void StartDownload()
		{
			this.State = DownloadState.Idle;

			MessageStartDownload responseMessage = new MessageStartDownload();
			responseMessage.From = this.Client.User.Id;
			responseMessage.To = this.To;
			responseMessage.Asset = this.Asset;

			this.Client.Connection.SendMessage(responseMessage);

			this.Receive();
		}

		#endregion

		#region public events

		public event DownloadStateChangedEventHandler StateChanged;

		public event DownloadProgressingEventHandler Progressing;

		public event EventHandler Completed;

		#endregion
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -