Example #1
0
        private void ReceiveItem()
        {
            CommunicationResult res;


            // Receive item info
            int length = 1 + 8 + 1024;

            if (clientEncryptor != null)
            {
                length = DataEncryptor.PredictAESLength(length);
            }
            res = TryRead(clientIpPort, length, true);

            if (!(res.Instruction == TTInstruction.Transfer_FileInfo || res.Instruction == TTInstruction.Transfer_FolderInfo))
            {
                throw new FailedReceivingException("Client sent unsupported item info.");
            }


            // Receive item
            long   size     = BitConverter.ToInt64(res.Data, 0);
            string fullName = Encoding.UTF8.GetString(res.Data, 8, 512 - 9).Replace("\0", string.Empty);

            if (res.Instruction == TTInstruction.Transfer_FileInfo)
            {
                ReceiveFile(fullName, size);
            }
            else
            {
                ReceiveFolder(fullName);
            };
        }
Example #2
0
        // Data
        private async Task ReceiveData()
        {
            transferStopwatch = new Stopwatch();
            transferStopwatch.Start();
            await Task.Run(() =>
            {
                // Receive info about transfer
                int length = 13;
                if (clientEncryptor != null)
                {
                    length = DataEncryptor.PredictAESLength(length);
                }
                CommunicationResult res = TryRead(clientIpPort, length, true);


                // Receive data
                int itemCount = 0;
                if (res.Instruction == TTInstruction.Transfer_TransferInfo)
                {
                    // Receive files individually
                    itemCount  = BitConverter.ToInt32(res.Data, 0);
                    totalBytes = BitConverter.ToInt64(res.Data, 4);

                    for (int i = 0; i < itemCount; i++)
                    {
                        ReceiveItem();
                    }
                }
                else
                {
                    throw new FailedReceivingException("Client sent wrong instruction about transfer.");
                }

                transferStopwatch.Stop();
                OnRecordableEvent($"Sucessfully received {itemCount} item/s ({Explorer.ExplorerControl.FormatFileSize(bytesReceived)}) in {TTNet.FormatTimeSpan(transferStopwatch.Elapsed)} at average { Explorer.ExplorerControl.FormatFileSize(bytesReceived * 1000 / transferStopwatch.ElapsedMilliseconds)}/s.", Console.ConsoleMessageType.Common);
            });
        }
Example #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();
            }
        }