예제 #1
0
        private bool PopulateDirectory(TcpClient tcpClient, DirectoryNode directoryNode, string destinationDir)
        {
            string directoryPath  = destinationDir + "\\" + directoryNode.DirectoryName;
            string senderUserName = directoryNode.SenderUserName;

            directoryPath = FilesUtils.CreateUniqueDirectory(directoryPath);

            foreach (var fileNode in directoryNode.FileNodes)
            {
                fileNode.SenderUserName = senderUserName;
                UpdateDict(tcpClient, fileNode, directoryPath);
                //bool completed = ReceiveDirectoryFile(tcpClient, fileNode, directoryPath);

                //if (!completed)
                //    return false;
            }

            foreach (var innerDirectoryNode in directoryNode.DirectoryNodes)
            {
                innerDirectoryNode.SenderUserName = senderUserName;
                bool completed = PopulateDirectory(tcpClient, innerDirectoryNode, directoryPath);

                if (!completed)
                {
                    return(false);
                }
            }

            return(true);
        }
예제 #2
0
        private DirectoryNode SendDirectoryNodeDescription(TcpClient tcpClient)
        {
            // preparing directory node
            DirectoryNode directoryNode = FilesUtils.BuildDirectoryNode(_path);

            directoryNode.SenderUserName = Properties.Settings.Default.Name;

            // sending directory node description
            string jsonDirectoryNodeDescription = JsonConvert.SerializeObject(directoryNode);

            TcpUtils.SendDescription(tcpClient, jsonDirectoryNodeDescription);

            return(directoryNode);
        }
예제 #3
0
        private bool ReceiveFileContent(TcpClient tcpClient, FileNode fileNode, string destinationDir)
        {
            string filePath = destinationDir + "\\" + fileNode.Name;

            NetworkStream networkStream = tcpClient.GetStream();

            networkStream.ReadTimeout = Constants.TRANSFER_TCP_READ_TIMEOUT * 1000;

            long fileContentLenghtReceived = 0;

            Byte[]     buffer     = new Byte[Constants.TRANSFER_TCP_BUFFER];
            FileStream fileStream = FilesUtils.CreateUniqueFile(filePath);

            fileNode.Name = Path.GetFileName(fileStream.Name);
            BinaryWriter fileWriter   = new BinaryWriter(fileStream);
            DateTime     baseDateTime = DateTime.Now;

            FileTransfer fileTransfer = new FileTransfer()
            {
                File                     = fileNode,
                Progress                 = 0,
                ContinueFileTransfer     = true,
                Status                   = TransferStatus.Pending,
                Sending                  = false,
                DestinationDirectoryPath = destinationDir,
                ManagementDateTime       = DateTime.Now
            };

            NewTransferEvent(fileTransfer);

            // FILE CONTENT
            while (fileContentLenghtReceived < fileNode.Dimension && fileTransfer.ContinueFileTransfer == true)
            {
                int bytesRead;

                if ((fileNode.Dimension - fileContentLenghtReceived) < buffer.Length)
                {
                    buffer = new Byte[fileNode.Dimension - fileContentLenghtReceived];
                }

                try
                {
                    IsConnected(tcpClient);
                    bytesRead = networkStream.Read(buffer, 0, buffer.Length);
                }
                catch (IOException ioe)
                {
                    // sender closed connection
                    fileWriter.Close();

                    File.Delete(fileStream.Name);
                    fileTransfer.Status = TransferStatus.Error;

                    return(false);
                }

                fileWriter.Write(buffer, 0, bytesRead);
                fileContentLenghtReceived += bytesRead;

                Thread.Sleep(Constants.TRANSFER_TCP_RECEIVER_DELAY);
                double progress = ((double)fileContentLenghtReceived / (double)fileNode.Dimension) * 100;
                fileTransfer.Progress = progress;

                TimeSpan remainingTimeSpan = TcpUtils.ComputeRemainingTime(baseDateTime, bytesRead, fileContentLenghtReceived, fileNode.Dimension);
                fileTransfer.RemainingTime = remainingTimeSpan;
                baseDateTime = DateTime.Now;
            }

            fileWriter.Close();

            if (!fileTransfer.ContinueFileTransfer)
            {
                File.Delete(fileStream.Name);
                fileTransfer.Status = TransferStatus.Canceled;

                return(false);
            }
            else
            {
                fileTransfer.Status = TransferStatus.Completed;

                return(true);
            }
        }