Implement file transfer invalid chunk detection

This commit is contained in:
2021-12-16 16:47:19 +10:00
parent 5b6fd24af4
commit a14b9b0e15
2 changed files with 25 additions and 11 deletions

View File

@ -2,6 +2,7 @@
using MIT_Packets.Transfer;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
@ -27,7 +28,8 @@ namespace MIT_Builder_Client.Handlers
if (packet is EndFileTransferPacket)
{
Console.WriteLine($"Transfer completed. Received {packetsReceived} packets.");
watch.Stop();
Console.WriteLine($"Transfer completed. Received {packetsReceived} packets. Took {watch.Elapsed.Seconds} seconds.");
fs.Flush();
fs.Close();
}
@ -35,8 +37,11 @@ namespace MIT_Builder_Client.Handlers
static int packetsReceived = 0;
static Stopwatch watch = new Stopwatch();
static void StartFileTransfer(StartFileTransferPacket packet)
{
watch.Start();
fileName = packet.FileName + "-transferred";
expectedSize = packet.ExpectedSize;
@ -47,10 +52,20 @@ namespace MIT_Builder_Client.Handlers
static void ReceiveChunk(SendFileChunkPacket packet)
{
Console.WriteLine(packet.ChunkSize);
fs.Write(packet.Chunk, 0, packet.ChunkSize);
currentSize += packet.ChunkSize;
packetsReceived += 1;
//Console.WriteLine("Chunk length: " + packet.Chunk.Length);
//Console.WriteLine("Chunk expected size: " + packet.ChunkSize);
//if (currentSize > 0)
if (packet.ChunkSize < 0 || packet.ChunkSize > packet.Chunk.Length)
Console.WriteLine("Invalid chunk.");
else
{
fs.Write(packet.Chunk, 0, packet.ChunkSize);
currentSize += packet.ChunkSize;
packetsReceived += 1;
}
/*if (((currentSize / expectedSize) * 100) % 25 == 0)
Console.WriteLine("Completed 25%");*/
//Console.WriteLine(currentSize);
}
}