raw message handling & nullable

This commit is contained in:
waltem01 2022-08-18 14:02:50 +02:00
parent f8e878a6fb
commit f72ab420a5
2 changed files with 31 additions and 28 deletions

View File

@ -1,29 +1,29 @@
using System; using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
using System.Threading; using System.Threading;
using System.Collections; using System.Text;
using System.Net;
using System;
namespace SocketLib; namespace SocketLib;
public static class Connection { public static class Connection {
public static int port; public static int port = 3000;
public static Socket self; public static Socket? self;
private static byte[] buffer; private static byte[] buffer = new byte[1024];
public static string ipAddress; public static string ipAddress = "127.0.0.1";
private static int listeners = 0; private static int listeners = 0;
public static Action<Socket> disconnectMethod; public static Action<Socket>? disconnectMethod;
public static Action<Socket> connectMethod; public static Action<Socket>? connectMethod;
public static List<MessageEvent> messageEvents; public static Action<Socket, string>? messageMethod;
public static List<MessageEvent>? messageEvents;
public static void Initialize(int _port=3000) { public static void Initialize(int? _port=null) {
// Initialize global variables // Initialize global variables
port = _port; if (_port != null)
buffer = new byte[1024]; port = (int)_port;
messageEvents = new List<MessageEvent>(); messageEvents = new List<MessageEvent>();
// Get computers' ipv4 address // Get computers' ipv4 address
ipAddress = "127.0.0.1";
try { try {
string hostName = Dns.GetHostName(); string hostName = Dns.GetHostName();
IPHostEntry entryList = Dns.GetHostEntry(hostName); IPHostEntry entryList = Dns.GetHostEntry(hostName);
@ -33,7 +33,8 @@ public static class Connection {
ipv4Entry = entry; ipv4Entry = entry;
break; break;
} }
ipAddress = ipv4Entry.ToString(); if (ipv4Entry != null)
ipAddress = ipv4Entry.ToString();
} catch (Exception) {} } catch (Exception) {}
} }
@ -161,15 +162,18 @@ public static class Connection {
// Console.WriteLine($"Event: {eventName}; Params: {rest}; Methods: {messageEvents.Count};"); // Console.WriteLine($"Event: {eventName}; Params: {rest}; Methods: {messageEvents.Count};");
foreach (MessageEvent mE in messageEvents) { if (messageMethod != null)
if (mE.name == eventName) { messageMethod.Invoke(other, message);
if (rest == "")
mE.method.DynamicInvoke(other); if (messageEvents != null)
else foreach (MessageEvent mE in messageEvents)
mE.method.DynamicInvoke(other, rest); if (mE.name == eventName) {
// Console.WriteLine("Method was Invoked."); if (rest == "")
} mE.method.DynamicInvoke(other);
} else
mE.method.DynamicInvoke(other, rest);
// Console.WriteLine("Method was Invoked.");
}
} }
} }

View File

@ -1,8 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net6.0</TargetFramework> <TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>