Split out the UDP listener for a single instance
This commit is contained in:
1 parent
6931846a64
commit
d3d437b0c8
5 files changed
+127
-68
No files matched your search
@@ -1,6 +1,8 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using Mistox.ResevedPackets;
|
||||
|
||||
namespace Mistox.Client {
|
||||
@@ -10,9 +12,16 @@ namespace Mistox.Client {
|
||||
public string ServerName { get; set; } = ""; // The Name of the server
|
||||
public bool Alive { get; set; } = true; // Is the server running
|
||||
public mTCPClient ServerConnection;
|
||||
public ConcurrentDictionary<string, mUDPClient> clientConnections;
|
||||
public UdpClient ClientSocket;
|
||||
public UDPReceiver UdpReceiver;
|
||||
public ConcurrentDictionary<UInt128, mUDPClient> clientConnections; // IP Address, Client
|
||||
|
||||
public MistoxClient(IPEndPoint Server) {
|
||||
// Create and listen on UDP
|
||||
ClientSocket = new UdpClient( Server.Port );
|
||||
UdpReceiver = new UDPReceiver( this );
|
||||
|
||||
// Create and connect to server on TCP
|
||||
Console.WriteLine( "The client is initilized and trying to connect to the server at ip : " + Server.Address );
|
||||
try {
|
||||
ServerConnection = new mTCPClient( this, Server );
|
||||
@@ -25,37 +34,35 @@ namespace Mistox.Client {
|
||||
Alive = true;
|
||||
}
|
||||
|
||||
/////////////////////////////
|
||||
// Receive Methods
|
||||
/////////////////////////////
|
||||
|
||||
public void UserConnected(UserConnectedPacket packet) {
|
||||
mUDPClient user = new mUDPClient(this, packet.ClientIPAddress, packet.ClientPort) {
|
||||
UserName = packet.UserName
|
||||
};
|
||||
clientConnections[packet.UserName] = user;
|
||||
clientConnections[user.GetConnectionKey()] = user;
|
||||
InvokeOnUserConnected(user);
|
||||
}
|
||||
|
||||
public void UserDisconnected(UserDisconnectedPacket packet) {
|
||||
mUDPClient copy = clientConnections[packet.UserName];
|
||||
clientConnections[packet.UserName].Alive = false;
|
||||
clientConnections[packet.UserName].Dispose();
|
||||
clientConnections[packet.UserName] = null;
|
||||
InvokeOnUserDisconnected( copy );
|
||||
mUDPClient client;
|
||||
bool found = clientConnections.TryGetValue(packet.ConnectionKey, out client);
|
||||
if (found) {
|
||||
InvokeOnUserDisconnected( client );
|
||||
client.Alive = false;
|
||||
client.Dispose();
|
||||
clientConnections[packet.ConnectionKey] = null;
|
||||
}
|
||||
}
|
||||
|
||||
/////////////////////////////
|
||||
// Events
|
||||
|
||||
public event Action onServerConnected;
|
||||
public void InvokeOnServerConnected(){
|
||||
Alive = true;
|
||||
onServerConnected?.Invoke();
|
||||
}
|
||||
|
||||
public event Action<mUDPClient> onUserConnected;
|
||||
public void InvokeOnUserConnected(mUDPClient ConnectedUser){
|
||||
onUserConnected?.Invoke(ConnectedUser);
|
||||
}
|
||||
/////////////////////////////
|
||||
|
||||
public event Action<dynamic> onDataReceived;
|
||||
public void InvokeOnReceived(dynamic DataPacket){
|
||||
public void InvokeOnReceived(dynamic DataPacket, IPEndPoint remote = null){
|
||||
switch (DataPacket as object) {
|
||||
case UserConnectedPacket packet:
|
||||
UserConnected(packet);
|
||||
@@ -72,6 +79,17 @@ namespace Mistox.Client {
|
||||
}
|
||||
}
|
||||
|
||||
public event Action onServerConnected;
|
||||
public void InvokeOnServerConnected(){
|
||||
Alive = true;
|
||||
onServerConnected?.Invoke();
|
||||
}
|
||||
|
||||
public event Action<mUDPClient> onUserConnected;
|
||||
public void InvokeOnUserConnected(mUDPClient ConnectedUser){
|
||||
onUserConnected?.Invoke(ConnectedUser);
|
||||
}
|
||||
|
||||
public event Action<mUDPClient> onUserDisconnected;
|
||||
public void InvokeOnUserDisconnected(mUDPClient DisconnectedUser){
|
||||
onUserDisconnected?.Invoke(DisconnectedUser);
|
||||
|
||||
Reference in new issue
Block a user