2020-12-14 16:24:52 +00:00
|
|
|
|
using QSB.Events;
|
2021-02-06 22:03:10 +00:00
|
|
|
|
using QSB.Utility;
|
2020-12-04 22:15:41 +00:00
|
|
|
|
using QuantumUNET;
|
2020-12-11 23:13:13 +00:00
|
|
|
|
using QuantumUNET.Components;
|
2020-12-07 21:19:16 +00:00
|
|
|
|
using QuantumUNET.Messages;
|
2020-08-22 18:08:25 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
2020-02-21 23:36:07 +01:00
|
|
|
|
|
2020-12-14 16:28:03 +00:00
|
|
|
|
namespace QSB.Messaging
|
2020-02-21 23:36:07 +01:00
|
|
|
|
{
|
2020-12-23 12:58:45 +00:00
|
|
|
|
public class MessageHandler<T> where T : QMessageBase, new()
|
2020-12-02 21:29:53 +00:00
|
|
|
|
{
|
|
|
|
|
public event Action<T> OnClientReceiveMessage;
|
|
|
|
|
public event Action<T> OnServerReceiveMessage;
|
2020-02-21 23:36:07 +01:00
|
|
|
|
|
2020-12-11 22:42:21 +00:00
|
|
|
|
private readonly short _eventType;
|
2020-08-09 11:11:45 +02:00
|
|
|
|
|
2020-12-02 21:29:53 +00:00
|
|
|
|
public MessageHandler(EventType eventType)
|
|
|
|
|
{
|
2020-12-23 12:58:45 +00:00
|
|
|
|
_eventType = (short)(eventType + QMsgType.Highest + 1);
|
2020-12-02 21:29:53 +00:00
|
|
|
|
if (QSBNetworkManager.Instance.IsReady)
|
|
|
|
|
{
|
|
|
|
|
Init();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
QSBNetworkManager.Instance.OnNetworkManagerReady += Init;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-13 20:44:32 +01:00
|
|
|
|
|
2020-12-02 21:29:53 +00:00
|
|
|
|
private void Init()
|
|
|
|
|
{
|
2020-12-23 12:58:45 +00:00
|
|
|
|
if (QNetworkServer.handlers.Keys.Contains(_eventType))
|
2020-12-02 21:29:53 +00:00
|
|
|
|
{
|
2020-12-23 12:58:45 +00:00
|
|
|
|
QNetworkServer.handlers.Remove(_eventType);
|
|
|
|
|
QNetworkManager.singleton.client.handlers.Remove(_eventType);
|
2020-12-02 21:29:53 +00:00
|
|
|
|
}
|
2021-06-18 22:38:32 +01:00
|
|
|
|
|
2020-12-23 12:58:45 +00:00
|
|
|
|
QNetworkServer.RegisterHandler(_eventType, OnServerReceiveMessageHandler);
|
|
|
|
|
QNetworkManager.singleton.client.RegisterHandler(_eventType, OnClientReceiveMessageHandler);
|
2020-12-02 21:29:53 +00:00
|
|
|
|
}
|
2020-02-21 23:36:07 +01:00
|
|
|
|
|
2020-12-02 21:29:53 +00:00
|
|
|
|
public void SendToAll(T message)
|
|
|
|
|
{
|
|
|
|
|
if (!QSBNetworkManager.Instance.IsReady)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-06-18 22:38:32 +01:00
|
|
|
|
|
2020-12-23 12:58:45 +00:00
|
|
|
|
QNetworkServer.SendToAll(_eventType, message);
|
2020-12-02 21:29:53 +00:00
|
|
|
|
}
|
2020-02-21 23:36:07 +01:00
|
|
|
|
|
2020-12-02 21:29:53 +00:00
|
|
|
|
public void SendToServer(T message)
|
|
|
|
|
{
|
|
|
|
|
if (!QSBNetworkManager.Instance.IsReady)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-06-18 22:38:32 +01:00
|
|
|
|
|
2020-12-23 12:58:45 +00:00
|
|
|
|
QNetworkManager.singleton.client.Send(_eventType, message);
|
2020-12-02 21:29:53 +00:00
|
|
|
|
}
|
2020-02-21 23:36:07 +01:00
|
|
|
|
|
2020-12-23 12:58:45 +00:00
|
|
|
|
private void OnClientReceiveMessageHandler(QNetworkMessage netMsg)
|
2020-12-02 21:29:53 +00:00
|
|
|
|
{
|
|
|
|
|
var message = netMsg.ReadMessage<T>();
|
2021-02-06 22:03:10 +00:00
|
|
|
|
OnClientReceiveMessage?.SafeInvoke(message);
|
2020-12-02 21:29:53 +00:00
|
|
|
|
}
|
2020-02-21 23:36:07 +01:00
|
|
|
|
|
2020-12-23 12:58:45 +00:00
|
|
|
|
private void OnServerReceiveMessageHandler(QNetworkMessage netMsg)
|
2020-12-02 21:29:53 +00:00
|
|
|
|
{
|
|
|
|
|
var message = netMsg.ReadMessage<T>();
|
2021-02-06 22:03:10 +00:00
|
|
|
|
OnServerReceiveMessage?.SafeInvoke(message);
|
2020-12-02 21:29:53 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-03 08:28:05 +00:00
|
|
|
|
}
|