📄 abstractfiletransportor.cs
字号:
namespace Imps.Client.Core
{
using Imps.Client.Core.P2P.AsyncTransportor;
using Imps.Client.Core.P2P.BlockingTransportor;
using Imps.Client.Core.P2P.FileTransportor;
using Imps.Client.Core.P2P.ICE;
using Imps.Client.Utils;
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Threading;
public abstract class AbstractFileTransportor : IFileTransportor
{
protected readonly Stopwatch elapsedTime = new Stopwatch();
protected readonly ManualResetEvent pauseEvent = new ManualResetEvent(true);
protected TransportState state;
protected TransportingFile targetFile;
protected long transportedBytes;
public event EventHandler<BlockTranEventArgs> BlockTransported;
public event EventHandler TransportCompleted;
public event EventHandler<TranFailedEventArgs> TransportFailed;
protected AbstractFileTransportor()
{
}
public static IFileTransportor CreateReceiver(PunchingResult result, TransportingFile file)
{
try
{
switch (result.Mode)
{
case TransportMode.P2PV2:
return new Imps.Client.Core.P2P.AsyncTransportor.Receiver(result, file);
case TransportMode.P2PV3:
return new Imps.Client.Core.P2P.BlockingTransportor.Receiver(result, file);
}
return null;
}
catch (Exception exception)
{
ClientLogger.WriteException(exception);
return null;
}
}
public static IFileTransportor CreateSender(PunchingResult result, TransportingFile file)
{
try
{
switch (result.Mode)
{
case TransportMode.P2PV2:
return new Imps.Client.Core.P2P.AsyncTransportor.Sender(result, file);
case TransportMode.P2PV3:
return new Imps.Client.Core.P2P.BlockingTransportor.Sender(result, file);
}
return null;
}
catch (Exception exception)
{
ClientLogger.WriteException(exception);
return null;
}
}
protected void FireBlockTransported(BlockTranEventArgs e)
{
if (this.BlockTransported != null)
{
try
{
this.BlockTransported.Invoke(this, e);
}
catch
{
}
}
}
protected void FireTransportCompleted()
{
if (this.TransportFailed != null)
{
try
{
this.TransportCompleted(this, EventArgs.Empty);
}
catch
{
}
}
}
protected void FireTransportFailed(TranFailedEventArgs e)
{
this.state = TransportState.Failed;
if (this.TransportFailed != null)
{
try
{
this.TransportFailed.Invoke(this, e);
}
catch
{
}
}
}
public virtual void Pause()
{
if (this.state == TransportState.Transporting)
{
this.state = TransportState.Paused;
this.pauseEvent.Reset();
}
}
public virtual void Resume()
{
if (this.state == TransportState.Paused)
{
this.state = TransportState.Transporting;
this.pauseEvent.Set();
}
}
public virtual void Start()
{
if (this.state == TransportState.Init)
{
this.state = TransportState.Transporting;
this.elapsedTime.Start();
}
}
public virtual void Stop()
{
if ((this.state == TransportState.Paused) || (this.state == TransportState.Transporting))
{
this.elapsedTime.Stop();
this.state = TransportState.Stoped;
}
}
public virtual int Rate
{
get
{
if (this.elapsedTime.get_ElapsedMilliseconds() > 0)
{
return (int) ((0x3e8 * this.transportedBytes) / this.elapsedTime.get_ElapsedMilliseconds());
}
return 0;
}
}
public TransportState State
{
get
{
return this.state;
}
}
public virtual TransportingFile TargetFile
{
get
{
return this.targetFile;
}
set
{
if ((this.targetFile == null) && (value != null))
{
this.targetFile = value;
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -