Added nicknames
This commit is contained in:
parent
98aa0467b1
commit
32c2491d25
|
@ -46,13 +46,17 @@ namespace EonaCat.Connections.Client.Example
|
||||||
|
|
||||||
// Subscribe to events
|
// Subscribe to events
|
||||||
_client.OnConnected += (sender, e) =>
|
_client.OnConnected += (sender, e) =>
|
||||||
|
{
|
||||||
Console.WriteLine($"Connected to server at {e.RemoteEndPoint}");
|
Console.WriteLine($"Connected to server at {e.RemoteEndPoint}");
|
||||||
|
};
|
||||||
|
|
||||||
_client.OnDataReceived += (sender, e) =>
|
_client.OnDataReceived += (sender, e) =>
|
||||||
Console.WriteLine($"Server says: {(e.IsBinary ? $"{e.Data.Length} bytes" : e.StringData)}");
|
Console.WriteLine($"Server says: {(e.IsBinary ? $"{e.Data.Length} bytes" : e.StringData)}");
|
||||||
|
|
||||||
_client.OnDisconnected += (sender, e) =>
|
_client.OnDisconnected += (sender, e) =>
|
||||||
|
{
|
||||||
Console.WriteLine("Disconnected from server");
|
Console.WriteLine("Disconnected from server");
|
||||||
|
};
|
||||||
|
|
||||||
await _client.ConnectAsync();
|
await _client.ConnectAsync();
|
||||||
|
|
||||||
|
|
|
@ -52,8 +52,15 @@ namespace EonaCat.Connections.Server.Example
|
||||||
Console.WriteLine($"Client {e.ClientId} connected with nickname: {e.Nickname}");
|
Console.WriteLine($"Client {e.ClientId} connected with nickname: {e.Nickname}");
|
||||||
|
|
||||||
_server.OnDataReceived += async (sender, e) =>
|
_server.OnDataReceived += async (sender, e) =>
|
||||||
|
{
|
||||||
|
if (e.HasNickname)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Received from {e.Nickname}: {(e.IsBinary ? $"{e.Data.Length} bytes" : e.StringData)}");
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
Console.WriteLine($"Received from {e.ClientId}: {(e.IsBinary ? $"{e.Data.Length} bytes" : e.StringData)}");
|
Console.WriteLine($"Received from {e.ClientId}: {(e.IsBinary ? $"{e.Data.Length} bytes" : e.StringData)}");
|
||||||
|
}
|
||||||
|
|
||||||
// Echo back the message
|
// Echo back the message
|
||||||
if (e.IsBinary)
|
if (e.IsBinary)
|
||||||
|
@ -67,7 +74,16 @@ namespace EonaCat.Connections.Server.Example
|
||||||
};
|
};
|
||||||
|
|
||||||
_server.OnDisconnected += (sender, e) =>
|
_server.OnDisconnected += (sender, e) =>
|
||||||
|
{
|
||||||
|
if (e.HasNickname)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Client {e.Nickname} disconnected");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
Console.WriteLine($"Client {e.ClientId} disconnected");
|
Console.WriteLine($"Client {e.ClientId} disconnected");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
await _server.StartAsync();
|
await _server.StartAsync();
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,7 @@
|
||||||
<Copyright>EonaCat (Jeroen Saey)</Copyright>
|
<Copyright>EonaCat (Jeroen Saey)</Copyright>
|
||||||
<PackageReadmeFile>readme.md</PackageReadmeFile>
|
<PackageReadmeFile>readme.md</PackageReadmeFile>
|
||||||
<PackageId>EonaCat.Connections</PackageId>
|
<PackageId>EonaCat.Connections</PackageId>
|
||||||
<Version>1.0.3</Version>
|
<Version>1.0.4</Version>
|
||||||
<Authors>EonaCat (Jeroen Saey)</Authors>
|
<Authors>EonaCat (Jeroen Saey)</Authors>
|
||||||
<PackageLicenseFile>LICENSE</PackageLicenseFile>
|
<PackageLicenseFile>LICENSE</PackageLicenseFile>
|
||||||
<PackageIcon>EonaCat.png</PackageIcon>
|
<PackageIcon>EonaCat.png</PackageIcon>
|
||||||
|
|
|
@ -5,6 +5,8 @@ namespace EonaCat.Connections.EventArguments
|
||||||
public class ConnectionEventArgs : EventArgs
|
public class ConnectionEventArgs : EventArgs
|
||||||
{
|
{
|
||||||
public string ClientId { get; set; }
|
public string ClientId { get; set; }
|
||||||
|
public string Nickname { get; set; }
|
||||||
|
public bool HasNickname => !string.IsNullOrEmpty(Nickname);
|
||||||
public IPEndPoint RemoteEndPoint { get; set; }
|
public IPEndPoint RemoteEndPoint { get; set; }
|
||||||
public DateTime Timestamp { get; set; } = DateTime.UtcNow;
|
public DateTime Timestamp { get; set; } = DateTime.UtcNow;
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,5 +11,6 @@ namespace EonaCat.Connections
|
||||||
public DateTime Timestamp { get; internal set; } = DateTime.UtcNow;
|
public DateTime Timestamp { get; internal set; } = DateTime.UtcNow;
|
||||||
public IPEndPoint RemoteEndPoint { get; internal set; }
|
public IPEndPoint RemoteEndPoint { get; internal set; }
|
||||||
public string Nickname { get; internal set; }
|
public string Nickname { get; internal set; }
|
||||||
|
public bool HasNickname => !string.IsNullOrEmpty(Nickname);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,7 +0,0 @@
|
||||||
namespace EonaCat.Connections.EventArguments
|
|
||||||
{
|
|
||||||
public class NicknameConnectionEventArgs : ConnectionEventArgs
|
|
||||||
{
|
|
||||||
public string Nickname { get; set; }
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -110,6 +110,8 @@ namespace EonaCat.Connections
|
||||||
public string IpAddress => _config != null ? _config.Host : string.Empty;
|
public string IpAddress => _config != null ? _config.Host : string.Empty;
|
||||||
public int Port => _config != null ? _config.Port : 0;
|
public int Port => _config != null ? _config.Port : 0;
|
||||||
private async Task ConnectUdp()
|
private async Task ConnectUdp()
|
||||||
|
{
|
||||||
|
await Task.Run(() =>
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
@ -126,6 +128,7 @@ namespace EonaCat.Connections
|
||||||
{
|
{
|
||||||
OnGeneralError?.Invoke(this, new ErrorEventArgs { Exception = ex, Message = "Failed to connect UDP" });
|
OnGeneralError?.Invoke(this, new ErrorEventArgs { Exception = ex, Message = "Failed to connect UDP" });
|
||||||
}
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -231,6 +234,8 @@ namespace EonaCat.Connections
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task ProcessReceivedDataAsync(byte[] data)
|
private async Task ProcessReceivedDataAsync(byte[] data)
|
||||||
|
{
|
||||||
|
await Task.Run(() =>
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
@ -272,6 +277,7 @@ namespace EonaCat.Connections
|
||||||
OnGeneralError?.Invoke(this, new ErrorEventArgs { Exception = ex, Message = "Error processing data" });
|
OnGeneralError?.Invoke(this, new ErrorEventArgs { Exception = ex, Message = "Error processing data" });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -400,6 +406,8 @@ namespace EonaCat.Connections
|
||||||
|
|
||||||
|
|
||||||
public async Task DisconnectAsync()
|
public async Task DisconnectAsync()
|
||||||
|
{
|
||||||
|
await Task.Run(() =>
|
||||||
{
|
{
|
||||||
_isConnected = false;
|
_isConnected = false;
|
||||||
_cancellation?.Cancel();
|
_cancellation?.Cancel();
|
||||||
|
@ -411,6 +419,7 @@ namespace EonaCat.Connections
|
||||||
OnDisconnected?.Invoke(this, new ConnectionEventArgs { ClientId = "self" });
|
OnDisconnected?.Invoke(this, new ConnectionEventArgs { ClientId = "self" });
|
||||||
|
|
||||||
_ = Task.Run(() => AutoReconnectAsync());
|
_ = Task.Run(() => AutoReconnectAsync());
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
|
|
|
@ -24,7 +24,7 @@ namespace EonaCat.Connections
|
||||||
private readonly object _statsLock = new object();
|
private readonly object _statsLock = new object();
|
||||||
|
|
||||||
public event EventHandler<ConnectionEventArgs> OnConnected;
|
public event EventHandler<ConnectionEventArgs> OnConnected;
|
||||||
public event EventHandler<NicknameConnectionEventArgs> OnConnectedWithNickname;
|
public event EventHandler<ConnectionEventArgs> OnConnectedWithNickname;
|
||||||
public event EventHandler<DataReceivedEventArgs> OnDataReceived;
|
public event EventHandler<DataReceivedEventArgs> OnDataReceived;
|
||||||
public event EventHandler<ConnectionEventArgs> OnDisconnected;
|
public event EventHandler<ConnectionEventArgs> OnDisconnected;
|
||||||
public event EventHandler<ErrorEventArgs> OnSslError;
|
public event EventHandler<ErrorEventArgs> OnSslError;
|
||||||
|
@ -346,7 +346,7 @@ namespace EonaCat.Connections
|
||||||
{
|
{
|
||||||
var nickname = stringData.Substring(9);
|
var nickname = stringData.Substring(9);
|
||||||
client.Nickname = nickname;
|
client.Nickname = nickname;
|
||||||
OnConnectedWithNickname?.Invoke(this, new NicknameConnectionEventArgs
|
OnConnectedWithNickname?.Invoke(this, new ConnectionEventArgs
|
||||||
{
|
{
|
||||||
ClientId = client.Id,
|
ClientId = client.Id,
|
||||||
RemoteEndPoint = client.RemoteEndPoint,
|
RemoteEndPoint = client.RemoteEndPoint,
|
||||||
|
@ -365,7 +365,7 @@ namespace EonaCat.Connections
|
||||||
{
|
{
|
||||||
client.Nickname = nickname;
|
client.Nickname = nickname;
|
||||||
}
|
}
|
||||||
OnConnectedWithNickname?.Invoke(this, new NicknameConnectionEventArgs
|
OnConnectedWithNickname?.Invoke(this, new ConnectionEventArgs
|
||||||
{
|
{
|
||||||
ClientId = client.Id,
|
ClientId = client.Id,
|
||||||
RemoteEndPoint = client.RemoteEndPoint,
|
RemoteEndPoint = client.RemoteEndPoint,
|
||||||
|
@ -486,7 +486,7 @@ namespace EonaCat.Connections
|
||||||
Buffer.BlockCopy(lengthPrefix, 0, framed, 0, lengthPrefix.Length);
|
Buffer.BlockCopy(lengthPrefix, 0, framed, 0, lengthPrefix.Length);
|
||||||
Buffer.BlockCopy(data, 0, framed, lengthPrefix.Length, data.Length);
|
Buffer.BlockCopy(data, 0, framed, lengthPrefix.Length, data.Length);
|
||||||
|
|
||||||
data = framed; // replace data with framed payload
|
data = framed; // replace the data with framed payload
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_config.Protocol == ProtocolType.TCP)
|
if (_config.Protocol == ProtocolType.TCP)
|
||||||
|
@ -545,6 +545,8 @@ namespace EonaCat.Connections
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task DisconnectClientAsync(string clientId)
|
private async Task DisconnectClientAsync(string clientId)
|
||||||
|
{
|
||||||
|
await Task.Run(() =>
|
||||||
{
|
{
|
||||||
if (_clients.TryRemove(clientId, out var client))
|
if (_clients.TryRemove(clientId, out var client))
|
||||||
{
|
{
|
||||||
|
@ -555,13 +557,14 @@ namespace EonaCat.Connections
|
||||||
client.Stream?.Dispose();
|
client.Stream?.Dispose();
|
||||||
client.AesEncryption?.Dispose();
|
client.AesEncryption?.Dispose();
|
||||||
|
|
||||||
OnDisconnected?.Invoke(this, new ConnectionEventArgs { ClientId = clientId, RemoteEndPoint = client.RemoteEndPoint });
|
OnDisconnected?.Invoke(this, new ConnectionEventArgs { ClientId = clientId, RemoteEndPoint = client.RemoteEndPoint, Nickname = client.Nickname });
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
OnGeneralError?.Invoke(this, new ErrorEventArgs { ClientId = clientId, Exception = ex, Message = "Error disconnecting client" });
|
OnGeneralError?.Invoke(this, new ErrorEventArgs { ClientId = clientId, Exception = ex, Message = "Error disconnecting client" });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Stop()
|
public void Stop()
|
||||||
|
|
44
README.md
44
README.md
|
@ -34,7 +34,6 @@ servers and clients with optional TLS (for TCP) and optional application-layer e
|
||||||
|
|
||||||
## Server example:
|
## Server example:
|
||||||
|
|
||||||
using EonaCat.Connections;
|
|
||||||
using EonaCat.Connections.Models;
|
using EonaCat.Connections.Models;
|
||||||
|
|
||||||
namespace EonaCat.Connections.Server.Example
|
namespace EonaCat.Connections.Server.Example
|
||||||
|
@ -71,11 +70,12 @@ servers and clients with optional TLS (for TCP) and optional application-layer e
|
||||||
var config = new Configuration
|
var config = new Configuration
|
||||||
{
|
{
|
||||||
Protocol = ProtocolType.TCP,
|
Protocol = ProtocolType.TCP,
|
||||||
Port = 8080,
|
Port = 1111,
|
||||||
UseSsl = true,
|
UseSsl = false,
|
||||||
UseAesEncryption = true,
|
UseAesEncryption = true,
|
||||||
MaxConnections = 100000,
|
MaxConnections = 100000,
|
||||||
ServerCertificate = new System.Security.Cryptography.X509Certificates.X509Certificate2("server.pfx", "p@ss"),
|
AesPassword = "EonaCat.Connections.Password",
|
||||||
|
//Certificate = new System.Security.Cryptography.X509Certificates.X509Certificate2("server.pfx", "p@ss")
|
||||||
};
|
};
|
||||||
|
|
||||||
_server = new NetworkServer(config);
|
_server = new NetworkServer(config);
|
||||||
|
@ -88,8 +88,15 @@ servers and clients with optional TLS (for TCP) and optional application-layer e
|
||||||
Console.WriteLine($"Client {e.ClientId} connected with nickname: {e.Nickname}");
|
Console.WriteLine($"Client {e.ClientId} connected with nickname: {e.Nickname}");
|
||||||
|
|
||||||
_server.OnDataReceived += async (sender, e) =>
|
_server.OnDataReceived += async (sender, e) =>
|
||||||
|
{
|
||||||
|
if (e.HasNickname)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Received from {e.Nickname}: {(e.IsBinary ? $"{e.Data.Length} bytes" : e.StringData)}");
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
Console.WriteLine($"Received from {e.ClientId}: {(e.IsBinary ? $"{e.Data.Length} bytes" : e.StringData)}");
|
Console.WriteLine($"Received from {e.ClientId}: {(e.IsBinary ? $"{e.Data.Length} bytes" : e.StringData)}");
|
||||||
|
}
|
||||||
|
|
||||||
// Echo back the message
|
// Echo back the message
|
||||||
if (e.IsBinary)
|
if (e.IsBinary)
|
||||||
|
@ -103,7 +110,16 @@ servers and clients with optional TLS (for TCP) and optional application-layer e
|
||||||
};
|
};
|
||||||
|
|
||||||
_server.OnDisconnected += (sender, e) =>
|
_server.OnDisconnected += (sender, e) =>
|
||||||
|
{
|
||||||
|
if (e.HasNickname)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Client {e.Nickname} disconnected");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
Console.WriteLine($"Client {e.ClientId} disconnected");
|
Console.WriteLine($"Client {e.ClientId} disconnected");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
await _server.StartAsync();
|
await _server.StartAsync();
|
||||||
}
|
}
|
||||||
|
@ -114,6 +130,7 @@ servers and clients with optional TLS (for TCP) and optional application-layer e
|
||||||
|
|
||||||
using EonaCat.Connections;
|
using EonaCat.Connections;
|
||||||
using EonaCat.Connections.Models;
|
using EonaCat.Connections.Models;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
namespace EonaCat.Connections.Client.Example
|
namespace EonaCat.Connections.Client.Example
|
||||||
{
|
{
|
||||||
|
@ -121,9 +138,9 @@ servers and clients with optional TLS (for TCP) and optional application-layer e
|
||||||
{
|
{
|
||||||
private static NetworkClient _client;
|
private static NetworkClient _client;
|
||||||
|
|
||||||
public static void Main(string[] args)
|
public static async Task Main(string[] args)
|
||||||
{
|
{
|
||||||
CreateClientAsync().ConfigureAwait(false);
|
await CreateClientAsync().ConfigureAwait(false);
|
||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
|
@ -131,13 +148,13 @@ servers and clients with optional TLS (for TCP) and optional application-layer e
|
||||||
var message = Console.ReadLine();
|
var message = Console.ReadLine();
|
||||||
if (!string.IsNullOrEmpty(message) && message.Equals("exit", StringComparison.OrdinalIgnoreCase))
|
if (!string.IsNullOrEmpty(message) && message.Equals("exit", StringComparison.OrdinalIgnoreCase))
|
||||||
{
|
{
|
||||||
_client.DisconnectAsync().ConfigureAwait(false);
|
await _client.DisconnectAsync().ConfigureAwait(false);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(message))
|
if (!string.IsNullOrEmpty(message))
|
||||||
{
|
{
|
||||||
_client.SendAsync(message).ConfigureAwait(false);
|
await _client.SendAsync(message).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -148,23 +165,28 @@ servers and clients with optional TLS (for TCP) and optional application-layer e
|
||||||
{
|
{
|
||||||
Protocol = ProtocolType.TCP,
|
Protocol = ProtocolType.TCP,
|
||||||
Host = "127.0.0.1",
|
Host = "127.0.0.1",
|
||||||
Port = 8080,
|
Port = 1111,
|
||||||
UseSsl = true,
|
UseSsl = false,
|
||||||
UseAesEncryption = true,
|
UseAesEncryption = true,
|
||||||
ServerCertificate = new System.Security.Cryptography.X509Certificates.X509Certificate2("client.pfx", "p@ss"),
|
AesPassword = "EonaCat.Connections.Password",
|
||||||
|
//Certificate = new System.Security.Cryptography.X509Certificates.X509Certificate2("client.pfx", "p@ss"),
|
||||||
};
|
};
|
||||||
|
|
||||||
_client = new NetworkClient(config);
|
_client = new NetworkClient(config);
|
||||||
|
|
||||||
// Subscribe to events
|
// Subscribe to events
|
||||||
_client.OnConnected += (sender, e) =>
|
_client.OnConnected += (sender, e) =>
|
||||||
|
{
|
||||||
Console.WriteLine($"Connected to server at {e.RemoteEndPoint}");
|
Console.WriteLine($"Connected to server at {e.RemoteEndPoint}");
|
||||||
|
};
|
||||||
|
|
||||||
_client.OnDataReceived += (sender, e) =>
|
_client.OnDataReceived += (sender, e) =>
|
||||||
Console.WriteLine($"Server says: {(e.IsBinary ? $"{e.Data.Length} bytes" : e.StringData)}");
|
Console.WriteLine($"Server says: {(e.IsBinary ? $"{e.Data.Length} bytes" : e.StringData)}");
|
||||||
|
|
||||||
_client.OnDisconnected += (sender, e) =>
|
_client.OnDisconnected += (sender, e) =>
|
||||||
|
{
|
||||||
Console.WriteLine("Disconnected from server");
|
Console.WriteLine("Disconnected from server");
|
||||||
|
};
|
||||||
|
|
||||||
await _client.ConnectAsync();
|
await _client.ConnectAsync();
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue