quantum-space-buddies/QuantumUNET/QSBNetworkMessageHandlers.cs

81 lines
1.9 KiB
C#
Raw Normal View History

2020-12-07 21:19:16 +00:00
using QuantumUNET.Messages;
using System.Collections.Generic;
2020-12-02 12:42:26 +00:00
using UnityEngine;
2020-12-04 22:14:53 +00:00
namespace QuantumUNET
2020-12-02 12:42:26 +00:00
{
2020-12-03 08:28:05 +00:00
internal class QSBNetworkMessageHandlers
2020-12-02 12:42:26 +00:00
{
2020-12-18 20:32:16 +00:00
private readonly Dictionary<short, QSBNetworkMessageDelegate> _msgHandlers = new Dictionary<short, QSBNetworkMessageDelegate>();
2020-12-03 11:56:32 +00:00
2020-12-02 12:42:26 +00:00
internal void RegisterHandlerSafe(short msgType, QSBNetworkMessageDelegate handler)
{
if (handler == null)
{
Debug.LogError("RegisterHandlerSafe id:" + msgType + " handler is null");
}
else
{
Debug.Log(string.Concat(new object[]
{
"RegisterHandlerSafe id:",
msgType,
" handler:",
handler.GetMethodName()
}));
2020-12-03 11:56:32 +00:00
if (!_msgHandlers.ContainsKey(msgType))
2020-12-02 12:42:26 +00:00
{
2020-12-03 11:56:32 +00:00
_msgHandlers.Add(msgType, handler);
2020-12-02 12:42:26 +00:00
}
}
}
public void RegisterHandler(short msgType, QSBNetworkMessageDelegate handler)
{
if (handler == null)
{
Debug.LogError("RegisterHandler id:" + msgType + " handler is null");
}
else if (msgType <= 31)
{
Debug.LogError("RegisterHandler: Cannot replace system message handler " + msgType);
}
else
{
2020-12-03 11:56:32 +00:00
if (_msgHandlers.ContainsKey(msgType))
2020-12-02 12:42:26 +00:00
{
Debug.Log("RegisterHandler replacing " + msgType);
2020-12-03 11:56:32 +00:00
_msgHandlers.Remove(msgType);
2020-12-02 12:42:26 +00:00
}
Debug.Log(string.Concat(new object[]
{
"RegisterHandler id:",
msgType,
" handler:",
handler.GetMethodName()
}));
2020-12-03 11:56:32 +00:00
_msgHandlers.Add(msgType, handler);
2020-12-02 12:42:26 +00:00
}
}
2020-12-03 11:56:32 +00:00
public void UnregisterHandler(short msgType) => _msgHandlers.Remove(msgType);
2020-12-02 12:42:26 +00:00
internal QSBNetworkMessageDelegate GetHandler(short msgType)
{
QSBNetworkMessageDelegate result;
2020-12-03 11:56:32 +00:00
if (_msgHandlers.ContainsKey(msgType))
2020-12-02 12:42:26 +00:00
{
2020-12-03 11:56:32 +00:00
result = _msgHandlers[msgType];
2020-12-02 12:42:26 +00:00
}
else
{
result = null;
}
return result;
}
2020-12-03 11:56:32 +00:00
internal Dictionary<short, QSBNetworkMessageDelegate> GetHandlers() => _msgHandlers;
2020-12-02 12:42:26 +00:00
2020-12-03 11:56:32 +00:00
internal void ClearMessageHandlers() => _msgHandlers.Clear();
2020-12-02 12:42:26 +00:00
}
2020-12-03 08:28:05 +00:00
}