Start on File Transfer Packets & Handler Implementation
This commit is contained in:
17
MIT Master Server/Handlers/ConfigurationHandler.cs
Normal file
17
MIT Master Server/Handlers/ConfigurationHandler.cs
Normal 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)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -49,6 +49,7 @@
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Handlers\ConfigurationHandler.cs" />
|
||||
<Compile Include="Program.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
@ -2,9 +2,11 @@
|
||||
using Articulate_Network.Events;
|
||||
using Articulate_Network.Managers;
|
||||
using MIT_Packets;
|
||||
using MIT_Packets.Transfer;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
@ -15,15 +17,19 @@ namespace MIT_Master_Server
|
||||
{
|
||||
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)
|
||||
{
|
||||
AnnouncePacket.Register();
|
||||
|
||||
server = new Server();
|
||||
|
||||
var watch = Stopwatch.StartNew();
|
||||
|
||||
TestPacket.Register();
|
||||
|
||||
Server server = new Server();
|
||||
|
||||
server.Start("127.0.0.1", 50).ContinueWith(t =>
|
||||
server.Start("10.136.5.59", 50).ContinueWith(t =>
|
||||
{
|
||||
|
||||
watch.Stop();
|
||||
@ -32,13 +38,61 @@ namespace MIT_Master_Server
|
||||
});
|
||||
|
||||
server.ClientConnected += Server_ClientConnected;
|
||||
server.DataReceived += Server_DataReceived;
|
||||
|
||||
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
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using Articulate_Network.Packets;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
@ -6,9 +7,15 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace MIT_Packets
|
||||
{
|
||||
public class AnnouncePacket
|
||||
public class AnnouncePacket : Packet
|
||||
{
|
||||
public int Type { get; set; }
|
||||
public bool RequestConfig { get; set; }
|
||||
public string MacAddress { get; set; }
|
||||
|
||||
public static void Register()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
14
MIT Packets/Configuration/ClientConfigPacket.cs
Normal file
14
MIT Packets/Configuration/ClientConfigPacket.cs
Normal 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
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
19
MIT Packets/Enums/ClientStatus.cs
Normal file
19
MIT Packets/Enums/ClientStatus.cs
Normal 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
|
||||
}
|
||||
}
|
||||
@ -44,8 +44,14 @@
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="AnnouncePacket.cs" />
|
||||
<Compile Include="Configuration\ClientConfigPacket.cs" />
|
||||
<Compile Include="Enums\ClientStatus.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="TestPacket.cs" />
|
||||
<Compile Include="Transfer\EndFileTransferPacket.cs" />
|
||||
<Compile Include="Transfer\SendFileChunkPacket.cs" />
|
||||
<Compile Include="Transfer\StartFileTransferPacket.cs" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
</Project>
|
||||
@ -1,5 +1,6 @@
|
||||
using Articulate_Network.Attributes;
|
||||
using Articulate_Network.Interfaces;
|
||||
using Articulate_Network.Packets;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@ -8,14 +9,10 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace MIT_Packets
|
||||
{
|
||||
public class TestPacket : IPacket
|
||||
public class TestPacket : Packet
|
||||
{
|
||||
public int TestOne { get; set; }
|
||||
public bool TestTwo { get; set; }
|
||||
public string TestThree { get; set; }
|
||||
public static void Register()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
14
MIT Packets/Transfer/EndFileTransferPacket.cs
Normal file
14
MIT Packets/Transfer/EndFileTransferPacket.cs
Normal 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; }
|
||||
}
|
||||
}
|
||||
18
MIT Packets/Transfer/SendFileChunkPacket.cs
Normal file
18
MIT Packets/Transfer/SendFileChunkPacket.cs
Normal 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; }
|
||||
}
|
||||
}
|
||||
15
MIT Packets/Transfer/StartFileTransferPacket.cs
Normal file
15
MIT Packets/Transfer/StartFileTransferPacket.cs
Normal 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; }
|
||||
}
|
||||
}
|
||||
15
MOE Builder Client/Configuration.cs
Normal file
15
MOE Builder Client/Configuration.cs
Normal 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; }
|
||||
}
|
||||
}
|
||||
57
MOE Builder Client/Handlers/FileTransferHandler.cs
Normal file
57
MOE Builder Client/Handlers/FileTransferHandler.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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>
|
||||
@ -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
MOE Builder Client/packages.config
Normal file
4
MOE Builder Client/packages.config
Normal 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
274
build-failed.xml
Normal file
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user