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.

96 lines
3.4KB

  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 override IEntitiesDB entitiesDB { get; set; }
  50. public override IEntityFactory entityFactory { get; set; }
  51. public ServerMessagingEngine(): base()
  52. {
  53. MainServer.Server.Instance.Connected += (_, __) => { MessageSender().Run(); };
  54. }
  55. public IEnumerator MessageSender()
  56. {
  57. while (!_isRunning)
  58. {
  59. yield return null;
  60. }
  61. while (_isRunning)
  62. {
  63. while (_messageQueue.Count != 0)
  64. {
  65. MessageQueueItem item = _messageQueue.Dequeue();
  66. API.Utility.Logging.MetaLog($"Sending message with id {item.msg.Id}");
  67. _sendMessage(item.code, ref item.msg, item.playerId);
  68. }
  69. yield return null;
  70. }
  71. }
  72. public void OnFrameworkDestroyed()
  73. {
  74. _isRunning = false;
  75. }
  76. public void OnFrameworkInitialized()
  77. {
  78. _isRunning = true;
  79. }
  80. }
  81. }