more work on api & rename to Pilz.Net

This commit is contained in:
Pilzinsel64
2024-08-16 06:59:39 +02:00
parent f57aef5f4f
commit 2efb4f141c
91 changed files with 299 additions and 241 deletions

View File

@@ -0,0 +1,20 @@
namespace Pilz.Net.Api;
public record class ApiAuthCheckEventArgs(string AuthKey)
{
private bool hasDenyed;
public bool Valid { get; set; }
public void Deny()
{
Valid = false;
hasDenyed = true;
}
public void Permit()
{
if (!hasDenyed)
Valid = true;
}
}

42
Pilz.Net/Api/ApiClient.cs Normal file
View File

@@ -0,0 +1,42 @@
namespace Pilz.Net.Api;
public class ApiClient(string apiUrl) : IApiClient
{
protected readonly HttpClient httpClient = new();
public virtual string ApiUrl { get; } = apiUrl;
public string? AuthKey { get; set; }
public virtual IMessageSerializer Serializer { get; set; } = new DefaultMessageSerializer();
public virtual async Task<ApiResponse> SendMessage<TResponse>(string url, ApiMessage message, IMessageSerializer? serializer)
{
serializer ??= Serializer;
var res = await Send(url, message, serializer);
return new(res.StatusCode);
}
public virtual async Task<ApiResponse<TResponse>> SendRequest<TResponse>(string url, ApiMessage message, IMessageSerializer? serializer) where TResponse : ApiMessage
{
serializer ??= Serializer;
var res = await Send(url, message, serializer);
TResponse? result = null;
if (res.IsSuccessStatusCode)
result = serializer.Deserialize(await res.Content.ReadAsStringAsync(), typeof(TResponse)) as TResponse;
return new(res.StatusCode, result);
}
protected virtual async Task<HttpResponseMessage> Send(string url, ApiMessage message, IMessageSerializer serializer)
{
var fullRequestUrl = ApiUrl + url;
var content = new StringContent(serializer.Serialize(message)!, null, "application/json");
content.Headers.Add("API-AUTH-KEY", AuthKey);
return await httpClient.PostAsync(fullRequestUrl, content);
}
}

View File

@@ -0,0 +1,5 @@
namespace Pilz.Net.Api;
public abstract class ApiMessage
{
}

View File

@@ -0,0 +1,13 @@
using System.Net;
namespace Pilz.Net.Api;
public record class ApiResponse(
HttpStatusCode StatusCode)
{
public void EnsureOk()
{
if (StatusCode != HttpStatusCode.OK)
throw new Exception("Api return is not ok!");
}
}

View File

@@ -0,0 +1,15 @@
using System.Net;
namespace Pilz.Net.Api;
public record class ApiResponse<T>(
HttpStatusCode StatusCode,
T? Message)
where T : ApiMessage
{
public void EnsureOk()
{
if (StatusCode != HttpStatusCode.OK)
throw new Exception("Api return is not ok!");
}
}

18
Pilz.Net/Api/ApiResult.cs Normal file
View File

@@ -0,0 +1,18 @@
using System.Net;
namespace Pilz.Net.Api;
public record class ApiResult(
HttpStatusCode StatusCode,
ApiMessage? Message = null)
{
public static ApiResult Ok()
{
return new(HttpStatusCode.OK);
}
public static ApiResult Ok(ApiMessage message)
{
return new(HttpStatusCode.OK, message);
}
}

176
Pilz.Net/Api/ApiServer.cs Normal file
View File

@@ -0,0 +1,176 @@
using Pilz.Extensions.Reflection;
using System.Net;
using System.Reflection;
using static Pilz.Net.Api.IApiServer;
namespace Pilz.Net.Api;
public class ApiServer(string apiUrl) : IApiServer
{
protected readonly Dictionary<string, Delegate> handlers = [];
protected readonly Dictionary<Type, IMessageSerializer> serializers = [];
protected readonly HttpListener httpListener = new();
public event OnCheckAuthenticationEventHandler? OnCheckAuthentication;
protected record PrivateApiResult(ApiResult Original, string? ResultJson);
public string ApiUrl { get; } = apiUrl;
public virtual bool EnableAuth { get; set; }
public IMessageSerializer Serializer { get; set; } = new DefaultMessageSerializer();
public virtual void Start()
{
httpListener.Start();
Listen();
}
public virtual void Stop()
{
httpListener.Stop();
}
public virtual void RegisterHandler<T>(T instance) where T : class
{
// Get all public instance methods
var methods = typeof(T).GetMethods(BindingFlags.Instance | BindingFlags.Public);
// Register each method
foreach (var method in methods)
RegisterHandler(method.CreateDelegate(instance));
}
public virtual void RegisterHandler(Delegate handler)
{
var method = handler.Method;
// Sanity checks
if (method.GetCustomAttribute<MessageHandlerAttribute>() is not MessageHandlerAttribute attribute
|| method.GetParameters().FirstOrDefault() is not ParameterInfo parameter
|| !parameter.ParameterType.IsAssignableTo(typeof(ApiMessage))
|| !method.ReturnType.IsAssignableTo(typeof(ApiResult)))
throw new NotSupportedException("The first parameter needs to be of type ApiMessage and must return an ApiResult object and the method must have the MessageHandlerAttribute.");
// Add handler
handlers.Add(ApiUrl + attribute.Route, handler);
}
protected virtual void Listen()
{
while (httpListener.IsListening)
CheckContext();
}
protected virtual void CheckContext()
{
var context = httpListener.GetContext();
void close() => context.Response.OutputStream.Close();
if (context.Request.HttpMethod != HttpMethod.Post.Method
|| context.Request.ContentType is not string contentType
|| !contentType.Contains("application/json"))
{
close();
return;
}
// Parse url
var path = context.Request.Url?.PathAndQuery.Replace(ApiUrl, string.Empty);
if (string.IsNullOrWhiteSpace(path) || context.Request.ContentLength64 <= 0)
{
close();
return;
}
// Read input content
using StreamReader input = new(context.Request.InputStream);
var contentJson = input.ReadToEnd();
// Get auth key
if (context.Request.Headers.Get("API-AUTH-KEY") is not string authKey)
authKey = null!;
// Handle message
if (HandleMessage(path, contentJson, authKey) is not PrivateApiResult result)
{
close();
return;
}
// Set response parameters
context.Response.StatusCode = (int)result.Original.StatusCode;
// Write response content
if (result.ResultJson is not null)
{
context.Response.ContentType = "application/json";
using StreamWriter output = new(context.Response.OutputStream);
output.Write(result);
}
close();
}
protected virtual PrivateApiResult? HandleMessage(string url, string json, string? authKey)
{
// Get handler
if (!handlers.TryGetValue(url, out var handler)
|| handler.Method.GetCustomAttribute<MessageHandlerAttribute>() is not MessageHandlerAttribute attribute)
return null;
// Check authentication
if (attribute.RequiesAuth && (string.IsNullOrWhiteSpace(authKey) || !CheckAuthentication(authKey)))
return null;
// Get required infos
var targetType = handler.Method.GetParameters().First().ParameterType;
var serializer = GetSerializer(attribute.Serializer);
// Deserialize
if (serializer.Deserialize(json, targetType) is not ApiMessage message)
return null;
// Invoke handler
if (handler.DynamicInvoke(message) is not ApiResult result)
return null;
// Return result without message
if (result.Message is null)
return new(result, null);
// Serializer
if (serializer.Serialize(result.Message) is not string resultStr)
return null;
// Return result with message
return new(result, resultStr);
}
protected virtual IMessageSerializer GetSerializer(Type? t)
{
if (t is not null)
{
if (serializers.TryGetValue(t, out var s) && s is not null)
return s;
else if (Activator.CreateInstance(t) is IMessageSerializer ss)
{
serializers.Add(t, ss);
return ss;
}
}
return Serializer;
}
protected virtual bool CheckAuthentication(string authKey)
{
if (OnCheckAuthentication != null)
{
var args = new ApiAuthCheckEventArgs(authKey);
OnCheckAuthentication?.Invoke(this, args);
return args.Valid;
}
return false;
}
}

View File

@@ -0,0 +1,28 @@
using Newtonsoft.Json;
namespace Pilz.Net.Api;
public class DefaultMessageSerializer : IMessageSerializer
{
private static JsonSerializerSettings? defaultSerializerSettings;
public JsonSerializerSettings DefaultSerializerSettings => defaultSerializerSettings ??= CreateDefaultSerializerSettings();
protected virtual JsonSerializerSettings CreateDefaultSerializerSettings()
{
return new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Auto,
};
}
public virtual string? Serialize(ApiMessage message)
{
return JsonConvert.SerializeObject(message, DefaultSerializerSettings);
}
public virtual ApiMessage? Deserialize(string json, Type target)
{
return JsonConvert.DeserializeObject(json, target, DefaultSerializerSettings) as ApiMessage;
}
}

View File

@@ -0,0 +1,14 @@
namespace Pilz.Net.Api;
public interface IApiClient
{
string ApiUrl { get; }
string? AuthKey { get; set; }
IMessageSerializer Serializer { get; }
Task<ApiResponse> SendMessage<TResponse>(string url, ApiMessage message, IMessageSerializer? serializer = null);
Task<ApiResponse<TResponse>> SendRequest<TResponse>(string url, ApiMessage message, IMessageSerializer? serializer = null) where TResponse : ApiMessage;
}

View File

@@ -0,0 +1,22 @@
namespace Pilz.Net.Api;
public interface IApiServer
{
public delegate void OnCheckAuthenticationEventHandler(object sender, ApiAuthCheckEventArgs e);
event OnCheckAuthenticationEventHandler? OnCheckAuthentication;
string ApiUrl { get; }
bool EnableAuth { get; set; }
IMessageSerializer Serializer { get; }
void Start();
void Stop();
void RegisterHandler<T>(T instance) where T : class;
void RegisterHandler(Delegate handler);
}

View File

@@ -0,0 +1,7 @@
namespace Pilz.Net.Api;
public interface IMessageSerializer
{
string? Serialize(ApiMessage message);
ApiMessage? Deserialize(string json, Type target);
}

View File

@@ -0,0 +1,9 @@
namespace Pilz.Net.Api;
[AttributeUsage(AttributeTargets.Method)]
public class MessageHandlerAttribute(string route) : Attribute
{
public string Route { get; set; } = route;
public Type? Serializer { get; set; }
public bool RequiesAuth { get; set; }
}

View File

@@ -0,0 +1,206 @@
using Newtonsoft.Json.Linq;
using System.Data;
using System.Net;
namespace Pilz.Net;
public abstract class ConnectionManagerBase(int port)
{
private const int HEADER_LENGTH = 12;
private bool listening = false;
private readonly Dictionary<int, Dictionary<int, byte[]>> dicData = [];
public int Port { get; private set; } = port;
public bool UseAssemblyQualifiedName { get; set; } = false;
public event RetriveDataEventHandler? RetriveData;
public delegate void RetriveDataEventHandler(ConnectionManagerBase manager, string senderIP, string cmd, object content);
public bool IsListening
{
get => listening;
protected set => listening = value;
}
~ConnectionManagerBase()
{
Stop();
}
public abstract void Start();
public abstract void Stop();
protected abstract void SendData(IPEndPoint endPoint, byte[] data);
protected abstract int GetBufferSize();
public virtual void Send(string empfängerIP, string cmd)
{
Send(empfängerIP, cmd, string.Empty);
}
public virtual void Send(string empfängerIP, string cmd, string info)
{
Send(empfängerIP, cmd, (object)info);
}
private void RaiseRetriveData(string senderIP, string cmd, object content)
{
RetriveData?.Invoke(this, senderIP, cmd, content);
}
protected void ProcessRetrivedData(string senderIP, byte[] buf)
{
int readInteger(int index) => buf[index] << 24 | buf[index + 1] << 16 | buf[index + 2] << 8 | buf[index + 3];
int dataID = readInteger(0);
int packageID = readInteger(4);
int packageCount = readInteger(8);
bool resolveData = true;
// Remember data
byte[] data = buf.Skip(HEADER_LENGTH).ToArray();
Dictionary<int, byte[]> dicMyData;
if (dicData.ContainsKey(dataID))
{
dicMyData = dicData[dataID];
if (dicMyData.ContainsKey(packageID))
dicMyData[packageID] = data;
else
dicMyData.Add(packageID, data);
}
else
{
dicMyData = new Dictionary<int, byte[]>() { { packageID, data } };
dicData.Add(dataID, dicMyData);
}
if (dicMyData.Count < packageCount)
resolveData = false;
// Resolve Data
if (resolveData)
{
dicMyData ??= dicData[dataID];
var myData = new List<byte>();
foreach (var kvp in dicMyData.OrderBy(n => n.Key))
myData.AddRange(kvp.Value);
dicMyData.Remove(dataID);
object content = null;
string cmd = string.Empty;
try
{
var res = DecodeFromBytes(myData.ToArray());
cmd = res.cmd;
content = res.content;
}
catch (Exception)
{
}
RaiseRetriveData(senderIP, cmd, content);
}
}
private Random _Send_rnd = new();
public void Send(string empfängerIP, string cmd, object content)
{
var ep = new IPEndPoint(NetworkFeatures.GetIPFromHost(empfängerIP).MapToIPv4(), Port);
var finalBuffer = new List<byte>();
int maxBufferSize = GetBufferSize();
int maxDataSize = maxBufferSize - HEADER_LENGTH;
byte[] data = EncodeToBytes(cmd, content, UseAssemblyQualifiedName);
int dataID = _Send_rnd.Next();
// Some methods for later user
void send() => SendData(ep, finalBuffer.ToArray());
void addInteger(int value)
{
finalBuffer.Add((byte)(value >> 24 & 0xFF));
finalBuffer.Add((byte)(value >> 16 & 0xFF));
finalBuffer.Add((byte)(value >> 8 & 0xFF));
finalBuffer.Add((byte)(value & 0xFF));
};
void addHeader(int packageID, int packagesCount)
{
addInteger(dataID); // Data ID
addInteger(packageID); // Package ID
addInteger(packagesCount); // Packages Count
};
// Send data (this if statement and else content might be useless)
if (data.Length > maxDataSize)
{
int curIndex = 0;
int curID = 0;
int packagesCount = (int)Math.Round(Math.Ceiling(data.Length / (double)maxDataSize));
while (curIndex < data.Length)
{
finalBuffer.Clear();
addHeader(curID, packagesCount);
for (int i = 1, loopTo = maxDataSize; i <= loopTo; i++)
{
if (curIndex < data.Length)
{
finalBuffer.Add(data[curIndex]);
curIndex += 1;
}
}
send();
curID += 1;
}
}
else
{
addHeader(0, 1);
finalBuffer.AddRange(data);
send();
}
}
private static byte[] EncodeToBytes(string cmd, object content, bool useAssemblyQualifiedName)
{
var ms = new MemoryStream();
var bw = new BinaryWriter(ms);
var obj = new JObject
{
// Write header
["Cmd"] = cmd,
["ContentType"] = useAssemblyQualifiedName ? (content?.GetType()?.AssemblyQualifiedName) : (content?.GetType()?.ToString()),
// Content
["Content"] = JToken.FromObject(content)
};
// Write Json to MemoryStream
bw.Write(System.Text.Encoding.Default.GetBytes(obj.ToString()));
// Get Buffer Bytes
byte[] buf = ms.ToArray();
ms.Close();
return buf;
}
private static (string cmd, object content) DecodeFromBytes(byte[] buf)
{
string contentstring = System.Text.Encoding.Default.GetString(buf);
object content = null;
var contentobj = JObject.Parse(contentstring);
string cmd = (string)contentobj["Cmd"];
string contenttypestring = (string)contentobj["ContentType"];
var contentlinq = contentobj["Content"];
if (!string.IsNullOrEmpty(contenttypestring))
{
var contenttype = Type.GetType(contenttypestring);
content = contentlinq.ToObject(contenttype);
}
return (cmd, content);
}
}

View File

@@ -0,0 +1,62 @@
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
namespace Pilz.Net;
public static class NetworkFeatures
{
public static IPAddress? GetIPFromHost(string hostName)
{
return Dns.GetHostAddresses(hostName).FirstOrDefault(n => n.AddressFamily == AddressFamily.InterNetwork);
}
public static object? GetHostFromIP(string ip)
{
return Dns.GetHostEntry(ip)?.HostName;
}
public static UnicastIPAddressInformation? GetLocalIPInformations()
{
UnicastIPAddressInformation? addr = null;
foreach (NetworkInterface adapter in NetworkInterface.GetAllNetworkInterfaces())
{
if (addr is null && adapter.OperationalStatus == OperationalStatus.Up && adapter.NetworkInterfaceType != NetworkInterfaceType.Tunnel && adapter.NetworkInterfaceType != NetworkInterfaceType.Loopback)
{
foreach (UnicastIPAddressInformation uni in adapter.GetIPProperties().UnicastAddresses)
{
if (addr is null && uni.Address.AddressFamily == AddressFamily.InterNetwork)
addr = uni;
}
}
}
return addr;
}
public static IPAddress? GetLocalIPAddress()
{
return GetLocalIPInformations()?.Address;
}
public static IPAddress? GetLocalIPv4Mask()
{
return GetLocalIPInformations()?.IPv4Mask;
}
public static IPAddress GetLocalBoradcastIP(UnicastIPAddressInformation ipInfo)
{
IPAddress? ip = null;
byte[] myIPBytes = ipInfo.Address.GetAddressBytes();
byte[] subnetBytes = ipInfo.IPv4Mask.GetAddressBytes();
byte[] broadcastBytes = new byte[myIPBytes.Length];
for (int i = 0, loopTo = subnetBytes.Length - 1; i <= loopTo; i++)
broadcastBytes[i] = (byte)(myIPBytes[i] | ~subnetBytes[i]);
ip = new IPAddress(broadcastBytes);
return ip;
}
}

20
Pilz.Net/Pilz.Net.csproj Normal file
View File

@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net8.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.VisualBasic" Version="10.3.0" />
<PackageReference Include="System.Data.DataSetExtensions" Version="4.5.0" />
<PackageReference Include="System.Net.Http" Version="4.3.4" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Pilz.Extensions\Pilz.Extensions.csproj" />
</ItemGroup>
</Project>

73
Pilz.Net/TCPManager.cs Normal file
View File

@@ -0,0 +1,73 @@
using System.Net;
using System.Net.Sockets;
namespace Pilz.Net;
public class TCPManager(int port) : ConnectionManagerBase(port)
{
private readonly TcpListener listener = new TcpListener(IPAddress.Any, port);
public int BufferSize { get; set; } = 10240;
public override void Start()
{
if (!IsListening)
{
listener.Start();
IsListening = true;
Task.Run(CheckRetriveData);
}
}
public override void Stop()
{
if (IsListening)
{
IsListening = false;
listener.Stop();
}
}
protected override int GetBufferSize()
{
return BufferSize;
}
private void CheckRetriveData()
{
while (IsListening)
{
if (listener.Pending())
{
var tcp = listener.AcceptTcpClient();
string ip = ((IPEndPoint)tcp.Client.RemoteEndPoint!).Address.ToString();
var Stream = tcp.GetStream();
byte[] buf = new byte[BufferSize];
tcp.ReceiveBufferSize = BufferSize;
Stream.Read(buf, 0, buf.Length);
tcp.Close();
ProcessRetrivedData(ip, buf);
}
}
}
protected override void SendData(IPEndPoint ep, byte[] buf)
{
var tcp = new TcpClient
{
SendBufferSize = BufferSize
};
tcp.Connect(ep);
var stream = tcp.GetStream();
// Send Data
stream.Write(buf, 0, buf.Length);
stream.Flush();
tcp.Client.Shutdown(SocketShutdown.Both);
tcp.Close();
}
}

83
Pilz.Net/UDPManager.cs Normal file
View File

@@ -0,0 +1,83 @@
using System.Net;
using System.Net.Sockets;
namespace Pilz.Net;
public class UDPManager : ConnectionManagerBase
{
private readonly UdpClient client;
private Task? listenTask = null;
private readonly CancellationTokenSource cancelTokenSource = new();
private readonly CancellationToken cancelToken;
public int MaxBufferSize { get; private set; } = 8192;
public UDPManager(int port) : base(port)
{
cancelToken = cancelTokenSource.Token;
client = new UdpClient(port);
}
~UDPManager()
{
client.Client.Shutdown(SocketShutdown.Both);
client.Close();
}
public override void Start()
{
if (!IsListening)
{
StartInternal();
}
}
private void StartInternal()
{
IsListening = true;
listenTask = Task.Run(() => { try { RetriveAnyData(cancelToken); } catch (Exception ex) { IsListening = false; } });
}
public override void Stop()
{
if (IsListening)
{
IsListening = false;
cancelTokenSource.Cancel();
listenTask?.Wait();
}
}
protected override int GetBufferSize()
{
return MaxBufferSize;
}
private void RetriveAnyData(CancellationToken ct)
{
void doExit() => ct.ThrowIfCancellationRequested();
var receiveTask = client.ReceiveAsync();
// Wait for the data and cancel if requested
receiveTask.Wait(ct);
byte[] buf = receiveTask.Result.Buffer;
string ip = receiveTask.Result.RemoteEndPoint.Address.ToString();
doExit();
ProcessRetrivedData(ip, buf);
doExit();
StartInternal();
}
protected override void SendData(IPEndPoint ep, byte[] buf)
{
var udp = new UdpClient();
udp.Connect(ep);
udp.Send(buf, buf.Length);
udp.Client.Shutdown(SocketShutdown.Both);
udp.Close();
}
}