commit b5bf127a86ec22afb6275d3af179ae2e01aec771 Author: Ano-sys Date: Sat Feb 21 02:01:40 2026 +0100 initial push diff --git a/DirectoryScanSender.sln b/DirectoryScanSender.sln new file mode 100644 index 0000000..a9a66d6 --- /dev/null +++ b/DirectoryScanSender.sln @@ -0,0 +1,16 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DirectoryScanSender", "DirectoryScanSender\DirectoryScanSender.csproj", "{48CE690B-CDE3-4DD2-BD8D-D8A8B789FA70}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {48CE690B-CDE3-4DD2-BD8D-D8A8B789FA70}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {48CE690B-CDE3-4DD2-BD8D-D8A8B789FA70}.Debug|Any CPU.Build.0 = Debug|Any CPU + {48CE690B-CDE3-4DD2-BD8D-D8A8B789FA70}.Release|Any CPU.ActiveCfg = Release|Any CPU + {48CE690B-CDE3-4DD2-BD8D-D8A8B789FA70}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/DirectoryScanSender/DirectoryScanSender.csproj b/DirectoryScanSender/DirectoryScanSender.csproj new file mode 100644 index 0000000..07f9ef8 --- /dev/null +++ b/DirectoryScanSender/DirectoryScanSender.csproj @@ -0,0 +1,11 @@ + + + + Exe + net10.0 + preview + enable + enable + + + diff --git a/DirectoryScanSender/Program.cs b/DirectoryScanSender/Program.cs new file mode 100644 index 0000000..c43cd61 --- /dev/null +++ b/DirectoryScanSender/Program.cs @@ -0,0 +1,129 @@ +using System.Net.Sockets; +using System.Text; +using System.Linq; + +namespace DirectoryScanSender; + +class FileSender : IDisposable +{ + private readonly TcpClient _client; + private readonly NetworkStream _stream; + public FileSender(string host, int port) + { + _client = new TcpClient(AddressFamily.InterNetwork); + _client.Connect(host, port); + _stream = _client.GetStream(); + } + + public void Send(byte[] data) + { + if (!_client.Connected) return; + + lock (_stream) + { + _stream.Write(new ReadOnlySpan(data)); + _stream.Flush(); + } + } + + public void Dispose() + { + _stream.Close(); + _client.Close(); + } + + ~FileSender() => Dispose(); +} + +class DirectoryScanSender : IDisposable +{ + private readonly FileSender _sender; + private readonly string _initPath; + + public DirectoryScanSender(string endpoint, int port, string path) + { + _sender = new FileSender(endpoint, port); + if(!Directory.Exists(path)) throw new ArgumentException("Path is no directory!"); + _initPath = path; + } + + private void ParseSingleFile(string path) + { + try + { + var data = Encoding.UTF8.GetBytes(path + "\n").Concat(File.ReadAllBytes(path)).ToArray(); + _sender.Send(data); + } + catch (Exception ex) + { + Console.WriteLine(ex.GetType().FullName); + Console.WriteLine(ex.Message); + Console.WriteLine(ex.StackTrace); + throw; + } + } + + private void MultiFileParser(string[] filepaths) + { + foreach (var filepath in filepaths) + { + ParseSingleFile(filepath); + } + } + + private void RecursiveWalkerHelper(string path) + { + var dirs = Directory.GetDirectories(path); + + ParallelOptions options = new() + { + MaxDegreeOfParallelism = 20, + }; + + Task.Run(() => + { + Parallel.ForEach(dirs, options, async (dir) => { RecursiveWalker(dir).GetAwaiter(); }); + }); + } + + private async Task RecursiveWalker(string path) + { + await Task.Run(() => RecursiveWalkerHelper(path)); + await Task.Run(() => MultiFileParser(Directory.GetFiles(path))); + } + + public async Task Run() => await RecursiveWalker(_initPath); + + public void Dispose() + { + _sender.Dispose(); + } + + ~DirectoryScanSender() + { + Dispose(); + } +} + +public static class Program +{ + private const string Host = "localhost"; + private const int Port = 7156; + + private static DirectoryScanSender? _directoryScanSender; + + public static void Main() + { + var desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); + + try + { + _directoryScanSender = new DirectoryScanSender(Host, Port, desktopPath); + _directoryScanSender.Run().GetAwaiter().GetResult(); + } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + } + } +} \ No newline at end of file