private void TargetTransporter_PreviewDecodingInformation(object sender, ResonancePreviewDecodingInfoEventArgs e)
 {
     if (TargetTransporter.CheckPending(e.DecodingInformation.Token))
     {
         return;
     }
     DoRouting(e, SourceTransporter);
 }
 /// <summary>
 /// Raises the <see cref="E:PreviewTargetDecodingInformation" /> event.
 /// </summary>
 /// <param name="args">The <see cref="ResonancePreviewDecodingInfoEventArgs"/> instance containing the event data.</param>
 protected virtual void OnPreviewTargetDecodingInformation(ResonancePreviewDecodingInfoEventArgs args)
 {
     PreviewTargetDecodingInformation?.Invoke(SourceTransporter, args);
 }
        private void DoRouting(ResonancePreviewDecodingInfoEventArgs e, IResonanceTransporter transporter)
        {
            try
            {
                if (transporter.State == ResonanceComponentState.Connected)
                {
                    if (RoutingMode == RoutingMode.TwoWay ||
                        (transporter == SourceTransporter && RoutingMode == RoutingMode.OneWayToSource) ||
                        (transporter == TargetTransporter && RoutingMode == RoutingMode.OneWayToTarget))
                    {
                        if (e.DecodingInformation.Type == ResonanceTranscodingInformationType.KeepAliveRequest)
                        {
                            return;
                        }
                        if (e.DecodingInformation.Type == ResonanceTranscodingInformationType.KeepAliveResponse)
                        {
                            return;
                        }

                        if (e.DecodingInformation.Type != ResonanceTranscodingInformationType.Disconnect)
                        {
                            e.Handled = true;

                            var args = new ResonancePreviewDecodingInfoEventArgs();
                            args.DecodingInformation = e.DecodingInformation;
                            args.RawData             = e.RawData;

                            if (transporter == SourceTransporter)
                            {
                                OnPreviewSourceDecodingInformation(args);
                            }
                            else
                            {
                                OnPreviewTargetDecodingInformation(args);
                            }

                            if (args.Handled)
                            {
                                return;
                            }

                            if (WritingMode == WritingMode.Standard)
                            {
                                ResonanceEncodingInformation info = new ResonanceEncodingInformation();
                                info.Completed    = e.DecodingInformation.Completed;
                                info.ErrorMessage = e.DecodingInformation.ErrorMessage;
                                info.HasError     = e.DecodingInformation.HasError;
                                info.IsCompressed = e.DecodingInformation.IsCompressed;
                                info.Message      = e.DecodingInformation.Message;
                                info.RPCSignature = e.DecodingInformation.RPCSignature;
                                info.Timeout      = e.DecodingInformation.Timeout;
                                info.Token        = e.DecodingInformation.Token;
                                info.Transcoding  = e.DecodingInformation.Transcoding;
                                info.Type         = e.DecodingInformation.Type;

                                transporter.SubmitEncodingInformation(info);
                            }
                            else
                            {
                                transporter.Adapter.Write(e.RawData);
                            }
                        }
                        else if (RouteDisconnection)
                        {
                            Task.Factory.StartNew(() =>
                            {
                                try
                                {
                                    if (transporter == SourceTransporter)
                                    {
                                        Logger.LogInformation($"Disconnection notification was received by source transporter. disconnecting target transporter...");
                                    }
                                    else
                                    {
                                        Logger.LogInformation($"Disconnection notification was received by target transporter. disconnecting source transporter...");
                                    }

                                    transporter.Disconnect();
                                }
                                catch (Exception ex)
                                {
                                    Logger.LogError(ex, "Error occurred while trying to disconnect the transporter.");
                                }
                            });
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, "Unexpected routing error has occurred.");
            }
        }