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

View File

@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
namespace MIT_Master_Server.Handlers
{
public class ConfigurationHandler
{
public void SendToClient(TcpClient client)
{
}
}
}

View File

@ -49,6 +49,7 @@
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Handlers\ConfigurationHandler.cs" />
<Compile Include="Program.cs" /> <Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup> </ItemGroup>

View File

@ -2,9 +2,11 @@
using Articulate_Network.Events; using Articulate_Network.Events;
using Articulate_Network.Managers; using Articulate_Network.Managers;
using MIT_Packets; using MIT_Packets;
using MIT_Packets.Transfer;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.IO;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Text; using System.Text;
@ -15,15 +17,19 @@ namespace MIT_Master_Server
{ {
class Program class Program
{ {
static Server server;
static FileInfo file = new FileInfo(@"D:\iPad_10.2_15.1_19B74_Restore.ipsw");
static int maxConcurrentTransfers = 5;
static void Main(string[] args) static void Main(string[] args)
{ {
AnnouncePacket.Register();
server = new Server();
var watch = Stopwatch.StartNew(); var watch = Stopwatch.StartNew();
TestPacket.Register(); server.Start("10.136.5.59", 50).ContinueWith(t =>
Server server = new Server();
server.Start("127.0.0.1", 50).ContinueWith(t =>
{ {
watch.Stop(); watch.Stop();
@ -32,13 +38,61 @@ namespace MIT_Master_Server
}); });
server.ClientConnected += Server_ClientConnected; server.ClientConnected += Server_ClientConnected;
server.DataReceived += Server_DataReceived;
Console.ReadLine(); Console.ReadLine();
} }
private static void Server_ClientConnected(object sender, ClientConnectedEventArgs e) private static async void Server_DataReceived(object sender, Articulate_Network.Events.DataReceivedEventArgs e)
{ {
Console.WriteLine($"Client Connected from: {(e.TcpClient.Client.RemoteEndPoint as IPEndPoint).Address}"); if (e.Packet is AnnouncePacket)
{
AnnouncePacket p = (AnnouncePacket)e.Packet;
Console.WriteLine($"Received client announcement. Requesting config: {p.RequestConfig}");
Console.WriteLine(p.MacAddress);
int packetsSent = 0;
if (p.RequestConfig)
{
await server.SendPacket(e.Client, new StartFileTransferPacket()
{
FileName = file.FullName,
ExpectedSize = file.Length
});
byte[] buffer = new byte[10240];
using (var fs = new FileStream(file.FullName, FileMode.Open, FileAccess.Read))
{
int bytesRead;
while ((bytesRead = await fs.ReadAsync(buffer, 0, buffer.Length)) > 0)
{
await server.SendPacket(e.Client, new SendFileChunkPacket()
{
Chunk = buffer,
ChunkSize = bytesRead
});
packetsSent += 1;
}
}
Console.WriteLine($"Completed file transfer. Sent {packetsSent} packets.");
await server.SendPacket(e.Client, new EndFileTransferPacket() { Completed = true });
}
}
}
private static async void Server_ClientConnected(object sender, ClientConnectedEventArgs e)
{
//Console.WriteLine($"Client Connected from: {(e.TcpClient.Client.RemoteEndPoint as IPEndPoint).Address}");
await server.SendPacket(e.TcpClient, new TestPacket()
{
TestOne = 50,
TestTwo = true
});
} }
} }
} }

View File

@ -1,4 +1,5 @@
using System; using Articulate_Network.Packets;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
@ -6,9 +7,15 @@ using System.Threading.Tasks;
namespace MIT_Packets namespace MIT_Packets
{ {
public class AnnouncePacket public class AnnouncePacket : Packet
{ {
public int Type { get; set; } public int Type { get; set; }
public bool RequestConfig { get; set; } public bool RequestConfig { get; set; }
public string MacAddress { get; set; }
public static void Register()
{
}
} }
} }

View File

@ -0,0 +1,14 @@
using Articulate_Network.Packets;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MIT_Packets.Configuration
{
public class ClientConfigPacket : Packet
{
}
}

View File

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MIT_Packets.Enums
{
public enum ClientStatus
{
Waiting,
Downloading_Image,
Applying_Image,
Installing_Drivers,
Joining,
Completed,
Idle
}
}

View File

@ -44,8 +44,14 @@
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="AnnouncePacket.cs" />
<Compile Include="Configuration\ClientConfigPacket.cs" />
<Compile Include="Enums\ClientStatus.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TestPacket.cs" /> <Compile Include="TestPacket.cs" />
<Compile Include="Transfer\EndFileTransferPacket.cs" />
<Compile Include="Transfer\SendFileChunkPacket.cs" />
<Compile Include="Transfer\StartFileTransferPacket.cs" />
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project> </Project>

View File

@ -1,5 +1,6 @@
using Articulate_Network.Attributes; using Articulate_Network.Attributes;
using Articulate_Network.Interfaces; using Articulate_Network.Interfaces;
using Articulate_Network.Packets;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -8,14 +9,10 @@ using System.Threading.Tasks;
namespace MIT_Packets namespace MIT_Packets
{ {
public class TestPacket : IPacket public class TestPacket : Packet
{ {
public int TestOne { get; set; } public int TestOne { get; set; }
public bool TestTwo { get; set; } public bool TestTwo { get; set; }
public string TestThree { get; set; } public string TestThree { get; set; }
public static void Register()
{
}
} }
} }

View File

@ -0,0 +1,14 @@
using Articulate_Network.Packets;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MIT_Packets.Transfer
{
public class EndFileTransferPacket : Packet
{
public bool Completed { get; set; }
}
}

View File

@ -0,0 +1,18 @@
using Articulate_Network.Attributes;
using Articulate_Network.Packets;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MIT_Packets.Transfer
{
public class SendFileChunkPacket : Packet
{
[PacketIgnore]
public int TransferID { get; set; }
public int ChunkSize { get; set; }
public byte[] Chunk { get; set; }
}
}

View File

@ -0,0 +1,15 @@
using Articulate_Network.Packets;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MIT_Packets.Transfer
{
public class StartFileTransferPacket : Packet
{
public string FileName { get; set; }
public long ExpectedSize { get; set; }
}
}

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; }
}
}

View File

@ -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);
}
}
}

View File

@ -36,6 +36,9 @@
<Reference Include="Articulate-Network"> <Reference Include="Articulate-Network">
<HintPath>..\..\Articulate-Network\bin\Debug\Articulate-Network.dll</HintPath> <HintPath>..\..\Articulate-Network\bin\Debug\Articulate-Network.dll</HintPath>
</Reference> </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" />
<Reference Include="System.Core" /> <Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" /> <Reference Include="System.Xml.Linq" />
@ -46,11 +49,20 @@
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Configuration.cs" />
<Compile Include="Handlers\FileTransferHandler.cs" />
<Compile Include="Program.cs" /> <Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="App.config" /> <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> </ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project> </Project>

View File

@ -1,8 +1,14 @@
using Articulate_Network; using Articulate_Network;
using Articulate_Network.Managers; 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;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net.NetworkInformation;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -10,21 +16,49 @@ namespace MIT_Builder_Client
{ {
class Program class Program
{ {
static Client client;
public static ClientStatus status = ClientStatus.Waiting;
static void Main(string[] args) 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) if (t.Result.Connected)
{ {
Console.WriteLine();
Console.WriteLine("Successfully connected to master server."); Console.WriteLine("Successfully connected to master server.");
AnnounceClient();
} }
}); });
client.DataReceived += Client_DataReceived;
Console.ReadLine(); 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);
}
} }
} }

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>

274
build-failed.xml Normal file

File diff suppressed because one or more lines are too long

274
build.xml Normal file

File diff suppressed because one or more lines are too long