Ejemplo n.º 1
0
 public void TransferProgressChanged(object sender, TransferProgressReport e)
 {
     if (e.PercentDone == 100)
     {
         MyGrid.Style = Application.Current.Resources["InvisibleElement"] as Style;
     }
     else
     {
         MyGrid.Style      = null;
         ProgressBar.Value = e.PercentDone;
         string s = e.IsSender ? "Sending" : "Receiving";
         ActiveItemTextBlock.Text = $"{s} {e.ActiveItem}";
         SizeTextBlock.Text       = e.TotalSize;
     }
 }
Ejemplo n.º 2
0
        private void SendFile(DirectoryItem file, string relativePath)
        {
            if (file.IsFolder)
            {
                throw new FailedSendingException("Cannot send folder as file.");
            }


            // Send file info
            byte[] infoBuffer = new byte[1 + 8 + 1024]; // instruction + fileSize + name (max 256 chars and 4 bytes per char)
            infoBuffer[0] = (byte)TTInstruction.Transfer_FileInfo;

            byte[] sizeBytes = BitConverter.GetBytes(file.Size);
            Array.Copy(sizeBytes, 0, infoBuffer, 1, sizeBytes.Length);

            string fullName = $"{relativePath}{file.Name}";

            byte[] fullNameBytes = Encoding.UTF8.GetBytes(fullName);
            Array.Copy(fullNameBytes, 0, infoBuffer, 1 + 8, fullNameBytes.Length);

            TrySend(infoBuffer, true);


            // Send file
            long bytesToSend              = file.Size;
            bool useEncryption            = serverEncryptor != null;
            TransferProgressReport report = new TransferProgressReport();

            report.TotalBytes   = totalBytes;
            report.ActiveItem   = file.Name;
            report.CurrentBytes = 0;
            report.IsSender     = true;
            transferProgress.Report(report);
            WriteResult result;

            using (FileStream fs = File.OpenRead(file.Path))
            {
                int bufferSize = 0;
                while (bytesToSend > 0)
                {
                    try
                    {
                        bufferSize = (int)Math.Min(bytesToSend, maxBufferSize);

                        byte[] buffer = new byte[bufferSize];
                        fs.Read(buffer, 0, bufferSize);

                        if (useEncryption)
                        {
                            buffer = serverEncryptor.AESEncryptBytes(buffer);
                        }

                        result = client.Send(buffer);
                        if (result.Status != WriteResultStatus.Success)
                        {
                            throw new FailedSendingException("Could not send data.");
                        }
                    }
                    catch (Exception e)
                    {
                        fs.Flush();
                        throw new FailedSendingException($"Failed while sending '{file.Name}' ({e.Message}).");
                    }


                    bytesToSend -= bufferSize;
                    bytesSent   += bufferSize;


                    if (bytesSent >= report.CurrentBytes + (totalBytes / 100) || bytesToSend == 0)
                    {
                        report.CurrentBytes = bytesSent;
                        transferProgress.Report(report);
                    }
                }
                fs.Flush();
            }
        }
Ejemplo n.º 3
0
        private void ReceiveFile(string fullName, long size)
        {
            string fileName = fullName;
            int    idx      = fullName.LastIndexOf('\\');

            if (idx != -1)
            {
                fileName = fullName.Substring(idx + 1);
            }


            // Receive
            long bytesToReceive           = size;
            bool useEncryption            = clientEncryptor != null;
            TransferProgressReport report = new TransferProgressReport();

            report.TotalBytes   = totalBytes;
            report.ActiveItem   = fileName;
            report.CurrentBytes = 0;
            report.IsSender     = false;
            transferProgress.Report(report);
            using (FileStream fs = File.Create($"{saveLocation}\\{fullName}"))
            {
                ReadResult result;
                int        bufferSize = 0;
                while (bytesToReceive > 0)
                {
                    try
                    {
                        bufferSize = (int)Math.Min(bytesToReceive, maxBufferSize);
                        byte[] receivedBuffer = null;
                        if (useEncryption)
                        {
                            result         = server.Read(clientIpPort, DataEncryptor.PredictAESLength(bufferSize));
                            receivedBuffer = clientEncryptor.AESDecryptBytes(result.Data);
                        }
                        else
                        {
                            result         = server.Read(clientIpPort, bufferSize);
                            receivedBuffer = result.Data;
                        }


                        if (result.Status != ReadResultStatus.Success)
                        {
                            fs.Flush();
                            throw new Exception("Did not receive data in time.");
                        }


                        fs.Write(receivedBuffer, 0, bufferSize);
                    }
                    catch (Exception e)
                    {
                        fs.Flush();
                        throw new FailedReceivingException($"Failed while receiving '{fileName}' ({e.Message}).");
                    }


                    bytesToReceive -= bufferSize;
                    bytesReceived  += bufferSize;


                    if (bytesReceived >= report.CurrentBytes + (totalBytes / 100) || bytesToReceive == 0)
                    {
                        report.CurrentBytes = bytesReceived;
                        transferProgress.Report(report);
                    }
                }
                fs.Flush();
            }
        }