Ejemplo n.º 1
0
        public ActorMessageEnvelope <T> ConvertTo <T>()
        {
            var envelope = new ActorMessageEnvelope <T>();

            envelope.CopyFrom(this);
            return(envelope);
        }
Ejemplo n.º 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) { }
            }
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 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));
     });
 }
Ejemplo n.º 5
0
        public void BeginBroadcast <T>(IEnumerable <string> remoteActorTypes, T message)
        {
            var envelope = new ActorMessageEnvelope <T>()
            {
                Message = message,
            };

            this.BeginBroadcast(remoteActorTypes, envelope);
        }
Ejemplo n.º 6
0
        public void BeginBroadcast <T>(string remoteActorType, T message)
        {
            var envelope = new ActorMessageEnvelope <T>()
            {
                Message = message,
            };

            this.BeginBroadcast <T>(remoteActorType, envelope);
        }
Ejemplo n.º 7
0
        public void BeginReply <T>(string channelIdentifier, T message)
        {
            var envelope = new ActorMessageEnvelope <T>()
            {
                Message = message,
            };

            this.BeginReply <T>(channelIdentifier, envelope);
        }
Ejemplo n.º 8
0
        public void Send <T>(string remoteActorType, T message)
        {
            var envelope = new ActorMessageEnvelope <T>()
            {
                Message = message,
            };

            this.Send <T>(remoteActorType, envelope);
        }
Ejemplo n.º 9
0
        public void BeginSend <T>(ActorIdentity remoteActor, T message)
        {
            var envelope = new ActorMessageEnvelope <T>()
            {
                Message = message,
            };

            this.BeginSend <T>(remoteActor, envelope);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 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;
        }
Ejemplo n.º 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;
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 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);
     }
 }
Ejemplo n.º 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)));
         });
     }
 }
Ejemplo n.º 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;
            }
        }
Ejemplo n.º 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);
     }
 }
Ejemplo n.º 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);
             }
         });
     }
 }
Ejemplo n.º 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);
     }
 }
Ejemplo n.º 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);
     }
 }
Ejemplo n.º 21
0
 public void BeginSend <T>(ActorIdentity remoteActor, ActorMessageEnvelope <T> message)
 {
     BeginSend(remoteActor, message.ToBytes(this.Encoder));
 }
Ejemplo n.º 22
0
 protected virtual void DoHandleMessage(ActorSender sender, ActorMessageEnvelope envelope)
 {
     envelope.HandledBy(this, GetAdmissibleMessageType(envelope.MessageType), this.Actor.Decoder, sender);
 }
Ejemplo n.º 23
0
 public virtual bool CanHandleMessage(ActorMessageEnvelope envelope)
 {
     return(_admissibleMessages.ContainsKey(envelope.MessageType));
 }
Ejemplo n.º 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); });
 }
Ejemplo n.º 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); });
 }
Ejemplo n.º 26
0
 public static byte[] ToBytes <T>(this ActorMessageEnvelope <T> envelope, IActorMessageEncoder encoder)
 {
     return(ToBytes(Marshal(envelope, encoder), encoder));
 }
Ejemplo n.º 27
0
 public static byte[] ToBytes(this ActorMessageEnvelope envelope, IActorMessageEncoder encoder)
 {
     return(encoder.EncodeMessage(envelope));
 }
Ejemplo n.º 28
0
 public void Send <T>(string remoteActorType, ActorMessageEnvelope <T> message)
 {
     BeginSend(remoteActorType, message.ToBytes(this.Encoder));
 }
Ejemplo n.º 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));
 }
Ejemplo n.º 30
0
 public IAsyncResult BeginSend <T>(ActorIdentity remoteActor, ActorMessageEnvelope <T> message, AsyncCallback callback, object state)
 {
     return(BeginSend(remoteActor, message.ToBytes(this.Encoder), callback, state));
 }