Fixed bug where files over 5kb were written incorrectly

This commit is contained in:
EntenKoeniq
2022-04-06 10:43:32 +02:00
parent ce00f07e89
commit 5991aba8fc
4 changed files with 13 additions and 19 deletions

View File

@ -74,7 +74,7 @@ namespace CoopClient
return false; return false;
} }
public static void Write(byte id, byte[] data) public static void Write(byte id, byte[] chunk)
{ {
lock (_filesFinished) lock (_filesFinished)
{ {
@ -94,7 +94,7 @@ namespace CoopClient
return; return;
} }
fs.Write(data, 0, data.Length); fs.Write(chunk, 0, chunk.Length);
lock (_downloadFiles) lock (_downloadFiles)
{ {
@ -105,7 +105,7 @@ namespace CoopClient
return; return;
} }
file.FileWritten += data.Length; file.FileWritten += chunk.Length;
if (file.FileWritten >= file.FileLength) if (file.FileWritten >= file.FileLength)
{ {

View File

@ -467,8 +467,6 @@ namespace CoopClient
{ {
GTA.UI.Notification.Show("~r~~h~Packet Error"); GTA.UI.Notification.Show("~r~~h~Packet Error");
Logger.Write($"[{packetType}] {ex.Message}", Logger.LogLevel.Server); Logger.Write($"[{packetType}] {ex.Message}", Logger.LogLevel.Server);
Logger.Write($"[{packetType}] {ex.Source}", Logger.LogLevel.Server);
Logger.Write($"[{packetType}] {ex.StackTrace}", Logger.LogLevel.Server);
Client.Disconnect($"Packet Error [{packetType}]"); Client.Disconnect($"Packet Error [{packetType}]");
} }
} }

View File

@ -3,7 +3,7 @@
<PropertyGroup> <PropertyGroup>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework> <TargetFramework>net6.0</TargetFramework>
<AssemblyVersion>1.47.4.0001</AssemblyVersion> <AssemblyVersion>1.47.5.0001</AssemblyVersion>
<FileVersion>1.0.0.0</FileVersion> <FileVersion>1.0.0.0</FileVersion>
<RepositoryUrl>https://github.com/GTACOOP-R/GTACoop-R</RepositoryUrl> <RepositoryUrl>https://github.com/GTACOOP-R/GTACoop-R</RepositoryUrl>
</PropertyGroup> </PropertyGroup>

View File

@ -54,27 +54,26 @@ namespace CoopServer
continue; continue;
} }
int MAX_BUFFER = fileInfo.Length < 5120 ? (int)fileInfo.Length : 5120; // 5KB const int MAX_BUFFER = 5120; // 5KB
byte[] buffer = new byte[MAX_BUFFER]; byte[] buffer = new byte[MAX_BUFFER];
ushort bytesRead = 0;
bool fileCreated = false; bool fileCreated = false;
DownloadFile newFile = null; DownloadFile newFile = null;
using (FileStream fs = File.Open(file, FileMode.Open, FileAccess.Read)) using (FileStream fs = File.Open(file, FileMode.Open, FileAccess.Read))
using (BufferedStream bs = new(fs)) using (BufferedStream bs = new(fs))
{ {
while (bs.Read(buffer, 0, MAX_BUFFER) != 0) // Reading 5KB chunks at time while ((bytesRead = (ushort)bs.Read(buffer, 0, MAX_BUFFER)) != 0) // Reading 5KB chunks at time
{ {
if (!fileCreated && (fileCreated = true)) if (!fileCreated && (fileCreated = true))
{ {
newFile = new() { FileID = fileCount, FileName = fileInfo.Name, FileLength = fileInfo.Length, FileData = new() }; newFile = new() { FileID = fileCount, FileName = fileInfo.Name, FileLength = fileInfo.Length, FileChunks = new() };
} }
newFile.FileData.Add(buffer); newFile.FileChunks.Add(buffer.Take(bytesRead).ToArray());
Logging.Debug($"[{fileInfo.Name}] {buffer.Length}");
} }
} }
Logging.Debug($"[{fileInfo.Name}] RESULT {newFile.FileData.Count} / {newFile.FileLength}");
_files.Add(newFile); _files.Add(newFile);
fileCount++; fileCount++;
} }
@ -197,7 +196,7 @@ namespace CoopServer
FileLength = file.FileLength FileLength = file.FileLength
}.PacketToNetOutGoingMessage(outgoingMessage); }.PacketToNetOutGoingMessage(outgoingMessage);
Server.MainNetServer.SendMessage(outgoingMessage, conn, NetDeliveryMethod.ReliableUnordered, (byte)ConnectionChannel.File); Server.MainNetServer.SendMessage(outgoingMessage, conn, NetDeliveryMethod.ReliableOrdered, (byte)ConnectionChannel.File);
}); });
} }
@ -229,7 +228,7 @@ namespace CoopServer
Send(NetHandle, file); Send(NetHandle, file);
if (_fileDataPosition >= file.FileData.Count) if (_fileDataPosition >= file.FileChunks.Count)
{ {
FilePosition++; FilePosition++;
_fileDataPosition = 0; _fileDataPosition = 0;
@ -240,8 +239,6 @@ namespace CoopServer
private void Send(long nethandle, DownloadFile file) private void Send(long nethandle, DownloadFile file)
{ {
Logging.Debug($"SEND [{file.FileName}][{_fileDataPosition}/{file.FileData.Count - 1}]");
NetConnection conn = Server.MainNetServer.Connections.FirstOrDefault(x => x.RemoteUniqueIdentifier == nethandle); NetConnection conn = Server.MainNetServer.Connections.FirstOrDefault(x => x.RemoteUniqueIdentifier == nethandle);
if (conn == null) if (conn == null)
{ {
@ -251,7 +248,7 @@ namespace CoopServer
NetOutgoingMessage outgoingMessage = Server.MainNetServer.CreateMessage(); NetOutgoingMessage outgoingMessage = Server.MainNetServer.CreateMessage();
new Packets.FileTransferTick() { ID = file.FileID, FileChunk = file.FileData[_fileDataPosition++] }.PacketToNetOutGoingMessage(outgoingMessage); new Packets.FileTransferTick() { ID = file.FileID, FileChunk = file.FileChunks[_fileDataPosition++] }.PacketToNetOutGoingMessage(outgoingMessage);
Server.MainNetServer.SendMessage(outgoingMessage, conn, NetDeliveryMethod.ReliableUnordered, (byte)ConnectionChannel.File); Server.MainNetServer.SendMessage(outgoingMessage, conn, NetDeliveryMethod.ReliableUnordered, (byte)ConnectionChannel.File);
} }
@ -267,7 +264,6 @@ namespace CoopServer
public byte FileID { get; set; } = 0; public byte FileID { get; set; } = 0;
public string FileName { get; set; } = string.Empty; public string FileName { get; set; } = string.Empty;
public long FileLength { get; set; } = 0; public long FileLength { get; set; } = 0;
public List<byte[]> FileChunks { get; set; } = null;
public List<byte[]> FileData { get; set; } = null;
} }
} }