public OpenLinkAsyncResult(Listener listener, AmqpLink link, AsyncCallback callback, object state) : base(callback, state) { this.Link = link; string node = link.IsReceiver ? ((Target)link.Settings.Target).Address.ToString() : ((Source)link.Settings.Source).Address.ToString(); LinkAsyncResult result = null; if (listener.TryMatchLink(node, false, link.IsReceiver, (r) => { return(this); }, out result)) { result.Signal(link); this.Complete(true); } }
// Find a matching client side link; otherwise wait AmqpLink AcceptLink(string node, bool isReceiver, Action <AmqpMessage> messageListener) { LinkAsyncResult result = null; if (this.TryMatchLink(node, true, isReceiver, (r) => { return(new AcceptLinkAsyncResult(r, messageListener, null, null)); }, out result)) { if (isReceiver) { ReceivingAmqpLink link = (ReceivingAmqpLink)result.Link; link.RegisterMessageListener(messageListener); } result.Signal(null); return(result.Link); } return(AcceptLinkAsyncResult.End(result)); }
bool TryMatchLink(string node, bool fromClient, bool isReceiver, Func<bool, LinkAsyncResult> requestCreator, out LinkAsyncResult result) { result = null; bool matched = false; lock (this.syncRoot) { List<LinkAsyncResult> requests; if (!this.nodes.TryGetValue(node, out requests)) { requests = new List<LinkAsyncResult>(); this.nodes.Add(node, requests); } for (int i = 0; i < requests.Count; ++i) { result = requests[i]; if (result.Match(fromClient, isReceiver)) { requests.RemoveAt(i); matched = true; break; } } if (!matched) { result = requestCreator(isReceiver); requests.Add(result); } } return matched; }
bool TryMatchLink(string node, bool fromClient, bool isReceiver, Func <bool, LinkAsyncResult> requestCreator, out LinkAsyncResult result) { result = null; bool matched = false; lock (this.syncRoot) { List <LinkAsyncResult> requests; if (!this.nodes.TryGetValue(node, out requests)) { requests = new List <LinkAsyncResult>(); this.nodes.Add(node, requests); } for (int i = 0; i < requests.Count; ++i) { result = requests[i]; if (result.Match(fromClient, isReceiver)) { requests.RemoveAt(i); matched = true; break; } } if (!matched) { result = requestCreator(isReceiver); requests.Add(result); } } return(matched); }