initial push
This commit is contained in:
@@ -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
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>net10.0</TargetFramework>
|
||||||
|
<LangVersion>preview</LangVersion>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
@@ -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<byte>(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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user