From 5671a7aac80413598f012bb305fe7bad94ab6fcb Mon Sep 17 00:00:00 2001 From: ti_mo Date: Sun, 22 Feb 2026 00:52:43 +0100 Subject: [PATCH] stringbuilder is faster than string but something is still off, most files dont have barely any content --- DirectoryScanReceiver/Program.cs | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/DirectoryScanReceiver/Program.cs b/DirectoryScanReceiver/Program.cs index 0aebdc9..07399e1 100644 --- a/DirectoryScanReceiver/Program.cs +++ b/DirectoryScanReceiver/Program.cs @@ -24,7 +24,7 @@ static class DataExtractor if (data.Length == 0) return; var s = data.Split('|').ToList(); - if(s.Count < 3) return; + if (s.Count < 3) return; Data.Name = s[0]; @@ -42,7 +42,7 @@ static class DataExtractor static class DataHandler { - public const string OutputDir = "./output"; + public const string OutputDir = "output"; public static void Parse(string data) { @@ -54,7 +54,16 @@ static class DataHandler if (parsed.Name.Length == 0 || parsed.Length == 0 || parsed.Content.Length == 0) return; - var newPath = Path.Combine(OutputDir, parsed.Name); + var processPath = Path.GetDirectoryName(Environment.ProcessPath); + if (processPath == null) return; + + var outputDir = Path.Combine(processPath, OutputDir); + var newPath = Path.Combine(outputDir, parsed.Name); + if (newPath == parsed.Name) + { + newPath = outputDir + Path.DirectorySeparatorChar + parsed.Name.TrimStart(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); + } // DO NOT WRITE SOMEWHERE ELSE THAN OUTPUT DIR + var fi = new FileInfo(newPath); var dir = Path.GetDirectoryName(newPath) ?? OutputDir; var di = new DirectoryInfo(dir); @@ -94,12 +103,13 @@ internal class Server { if(!_listener.Pending()) continue; - var peer = _listener.AcceptTcpClient(); - var stream = peer.GetStream(); + using var peer = _listener.AcceptTcpClient(); + using var stream = peer.GetStream(); Console.WriteLine($"Connected to Peer: {peer.Client.RemoteEndPoint}"); while (GetState(peer) == TcpState.Established) { + StringBuilder sb = new(); var buffer = ""; try @@ -120,14 +130,15 @@ internal class Server var recvCount = stream.Read(recvBytes); if(recvCount <= 0) continue; - // TODO: for is to heavy - for (var i = 0; i < recvCount; i++) buffer += (char)recvBytes[i]; + // TODO: for and string are to heavy + sb.Append(Encoding.UTF8.GetString(recvBytes)); availableBytes -= recvCount; } } } catch (Exception ex) { Console.WriteLine(ex.Message); } + buffer = sb.ToString(); if(buffer.Length == 0) continue; foreach (var b in buffer.Split("\r\n\r\n"))