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