downloaddaemon.cs

来自「该源代码用 C# 写成」· CS 代码 · 共 315 行

CS
315
字号
using System;
using System.IO;
using System.Threading;
using Org.InteliIM.Configurations;

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

		public DownloadDaemon(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 DownloadDaemonState state = DownloadDaemonState.Idle;

		private Thread threadSend = null;

		#endregion

		#region private methods

		/// <summary>
		/// Private implementation of sending file, should not use it directly.
		/// </summary>
		private void _Send()
		{
			try
			{
				Stream S = File.OpenRead(this.Asset.Path);

				BinaryReader br = new BinaryReader(S);

				byte[] buffer = new byte[this.BufferSize];

				bool hasBegin = false;

				int read;

				int no = 0;

				this.State = DownloadDaemonState.Sending;

				while ((read = br.Read(buffer, 0, buffer.Length)) > 0)
				{
					MessageDownloadData responseMessage = new MessageDownloadData();
					responseMessage.From = this.Client.User.Id;
					responseMessage.To = this.To;
					responseMessage.Data.Asset = this.Asset;
					responseMessage.Data.No = no;
					responseMessage.Data.Cancel = false;

					byte[] bufferHasRead
						= new byte[read];

					for (int i = 0; i < read; i++)
					{
						bufferHasRead[i] = buffer[i];
					}

					responseMessage.Data.Buffer = bufferHasRead;

					responseMessage.Data.Begin = !hasBegin;
					responseMessage.Data.End = false;

					if (read < buffer.Length)
					{
						responseMessage.Data.End = true;
					}

					this.Client.Connection.SendMessage(responseMessage);

					hasBegin = true;

					float progress = (float)(no * this.BufferSize)
						/ this.Asset.Size;

					if (responseMessage.Data.End)
					{
						progress = 1.0f;
					}

					this.OnProgressing(new DownloadProgressingEventArgs(progress));

					no++;

					buffer = new byte[this.BufferSize];

					Thread.Sleep(50);
				}

				br.Close();

				this.State = DownloadDaemonState.Complete;
			}
			catch
			{
			}
		}

		/// <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 MessageStartDownload)
			{
				this.Send();
			}
		}

		#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 DownloadDaemonState State
		{
			get
			{
				return this.state;
			}
			set
			{
				this.state = value;

				if (value == DownloadDaemonState.Idle
					|| value == DownloadDaemonState.Complete)
				{
					this.Client.MessageReceived -= new MessageReceivedOrSentEventHandler(Client_MessageReceived);
				}

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

		#endregion

		#region public methods

		public void OnStateChanged(DownloadDaemonStateChangedEventArgs 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 != DownloadDaemonState.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);

				if (this.threadSend != null)
				{
					try
					{
						this.threadSend.Abort();
					}
					catch { }
					finally
					{
						this.threadSend = null;
					}
				}

				this.State = DownloadDaemonState.Idle;
			}
		}

		/// <summary>
		/// Send file
		/// </summary>
		public void Send()
		{
			if (this.State == DownloadDaemonState.Idle)
			{
				if (this.Asset.Path == "")
				{
					this.Stop();
				}
				else
				{
					this.State = DownloadDaemonState.Sending;

					ThreadStart ts = new ThreadStart(this._Send);

					threadSend = new Thread(ts);
					threadSend.Start();
				}
			}
		}

		#endregion

		#region public events

		public event DownloadDaemonStateChangedEventHandler StateChanged;

		public event DownloadProgressingEventHandler Progressing;

		public event EventHandler Completed;

		#endregion
	}
}

⌨️ 快捷键说明

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