diff --git a/EonaCat.Connections.Client/EonaCat.Connections.Client.csproj b/EonaCat.Connections.Client/EonaCat.Connections.Client.csproj
index c448043..f01369b 100644
--- a/EonaCat.Connections.Client/EonaCat.Connections.Client.csproj
+++ b/EonaCat.Connections.Client/EonaCat.Connections.Client.csproj
@@ -11,10 +11,4 @@
-
-
- PreserveNewest
-
-
-
diff --git a/EonaCat.Connections.Client/Program.cs b/EonaCat.Connections.Client/Program.cs
index 16e395b..639c227 100644
--- a/EonaCat.Connections.Client/Program.cs
+++ b/EonaCat.Connections.Client/Program.cs
@@ -1,82 +1,310 @@
using EonaCat.Connections.Models;
+using EonaCat.Connections.Processors;
+using System.Reflection;
+using System.Text;
namespace EonaCat.Connections.Client.Example
{
- // This file is part of the EonaCat project(s) which is released under the Apache License.
- // See the LICENSE file or go to https://EonaCat.com/license for full license details.
-
public class Program
{
- private static NetworkClient _client;
+ private const bool UseProcessor = true;
+ private const bool IsHeartBeatEnabled = false;
+ private static List _clients = new List();
+ private static long clientCount = 1;
+ private static long clientsConnected = 0;
+ private static string _jsonContent;
+
+ //public static string SERVER_IP = "10.40.11.22";
+ public static string SERVER_IP = "127.0.0.1";
+
+ private static Dictionary> _clientsProcessors = new Dictionary>();
+ private static bool testDataProcessor;
+
+ public static bool WaitForMessage { get; private set; }
+ public static bool PressEnterForNextMessage { get; private set; }
+ public static bool ToConsoleOnly { get; private set; } = true;
+ public static bool UseJson { get; private set; } = true;
+ public static bool TESTBYTES { get; private set; }
+ public static bool UseJsonProcessorTest { get; private set; } = false;
public static async Task Main(string[] args)
{
- await CreateClientAsync().ConfigureAwait(false);
+ for (long i = 0; i < clientCount; i++)
+ {
+ var clientName = $"User {i}";
+ var client = await CreateClientAsync().ConfigureAwait(false);
+ _clients.Add(client);
+
+ if (testDataProcessor)
+ {
+ _clientsProcessors[client] = new JsonDataProcessor();
+ }
+ else
+ {
+ _ = StartClientAsync(clientName, client);
+ }
+ }
while (true)
{
- if (!_client.IsConnected)
- {
- await Task.Delay(1000).ConfigureAwait(false);
- continue;
- }
+ string message = string.Empty;
- Console.Write("Enter message to send (or 'exit' to quit): ");
- var message = Console.ReadLine();
+ if (WaitForMessage)
+ {
+ Console.Write("Enter message to send (or 'exit' to quit): ");
+ message = Console.ReadLine();
+ }
if (!string.IsNullOrEmpty(message) && message.Equals("exit", StringComparison.OrdinalIgnoreCase))
{
- await _client.DisconnectAsync().ConfigureAwait(false);
+ foreach (var client in _clients)
+ {
+ await client.DisconnectClientAsync().ConfigureAwait(false);
+ }
break;
}
+ var jsonUrl = "https://samples.json-format.com/employees/json/employees_500KB.json";
+
+ try
+ {
+ if (string.IsNullOrEmpty(_jsonContent) && UseJson)
+ {
+ using var httpClient = new HttpClient();
+ _jsonContent = await httpClient.GetStringAsync(jsonUrl);
+
+ var jsonSize = Encoding.UTF8.GetByteCount(_jsonContent);
+ WriteToLog($"Using large JSON file (size: {jsonSize / 1024 / 1024} MB)");
+ }
+
+ if (UseJson)
+ {
+ message = _jsonContent;
+ }
+
+ if (UseJsonProcessorTest)
+ {
+ foreach (var client in _clients)
+ {
+ var processor = _clientsProcessors[client];
+ processor.OnProcessTextMessage += (sender, e) =>
+ {
+ WriteToLog($"Processed message from {e.ClientName}: {e.Text}");
+ };
+ processor.OnProcessMessage += (sender, e) =>
+ {
+ WriteToLog($"Processed JSON message from {e.ClientName} ({e.ClientEndpoint}): {e.RawData}");
+ };
+ processor.MaxAllowedBufferSize = 10 * 1024 * 1024; // 10 MB
+ processor.MaxMessagesPerBatch = 5;
+ var json = _jsonContent;
+
+ while (true)
+ {
+ processor.Process(json, "TestClient");
+ await Task.Delay(100).ConfigureAwait(false);
+ }
+ }
+ }
+ }
+ catch (Exception exception)
+ {
+ WriteToLog($"Failed to download large JSON file: {exception.Message}");
+ }
+
if (!string.IsNullOrEmpty(message))
{
- await _client.SendAsync(message).ConfigureAwait(false);
+ foreach (var client in _clients)
+ {
+ if (TESTBYTES)
+ {
+ var bytes = new byte[] { 0x00, 0x04, 0x31, 0x32, 0x30, 0x30 };
+ await client.SendAsync(bytes).ConfigureAwait(false);
+ }
+ else
+ {
+ await client.SendAsync(message).ConfigureAwait(false);
+ }
+ }
}
+
+ await Task.Delay(1000).ConfigureAwait(false);
}
}
- private static async Task CreateClientAsync()
+ private static async Task StartClientAsync(string clientName, NetworkClient client)
+ {
+ await client.ConnectAsync().ConfigureAwait(false);
+
+ // Send nickname
+ await client.SendNicknameAsync(clientName);
+
+ // Send a message
+ await client.SendAsync($"Hello server, my name is {clientName}!");
+ }
+
+ private static async Task CreateClientAsync()
{
var config = new Configuration
{
Protocol = ProtocolType.TCP,
- Host = "127.0.0.1",
+ Host = SERVER_IP,
Port = 1111,
- UseSsl = true,
- UseAesEncryption = true,
+ UseSsl = false,
+ UseAesEncryption = false,
+ EnableHeartbeat = IsHeartBeatEnabled,
AesPassword = "EonaCat.Connections.Password",
Certificate = new System.Security.Cryptography.X509Certificates.X509Certificate2("client.pfx", "p@ss"),
};
- _client = new NetworkClient(config);
-
- _client.OnGeneralError += (sender, e) =>
- Console.WriteLine($"Error: {e.Message}");
+ var client = new NetworkClient(config);
// Subscribe to events
- _client.OnConnected += async (sender, e) =>
+ client.OnConnected += (sender, e) =>
{
+ WriteToLog($"Connected to server at {e.RemoteEndPoint}");
Console.WriteLine($"Connected to server at {e.RemoteEndPoint}");
-
- // Set nickname
- await _client.SendNicknameAsync("TestUser");
-
- // Send a message
- await _client.SendAsync("Hello server!");
+ Console.Title = $"Total clients {++clientsConnected}";
};
- _client.OnDataReceived += (sender, e) =>
- Console.WriteLine($"Server says: {(e.IsBinary ? $"{e.Data.Length} bytes" : e.StringData)}");
-
- _client.OnDisconnected += (sender, e) =>
+ if (UseProcessor)
{
- Console.WriteLine("Disconnected from server");
+ _clientsProcessors[client] = new JsonDataProcessor