public void OnSyncMessage <P>(ActorSender sender, ActorMessageEnvelope <P> response) { if (response == null || string.IsNullOrWhiteSpace(response.CorrelationID)) { throw new InvalidOperationException("Invalid or empty message CorrelationID."); } BlockingCallbackHolder callback = null; if (_callbacks.TryRemove(response.CorrelationID, out callback) && callback != null) { var action = callback.Action as Action <ActorMessageEnvelope <P> >; if (action != null) { action.Invoke(response); } try { callback.Waiter.Set(); } catch (ObjectDisposedException) { } } }
public ActorMessageEnvelope <P> SendMessage <R, P>(string remoteActorType, ActorMessageEnvelope <R> request, TimeSpan timeout) { ActorMessageEnvelope <P> response = default(ActorMessageEnvelope <P>); Action <ActorMessageEnvelope <P> > callback = (r) => { response = r; }; try { ManualResetEvent waiter = new ManualResetEvent(false); _callbacks.Add(request.MessageID, new BlockingCallbackHolder(request.MessageID, waiter, callback)); this.BeginSend(remoteActorType, request.ToBytes(this.Encoder)); if (!waiter.WaitOne(timeout)) { _log.ErrorFormat("Timeout when waiting message [{0}] after {1} seconds.", request.MessageType, timeout.TotalSeconds); } waiter.Reset(); waiter.Dispose(); BlockingCallbackHolder throwAway = null; _callbacks.TryRemove(request.MessageID, out throwAway); } catch (Exception ex) { _log.Error(ex.Message, ex); } return(response); }