-
Notifications
You must be signed in to change notification settings - Fork 0
/
SFile.cs
138 lines (130 loc) · 4.62 KB
/
SFile.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using System.Net.Sockets;
using System.Text;
using FileSyncServer.Config;
using ProtoBuf;
using RocksDbSharp;
using Serilog;
using TransferLib;
using XXHash3NET;
namespace FileSyncServer;
public class SFile
{
private FileStream _fileStream;
private readonly FsInit _fsInit;
private RocksDb _rocksDb;
private readonly Socket _socket;
public string GetFilePath()
{
return _fsInit.FilePath + "/" + _fsInit.FileName;
}
public SFile(Socket socket, FsInit fsInit, RocksDb rocksDb)
{
_rocksDb = rocksDb;
_socket = socket;
_fsInit = fsInit;
}
public void FileSyncInit()
{
//TODO: Settings.Instance.WorkingDirectory + _fsInit.FilePath may create error if FilePath is going to be relative in the future !
Console.WriteLine();
Console.WriteLine($"WORKING ON FUUID: {_fsInit.FuuId.ToString()}");
Directory.CreateDirectory(Settings.Instance.WorkingDirectory + _fsInit.FilePath);
_fileStream = new FileStream($"{Settings.Instance.WorkingDirectory}{_fsInit.FilePath}/{_fsInit.FileName}",
new FileStreamOptions
{
Mode = FileMode.Create,
Access = FileAccess.ReadWrite,
PreallocationSize = _fsInit.FileSize
});
try
{
byte[] arr = new byte[_fsInit.FuuId.ToByteArray().Length + 1];
arr[0] = 0;
if (!_rocksDb.HasKey(_fsInit.FuuId.ToByteArray()))
{
_fsInit.FuuId.ToByteArray().CopyTo(arr, 1);
Console.WriteLine($"CREATING NEW FILE WITH FUUID:{_fsInit.FuuId.ToString()}");
_rocksDb.Put(_fsInit.FuuId.ToByteArray(), BitConverter.GetBytes((ulong)0));
}
_rocksDb.Put(arr, Encoding.UTF8.GetBytes( $"{_fsInit.FilePath}/{_fsInit.FileName}"));
var serverHash =_rocksDb.Get(_fsInit.FuuId.ToByteArray()); //THE HECK IS THIS ??? (ok this points out to the file hash on server side
Console.WriteLine($"Hash of file with FUUID:" +
$" {_fsInit.FuuId.ToString()}"+
$" is: {BitConverter.ToUInt64(serverHash)}");
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
Console.WriteLine($"{Settings.Instance.WorkingDirectory}{_fsInit.FilePath}/{_fsInit.FileName}");
_rocksDb.Put(new Guid().ToByteArray(), Encoding.UTF8.GetBytes(_fsInit.FilePath+"/"+_fsInit.FileName));
var fsInitResponse = new FSInitResponse
{
FileId = _fsInit.FileId,
IsAccepted = true
};
var memoryStream = new MemoryStream();
Serializer.Serialize(memoryStream, fsInitResponse);
Console.WriteLine("SENT");
lock(_socket){
_socket.Send(new Packet(memoryStream.ToArray(), PacketType.FileSyncInitResponse, (int)memoryStream.Length)
.ToBytes());
}
}
public void WriteData(byte[] data, int length)
{
try
{
//Console.WriteLine($"Written: {length}");
//Console.WriteLine(data[0] + data[1]);
_fileStream.Write(data, 0, length);
}
catch (Exception e)
{
Log.Error(e.ToString());
Console.WriteLine(e);
throw;
}
}
public void CheckHash(FSUploadCheckHash _fsFile)
{
Console.WriteLine("CHECK");
_fileStream.Flush();
ulong hash64;
using var memoryStream = new MemoryStream();
{
hash64 = XXHash3.Hash64(_fileStream);
Console.WriteLine(hash64);
_fileStream.Close();
}
if (_fsFile.Hash == hash64)
{
Serializer.Serialize(memoryStream, new FSUploadCheckHashResponse()
{
FileId = _fsFile.FileId,
IsCorrect = true
});
}
else
{
Serializer.Serialize(memoryStream, new FSUploadCheckHashResponse
{
FileId = _fsFile.FileId,
IsCorrect = false
});
}
lock(_socket){
_socket.Send(new Packet(memoryStream.ToArray(), PacketType.FileSyncUploadCheckHashResponse,
(int)memoryStream.Length)
.ToBytes());
}
}
public void FinishSync()
{
FileInfo fileInfo = new FileInfo($"{Settings.Instance.WorkingDirectory}{_fsInit.FilePath}/{_fsInit.FileName}");
fileInfo.LastAccessTime = _fsInit.LastAccessTime;
fileInfo.LastWriteTime = _fsInit.LastWriteTime;
fileInfo.CreationTime = _fsInit.CreationTime;
}
}