예제 #1
0
        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;
            }
        }
예제 #2
0
        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());
                }
        }
예제 #3
0
        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)}");
        }
예제 #4
0
        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());
            }
        }