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.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<Socket> disconnectMethod;
public static Action<Socket> connectMethod;
public static List<MessageEvent> messageEvents;
public static Action<Socket>? disconnectMethod;
public static Action<Socket>? connectMethod;
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
port = _port;
buffer = new byte[1024];
if (_port != null)
port = (int)_port;
messageEvents = new List<MessageEvent>();
// 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.");
}
}
}

View File

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