📄 downloaddaemon.cs
字号:
using System;
using System.IO;
using System.Threading;
using Org.InteliIM.Configurations;
using Org.InteliIM.Communications;
namespace Org.InteliIM.Applications.Messenger.FileSharing
{
/// <summary>
/// Download daemon, used for data sender.
/// </summary>
public class DownloadDaemon
{
/// <summary>
/// Initliazes a new instance of the DownloadDaemon class.
/// </summary>
/// <param name="client"></param>
public DownloadDaemon(IMClient client)
{
this.Client = client;
this.Client.MessageReceived += new MessageReceivedOrSentEventHandler(Client_MessageReceived);
}
private Thread threadSend = null;
/// <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.Owner.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>
/// Handles 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();
}
}
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 or sets the size of the buffer (sending or receiving).
/// </summary>
/// <state></state>
public long BufferSize
{
get
{
return 16384;
}
}
private DownloadDaemonState state = DownloadDaemonState.Idle;
/// <summary>
/// Gets or sets the state.
/// </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));
}
}
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>
/// Stop.
/// </summary>
public void Stop()
{
if (this.State != DownloadDaemonState.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);
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();
}
}
}
public event DownloadDaemonStateChangedEventHandler StateChanged;
public event DownloadProgressingEventHandler Progressing;
public event EventHandler Completed;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -