예제 #1
0
        private static void InternalOnRequest(string @event, Delegate callback, Func <InboundMessage, IEnumerable <object> > func)
        {
            Logger.Trace($"OnRequest: \"{@event}\" attached to \"{callback.Method.DeclaringType?.Name}.{callback.Method.Name}({string.Join(", ", callback.Method.GetParameters().Select(p => p.ParameterType + " " + p.Name))})\"");

            _events[@event] += new Action <byte[]>(data =>
            {
                var message = InboundMessage.From(data);

                Logger.Trace(message.Payloads.Count > 0
                                        ? $"OnRequest 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)}"
                                        : $"OnRequest Received: \"{message.Event}\" with no payloads");

                var args = new List <object>
                {
                    new CommunicationMessage(@event, message.Id, true)
                };

                args.AddRange(func(message));

                callback.DynamicInvoke(args.ToArray());
            });
        }
예제 #2
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;
            }
        }