Start on File Transfer Packets & Handler Implementation

This commit is contained in:
2021-12-15 15:18:47 +10:00
parent e5b64f600c
commit 5b6fd24af4
18 changed files with 849 additions and 17 deletions
+15
View File
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MIT_Builder_Client
{
public class Configuration
{
public string Username { get; set; }
public string Password { get; set; }
public int Drivers { get; set; }
}
}
@@ -0,0 +1,57 @@
using Articulate_Network.Packets;
using MIT_Packets.Transfer;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MIT_Builder_Client.Handlers
{
public class FileTransferHandler
{
static string fileName;
static long expectedSize;
static long currentSize = 0;
static FileStream fs;
public static void HandleTransfer(Packet packet)
{
if (packet is StartFileTransferPacket)
StartFileTransfer((StartFileTransferPacket)packet);
if (packet is SendFileChunkPacket)
ReceiveChunk((SendFileChunkPacket)packet);
if (packet is EndFileTransferPacket)
{
Console.WriteLine($"Transfer completed. Received {packetsReceived} packets.");
fs.Flush();
fs.Close();
}
}
static int packetsReceived = 0;
static void StartFileTransfer(StartFileTransferPacket packet)
{
fileName = packet.FileName + "-transferred";
expectedSize = packet.ExpectedSize;
Console.WriteLine($"Preparing to receive {fileName} expected size: {expectedSize}");
fs = File.Create(fileName);
}
static void ReceiveChunk(SendFileChunkPacket packet)
{
Console.WriteLine(packet.ChunkSize);
fs.Write(packet.Chunk, 0, packet.ChunkSize);
currentSize += packet.ChunkSize;
packetsReceived += 1;
//Console.WriteLine(currentSize);
}
}
}
@@ -36,6 +36,9 @@
<Reference Include="Articulate-Network">
<HintPath>..\..\Articulate-Network\bin\Debug\Articulate-Network.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.13.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
@@ -46,11 +49,20 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Configuration.cs" />
<Compile Include="Handlers\FileTransferHandler.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MIT Packets\MIT Packets.csproj">
<Project>{EC6CF112-FDFE-4ACD-98E2-372E0D9ED908}</Project>
<Name>MIT Packets</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
+37 -3
View File
@@ -1,8 +1,14 @@
using Articulate_Network;
using Articulate_Network.Managers;
using MIT_Builder_Client.Handlers;
using MIT_Packets;
using MIT_Packets.Enums;
using MIT_Packets.Transfer;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
@@ -10,21 +16,49 @@ namespace MIT_Builder_Client
{
class Program
{
static Client client;
public static ClientStatus status = ClientStatus.Waiting;
static void Main(string[] args)
{
Client client = new Client();
AnnouncePacket.Register();
client.Connect("127.0.0.1", 50).ContinueWith(t =>
client = new Client();
client.Connect("10.136.5.59", 50).ContinueWith(t =>
{
if (t.Result.Connected)
{
Console.WriteLine();
Console.WriteLine("Successfully connected to master server.");
AnnounceClient();
}
});
client.DataReceived += Client_DataReceived;
Console.ReadLine();
}
static void AnnounceClient()
{
string macAddress = NetworkInterface.GetAllNetworkInterfaces().Where(nic => nic.OperationalStatus == OperationalStatus.Up).Select(nic => nic.GetPhysicalAddress().ToString()).FirstOrDefault();
client.SendPacket(new AnnouncePacket()
{
Type = 1,
RequestConfig = true,
MacAddress = macAddress
});
}
private static void Client_DataReceived(object sender, Articulate_Network.Events.DataReceivedEventArgs e)
{
//Console.WriteLine($"Received Packet: {e.Packet.GetType().Name}");
if (e.Packet is StartFileTransferPacket || e.Packet is SendFileChunkPacket || e.Packet is EndFileTransferPacket)
FileTransferHandler.HandleTransfer(e.Packet);
}
}
}
+4
View File
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="13.0.1" targetFramework="net46" />
</packages>