Пример #1
0
        public ActorMessageEnvelope <T> ConvertTo <T>()
        {
            var envelope = new ActorMessageEnvelope <T>();

            envelope.CopyFrom(this);
            return(envelope);
        }
Пример #2
0
        public void OnSyncMessage <P>(ActorIdentity remoteActor, 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) { }
            }
        }
Пример #3
0
        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);
        }
Пример #4
0
 public static void HandledBy <T>(this ActorMessageEnvelope envelope, object handlerFrom, Type messageType, IActorMessageDecoder decoder, T state, Func <string, string> getHandlerName) where T : class
 {
     HandledBy <T>(envelope, handlerFrom, messageType, decoder, state,
                   (object o) =>
     {
         return(o.GetType().GetMethod(getHandlerName(envelope.MessageType), BindingFlags.NonPublic | BindingFlags.Instance));
     });
 }
Пример #5
0
        public void BeginBroadcast <T>(IEnumerable <string> remoteActorTypes, T message)
        {
            var envelope = new ActorMessageEnvelope <T>()
            {
                Message = message,
            };

            this.BeginBroadcast(remoteActorTypes, envelope);
        }
Пример #6
0
        public void BeginBroadcast <T>(string remoteActorType, T message)
        {
            var envelope = new ActorMessageEnvelope <T>()
            {
                Message = message,
            };

            this.BeginBroadcast <T>(remoteActorType, envelope);
        }
Пример #7
0
        public void BeginReply <T>(string channelIdentifier, T message)
        {
            var envelope = new ActorMessageEnvelope <T>()
            {
                Message = message,
            };

            this.BeginReply <T>(channelIdentifier, envelope);
        }
Пример #8
0
        public void Send <T>(string remoteActorType, T message)
        {
            var envelope = new ActorMessageEnvelope <T>()
            {
                Message = message,
            };

            this.Send <T>(remoteActorType, envelope);
        }
Пример #9
0
        public void BeginSend <T>(ActorIdentity remoteActor, T message)
        {
            var envelope = new ActorMessageEnvelope <T>()
            {
                Message = message,
            };

            this.BeginSend <T>(remoteActor, envelope);
        }
Пример #10
0
        public P Send <R, P>(string remoteActorType, R request, TimeSpan timeout)
        {
            var envelope = new ActorMessageEnvelope <R>()
            {
                Message = request,
            };

            return(this.Send <R, P>(remoteActorType, envelope, timeout).Message);
        }
Пример #11
0
        public void CopyFrom <T>(ActorMessageEnvelope <T> source)
        {
            this.MessageID   = source.MessageID;
            this.MessageTime = source.MessageTime;

            this.CorrelationID   = source.CorrelationID;
            this.CorrelationTime = source.CorrelationTime;

            this.MessageType = source.MessageType;
        }
Пример #12
0
        public void CopyFrom(ActorMessageEnvelope source)
        {
            this.MessageID   = source.MessageID;
            this.MessageTime = source.MessageTime;

            this.CorrelationID   = source.CorrelationID;
            this.CorrelationTime = source.CorrelationTime;

            this.SourceEndpoint = source.SourceEndpoint == null ? null : source.SourceEndpoint.Clone();
            this.TargetEndpoint = source.TargetEndpoint == null ? null : source.TargetEndpoint.Clone();

            this.MessageType = source.MessageType;
        }
Пример #13
0
        private void OnInvokeMethodRequest(ActorSender sender, ActorMessageEnvelope <InvokeMethodRequest> request)
        {
            request.Message.Deserialize(_methodFixture.ArgumentDecoder);
            var message = InvokeMethod(request.Message);

            message.Serialize(_methodFixture.ArgumentEncoder);

            var response = new ActorMessageEnvelope <InvokeMethodResponse>()
            {
                CorrelationID   = request.MessageID,
                CorrelationTime = request.MessageTime,
                Message         = message,
            };

            this.BeginReply(sender.ChannelIdentifier, response);
        }
Пример #14
0
 protected override void DoHandleMessage(ActorIdentity remoteActor, ActorMessageEnvelope envelope)
 {
     if (GetAdmissibleMessageHandleStrategy(envelope.MessageType).IsRequestResponseModel)
     {
         envelope.HandledBy(this.Actor, GetAdmissibleMessageType(envelope.MessageType), this.Actor.Decoder, remoteActor,
                            (object o) =>
         {
             return(o
                    .GetType()
                    .GetMethod("OnSyncMessage")
                    .MakeGenericMethod(GetAdmissibleMessageType(envelope.MessageType)));
         });
     }
     else
     {
         base.DoHandleMessage(remoteActor, envelope);
     }
 }
Пример #15
0
 protected override void DoHandleMessage(ActorSender sender, ActorMessageEnvelope envelope)
 {
     if (GetAdmissibleMessageHandleStrategy(envelope.MessageType).IsOneWay)
     {
         base.DoHandleMessage(sender, envelope);
     }
     else
     {
         envelope.HandledBy(this.Actor, GetAdmissibleMessageType(envelope.MessageType), this.Actor.Decoder, sender,
                            (object o) =>
         {
             return(o
                    .GetType()
                    .GetMethod("OnSyncMessage")
                    .MakeGenericMethod(GetAdmissibleMessageType(envelope.MessageType)));
         });
     }
 }
Пример #16
0
        public static void HandledBy <T>(this ActorMessageEnvelope envelope, object handlerFrom, Type messageType, IActorMessageDecoder decoder, T state, Func <object, MethodInfo> getHandlerMethod) where T : class
        {
            var instantiateMethod = typeof(ActorMessageEnvelopeExtensions)
                                    .GetMethod("Instantiate", new Type[] { typeof(ActorMessageEnvelope), typeof(IActorMessageDecoder) })
                                    .MakeGenericMethod(messageType);
            var instantiatedEnvelope = instantiateMethod.Invoke(null, new object[] { envelope, decoder });

            try
            {
                var messageHandlerMethod = getHandlerMethod(handlerFrom);
                messageHandlerMethod.Invoke(handlerFrom, new object[] { state, instantiatedEnvelope });
            }
            catch (Exception ex)
            {
                _log.ErrorFormat("HandledBy, MessageType[{0}], ErrorMessage[{1}].", messageType.Name, ex);
                throw;
            }
        }
Пример #17
0
 protected override void DoHandleMessage(ActorIdentity remoteActor, ActorMessageEnvelope envelope)
 {
     if (IsRateLimited)
     {
         _rateLimiter.Wait();
         try
         {
             base.DoHandleMessage(remoteActor, envelope);
         }
         finally
         {
             _rateLimiter.Release();
         }
     }
     else
     {
         base.DoHandleMessage(remoteActor, envelope);
     }
 }
Пример #18
0
 public void HandleMessage(ActorIdentity remoteActor, ActorMessageEnvelope envelope)
 {
     if (!GetAdmissibleMessageHandleStrategy(envelope.MessageType).IsHandledInSeparateThread)
     {
         DoHandleMessage(remoteActor, envelope);
     }
     else
     {
         Task.Run(() =>
         {
             try
             {
                 DoHandleMessage(remoteActor, envelope);
             }
             catch (Exception ex)
             {
                 _log.Error(ex.Message, ex);
             }
         });
     }
 }
Пример #19
0
 public virtual void HandleMessage(ActorSender sender, ActorMessageEnvelope envelope)
 {
     if (GetAdmissibleMessageHandleStrategy(envelope.MessageType).IsAsyncPattern)
     {
         Task.Factory.StartNew(() =>
         {
             try
             {
                 DoHandleMessage(sender, envelope);
             }
             catch (Exception ex)
             {
                 _log.Error(ex.Message, ex);
             }
         },
                               TaskCreationOptions.PreferFairness);
     }
     else
     {
         DoHandleMessage(sender, envelope);
     }
 }
Пример #20
0
 public void HandleMessage(ActorIdentity remoteActor, ActorMessageEnvelope envelope)
 {
     if (!GetAdmissibleMessageHandleStrategy(envelope.MessageType).IsHandledInSeparateThread)
     {
         DoHandleMessage(remoteActor, envelope);
     }
     else
     {
         Task.Factory.StartNew(() =>
         {
             try
             {
                 DoHandleMessage(remoteActor, envelope);
             }
             catch (Exception ex)
             {
                 _log.Error(ex.Message, ex);
             }
         },
                               TaskCreationOptions.PreferFairness);
     }
 }
Пример #21
0
 public void BeginSend <T>(ActorIdentity remoteActor, ActorMessageEnvelope <T> message)
 {
     BeginSend(remoteActor, message.ToBytes(this.Encoder));
 }
Пример #22
0
 protected virtual void DoHandleMessage(ActorSender sender, ActorMessageEnvelope envelope)
 {
     envelope.HandledBy(this, GetAdmissibleMessageType(envelope.MessageType), this.Actor.Decoder, sender);
 }
Пример #23
0
 public virtual bool CanHandleMessage(ActorMessageEnvelope envelope)
 {
     return(_admissibleMessages.ContainsKey(envelope.MessageType));
 }
Пример #24
0
 public static void HandledBy <T>(this ActorMessageEnvelope envelope, object handlerFrom, Type messageType, IActorMessageDecoder decoder, T state) where T : class
 {
     HandledBy <T>(envelope, handlerFrom, messageType, decoder, state, (string s) => { return(@"On" + s); });
 }
Пример #25
0
 public static void HandledBy(this ActorMessageEnvelope envelope, object handlerFrom, Type messageType, IActorMessageDecoder decoder)
 {
     HandledBy(envelope, handlerFrom, messageType, decoder, (string s) => { return(@"On" + s); });
 }
Пример #26
0
 public static byte[] ToBytes <T>(this ActorMessageEnvelope <T> envelope, IActorMessageEncoder encoder)
 {
     return(ToBytes(Marshal(envelope, encoder), encoder));
 }
Пример #27
0
 public static byte[] ToBytes(this ActorMessageEnvelope envelope, IActorMessageEncoder encoder)
 {
     return(encoder.EncodeMessage(envelope));
 }
Пример #28
0
 public void Send <T>(string remoteActorType, ActorMessageEnvelope <T> message)
 {
     BeginSend(remoteActorType, message.ToBytes(this.Encoder));
 }
Пример #29
0
 public IAsyncResult BeginSend <T>(string remoteActorType, string remoteActorName, ActorMessageEnvelope <T> message, AsyncCallback callback, object state)
 {
     return(BeginSend(remoteActorType, remoteActorName, message.ToBytes(this.Encoder), callback, state));
 }
Пример #30
0
 public IAsyncResult BeginSend <T>(ActorIdentity remoteActor, ActorMessageEnvelope <T> message, AsyncCallback callback, object state)
 {
     return(BeginSend(remoteActor, message.ToBytes(this.Encoder), callback, state));
 }