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

📄 download.cs

📁 这个版本(InteliIM Open Kernel Release 2)是目前最新的(首次公开)
💻 CS
字号:
using System;
using System.Collections;
using System.IO;
using Org.InteliIM.Configurations;
using Org.InteliIM;
using Org.InteliIM.Communications;

namespace Org.InteliIM.Applications.Messenger.FileSharing
{
	/// <summary>
	/// Download, used for data receiver.
	/// </summary>
	public class Download
    {
        /// <summary>
        /// Initializes a new instance of the Download class.
        /// </summary>
        /// <param name="client"></param>
        public Download(IMClient client)
        {
            this.Client = client;

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

        private Stream m_ReceivedStream = null;

        private BinaryWriter m_ReceivedBw = null;

        private ArrayList receivedBlocks = new ArrayList();

        /// <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;
                    }
                }
            }
        }

        private IMClient client;

        /// <summary>
        /// Gets or sets the client.
        /// </summary>
        /// <value></value>
        public IMClient Client
        {
            get
            {
                return this.client;
            }
            set
            {
                this.client = value;
            }
        }

        private string to;

        /// <summary>
        /// Gets or sets the receiver.
        /// </summary>
        /// <value></value>
        public string To
        {
            get
            {
                return this.to;
            }
            set
            {
                this.to = value;
            }
        }

        private Asset asset;

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

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

        /// <summary>
        /// Gets the buffer size.
        /// </summary>
        /// <state></state>
        public long BufferSize
        {
            get
            {
                return 16384;
            }
        }

        private DownloadState state = DownloadState.Idle;

        /// <summary>
        /// Gets or sets the state.
        /// </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));
            }
        }

        private string saveFileName = null;

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

        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>
        /// Stop.
        /// </summary>
        public void Stop()
        {
            if (this.State != DownloadState.Idle)
            {
                MessageDownloadData responseMessage = new MessageDownloadData();
                responseMessage.From = this.Client.Owner.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>
        /// Receive.
        /// </summary>
        public void Receive()
        {
            Stop();

            this.State = DownloadState.Receiving;
        }

        /// <summary>
        /// Starts or stops the download.
        /// </summary>
        public void StartDownload()
        {
            this.State = DownloadState.Idle;

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

            this.Client.Connection.SendMessage(responseMessage);

            this.Receive();
        }

        public event DownloadStateChangedEventHandler StateChanged;

        public event DownloadProgressingEventHandler Progressing;

        public event EventHandler Completed;
    }

}

⌨️ 快捷键说明

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