CLre/CLre_server/API/Synergy/Message.cs

63 lines
1.9 KiB
C#
Raw Normal View History

2021-05-27 01:22:23 +01:00
using System;
using System.Collections.Generic;
using GameNetworkLayer.Shared;
namespace CLre_server.API.Synergy
{
public static class Message
{
internal const NetworkDispatcherCode CLre_MESSAGE_NETCODE = (NetworkDispatcherCode) 219;
private static readonly Dictionary<uint, Action<ReceiveMessageArgs>> handlers =
new Dictionary<uint, Action<ReceiveMessageArgs>>();
private static readonly ServerMessagingEngine msgEngine = new ServerMessagingEngine();
private static readonly List<int> clrePlayers = new List<int>();
public static void SendToAllCLreClients(ref SerializedCLreMessage message)
{
foreach (var playerId in clrePlayers)
{
SendCLreMessage(playerId, ref message);
}
}
public static void SendCLreMessage(int playerId, ref SerializedCLreMessage message)
{
msgEngine.EnqueueMessage(playerId, ref message);
}
public static void RegisterListener(uint id, Action<ReceiveMessageArgs> handler)
{
if (handlers.TryGetValue(id, out Action<ReceiveMessageArgs> existing))
{
existing += handler;
handlers[id] = existing;
}
else
{
handlers[id] = handler;
}
}
internal static void HandleMessageReceive(int playerId, ref SerializedCLreMessage msg)
{
ReceiveMessageArgs payload = new ReceiveMessageArgs
{
Message = msg,
PlayerId = playerId,
};
if (handlers.TryGetValue(msg.Id, out Action<ReceiveMessageArgs> h))
{
h(payload);
}
}
}
public struct ReceiveMessageArgs
{
public SerializedCLreMessage Message;
public int PlayerId;
}
}