Connection and sending works

This commit is contained in:
ti_mo
2026-02-21 23:39:11 +01:00
parent b5bf127a86
commit b124c9667f
+21 -27
View File
@@ -1,13 +1,15 @@
using System.Net.Sockets; using System.Diagnostics;
using System.Net.Sockets;
using System.Text; using System.Text;
using System.Linq; using System.Linq;
namespace DirectoryScanSender; namespace DirectoryScanSender;
class FileSender : IDisposable internal class FileSender : IDisposable
{ {
private readonly TcpClient _client; private readonly TcpClient _client;
private readonly NetworkStream _stream; private readonly NetworkStream _stream;
public FileSender(string host, int port) public FileSender(string host, int port)
{ {
_client = new TcpClient(AddressFamily.InterNetwork); _client = new TcpClient(AddressFamily.InterNetwork);
@@ -18,6 +20,7 @@ class FileSender : IDisposable
public void Send(byte[] data) public void Send(byte[] data)
{ {
if (!_client.Connected) return; if (!_client.Connected) return;
Console.WriteLine($"Sending {data.Length} bytes");
lock (_stream) lock (_stream)
{ {
@@ -30,20 +33,21 @@ class FileSender : IDisposable
{ {
_stream.Close(); _stream.Close();
_client.Close(); _client.Close();
GC.SuppressFinalize(this);
} }
~FileSender() => Dispose(); ~FileSender() => Dispose();
} }
class DirectoryScanSender : IDisposable internal class DirectoryScanSender : IDisposable
{ {
private readonly FileSender _sender; private readonly FileSender _sender;
private readonly string _initPath; private readonly string _initPath;
public DirectoryScanSender(string endpoint, int port, string path) public DirectoryScanSender(string endpoint, int port, string path)
{ {
_sender = new FileSender(endpoint, port);
if(!Directory.Exists(path)) throw new ArgumentException("Path is no directory!"); if(!Directory.Exists(path)) throw new ArgumentException("Path is no directory!");
_sender = new FileSender(endpoint, port);
_initPath = path; _initPath = path;
} }
@@ -51,7 +55,10 @@ class DirectoryScanSender : IDisposable
{ {
try try
{ {
var data = Encoding.UTF8.GetBytes(path + "\n").Concat(File.ReadAllBytes(path)).ToArray(); // pattern: [PATH]|[LENGTH]|[CONTENT]\r\n\r\n
var fileContent = new byte[1]; // File.ReadAllBytes(path);
for(var i = 0; i < fileContent.Length; i++) if(fileContent[i] == (byte)'\r') fileContent[i] = (byte)' ';
var data = Encoding.UTF8.GetBytes(path + "|").Concat(Encoding.UTF8.GetBytes(path.Length + "|")).Concat(fileContent).Concat(Encoding.UTF8.GetBytes("\r\n\r\n")).ToArray();
_sender.Send(data); _sender.Send(data);
} }
catch (Exception ex) catch (Exception ex)
@@ -59,30 +66,23 @@ class DirectoryScanSender : IDisposable
Console.WriteLine(ex.GetType().FullName); Console.WriteLine(ex.GetType().FullName);
Console.WriteLine(ex.Message); Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace); Console.WriteLine(ex.StackTrace);
throw;
} }
} }
private void MultiFileParser(string[] filepaths) private void MultiFileParser(string[] filepaths)
{ {
foreach (var filepath in filepaths) foreach (var filepath in filepaths) ParseSingleFile(filepath);
{
ParseSingleFile(filepath);
}
} }
private void RecursiveWalkerHelper(string path) private void RecursiveWalkerHelper(string path)
{ {
var dirs = Directory.GetDirectories(path); var dirs = Directory.GetDirectories(path);
ParallelOptions options = new() ParallelOptions options = new() { MaxDegreeOfParallelism = 20 };
{
MaxDegreeOfParallelism = 20,
};
Task.Run(() => Task.Run(() =>
{ {
Parallel.ForEach(dirs, options, async (dir) => { RecursiveWalker(dir).GetAwaiter(); }); Parallel.ForEach(dirs, options, dir => { RecursiveWalker(dir).GetAwaiter(); });
}); });
} }
@@ -97,12 +97,10 @@ class DirectoryScanSender : IDisposable
public void Dispose() public void Dispose()
{ {
_sender.Dispose(); _sender.Dispose();
GC.SuppressFinalize(this);
} }
~DirectoryScanSender() ~DirectoryScanSender() => Dispose();
{
Dispose();
}
} }
public static class Program public static class Program
@@ -110,20 +108,16 @@ public static class Program
private const string Host = "localhost"; private const string Host = "localhost";
private const int Port = 7156; private const int Port = 7156;
private static DirectoryScanSender? _directoryScanSender;
public static void Main() public static void Main()
{ {
var desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); var desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
try try
{ {
_directoryScanSender = new DirectoryScanSender(Host, Port, desktopPath); // TODO: Start process to not be dependent on parent lifetime
_directoryScanSender.Run().GetAwaiter().GetResult(); var directoryScanSender = new DirectoryScanSender(Host, Port, desktopPath);
} directoryScanSender.Run().GetAwaiter().GetResult();
catch (Exception ex)
{
Console.WriteLine(ex.Message);
} }
catch (Exception ex) { Console.WriteLine(ex.Message); }
} }
} }