Connection and sending works
This commit is contained in:
@@ -1,13 +1,15 @@
|
||||
using System.Net.Sockets;
|
||||
using System.Diagnostics;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using System.Linq;
|
||||
|
||||
namespace DirectoryScanSender;
|
||||
|
||||
class FileSender : IDisposable
|
||||
internal class FileSender : IDisposable
|
||||
{
|
||||
private readonly TcpClient _client;
|
||||
private readonly NetworkStream _stream;
|
||||
|
||||
public FileSender(string host, int port)
|
||||
{
|
||||
_client = new TcpClient(AddressFamily.InterNetwork);
|
||||
@@ -18,6 +20,7 @@ class FileSender : IDisposable
|
||||
public void Send(byte[] data)
|
||||
{
|
||||
if (!_client.Connected) return;
|
||||
Console.WriteLine($"Sending {data.Length} bytes");
|
||||
|
||||
lock (_stream)
|
||||
{
|
||||
@@ -30,20 +33,21 @@ class FileSender : IDisposable
|
||||
{
|
||||
_stream.Close();
|
||||
_client.Close();
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
~FileSender() => Dispose();
|
||||
}
|
||||
|
||||
class DirectoryScanSender : IDisposable
|
||||
internal 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!");
|
||||
_sender = new FileSender(endpoint, port);
|
||||
_initPath = path;
|
||||
}
|
||||
|
||||
@@ -51,7 +55,10 @@ class DirectoryScanSender : IDisposable
|
||||
{
|
||||
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);
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -59,30 +66,23 @@ class DirectoryScanSender : IDisposable
|
||||
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);
|
||||
}
|
||||
foreach (var filepath in filepaths) ParseSingleFile(filepath);
|
||||
}
|
||||
|
||||
private void RecursiveWalkerHelper(string path)
|
||||
{
|
||||
var dirs = Directory.GetDirectories(path);
|
||||
|
||||
ParallelOptions options = new()
|
||||
{
|
||||
MaxDegreeOfParallelism = 20,
|
||||
};
|
||||
ParallelOptions options = new() { MaxDegreeOfParallelism = 20 };
|
||||
|
||||
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()
|
||||
{
|
||||
_sender.Dispose();
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
~DirectoryScanSender()
|
||||
{
|
||||
Dispose();
|
||||
}
|
||||
~DirectoryScanSender() => Dispose();
|
||||
}
|
||||
|
||||
public static class Program
|
||||
@@ -110,20 +108,16 @@ 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);
|
||||
// TODO: Start process to not be dependent on parent lifetime
|
||||
var directoryScanSender = new DirectoryScanSender(Host, Port, desktopPath);
|
||||
directoryScanSender.Run().GetAwaiter().GetResult();
|
||||
}
|
||||
catch (Exception ex) { Console.WriteLine(ex.Message); }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user