|
| 1 | +using System; |
| 2 | +using System.Collections.Concurrent; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Linq; |
| 5 | +using System.Text; |
| 6 | +using System.Threading; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using Sandbox.ModAPI; |
| 9 | +using Torch.Mod.Messages; |
| 10 | +using VRage; |
| 11 | +using VRage.Game.ModAPI; |
| 12 | +using VRage.Utils; |
| 13 | +using Task = ParallelTasks.Task; |
| 14 | + |
| 15 | +namespace Torch.Mod |
| 16 | +{ |
| 17 | + public static class ModCommunication |
| 18 | + { |
| 19 | + public const ushort NET_ID = 4352; |
| 20 | + private static bool _closing; |
| 21 | + private static ConcurrentQueue<MessageBase> _outgoing; |
| 22 | + private static ConcurrentQueue<byte[]> _incoming; |
| 23 | + private static List<IMyPlayer> _playerCache; |
| 24 | + private static FastResourceLock _lock; |
| 25 | + private static Task _task; |
| 26 | + |
| 27 | + public static void Register() |
| 28 | + { |
| 29 | + MyLog.Default.WriteLineAndConsole("TORCH MOD: Registering mod communication."); |
| 30 | + _outgoing = new ConcurrentQueue<MessageBase>(); |
| 31 | + _incoming = new ConcurrentQueue<byte[]>(); |
| 32 | + _playerCache = new List<IMyPlayer>(); |
| 33 | + _lock = new FastResourceLock(); |
| 34 | + |
| 35 | + |
| 36 | + MyAPIGateway.Multiplayer.RegisterMessageHandler(NET_ID, MessageHandler); |
| 37 | + //background thread to handle de/compression and processing |
| 38 | + _task = MyAPIGateway.Parallel.StartBackground(DoProcessing); |
| 39 | + MyLog.Default.WriteLineAndConsole("TORCH MOD: Mod communication registered successfully."); |
| 40 | + } |
| 41 | + |
| 42 | + public static void Unregister() |
| 43 | + { |
| 44 | + MyLog.Default.WriteLineAndConsole("TORCH MOD: Unregistering mod communication."); |
| 45 | + MyAPIGateway.Multiplayer.UnregisterMessageHandler(NET_ID, MessageHandler); |
| 46 | + _closing = true; |
| 47 | + ReleaseLock(); |
| 48 | + _task.Wait(); |
| 49 | + } |
| 50 | + |
| 51 | + private static void MessageHandler(byte[] bytes) |
| 52 | + { |
| 53 | + _incoming.Enqueue(bytes); |
| 54 | + ReleaseLock(); |
| 55 | + } |
| 56 | + |
| 57 | + public static void DoProcessing() |
| 58 | + { |
| 59 | + while (!_closing) |
| 60 | + { |
| 61 | + try |
| 62 | + { |
| 63 | + byte[] incoming; |
| 64 | + while (_incoming.TryDequeue(out incoming)) |
| 65 | + { |
| 66 | + MessageBase m; |
| 67 | + try |
| 68 | + { |
| 69 | + var o = MyCompression.Decompress(incoming); |
| 70 | + m = MyAPIGateway.Utilities.SerializeFromBinary<MessageBase>(o); |
| 71 | + } |
| 72 | + catch (Exception ex) |
| 73 | + { |
| 74 | + MyLog.Default.WriteLineAndConsole($"TORCH MOD: Failed to deserialize message! {ex}"); |
| 75 | + continue; |
| 76 | + } |
| 77 | + if (MyAPIGateway.Multiplayer.IsServer) |
| 78 | + m.ProcessServer(); |
| 79 | + else |
| 80 | + m.ProcessClient(); |
| 81 | + } |
| 82 | + |
| 83 | + if (!_outgoing.IsEmpty) |
| 84 | + { |
| 85 | + List<MessageBase> tosend = new List<MessageBase>(_outgoing.Count); |
| 86 | + MessageBase outMessage; |
| 87 | + while (_outgoing.TryDequeue(out outMessage)) |
| 88 | + { |
| 89 | + var b = MyAPIGateway.Utilities.SerializeToBinary(outMessage); |
| 90 | + outMessage.CompressedData = MyCompression.Compress(b); |
| 91 | + tosend.Add(outMessage); |
| 92 | + } |
| 93 | + |
| 94 | + MyAPIGateway.Utilities.InvokeOnGameThread(() => |
| 95 | + { |
| 96 | + MyAPIGateway.Players.GetPlayers(_playerCache); |
| 97 | + foreach (var outgoing in tosend) |
| 98 | + { |
| 99 | + switch (outgoing.TargetType) |
| 100 | + { |
| 101 | + case MessageTarget.Single: |
| 102 | + MyAPIGateway.Multiplayer.SendMessageTo(NET_ID, outgoing.CompressedData, outgoing.Target); |
| 103 | + break; |
| 104 | + case MessageTarget.Server: |
| 105 | + MyAPIGateway.Multiplayer.SendMessageToServer(NET_ID, outgoing.CompressedData); |
| 106 | + break; |
| 107 | + case MessageTarget.AllClients: |
| 108 | + foreach (var p in _playerCache) |
| 109 | + { |
| 110 | + if (p.SteamUserId == MyAPIGateway.Multiplayer.MyId) |
| 111 | + continue; |
| 112 | + MyAPIGateway.Multiplayer.SendMessageTo(NET_ID, outgoing.CompressedData, p.SteamUserId); |
| 113 | + } |
| 114 | + break; |
| 115 | + case MessageTarget.AllExcept: |
| 116 | + foreach (var p in _playerCache) |
| 117 | + { |
| 118 | + if (p.SteamUserId == MyAPIGateway.Multiplayer.MyId || outgoing.Ignore.Contains(p.SteamUserId)) |
| 119 | + continue; |
| 120 | + MyAPIGateway.Multiplayer.SendMessageTo(NET_ID, outgoing.CompressedData, p.SteamUserId); |
| 121 | + } |
| 122 | + break; |
| 123 | + default: |
| 124 | + throw new Exception(); |
| 125 | + } |
| 126 | + } |
| 127 | + _playerCache.Clear(); |
| 128 | + }); |
| 129 | + } |
| 130 | + |
| 131 | + AcquireLock(); |
| 132 | + } |
| 133 | + catch (Exception ex) |
| 134 | + { |
| 135 | + MyLog.Default.WriteLineAndConsole($"TORCH MOD: Exception occurred in communication thread! {ex}"); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + MyLog.Default.WriteLineAndConsole("TORCH MOD: COMMUNICATION THREAD: EXIT SIGNAL RECIEVED!"); |
| 140 | + //exit signal received. Clean everything and GTFO |
| 141 | + _outgoing = null; |
| 142 | + _incoming = null; |
| 143 | + _playerCache = null; |
| 144 | + _lock = null; |
| 145 | + } |
| 146 | + |
| 147 | + public static void SendMessageTo(MessageBase message, ulong target) |
| 148 | + { |
| 149 | + if (!MyAPIGateway.Multiplayer.IsServer) |
| 150 | + throw new Exception("Only server can send targeted messages"); |
| 151 | + message.Target = target; |
| 152 | + message.TargetType = MessageTarget.Single; |
| 153 | + MyLog.Default.WriteLineAndConsole($"Sending message of type {message.GetType().FullName}"); |
| 154 | + _outgoing.Enqueue(message); |
| 155 | + ReleaseLock(); |
| 156 | + } |
| 157 | + |
| 158 | + public static void SendMessageToClients(MessageBase message) |
| 159 | + { |
| 160 | + if (!MyAPIGateway.Multiplayer.IsServer) |
| 161 | + throw new Exception("Only server can send targeted messages"); |
| 162 | + message.TargetType = MessageTarget.AllClients; |
| 163 | + _outgoing.Enqueue(message); |
| 164 | + ReleaseLock(); |
| 165 | + } |
| 166 | + |
| 167 | + public static void SendMessageExcept(MessageBase message, params ulong[] ignoredUsers) |
| 168 | + { |
| 169 | + if (MyAPIGateway.Multiplayer.IsServer) |
| 170 | + throw new Exception("Only server can send targeted messages"); |
| 171 | + message.TargetType = MessageTarget.AllExcept; |
| 172 | + message.Ignore = ignoredUsers; |
| 173 | + _outgoing.Enqueue(message); |
| 174 | + ReleaseLock(); |
| 175 | + } |
| 176 | + |
| 177 | + public static void SendMessageToServer(MessageBase message) |
| 178 | + { |
| 179 | + message.TargetType = MessageTarget.Server; |
| 180 | + _outgoing.Enqueue(message); |
| 181 | + ReleaseLock(); |
| 182 | + } |
| 183 | + |
| 184 | + private static void ReleaseLock() |
| 185 | + { |
| 186 | + while(!_lock.TryAcquireExclusive()) |
| 187 | + _lock.ReleaseExclusive(); |
| 188 | + _lock.ReleaseExclusive(); |
| 189 | + } |
| 190 | + |
| 191 | + private static void AcquireLock() |
| 192 | + { |
| 193 | + ReleaseLock(); |
| 194 | + _lock.AcquireExclusive(); |
| 195 | + _lock.AcquireExclusive(); |
| 196 | + } |
| 197 | + } |
| 198 | +} |
0 commit comments