public static void DoProcessing() { while (!_closing) { try { byte[] incoming; while (_incoming.TryDequeue(out incoming)) { MessageBase m; try { var o = MyCompression.Decompress(incoming); m = MyAPIGateway.Utilities.SerializeFromBinary <MessageBase>(o); } catch (Exception ex) { MyLog.Default.WriteLineAndConsole($"TORCH MOD: Failed to deserialize message! {ex}"); continue; } if (MyAPIGateway.Multiplayer.IsServer) { m.ProcessServer(); } else { m.ProcessClient(); } } if (!_outgoing.IsEmpty) { List <MessageBase> tosend = new List <MessageBase>(_outgoing.Count); MessageBase outMessage; while (_outgoing.TryDequeue(out outMessage)) { var b = MyAPIGateway.Utilities.SerializeToBinary(outMessage); outMessage.CompressedData = MyCompression.Compress(b); tosend.Add(outMessage); } MyAPIGateway.Utilities.InvokeOnGameThread(() => { MyAPIGateway.Players.GetPlayers(_playerCache); foreach (var outgoing in tosend) { switch (outgoing.TargetType) { case MessageTarget.Single: MyAPIGateway.Multiplayer.SendMessageTo(NET_ID, outgoing.CompressedData, outgoing.Target); break; case MessageTarget.Server: MyAPIGateway.Multiplayer.SendMessageToServer(NET_ID, outgoing.CompressedData); break; case MessageTarget.AllClients: foreach (var p in _playerCache) { if (p.SteamUserId == MyAPIGateway.Multiplayer.MyId) { continue; } MyAPIGateway.Multiplayer.SendMessageTo(NET_ID, outgoing.CompressedData, p.SteamUserId); } break; case MessageTarget.AllExcept: foreach (var p in _playerCache) { if (p.SteamUserId == MyAPIGateway.Multiplayer.MyId || outgoing.Ignore.Contains(p.SteamUserId)) { continue; } MyAPIGateway.Multiplayer.SendMessageTo(NET_ID, outgoing.CompressedData, p.SteamUserId); } break; default: throw new Exception(); } } _playerCache.Clear(); }); } AcquireLock(); } catch (Exception ex) { MyLog.Default.WriteLineAndConsole($"TORCH MOD: Exception occurred in communication thread! {ex}"); } } MyLog.Default.WriteLineAndConsole("TORCH MOD: COMMUNICATION THREAD: EXIT SIGNAL RECIEVED!"); //exit signal received. Clean everything and GTFO _outgoing = null; _incoming = null; _playerCache = null; _lock = null; }
/// <summary> /// Unpacks commands and handles arguments /// </summary> /// <param name="msg">Data chunck recived from the network</param> private void HandleIncomingPacket(byte[] msg) { try { Command cmd = MyAPIGateway.Utilities.SerializeFromBinary <Command>(msg); if (LogNetworkTraffic) { MyLog.Default.Info($"[NetworkAPI] ----- TRANSMISSION RECIEVED -----"); MyLog.Default.Info($"[NetworkAPI] Type: {((cmd.IsProperty) ? "Property" : $"Command ID: {cmd.CommandString}")}, {(cmd.IsCompressed ? "Compressed, " : "")}From: {cmd.SteamId} "); } if (cmd.IsCompressed) { cmd.Data = MyCompression.Decompress(cmd.Data); cmd.IsCompressed = false; } if (cmd.IsProperty) { NetSync <object> .RouteMessage(MyAPIGateway.Utilities.SerializeFromBinary <SyncData>(cmd.Data), cmd.SteamId, cmd.Timestamp); } else { if (!string.IsNullOrWhiteSpace(cmd.Message)) { if (!MyAPIGateway.Utilities.IsDedicated) { if (MyAPIGateway.Session != null) { MyAPIGateway.Utilities.ShowMessage(ModName, cmd.Message); } } if (MyAPIGateway.Multiplayer.IsServer) { SendCommand(null, cmd.Message); } } if (cmd.CommandString != null) { OnCommandRecived?.Invoke(cmd.SteamId, cmd.CommandString, cmd.Data, new DateTime(cmd.Timestamp)); string command = cmd.CommandString.Split(' ')[0]; if (NetworkCommands.ContainsKey(command)) { NetworkCommands[command]?.Invoke(cmd.SteamId, cmd.CommandString, cmd.Data, new DateTime(cmd.Timestamp)); } } } if (LogNetworkTraffic) { MyLog.Default.Info($"[NetworkAPI] ----- END -----"); } } catch (Exception e) { MyLog.Default.Error($"[NetworkAPI] Failure in message processing:\n{e.ToString()}"); } }
public static void DoProcessing() { while (!_closing) { try { MessageBase m; try { m = _processing.Take(); } catch { continue; } MyLog.Default.WriteLineAndConsole($"Processing message: {m.GetType().Name}"); if (m is IncomingMessage) //process incoming messages { MessageBase i; try { var o = MyCompression.Decompress(m.CompressedData); m.CompressedData = null; _messagePool.Return((IncomingMessage)m); i = MyAPIGateway.Utilities.SerializeFromBinary <MessageBase>(o); } catch (Exception ex) { MyLog.Default.WriteLineAndConsole($"TORCH MOD: Failed to deserialize message! {ex}"); continue; } if (TorchModCore.Debug) { MyAPIGateway.Utilities.ShowMessage("Torch", $"Received message of type {i.GetType().Name}"); } if (MyAPIGateway.Multiplayer.IsServer) { i.ProcessServer(); } else { i.ProcessClient(); } } else //process outgoing messages { if (TorchModCore.Debug) { MyAPIGateway.Utilities.ShowMessage("Torch", $"Sending message of type {m.GetType().Name}"); } var b = MyAPIGateway.Utilities.SerializeToBinary(m); m.CompressedData = MyCompression.Compress(b); switch (m.TargetType) { case MessageTarget.Single: MyAPIGateway.Multiplayer.SendMessageTo(NET_ID, m.CompressedData, m.Target); break; case MessageTarget.Server: MyAPIGateway.Multiplayer.SendMessageToServer(NET_ID, m.CompressedData); break; case MessageTarget.AllClients: MyAPIGateway.Players.GetPlayers(_playerCache); foreach (var p in _playerCache) { if (p.SteamUserId == MyAPIGateway.Multiplayer.MyId) { continue; } MyAPIGateway.Multiplayer.SendMessageTo(NET_ID, m.CompressedData, p.SteamUserId); } break; case MessageTarget.AllExcept: MyAPIGateway.Players.GetPlayers(_playerCache); foreach (var p in _playerCache) { if (p.SteamUserId == MyAPIGateway.Multiplayer.MyId || m.Ignore.Contains(p.SteamUserId)) { continue; } MyAPIGateway.Multiplayer.SendMessageTo(NET_ID, m.CompressedData, p.SteamUserId); } break; default: throw new Exception(); } _playerCache.Clear(); } } catch (Exception ex) { MyLog.Default.WriteLineAndConsole($"TORCH MOD: Exception occurred in communication thread! {ex}"); } } MyLog.Default.WriteLineAndConsole("TORCH MOD: INFO: Communication thread shut down successfully! THIS IS NOT AN ERROR"); //exit signal received. Clean everything and GTFO _processing?.Dispose(); _processing = null; _messagePool?.Clean(); _messagePool = null; _playerCache = null; }
public static byte[] Decompress(this byte[] data) { return(MyCompression.Decompress(data)); }
public static void DoProcessing() { while (!_closing) { try { var m = _processing.Take(); MyLog.Default.WriteLineAndConsole($"Processing message: {m.GetType().Name}"); if (m is IncomingMessage) { MessageBase i; try { var o = MyCompression.Decompress(m.CompressedData); m.CompressedData = null; _messagePool.Return((IncomingMessage)m); i = MyAPIGateway.Utilities.SerializeFromBinary <MessageBase>(o); } catch (Exception ex) { MyLog.Default.WriteLineAndConsole($"TORCH MOD: Failed to deserialize message! {ex}"); continue; } if (MyAPIGateway.Multiplayer.IsServer) { i.ProcessServer(); } else { i.ProcessClient(); } } else { var b = MyAPIGateway.Utilities.SerializeToBinary(m); m.CompressedData = MyCompression.Compress(b); MyAPIGateway.Utilities.InvokeOnGameThread(() => { switch (m.TargetType) { case MessageTarget.Single: MyAPIGateway.Multiplayer.SendMessageTo(NET_ID, m.CompressedData, m.Target); break; case MessageTarget.Server: MyAPIGateway.Multiplayer.SendMessageToServer(NET_ID, m.CompressedData); break; case MessageTarget.AllClients: MyAPIGateway.Players.GetPlayers(_playerCache); foreach (var p in _playerCache) { if (p.SteamUserId == MyAPIGateway.Multiplayer.MyId) { continue; } MyAPIGateway.Multiplayer.SendMessageTo(NET_ID, m.CompressedData, p.SteamUserId); } break; case MessageTarget.AllExcept: MyAPIGateway.Players.GetPlayers(_playerCache); foreach (var p in _playerCache) { if (p.SteamUserId == MyAPIGateway.Multiplayer.MyId || m.Ignore.Contains(p.SteamUserId)) { continue; } MyAPIGateway.Multiplayer.SendMessageTo(NET_ID, m.CompressedData, p.SteamUserId); } break; default: throw new Exception(); } _playerCache.Clear(); }); } } catch (Exception ex) { MyLog.Default.WriteLineAndConsole($"TORCH MOD: Exception occurred in communication thread! {ex}"); } } MyLog.Default.WriteLineAndConsole("TORCH MOD: COMMUNICATION THREAD: EXIT SIGNAL RECEIVED!"); //exit signal received. Clean everything and GTFO _processing?.Dispose(); _processing = null; _messagePool?.Clean(); _messagePool = null; _playerCache = null; }