public TransferEventArgs(TransferConfig config) { this.Config = config; }
public void Send(TransferConfig config, IPEndPoint remoteIpe) { Contract.Requires(config != null && remoteIpe != null); var controlChunk = new SendChunkModel(remoteIpe); controlChunk.Client.Send(config); var e = new TransferEventArgs(config); this.OnPrepare(e); byte[] buffer = new byte[1]; controlChunk.Client.Receive(buffer); if (e.Cancel || buffer[0] == 0 || !controlChunk.Client.Connected) { controlChunk.Client.Close(); return; } var chunkGroup = new SendChunkModel[config.ChunkCount]; chunkGroup[0] = controlChunk; for (int i = 1; i < chunkGroup.Length; i++) { chunkGroup[i] = new SendChunkModel(remoteIpe); } e.Progress = new TransferProgress(); e.Progress.Start(config.FileLength); #region Breakpoint int perPairCount = PerLongSize * 2, count = perPairCount * chunkGroup.Length; byte[] bufferInfo = new byte[count]; long oddSize, avgSize = Math.DivRem(config.FileLength, (long)chunkGroup.Length, out oddSize); if (controlChunk.Client.Receive(bufferInfo) == 4) { for (int i = 0, j = chunkGroup.Length - 1; i < chunkGroup.Length; i++) { chunkGroup[i].Initialize(config.FilePath, i * avgSize, i == j ? avgSize + oddSize : avgSize); } } else { long fValue, tValue; for (int i = 0, j = chunkGroup.Length - 1; i < chunkGroup.Length; i++) { fValue = BitConverter.ToInt64(bufferInfo, i * perPairCount); tValue = BitConverter.ToInt64(bufferInfo, i * perPairCount + PerLongSize); chunkGroup[i].Initialize(config.FilePath, i * avgSize, i == j ? avgSize + oddSize : avgSize, fValue, tValue); App.LogDebug("[SendBreakpoint] {0} {1}:{2}/{3}.", remoteIpe, i, fValue, tValue); } } #endregion TaskHelper.Factory.StartNew(() => Parallel.ForEach(chunkGroup, chunk => chunk.Run())); long bytesTransferred = 0L; do { chunkGroup.ReportSpeed(e.Progress, ref bytesTransferred); this.OnProgressChanged(e); Thread.Sleep(1000); }while (!chunkGroup.IsAllCompleted()); chunkGroup.ReportSpeed(e.Progress, ref bytesTransferred); this.OnProgressChanged(e); e.Progress.Stop(); this.OnCompleted(e); }