Push after nearly loosing all because rider ment to corrupt the .cs

This commit is contained in:
Ano-sys
2026-02-21 02:00:08 +01:00
commit 572bcb32c6
3 changed files with 130 additions and 0 deletions
+16
View File
@@ -0,0 +1,16 @@
Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DirectoryScanReceiver", "DirectoryScanReceiver\DirectoryScanReceiver.csproj", "{9A8FD5F2-1846-4663-8573-9946C240D2EE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{9A8FD5F2-1846-4663-8573-9946C240D2EE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9A8FD5F2-1846-4663-8573-9946C240D2EE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9A8FD5F2-1846-4663-8573-9946C240D2EE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9A8FD5F2-1846-4663-8573-9946C240D2EE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
+104
View File
@@ -0,0 +1,104 @@
using System.Net;
using System.Net.Sockets;
namespace DirectoryScanReceiver;
static class DataExtractor
{
public static string? ExtractHeader(byte[] data)
{
int idx = Array.IndexOf(data, (byte)'\n');
return Encoding.UTF8.GetString(data[..idx]);
}
public static byte[] ExtractContent(byte[] data)
{
return data[Array.IndexOf(data, (byte)'\n')..];
}
}
static class DataHandler
{
const string OutputDir = "./output";
public static void Parse(byte[] data)
{
if (data.Length == 0) return;
var header = DataExtractor.ExtractHeader(data);
var content = DataExtractor.ExtractContent(data);
if (header is null || content is null) return;
var fi = new FileInfo(header);
var dir = Path.GetDirectoryName(header) ?? OutputDir;
var di = new DirectoryInfo(dir);
if (!di.Exists) di.Create();
if (fi.Exists) return;
Console.WriteLine($"Writing File: {header}");
fi.Create().WriteAsync(new ReadOnlyMemory<byte>(content));
}
}
class Server
{
private TcpListener _listener;
private NetworkStream _stream;
public Server(int port)
{
_listener = new TcpListener(IPAddress.Parse("127.0.0.1"), port);
_listener.Start();
}
public void Receiver()
{
// var bytes = new byte[134217728]; // 128 MB
while (true)
{
if(!_listener.Pending()) continue;
var peer = _listener.AcceptTcpClient();
var stream = peer.GetStream();
Console.WriteLine($"Connected to Peer: {peer.Client.RemoteEndPoint}");
while (peer.Connected)
{
var availableBytes = peer.Available;
if(availableBytes <= 0) continue;
var bytes = new byte[availableBytes];
try
{
var recvCount = stream.Read(bytes);
if(recvCount <= 0) continue;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.WriteLine("Received:\n" + BitConverter.ToString(bytes));
DataHandler.Parse(bytes);
}
Console.WriteLine("Disconnected from Peer!");
}
}
}
public class Program
{
public static void Main()
{
while (true)
{
var server = new Server(7156);
Console.WriteLine("Started Server!");
Console.WriteLine("Waiting for data!");
server.Receiver();
}
}
}