/// <summary> /// Enlist item for execution. /// </summary> protected override void OnExecute(Envelope envelope) { WaitCallback waitCallback = _waitCallback; if (waitCallback != null) { ThreadPool.QueueUserWorkItem(waitCallback, envelope); } }
protected override void OnExecute(Envelope envelope) { // Process the incoming request, we will simply execute in on default thread pool. WaitCallback del = delegate(object state) { base.Client.PerformExecution((Envelope)envelope); }; ThreadPool.QueueUserWorkItem(del, envelope); }
/// <summary> /// Enlist item for execution. /// </summary> public virtual void Execute(Envelope envelope) { ActiveClient client = _client; if (envelope.ExecutionModel == Envelope.ExecutionModelEnum.Direct) { if (client != null) { client.PerformExecution(envelope); } } else { OnExecute(envelope); } }
/// <summary> /// Send an item to a single repicient. /// </summary> /// <returns></returns> public Outcomes Send(ClientId senderId, ClientId receiverId, Envelope envelope, TimeSpan? requestConfirmTimeout, bool showErrorsDiagnostics) { return Send(senderId, new ClientId[] { receiverId }, envelope, requestConfirmTimeout, showErrorsDiagnostics); }
/// <summary> /// Actually supply the item to client. /// </summary> /// <param name="senderId"></param> /// <param name="receiverId"></param> /// <param name="envelope"></param> /// <param name="requestConfirm">In local mode we have receival confirmation, so this value is ignored here (as result is always assured true).</param> /// <returns></returns> protected virtual SendToClientResultEnum DoSendToClient(ClientId senderId, ClientId receiverId, Envelope envelope, TimeSpan? requestConfirmTimeout) { if (receiverId.MessageBus != this) { //// Maybe this is a "lost" id, try to see if it is one of ours. //if (receiverId.MessageBus == null && _guidToIndexHotSwap.ContainsKey(receiverId.Guid)) //{// Yes! // receiverId.MessageBus = this; //} return SendToClientResultEnum.ClientNotFound; } MessageBusClient client = GetLocalClientByIndex(receiverId.LocalMessageBusIndex); if (client == null) { return SendToClientResultEnum.ClientNotFound; } ISerializer serializer = _serializer; if (serializer == null) { return SendToClientResultEnum.Failure; } // Duplicate what (if anything) as according to envelope duplication model. envelope = envelope.Duplicate(serializer); envelope.History.PushStamp(new EnvelopeStamp(PendingStampId, receiverId, senderId)); if (client.Receive(envelope)) { return SendToClientResultEnum.Success; } else { return SendToClientResultEnum.Failure; } }
/// <summary> /// Implementations handle this to do the actual execution. /// </summary> protected abstract void OnExecute(Envelope envelope);
protected void RaiseEnvelopeReceivedEvent(Envelope envelope) { EnvelopeUpdateDelegate envelopeReceivedDelegate = EnvelopeReceivedEvent; if (envelopeReceivedDelegate != null) { envelopeReceivedDelegate(this, envelope); } }
/// <summary> /// Send an item to multiple recipients. /// </summary> protected override Outcomes DoSend(ClientId senderId, IEnumerable<ClientId> receiversIds, Envelope envelope, TimeSpan? requestConfirmTimeout, bool showErrorsDiagnostics) { if (IsDisposed) {// Possible to get disposed while operating here. return Outcomes.SystemFailture; } //if (envelope.Address != null) //{ // SystemMonitor.OperationError("Envelope transport direction not clear."); // return false; //} bool result = true; foreach (ClientId receiverId in receiversIds) { if (DoSendToClient(senderId, receiverId, envelope, requestConfirmTimeout) != SendToClientResultEnum.Success) { result = false; } } return result ? Outcomes.Success : Outcomes.Failure; }
/// <summary> /// Addressed envelope. /// </summary> protected Outcomes Send(Envelope envelope) { if (envelope.Address != null) { EnvelopeStamp stamp = envelope.Address.PopStamp(); if (stamp != null) { return Send(stamp.SenderIndex, stamp.ReceiverIndex, envelope, null, false); } } return Outcomes.Failure; }
internal abstract bool Receive(Envelope envelope);
/// <summary> /// /// </summary> protected virtual void OnPerformExecution(Envelope envelope) { }
/// <summary> /// This will transport the envelope to the server, if it can. /// </summary> protected override Outcomes DoSend(ClientId senderId, IEnumerable<ClientId> receiversIds, Envelope envelope, TimeSpan? requestConfirmTimeout, bool showErrorsDiagnostics) { bool result = true; foreach (ClientId id in receiversIds) { result = result && DoSendToClient(senderId, id, envelope, requestConfirmTimeout) == SendToClientResultEnum.Success; } return result ? Outcomes.Success : Outcomes.Failure; }
/// <summary> /// Send a responce to an envelope received. /// </summary> /// <param name="receivedEnvelope">The enveloped received, we shall respond to it.</param> /// <param name="envelope">The new envelope, we are sending as responce.</param> /// <returns></returns> public Outcomes Respond(Envelope receivedEnvelope, Envelope envelope) { envelope.Address = EnvelopeTransportation.CreateResponseTransport(receivedEnvelope.History); return Send(envelope); }
/// <summary> /// /// </summary> public virtual void PerformExecution(Envelope envelope) { RaiseEnvelopeExecutingEvent(envelope); OnPerformExecution(envelope); RaiseEnvelopeExecutedEvent(envelope); }
/// <summary> /// /// </summary> /// <param name="senderId"></param> /// <param name="receiverId"></param> /// <param name="envelope"></param> /// <param name="requestConfirm">Only valid for remote clients, since all local calls are confirmed or denied by default.</param> /// <returns></returns> protected override SendToClientResultEnum DoSendToClient(ClientId senderId, ClientId receiverId, Envelope envelope, TimeSpan? requestConfirmTimeout) { if (receiverId.IsMessageBusIndexValid && (receiverId.MessageBus == this)) //|| receiverId.MessageBus == null)) // This allows for "lost" ids. {// Receiver seems to be a local item. SendToClientResultEnum result = base.DoSendToClient(senderId, receiverId, envelope, requestConfirmTimeout); if (result != SendToClientResultEnum.ClientNotFound) { return result; } } int clientSocketId = 0; lock (_syncRoot) { if (_remoteClientNetId.TryGetValue(receiverId, out clientSocketId) == false) { return SendToClientResultEnum.ClientNotFound; } } // Send message. EnvelopeMessage message = new EnvelopeMessage() { Envelope = envelope, Receivers = new ClientId[] { receiverId }, Sender = senderId, RequestResponse = false }; if (ToClient(clientSocketId, message, requestConfirmTimeout) == false) { return SendToClientResultEnum.Failure; } else { return SendToClientResultEnum.Success; } }
protected override void OnExecute(Envelope envelope) { ThreadPoolFast pool = ThreadPool; if (pool != null) { pool.QueueFastDelegate(this, _performExecutionDelegate, envelope); // Other invocation options. //ThreadPoolFastEx.TargetInfo targetInfo = new ThreadPoolFastEx.TargetInfo(string.Empty, this, // _singleFastInvokeDelegate, threadPool, envelope); //threadPool.QueueTargetInfo(targetInfo); } }
/// <summary> /// /// </summary> /// <param name="requestConfirm">Only valid for remote clients, since all local calls are confirmed or denied by default.</param> protected override SendToClientResultEnum DoSendToClient(ClientId senderId, ClientId receiverId, Envelope envelope, TimeSpan? requestConfirmTimeout) { if (receiverId.IsMessageBusIndexValid && receiverId.MessageBus == this) {// Seems to be a local client Id. SendToClientResultEnum result = base.DoSendToClient(senderId, receiverId, envelope, requestConfirmTimeout); if (result != SendToClientResultEnum.ClientNotFound) { return result; } } // Receiver was not local in parrent, try remote. if (IsConnected == false) { return SendToClientResultEnum.Failure; } EnvelopeMessage message = new EnvelopeMessage() { Envelope = envelope, Receivers = new ClientId[] { receiverId }, Sender = senderId }; return ToServer(message, requestConfirmTimeout) ? SendToClientResultEnum.Success : SendToClientResultEnum.Failure; }
internal object Invoke(object target, Envelope envelope) { object[] parameters = null; if (ClientInstance != null && EnvelopeRequired) { parameters = new object[] { ClientInstance, envelope, envelope.Message}; } else if (ClientInstance != null && EnvelopeRequired == false) { parameters = new object[] { ClientInstance, envelope.Message}; } else if (ClientInstance == null && EnvelopeRequired) { parameters = new object[] { envelope, envelope.Message}; } else if (ClientInstance == null && EnvelopeRequired == false) { parameters = new object[] { envelope.Message}; } return DelegateInstance(target, parameters); }
protected override void OnPerformExecution(Envelope envelope) { Dictionary<Type, TypeHandler> typeHandlersHotSwap = _typeHandlersHotSwap; Type messageType = envelope.Message.GetType(); object target = _source; if (target != null && envelope.Message != null && typeHandlersHotSwap != null) { TypeHandler handler; if (typeHandlersHotSwap.TryGetValue(messageType, out handler)) { handler.Invoke(target, envelope); } } }
/// <summary> /// Receive an envelope for executing. /// </summary> internal override bool Receive(Envelope envelope) { ExecutionStrategy executionStrategy = _executionStrategy; if (executionStrategy != null) { executionStrategy.Execute(envelope); } RaiseEnvelopeReceivedEvent(envelope); return true; }
/// <summary> /// Send an item to multiple recipients. /// </summary> public Outcomes Send(ClientId senderId, IEnumerable<ClientId> receiversIds, Envelope envelope, TimeSpan? requestConfirmTimeout, bool showErrorsDiagnostics) { return DoSend(senderId, receiversIds, envelope, requestConfirmTimeout, showErrorsDiagnostics); }
protected abstract Outcomes DoSend(ClientId senderIndex, IEnumerable<ClientId> receiversIndeces, Envelope envelope, TimeSpan? requestConfirmTimeout, bool showErrorsDiagnostics);
/// <summary> /// This may actually *not* duplicate the object, since it follows the DuplicationMode. /// </summary> /// <returns></returns> public virtual Envelope Duplicate(ISerializer serializer) { Envelope newEnvelope = this; if (_duplicationMode == DuplicationModeEnum.DuplicateEnvelope || _duplicationMode == DuplicationModeEnum.DuplicateBoth) { //newObject = (Envelope)this.MemberwiseClone(); //newObject = (Envelope)serializer.Duplicate(this); newEnvelope = new Envelope() { _duplicationMode = this._duplicationMode }; EnvelopeTransportation transportHistory = _transportHistory; EnvelopeTransportation transportTargetAddress = _transportTargetAddress; if (transportHistory != null) { newEnvelope._transportHistory = transportHistory.Duplicate(); } if (transportTargetAddress != null) { newEnvelope._transportTargetAddress = transportTargetAddress.Duplicate(); } } if (_message != null && (_duplicationMode == DuplicationModeEnum.DuplicateMessage || _duplicationMode == DuplicationModeEnum.DuplicateBoth)) { if (_message is ICloneable) { newEnvelope._message = ((ICloneable)_message).Clone(); } else if (_message.GetType().IsClass) {// We need to use the slow cloning mechanism. newEnvelope._message = serializer.Duplicate(_message); #if Matrix_Diagnostics SystemMonitor.OperationErrorIf(newEnvelope._message == null, "Failed to serialize message [" + _message.GetType().Name + "]."); #endif } // Value type items are supposed to be copied by referencing. } return newEnvelope; }