Null check instead of initilizing empty work

This commit is contained in:
derek.holloway committed 2026-08-10 07:26:18 -07:00
1 parent c8a0aef289
commit d7581fbdeb
2 files changed
+10 -22

No files matched your search

+7 -16
View File
@@ -6,7 +6,6 @@ using System.Net.Sockets;
using System.Numerics; using System.Numerics;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using MessagePack.Formatters;
using Mistox.ResevedPackets; using Mistox.ResevedPackets;
namespace Mistox.Client { namespace Mistox.Client {
@@ -37,14 +36,6 @@ namespace Mistox.Client {
UserName = userName; UserName = userName;
ConnectionKey = connectionKey; ConnectionKey = connectionKey;
onServerConnected += () => {};
onUserConnected += (_) => {};
onUserJoined += (_) => {};
onDataReceived += (_,_) => {};
onUserLeft += (_) => {};
onUserDisconnected += (_) => {};
onServerDisconnected += () => {};
// Create and connect to server on TCP // Create and connect to server on TCP
Console.WriteLine( "The client is initilized and trying to connect to the server at ip : " + Server.Address ); Console.WriteLine( "The client is initilized and trying to connect to the server at ip : " + Server.Address );
try { try {
@@ -148,7 +139,7 @@ namespace Mistox.Client {
///////////////////////////// /////////////////////////////
// When (self) connects to the server // When (self) connects to the server
public event Action onServerConnected; public event Action? onServerConnected;
public void InvokeOnServerConnected(){ public void InvokeOnServerConnected(){
Alive = true; Alive = true;
Task.Run(async () => { Task.Run(async () => {
@@ -160,19 +151,19 @@ namespace Mistox.Client {
} }
// When any user connects to the server (self) hasn't seen yet // When any user connects to the server (self) hasn't seen yet
public event Action<mUDPClient> onUserJoined; public event Action<mUDPClient>? onUserJoined;
public void InvokeOnUserJoined(mUDPClient User){ public void InvokeOnUserJoined(mUDPClient User){
onUserJoined?.Invoke(User); onUserJoined?.Invoke(User);
} }
// When user is within range of (self) and network rendering starts // When user is within range of (self) and network rendering starts
public event Action<mUDPClient> onUserConnected; public event Action<mUDPClient>? onUserConnected;
public void InvokeOnUserConnected(mUDPClient User){ public void InvokeOnUserConnected(mUDPClient User){
onUserConnected?.Invoke(User); onUserConnected?.Invoke(User);
} }
// When generic data is received // When generic data is received
public event Action<mUDPClient?, dynamic> onDataReceived; public event Action<mUDPClient?, dynamic>? onDataReceived;
public void InvokeOnReceived(dynamic DataPacket, mUDPClient? remote = null){ public void InvokeOnReceived(dynamic DataPacket, mUDPClient? remote = null){
switch (DataPacket as object) { switch (DataPacket as object) {
case HeartbeatPacket packet: case HeartbeatPacket packet:
@@ -193,19 +184,19 @@ namespace Mistox.Client {
} }
// When user is no longer in range of (self) and network rendering stops // When user is no longer in range of (self) and network rendering stops
public event Action<mUDPClient> onUserDisconnected; public event Action<mUDPClient>? onUserDisconnected;
public void InvokeOnUserDisconnected(mUDPClient User){ public void InvokeOnUserDisconnected(mUDPClient User){
onUserDisconnected?.Invoke(User); onUserDisconnected?.Invoke(User);
} }
// When any user disconnects from the server that isn't (self) // When any user disconnects from the server that isn't (self)
public event Action<mUDPClient> onUserLeft; public event Action<mUDPClient>? onUserLeft;
public void InvokeOnUserLeft(mUDPClient User){ public void InvokeOnUserLeft(mUDPClient User){
onUserLeft?.Invoke(User); onUserLeft?.Invoke(User);
} }
// When (self) disconnects to the server // When (self) disconnects to the server
public event Action onServerDisconnected; public event Action? onServerDisconnected;
public void InvokeOnServerDisconnected(){ public void InvokeOnServerDisconnected(){
Alive = false; Alive = false;
onServerDisconnected?.Invoke(); onServerDisconnected?.Invoke();
+3 -6
View File
@@ -22,9 +22,6 @@ namespace Mistox.Server {
public MistoxServer(IPEndPoint ListenerAddress, string serverName) { public MistoxServer(IPEndPoint ListenerAddress, string serverName) {
ServerName = serverName; ServerName = serverName;
Listener = new ServerListener(ListenerAddress, this); Listener = new ServerListener(ListenerAddress, this);
onUserConnected += (_) => {};
onDataReceived += (_, _) => {};
onUserDisconnected += (_) => {};
Console.WriteLine( "The Server is initilized and waiting for clients to connect at port : " + ListenerAddress.Port ); Console.WriteLine( "The Server is initilized and waiting for clients to connect at port : " + ListenerAddress.Port );
} }
@@ -75,13 +72,13 @@ namespace Mistox.Server {
// Events // Events
///////////////////////////// /////////////////////////////
public event Action<ServerConnection> onUserConnected; public event Action<ServerConnection>? onUserConnected;
public void InvokeUserConnected( ServerConnection user ){ public void InvokeUserConnected( ServerConnection user ){
Connections[user.UserName] = user; Connections[user.UserName] = user;
onUserConnected?.Invoke( user ); onUserConnected?.Invoke( user );
} }
public event Action<ServerConnection, object> onDataReceived; public event Action<ServerConnection, object>? onDataReceived;
public void InvokeDataReceived( ServerConnection user, dynamic data ){ public void InvokeDataReceived( ServerConnection user, dynamic data ){
switch (data) { switch (data) {
case UserAuthPacket packet when !user.IsAuthenticated: case UserAuthPacket packet when !user.IsAuthenticated:
@@ -104,7 +101,7 @@ namespace Mistox.Server {
} }
} }
public event Action<ServerConnection> onUserDisconnected; public event Action<ServerConnection>? onUserDisconnected;
public void InvokeUserDisconnected( ServerConnection user ){ public void InvokeUserDisconnected( ServerConnection user ){
Connections.Remove(user.UserName, out _); Connections.Remove(user.UserName, out _);
onUserDisconnected?.Invoke( user ); onUserDisconnected?.Invoke( user );