Connection is established correctly now - need to improve content handling
This commit is contained in:
@@ -1,51 +1,75 @@
|
||||
using System.Net;
|
||||
using System.Net.NetworkInformation;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
|
||||
namespace DirectoryScanReceiver;
|
||||
|
||||
struct Data
|
||||
{
|
||||
public string Name;
|
||||
public int Length;
|
||||
public string Content;
|
||||
|
||||
public override string ToString() => $"Name: {Name}\nLength: {Length}";
|
||||
}
|
||||
|
||||
static class DataExtractor
|
||||
{
|
||||
public static string? ExtractHeader(byte[] data)
|
||||
{
|
||||
int idx = Array.IndexOf(data, (byte)'\n');
|
||||
return Encoding.UTF8.GetString(data[..idx]);
|
||||
}
|
||||
public static Data Data;
|
||||
|
||||
public static byte[] ExtractContent(byte[] data)
|
||||
public static void LoadAndParseData(string data)
|
||||
{
|
||||
return data[Array.IndexOf(data, (byte)'\n')..];
|
||||
Data = new Data();
|
||||
if (data.Length == 0) return;
|
||||
|
||||
var s = data.Split('|').ToList();
|
||||
if(s.Count < 3) return;
|
||||
|
||||
Data.Name = s[0];
|
||||
|
||||
try { Data.Length = int.Parse(s[1]); }
|
||||
catch (Exception ex) { Console.WriteLine(ex.Message); }
|
||||
|
||||
var sb = new StringBuilder();
|
||||
foreach (var sub in s[2..]) sb.Append(sub);
|
||||
var content = sb.ToString();
|
||||
|
||||
var length = Math.Min(content.Length, Data.Length);
|
||||
Data.Content = content[..length];
|
||||
}
|
||||
}
|
||||
|
||||
static class DataHandler
|
||||
{
|
||||
const string OutputDir = "./output";
|
||||
public const string OutputDir = "./output";
|
||||
|
||||
public static void Parse(byte[] data)
|
||||
public static void Parse(string data)
|
||||
{
|
||||
if (data.Length == 0) return;
|
||||
|
||||
var header = DataExtractor.ExtractHeader(data);
|
||||
var content = DataExtractor.ExtractContent(data);
|
||||
DataExtractor.LoadAndParseData(data);
|
||||
var parsed = DataExtractor.Data;
|
||||
Console.WriteLine(parsed);
|
||||
|
||||
if (header is null || content is null) return;
|
||||
if (parsed.Name.Length == 0 || parsed.Length == 0 || parsed.Content.Length == 0) return;
|
||||
|
||||
var fi = new FileInfo(header);
|
||||
var dir = Path.GetDirectoryName(header) ?? OutputDir;
|
||||
var newPath = Path.Combine(OutputDir, parsed.Name);
|
||||
var fi = new FileInfo(newPath);
|
||||
var dir = Path.GetDirectoryName(newPath) ?? OutputDir;
|
||||
var di = new DirectoryInfo(dir);
|
||||
|
||||
if (!di.Exists) di.Create();
|
||||
if (fi.Exists) return;
|
||||
|
||||
Console.WriteLine($"Writing File: {header}");
|
||||
fi.Create().WriteAsync(new ReadOnlyMemory<byte>(content));
|
||||
Console.WriteLine($"Writing File: {newPath}");
|
||||
fi.Create().Write(Encoding.UTF8.GetBytes(parsed.Content));
|
||||
}
|
||||
}
|
||||
|
||||
class Server
|
||||
internal class Server
|
||||
{
|
||||
private TcpListener _listener;
|
||||
private NetworkStream _stream;
|
||||
private readonly TcpListener _listener;
|
||||
|
||||
public Server(int port)
|
||||
{
|
||||
@@ -53,9 +77,18 @@ class Server
|
||||
_listener.Start();
|
||||
}
|
||||
|
||||
public static TcpState GetState(TcpClient tcpClient)
|
||||
{
|
||||
var foo = IPGlobalProperties.GetIPGlobalProperties()
|
||||
.GetActiveTcpConnections()
|
||||
.SingleOrDefault(x => x.LocalEndPoint.Equals(tcpClient.Client.LocalEndPoint));
|
||||
return foo is not null ? foo.State : TcpState.Unknown;
|
||||
}
|
||||
|
||||
|
||||
public void Receiver()
|
||||
{
|
||||
// var bytes = new byte[134217728]; // 128 MB
|
||||
if(Directory.Exists(DataHandler.OutputDir)) Directory.Delete(DataHandler.OutputDir, true);
|
||||
|
||||
while (true)
|
||||
{
|
||||
@@ -65,40 +98,67 @@ class Server
|
||||
var stream = peer.GetStream();
|
||||
|
||||
Console.WriteLine($"Connected to Peer: {peer.Client.RemoteEndPoint}");
|
||||
while (peer.Connected)
|
||||
while (GetState(peer) == TcpState.Established)
|
||||
{
|
||||
var availableBytes = peer.Available;
|
||||
if(availableBytes <= 0) continue;
|
||||
var buffer = "";
|
||||
|
||||
var bytes = new byte[availableBytes];
|
||||
try
|
||||
{
|
||||
var recvCount = stream.Read(bytes);
|
||||
if(recvCount <= 0) continue;
|
||||
}
|
||||
catch (Exception ex)
|
||||
var start = DateTime.Now;
|
||||
while (!buffer.EndsWith("\r\n\r\n"))
|
||||
{
|
||||
Console.WriteLine(ex.Message);
|
||||
if (DateTime.Now.Subtract(start).TotalMilliseconds >= 10000)
|
||||
{
|
||||
Console.WriteLine("Receive Timeout!");
|
||||
break;
|
||||
}
|
||||
|
||||
Console.WriteLine("Received:\n" + BitConverter.ToString(bytes));
|
||||
DataHandler.Parse(bytes);
|
||||
var availableBytes = peer.Available;
|
||||
while (availableBytes > 0)
|
||||
{
|
||||
var recvBytes = new byte[availableBytes];
|
||||
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];
|
||||
availableBytes -= recvCount;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex) { Console.WriteLine(ex.Message); }
|
||||
|
||||
if(buffer.Length == 0) continue;
|
||||
|
||||
foreach (var b in buffer.Split("\r\n\r\n"))
|
||||
{
|
||||
Console.WriteLine($"Received {b.Length} bytes");
|
||||
DataHandler.Parse(b);
|
||||
}
|
||||
}
|
||||
Console.WriteLine("Disconnected from Peer!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class Program
|
||||
public static class Program
|
||||
{
|
||||
public static void Main()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
var server = new Server(7156);
|
||||
Console.WriteLine("Started Server!");
|
||||
while (true)
|
||||
{
|
||||
Console.WriteLine("Waiting for data!");
|
||||
try
|
||||
{
|
||||
server.Receiver();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("Receiver crashed!");
|
||||
Console.WriteLine(ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user