Unofficial CardLife revival project, pronounced like "celery"
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
3.3KB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using GameNetworkLayer.Shared;
  4. using Svelto.Context;
  5. using Svelto.ECS;
  6. namespace CLre_server.API.Synergy
  7. {
  8. class ServerMessagingEngine: Engines.ServerEnginePreBuild, IWaitForFrameworkDestruction, IWaitForFrameworkInitialization
  9. {
  10. private struct MessageQueueItem
  11. {
  12. public SerializedCLreMessage msg;
  13. public int playerId;
  14. public NetworkDispatcherCode code;
  15. }
  16. private Utility.Reflection.INetMsgServerSender_SendMessage<SerializedCLreMessage> _sendMessage;
  17. private Utility.Reflection.INetMsgServerListener_RegisterListener<SerializedCLreMessage> _registerListener;
  18. private Queue<MessageQueueItem> _messageQueue = new Queue<MessageQueueItem>(10);
  19. private bool _isRunning = false;
  20. public override void Ready()
  21. {
  22. //Utility.Logging.MetaLog("Building send message delegate");
  23. _sendMessage =
  24. Utility.Reflection.MethodAsDelegate<Utility.Reflection.INetMsgServerSender_SendMessage<SerializedCLreMessage>>(
  25. "GameNetworkLayer.Server.NetMessageServerSender:SendMessage",
  26. generics: new [] {typeof(SerializedCLreMessage)},
  27. instance: MainGameServer_SetupContainer_Patch.netMessageSender);
  28. //Utility.Logging.MetaLog("Building register listener delegate");
  29. _registerListener =
  30. Utility.Reflection.MethodAsDelegate<Utility.Reflection.INetMsgServerListener_RegisterListener<SerializedCLreMessage>>(
  31. "GameNetworkLayer.Server.NetMessageServerListener:RegisterListener",
  32. generics: new [] {typeof(SerializedCLreMessage)},
  33. instance: MainGameServer_SetupContainer_Patch.netMessageListener);
  34. _registerListener(Message.CLre_MESSAGE_NETCODE, OnMessageReceived);
  35. }
  36. private void OnMessageReceived(int playerId, ref SerializedCLreMessage data)
  37. {
  38. Message.HandleMessageReceive(playerId, ref data);
  39. }
  40. internal void EnqueueMessage(int playerId, ref SerializedCLreMessage msg)
  41. {
  42. _messageQueue.Enqueue(new MessageQueueItem
  43. {
  44. msg = msg,
  45. playerId = playerId,
  46. code = Message.CLre_MESSAGE_NETCODE,
  47. });
  48. }
  49. public ServerMessagingEngine(): base()
  50. {
  51. MainServer.Server.Instance.Connected += (_, __) => { MessageSender().Run(); };
  52. }
  53. public IEnumerator MessageSender()
  54. {
  55. while (!_isRunning)
  56. {
  57. yield return null;
  58. }
  59. while (_isRunning)
  60. {
  61. while (_messageQueue.Count != 0)
  62. {
  63. MessageQueueItem item = _messageQueue.Dequeue();
  64. API.Utility.Logging.MetaLog($"Sending message with id {item.msg.Id}");
  65. _sendMessage(item.code, ref item.msg, item.playerId);
  66. }
  67. yield return null;
  68. }
  69. }
  70. public void OnFrameworkDestroyed()
  71. {
  72. _isRunning = false;
  73. }
  74. public void OnFrameworkInitialized()
  75. {
  76. _isRunning = true;
  77. }
  78. }
  79. }