private void SendFileContent(TcpClient tcpClient, string filePath) { // preparing file stream for reading FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read); long fileDimension = fileStream.Length; // preparing network stream for writing NetworkStream networkStream = tcpClient.GetStream(); networkStream.WriteTimeout = Constants.TRANSFER_TCP_WRITE_TIMEOUT; // preparing file node for file transfer FileNode fileNode = new FileNode { Name = Path.GetFileName(filePath), SenderUserName = Properties.Settings.Default.Name, ReceiverUserName = _destinationUserName }; // preparing file transfer for main window FileTransfer fileTransfer = new FileTransfer() { File = fileNode, Progress = 0, ContinueFileTransfer = true, Status = TransferStatus.Pending, Sending = true, ManagementDateTime = DateTime.Now }; NewTransferEvent(fileTransfer); Byte[] fileContentBuffer = new Byte[Constants.TRANSFER_TCP_BUFFER]; int bytesRead; long totalBytesRead = 0; // start time for progress bar value calculation DateTime startDateTime = DateTime.Now; // iterating while reading file while ((bytesRead = fileStream.Read(fileContentBuffer, 0, fileContentBuffer.Length)) > 0 && fileTransfer.ContinueFileTransfer) { try { // writing file content on network stream networkStream.Write(fileContentBuffer, 0, bytesRead); } catch (IOException ioe) { fileTransfer.Status = TransferStatus.Error; return; } totalBytesRead += bytesRead; double progress = (((double)totalBytesRead / (double)fileStream.Length) * 100); fileTransfer.Progress = progress; Thread.Sleep(Constants.TRANSFER_TCP_SENDER_DELAY); TimeSpan remainingTimeSpan = TcpUtils.ComputeRemainingTime(startDateTime, bytesRead, totalBytesRead, fileDimension); fileTransfer.RemainingTime = remainingTimeSpan; // updating start time startDateTime = DateTime.Now; } fileStream.Close(); if (fileTransfer.ContinueFileTransfer) { fileTransfer.Status = TransferStatus.Completed; } else { fileTransfer.Status = TransferStatus.Canceled; } }
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); } }