private static async Task <InboundMessage> InternalRequest(string @event, IClient target, params object[] payloads) { var tcs = new TaskCompletionSource <InboundMessage>(); var callback = new Action <byte[]>(data => { var message = InboundMessage.From(data); _logger.Trace($"Request Received: {PrintInboundMessage(message)}"); tcs.SetResult(message); }); var msg = new OutboundMessage { Id = Guid.NewGuid(), Target = target, Event = @event, Payloads = payloads.Select(p => Serializer.Serialize(p)).ToList() }; try { _events[$"{msg.Id}:{@event}"] += callback; Emit(msg); return(await tcs.Task); } finally { _events[$"{msg.Id}:{@event}"] -= callback; } }
public static byte[] Pack(OutboundMessage message) { var json = Serialize(message); using (var compressedStream = new ZlibStream(new MemoryStream(json, false), CompressionMode.Compress, CompressionLevel.Default, false)) using (var outputStream = new MemoryStream()) { compressedStream.CopyTo(outputStream); return(outputStream.ToArray()); } }
private static string PrintOutboundMessage(OutboundMessage message) { var str = $"\"{message.Event}\" with "; if (message.Target != null) { str += $"to {message.Target.Handle} "; } if (message.Payloads.Count < 1) { return(str + "no payloads"); } return(str + $"payload:{Environment.NewLine}\t{string.Join($"{Environment.NewLine}\t", message.Payloads)}"); }
private static async void Emit(OutboundMessage message) { _logger.Trace($"Fire: \"{PrintOutboundMessage(message)}\""); // Marshall back to the main thread in order to use a native call. await BaseScript.Delay(0); if (message.Target != null) { _logger.Trace($"TriggerClientEvent: Using PlayerList with {(_players.Count() > 1 ? "players" : "player")}"); BaseScript.TriggerClientEvent(_players[message.Target.Handle], message.Event, message.Pack()); } else { _logger.Trace("TriggerClientEvent: All clients"); BaseScript.TriggerClientEvent(message.Event, message.Pack()); } }