Files
EonaCat.Connections/EonaCat.Connections.Server/Program.cs
2025-11-28 19:37:09 +01:00

157 lines
5.6 KiB
C#

// 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.
using EonaCat.Connections.Models;
using System.Reflection;
namespace EonaCat.Connections.Server.Example
{
public class Program
{
private const bool IsHeartBeatEnabled = false;
private static NetworkServer _server;
public static bool WaitForMessage { get; private set; } = true;
public static bool ToConsoleOnly { get; private set; } = true;
public static async Task Main(string[] args)
{
CreateServerAsync().ConfigureAwait(false);
while (true)
{
var message = string.Empty;
if (WaitForMessage)
{
Console.Write("Enter message to send (or 'exit' to quit): ");
message = Console.ReadLine();
}
if (!string.IsNullOrEmpty(message) && message.Equals("exit", StringComparison.OrdinalIgnoreCase))
{
_server.Stop();
_server.Dispose();
Console.WriteLine("Server stopped.");
break;
}
if (!string.IsNullOrEmpty(message))
{
await _server.BroadcastAsync(message).ConfigureAwait(false);
}
await Task.Delay(5000).ConfigureAwait(false);
}
}
private static async Task CreateServerAsync()
{
var config = new Configuration
{
Protocol = ProtocolType.TCP,
Host = "0.0.0.0",
Port = 1111,
UseSsl = false,
UseAesEncryption = false,
MaxConnections = 100000,
AesPassword = "EonaCat.Connections.Password",
Certificate = new System.Security.Cryptography.X509Certificates.X509Certificate2("server.pfx", "p@ss"),
EnableHeartbeat = IsHeartBeatEnabled
};
_server = new NetworkServer(config);
// Subscribe to events
_server.OnConnected += (sender, e) =>
{
WriteToLog($"Client {e.ClientId} connected from {e.RemoteEndPoint}");
Console.WriteLine($"New connection from {e.RemoteEndPoint} with Client ID: {e.ClientId}");
};
_server.OnConnectedWithNickname += (sender, e) => WriteToLog($"Client {e.ClientId} connected with nickname: {e.Nickname}");
_server.OnDataReceived += async (sender, e) =>
{
WriteToLog($"Received from {e.ClientId} ({e.RemoteEndPoint.ToString()}): {(e.IsBinary ? $"{e.Data.Length} bytes" : "a message")}");
// Echo back the message
if (e.IsBinary)
{
await _server.SendToClientAsync(e.ClientId, e.Data);
}
else
{
await _server.SendToClientAsync(e.ClientId, e.StringData);
}
};
_server.OnDisconnected += (sender, e) =>
{
var message = string.Empty;
if (e.Reason == DisconnectReason.LocalClosed)
{
if (e.Exception != null)
{
message = $"{e.Nickname} disconnected from server (local close). Exception: {e.Exception.Message}";
}
else
{
message = $"{e.Nickname} disconnected from server (local close).";
}
}
else
{
if (e.Exception != null)
{
message = $"{e.Nickname} disconnected from server (remote close). Reason: {e.Reason}. Exception: {e.Exception.Message}";
}
else
{
message = $"{e.Nickname} disconnected from server (remote close). Reason: {e.Reason}";
}
}
WriteToLog(message);
Console.WriteLine(message);
};
_ = _server.StartAsync();
}
public static void WriteToLog(string message)
{
try
{
if (ToConsoleOnly)
{
var dateTimeNow = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
Console.WriteLine($"{dateTimeNow}: {message}");
return;
}
var logFilePath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? ".", "server_log.txt");
var logMessage = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss} - {message}{Environment.NewLine}";
if (!File.Exists(logFilePath))
{
File.WriteAllText(logFilePath, logMessage);
return;
}
if (new FileInfo(logFilePath).Length > 5 * 1024 * 1024) // 5 MB
{
var archiveFilePath = Path.Combine(Path.GetDirectoryName(logFilePath) ?? ".", $"server_log{DateTime.Now:yyyyMMdd_HHmmss}.txt");
File.Move(logFilePath, archiveFilePath);
File.WriteAllText(logFilePath, logMessage);
return;
}
File.AppendAllText(logFilePath, logMessage);
}
catch
{
// Ignore logging errors
}
}
}
}