2020-02-21 22:36:07 +00:00
|
|
|
|
using System;
|
|
|
|
|
using UnityEngine.Networking;
|
|
|
|
|
|
|
|
|
|
namespace QSB.Messaging
|
|
|
|
|
{
|
|
|
|
|
// Extend this to create new message handlers.
|
|
|
|
|
public class MessageHandler<T> where T : QSBMessage, new()
|
|
|
|
|
{
|
|
|
|
|
public event Action<T> OnClientReceiveMessage;
|
|
|
|
|
public event Action<T> OnServerReceiveMessage;
|
|
|
|
|
|
|
|
|
|
public MessageHandler()
|
2020-03-13 19:44:32 +00:00
|
|
|
|
{
|
|
|
|
|
if (QSBNetworkManager.IsReady)
|
|
|
|
|
{
|
|
|
|
|
Init();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
QSBNetworkManager.OnNetworkManagerReady.AddListener(Init);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Init()
|
2020-02-21 22:36:07 +00:00
|
|
|
|
{
|
|
|
|
|
var message = (T)Activator.CreateInstance(typeof(T));
|
|
|
|
|
NetworkServer.RegisterHandler((short)message.MessageType, OnServerReceiveMessageHandler);
|
|
|
|
|
NetworkManager.singleton.client.RegisterHandler((short)message.MessageType, OnClientReceiveMessageHandler);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SendToAll(T message)
|
|
|
|
|
{
|
2020-03-13 19:44:32 +00:00
|
|
|
|
if (!QSBNetworkManager.IsReady)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-02-21 22:36:07 +00:00
|
|
|
|
NetworkServer.SendToAll((short)message.MessageType, message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SendToServer(T message)
|
|
|
|
|
{
|
2020-03-13 19:44:32 +00:00
|
|
|
|
if (!QSBNetworkManager.IsReady)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-02-21 22:36:07 +00:00
|
|
|
|
NetworkManager.singleton.client.Send((short)message.MessageType, message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnClientReceiveMessageHandler(NetworkMessage netMsg)
|
|
|
|
|
{
|
|
|
|
|
var message = netMsg.ReadMessage<T>();
|
|
|
|
|
OnClientReceiveMessage?.Invoke(message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnServerReceiveMessageHandler(NetworkMessage netMsg)
|
|
|
|
|
{
|
|
|
|
|
var message = netMsg.ReadMessage<T>();
|
|
|
|
|
OnServerReceiveMessage?.Invoke(message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|