public void HandleTransferAccepted(object sender, EventArgs e) { CLanFileTransferRequest req = sender as CLanFileTransferRequest; CLanFileTransfer cft = new CLanFileTransfer(req.From, req.Files, CLanTransferType.RECEIVE); cft.Start(); // Start the working thread, that will also ask for the save directory }
public void OnTransferRemoved(CLanFileTransfer ctf) { TransferRemoved?.Invoke(this, ctf); }
public void ReceiveFiles(CLanFileTransfer cft, string rootFolder) { Socket other = cft.currentSocket; List <CLanFile> files = CLanFile.EnforceDuplicatePolicy(cft.Files, rootFolder); BackgroundWorker bw = cft.BW; long totalSize = files.Sum(f => f.Size); long receivedSize = 0; byte[] buffer = new byte[BUFFER_SIZE]; Stopwatch sw = Stopwatch.StartNew(); using (other) { // Here I already applied the renaming/overwriting policy for duplicate files, // so I can simply receive them foreach (CLanFile f in files) { if (bw.CancellationPending) { break; } // Update the View cft.CurrentFile = f.Name; long currentReceivedSize = 0; using (NetworkStream stream = new NetworkStream(other)) using (FileStream fstream = new FileStream(rootFolder + f.Name, FileMode.OpenOrCreate, FileAccess.Write)) { if (stream.CanRead) { int oldProgress = 0; long oldSecondsLeft = 0; int timeUpdateCounter = 0; int timeUpdateStep = 8092; int progressSteadyStep = 10; while (currentReceivedSize < f.Size && !bw.CancellationPending) { Array.Clear(buffer, 0, BUFFER_SIZE); int size = stream.Read(buffer, 0, (int)Math.Min(BUFFER_SIZE, f.Size - currentReceivedSize)); if (size <= 0) // I am receiving and the sender closed the transfer { break; } fstream.Write(buffer, 0, size); currentReceivedSize += size; receivedSize += size; int progress = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(receivedSize) * 100 / Convert.ToDouble(totalSize))); if (oldProgress != progress) { oldProgress = progress; bw.ReportProgress(progress); } if (progress > progressSteadyStep) { timeUpdateStep = 1024; timeUpdateCounter = 0; } // Update the time left once any 512 packets (max 512KB) received if (timeUpdateCounter == 0) { long sizeUpToNow = receivedSize; long timeUpToNow = sw.ElapsedMilliseconds; long sizeLeft = totalSize - receivedSize; long timeLeft = (timeUpToNow * sizeLeft) / sizeUpToNow; long secondsLeft = timeLeft / 1000; if (oldSecondsLeft != secondsLeft) { oldSecondsLeft = secondsLeft; cft.UpdateTimeLeft(secondsLeft); } } timeUpdateCounter = (timeUpdateCounter++) % timeUpdateStep; } } } if (bw.CancellationPending) { // I am the receiver and I stopped the transfer // Delete pending file File.Delete(rootFolder + f.Name); // It will jump out of the foreach, closing the socket, causing an exception to occur on the other side, // that will be catched to acknowledged the end of the transfer break; } if (currentReceivedSize != f.Size) { // I am the receiver and the sender stopped the transfer Trace.WriteLine("Transfer was cancelled by the sender"); // Delete pending file File.Delete(rootFolder + f.Name); break; } Trace.WriteLine("File received"); } } }
public void SendFiles(CLanFileTransfer cft) { Socket other = cft.currentSocket; List <CLanFile> files = cft.Files; BackgroundWorker bw = cft.BW; long totalSize = files.Sum(f => f.Size); long sentSize = 0; byte[] buffer = new byte[BUFFER_SIZE]; Stopwatch sw = Stopwatch.StartNew(); using (other) { foreach (CLanFile f in files) { if (bw.CancellationPending) { break; } // Update the View cft.CurrentFile = f.Name; long currentSentSize = 0; using (NetworkStream stream = new NetworkStream(other)) using (FileStream fstream = new FileStream(f.RelativePath, FileMode.Open, FileAccess.Read)) { try { if (stream.CanWrite) { int oldProgress = 0; long oldSecondsLeft = 0; int timeUpdateCounter = 0; int timeUpdateStep = 8092; int progressSteadyStep = 10; while (currentSentSize < f.Size && !bw.CancellationPending) { Array.Clear(buffer, 0, BUFFER_SIZE); int size = fstream.Read(buffer, 0, BUFFER_SIZE); stream.Write(buffer, 0, size); currentSentSize += size; sentSize += size; int progress = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(sentSize) * 100 / Convert.ToDouble(totalSize))); if (oldProgress != progress) { oldProgress = progress; bw.ReportProgress(progress); } if (progress > progressSteadyStep) { timeUpdateStep = 1024; timeUpdateCounter = 0; } // Update the time left once any 512 packets (max 512KB) sent if (timeUpdateCounter == 0) { long sizeUpToNow = sentSize; long timeUpToNow = sw.ElapsedMilliseconds; long sizeLeft = totalSize - sentSize; long timeLeft = (timeUpToNow * sizeLeft) / sizeUpToNow; long secondsLeft = timeLeft / 1000; if (oldSecondsLeft != secondsLeft) { oldSecondsLeft = secondsLeft; cft.UpdateTimeLeft(secondsLeft); } } timeUpdateCounter = (timeUpdateCounter++) % timeUpdateStep; } // I am the sender and I stopped the transfer if (bw.CancellationPending) { Trace.WriteLine("Transfer was cancelled by me"); break; } } } catch (IOException ioe) { // I am the sender and the receiver stopped the transfer Trace.WriteLine("The receiver stopped the transfer"); break; } } } } }