Exemple #1
0
        private void StoreParameters(List <string> parameters)
        {
            List <CLanFile> batchFiles = CLanFile.GetFiles(parameters);

            // The dispatcher is needed because this method is called from a separated thread
            Current.Dispatcher.Invoke(() =>
            {
                batchFiles.ForEach(SelectedFiles.Add);
            });
        }
        // A common method that handles both selection of files and folders. The interaction
        // logic is the same in both cases, the only different thing is how the CommonOpenFileDialog
        // is instantiated.
        private void selectFilesOrFolders(CommonOpenFileDialog cofd)
        {
            CommonFileDialogResult result = cofd.ShowDialog();

            if (result != CommonFileDialogResult.Ok)
            {
                return;
            }

            // The list of entries selected by the user
            List <String> sFiles = new List <string>(cofd.FileNames);

            // Converts the list of filenames to actual CLanFile objects, expanding the directory structure if needed
            files = CLanFile.GetFiles(sFiles);

            selectionButtonBox.Visibility = Visibility.Hidden;
            continueButtonBox.Visibility  = Visibility.Visible;
        }
Exemple #3
0
        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");
                }
            }
        }