} = 1;                                            //Increment when appending to protocol header.

        /// <summary>
        /// Encodes the specified encoding information header using the specified binary writer.
        /// </summary>
        /// <param name="writer">The binary writer.</param>
        /// <param name="info">The encoding information.</param>
        public virtual void Encode(BinaryWriter writer, ResonanceEncodingInformation info)
        {
            writer.Write(ProtocolVersion); //None zero byte must be written here to not confuse with Handshake messages !
            writer.WriteShortASCII(info.Transcoding);
            writer.Write(info.IsCompressed);
            writer.WriteShortASCII(info.Token);
            writer.Write((byte)info.Type);

            if (info.RPCSignature != null)
            {
                writer.WriteShortASCII(info.RPCSignature.ToString());
            }
            else
            {
                writer.WriteShortASCII(String.Empty);
            }

            writer.Write((byte)(info.Timeout ?? 0));


            if (info.Type == ResonanceTranscodingInformationType.Response || info.Type == ResonanceTranscodingInformationType.MessageSyncACK)
            {
                writer.Write(info.Completed);
                writer.Write(info.HasError);
                writer.WriteUTF8(info.ErrorMessage);
            }
            else if (info.Type == ResonanceTranscodingInformationType.Disconnect)
            {
                writer.WriteUTF8(info.ErrorMessage);
            }

            writer.Write((uint)writer.BaseStream.Position + sizeof(uint)); //Increase size when adding fields.
            //Add new fields here...
        }
Exemplo n.º 2
0
        /// <summary>
        /// Encodes the specified encoding information.
        /// </summary>
        /// <param name="info">The encoding information.</param>
        /// <returns></returns>
        public virtual byte[] Encode(ResonanceEncodingInformation info)
        {
            info.IsCompressed = CompressionConfiguration.Enabled;
            info.Transcoding  = _transcodingName;

            using (MemoryStream ms = new MemoryStream())
            {
                using (BinaryWriter writer = new BinaryWriter(ms))
                {
                    _headerTranscoder.Encode(writer, info);

                    if (info.Type != ResonanceTranscodingInformationType.KeepAliveRequest
                        &&
                        info.Type != ResonanceTranscodingInformationType.KeepAliveResponse
                        &&
                        info.Type != ResonanceTranscodingInformationType.Disconnect
                        &&
                        info.Type != ResonanceTranscodingInformationType.MessageSyncACK
                        &&
                        !info.HasError)
                    {
                        byte[] msgData = null;

                        using (MemoryStream msgMs = new MemoryStream())
                        {
                            using (BinaryWriter msgWriter = new BinaryWriter(msgMs))
                            {
                                Encode(msgWriter, info.Message);
                                msgData = msgMs.ToArray();
                            }
                        }

                        if (EncryptionConfiguration.Enabled)
                        {
                            msgData = EncryptMessageData(msgData);
                        }

                        if (CompressionConfiguration.Enabled)
                        {
                            msgData = CompressMessageData(msgData);
                        }

                        writer.Write(msgData);
                    }

                    return(ms.ToArray());
                }
            }
        }
Exemplo n.º 3
0
        public void Default_Header_Transcoding()
        {
            ResonanceEncodingInformation encodeInfo = new ResonanceEncodingInformation();

            encodeInfo.Completed    = true;
            encodeInfo.ErrorMessage = "Test";
            encodeInfo.HasError     = true;
            encodeInfo.IsCompressed = true;
            encodeInfo.Token        = Guid.NewGuid().ToString();
            encodeInfo.Transcoding  = "test";
            encodeInfo.RPCSignature = RPCSignature.FromString("Method:Service.MyMethod");
            encodeInfo.Type         = ResonanceTranscodingInformationType.Response;

            ResonanceDefaultHeaderTranscoder transcoder = new ResonanceDefaultHeaderTranscoder();

            using (MemoryStream mWrite = new MemoryStream())
            {
                using (BinaryWriter writer = new BinaryWriter(mWrite))
                {
                    transcoder.Encode(writer, encodeInfo);
                }

                ResonanceDecodingInformation decodeInfo = new ResonanceDecodingInformation();

                using (MemoryStream mRead = new MemoryStream(mWrite.ToArray()))
                {
                    using (BinaryReader reader = new BinaryReader(mRead))
                    {
                        transcoder.Decode(reader, decodeInfo);
                    }
                }

                Assert.AreEqual(encodeInfo.Completed, decodeInfo.Completed);
                Assert.AreEqual(encodeInfo.ErrorMessage, decodeInfo.ErrorMessage);
                Assert.AreEqual(encodeInfo.HasError, decodeInfo.HasError);
                Assert.AreEqual(encodeInfo.IsCompressed, decodeInfo.IsCompressed);
                Assert.AreEqual(encodeInfo.Token, decodeInfo.Token);
                Assert.AreEqual(encodeInfo.Transcoding, decodeInfo.Transcoding);
                Assert.AreEqual(encodeInfo.Type, decodeInfo.Type);
                Assert.AreEqual(encodeInfo.RPCSignature.ToString(), "Method:Service.MyMethod");
            }
        }
Exemplo n.º 4
0
        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.");
            }
        }