示例#1
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());
                }
        }
示例#2
0
        private static async void Emit(OutboundMessage message)
        {
            if (message.Event != CommunicationEvents.LogMirror)
            {
                Logger.Trace(message.Payloads.Count > 0
                                ? $"Emit: \"{message.Event}\" with {message.Payloads.Count} payload{(message.Payloads.Count > 1 ? "s" : string.Empty)}:{Environment.NewLine}\t{string.Join($"{Environment.NewLine}\t", message.Payloads)}"
                                : $"Emit: \"{message.Event}\" with no payloads");
            }

            // Marshall back to the main thread in order to use a native call.
            await BaseScript.Delay(0);

            BaseScript.TriggerServerEvent(message.Event, message.Pack());
        }
示例#3
0
        private static async Task <InboundMessage> InternalRequest(string @event, params object[] payloads)
        {
            var tcs = new TaskCompletionSource <InboundMessage>();

            var callback = new Action <byte[]>(data =>
            {
                var message = InboundMessage.From(data);

                Logger.Trace(message.Payloads.Count > 0
                                        ? $"Request Received: \"{message.Event}\" with {message.Payloads.Count} payload{(message.Payloads.Count > 1 ? "s" : string.Empty)}:{Environment.NewLine}\t{string.Join($"{Environment.NewLine}\t", message.Payloads)}"
                                        : $"Request Received: \"{message.Event}\" with no payloads");

                tcs.SetResult(message);
            });


            var msg = new OutboundMessage
            {
                Id       = Guid.NewGuid(),
                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;
            }
        }