72 lines
2.5 KiB
C#
72 lines
2.5 KiB
C#
|
using PacketIO;
|
|||
|
|
|||
|
namespace ProtocolLib;
|
|||
|
|
|||
|
public class Program
|
|||
|
{
|
|||
|
public static void Main(string[] args)
|
|||
|
{
|
|||
|
// Make tree file.
|
|||
|
using (FileStream fs = new FileStream("tree.bin", FileMode.OpenOrCreate))
|
|||
|
{
|
|||
|
DirectoryInfo directoryInfo = new DirectoryInfo("i");
|
|||
|
if (directoryInfo.Exists)
|
|||
|
{
|
|||
|
foreach (FileInfo fileInfo in directoryInfo.GetFiles())
|
|||
|
{
|
|||
|
using (PacketFile file = new PacketFile(fileInfo.Name, fileInfo.Open(FileMode.Open, FileAccess.ReadWrite)))
|
|||
|
{
|
|||
|
TreePacketFile.WriteTreeFile(fs, file).ConfigureAwait(false).GetAwaiter().GetResult();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// Get files from tree file.
|
|||
|
using (FileStream fs = new FileStream("tree.bin", FileMode.OpenOrCreate))
|
|||
|
{
|
|||
|
IEnumerable<IPacketFile> files = TreePacketFile.ReadTreeFiles(fs).ConfigureAwait(false).GetAwaiter().GetResult();
|
|||
|
foreach (IPacketFile ifile in files)
|
|||
|
{
|
|||
|
PacketFile file = (PacketFile) ifile;
|
|||
|
using (FileStream outStream = new FileStream(file.GetFileName(), FileMode.OpenOrCreate))
|
|||
|
{
|
|||
|
byte[] buffer = new byte[8192];
|
|||
|
int c;
|
|||
|
while ((c = file.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false).GetAwaiter().GetResult()) > 0)
|
|||
|
{
|
|||
|
outStream.Write(buffer, 0, c);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
// Paket test.
|
|||
|
using (MemoryStream ms = new MemoryStream())
|
|||
|
{
|
|||
|
PacketStream packetStream = new PacketStream(ms);
|
|||
|
|
|||
|
byte[] buffer = System.Text.Encoding.UTF8.GetBytes("Hello, World!!!");
|
|||
|
foreach (Packet packet in PacketFactory.CreatePacket(DataOperator.Connect, DataType.Binary, buffer))
|
|||
|
{
|
|||
|
packetStream.WritePacket(packet).ConfigureAwait(false).GetAwaiter().GetResult();
|
|||
|
}
|
|||
|
|
|||
|
ms.Seek(0, SeekOrigin.Begin);
|
|||
|
|
|||
|
Packet? packet1 = null;
|
|||
|
do
|
|||
|
{
|
|||
|
packet1 = packetStream.ReadPacket().ConfigureAwait(false).GetAwaiter().GetResult();
|
|||
|
if (packet1 is object)
|
|||
|
{
|
|||
|
Console.Write($"{System.Text.Encoding.UTF8.GetString(packet1.Payload)}");
|
|||
|
}
|
|||
|
} while (packet1 is object);
|
|||
|
|
|||
|
Console.WriteLine();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|