You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Zero/ZeroLevel/Services/Network/FileTransfer/FileReader.cs

58 lines
1.8 KiB

5 years ago
using System;
using System.Collections.Generic;
using System.IO;
using ZeroLevel.Services.HashFunctions;
5 years ago
5 years ago
namespace ZeroLevel.Network.FileTransfer
5 years ago
{
internal sealed class FileReader
{
private readonly FileStartFrame _startInfo;
public string Path { get; }
private const int CHUNK_SIZE = 512 * 1024;
public FileReader(string path)
{
Path = path;
_startInfo = FileStartFrame.GetTransferFileInfo(path);
}
public FileStartFrame GetStartInfo()
{
return _startInfo;
}
public IEnumerable<FileFrame> Read()
{
long offset = 0;
using (FileStream stream = new FileStream(Path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
int bytesRead;
var buffer = new byte[CHUNK_SIZE];
while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0)
{
var fragment = new FileFrame
{
5 years ago
UploadFileTaskId = _startInfo.UploadFileTaskId,
5 years ago
Offset = offset * CHUNK_SIZE,
Payload = new byte[bytesRead]
};
Array.Copy(buffer, 0, fragment.Payload, 0, bytesRead);
var hash = Murmur3.ComputeHash(fragment.Payload);
fragment.ChecksumL = BitConverter.ToUInt64(hash, 0);
fragment.ChecksumH = BitConverter.ToUInt64(hash, 8);
5 years ago
offset = offset + 1;
yield return fragment;
}
5 years ago
}
5 years ago
}
public FileEndFrame GetCompleteInfo()
{
5 years ago
return new FileEndFrame { UploadFileTaskId = _startInfo.UploadFileTaskId };
5 years ago
}
}
}

Powered by TurnKey Linux.