public static async Task ReceiveFileWithStreams(long fileSize, string fileName, StreamCommunication streamCommunication) { long fileParts = ProtocolHelpers.CalculateFileParts(fileSize); long offset = 0; long currentPart = 1; while (fileSize > offset) { byte[] data; if (currentPart == fileParts) { var lastPartSize = (int)(fileSize - offset); data = await streamCommunication.ReadAsync(lastPartSize); offset += lastPartSize; } else { data = await streamCommunication.ReadAsync(ProtocolConstants.MaxPacketSize); offset += ProtocolConstants.MaxPacketSize; } FileStreamHandler.Write(fileName, data); currentPart++; } }
public static void SendFileWithStream(long fileSize, string path, StreamCommunication streamCommunication) { long fileParts = ProtocolHelpers.CalculateFileParts(fileSize); long offset = 0; long currentPart = 1; while (fileSize > offset) { byte[] data; if (currentPart == fileParts) { var lastPartSize = (int)(fileSize - offset); data = FileStreamHandler.Read(path, offset, lastPartSize); offset += lastPartSize; } else { data = FileStreamHandler.Read(path, offset, ProtocolConstants.MaxPacketSize); offset += ProtocolConstants.MaxPacketSize; } streamCommunication.Write(data); currentPart++; } }