private async Task <InboundMessage> MakeRequest(params object[] payloads) { var tcs = new TaskCompletionSource <InboundMessage>(); var callback = new Action <byte[]>(data => { var message = InboundMessage.From(data); LogReceived(message); tcs.SetResult(message); }); try { this.handler.Attach(this.@event, callback); Trigger(payloads); return(await tcs.Task); } finally { this.handler.Detach(this.@event, callback); } }
private void Attach(Delegate callback, Func <InboundMessage, IEnumerable <object> > func) { this.handler.Attach(this.@event, new Action <byte[]>(data => { var message = InboundMessage.From(data); if (!message.Event.StartsWith("nfive:log:")) { LogReceived(message); } var args = new List <object> { new RpcEvent(message.Event) }; args.AddRange(func(message)); callback.DynamicInvoke(args.ToArray()); })); }
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()); }); }
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; } }