コード例 #1
0
        public override void WritePacketBody(Stream stream, AmfMessageDescriptor descriptor)
        {
            var writer = new AmfStreamWriter(stream);

            WriteUtf8(writer, descriptor.Target);
            WriteUtf8(writer, descriptor.Response);
            writer.Write(-1);
        }
コード例 #2
0
        /// <summary>
        /// The read packet body.
        /// </summary>
        /// <param name="stream">
        /// The stream.
        /// </param>
        /// <returns>
        /// The <see cref="AmfMessageDescriptor"/>.
        /// </returns>
        /// <exception cref="FormatException">
        /// </exception>
        public override sealed AmfMessageDescriptor ReadPacketBody(Stream stream)
        {
            var        reader  = new AmfStreamReader(stream);
            AmfContext context = this.CreateDefaultContext();

            try
            {
                var descriptor = new AmfMessageDescriptor {
                    Target = ReadString(reader), Response = ReadString(reader)
                };

                reader.ReadInt32(); // Message length

                context.ResetReferences();
                return(descriptor);
            }
            catch (Exception e)
            {
                throw new FormatException(Errors.Amf0Deserializer_ReadPacketMessages_InvalidFormat, e);
            }
        }
コード例 #3
0
ファイル: AmfMessage.cs プロジェクト: artema/DotAmf
 public AmfMessage(AmfMessageDescriptor descriptor)
 {
     Target = descriptor.Target;
     Response = descriptor.Response;
 }
コード例 #4
0
 public abstract void WritePacketBody(Stream stream, AmfMessageDescriptor descriptor);
コード例 #5
0
        /// <summary>
        /// Encode an AMF packet from an AMFX format.
        /// </summary>
        /// <exception cref="FormatException">Error during encoding.</exception>
        public void Encode(Stream stream, XmlReader input)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }
            if (!stream.CanWrite)
            {
                throw new ArgumentException(Errors.AmfPacketWriter_Write_StreamNotWriteable, "stream");
            }
            if (input == null)
            {
                throw new ArgumentNullException("input");
            }

            try
            {
                #region Encode data
                var encoder         = CreateEncoder(_options);
                var amfStreamWriter = new AmfStreamWriter(stream);

                WriteAmfVersion(amfStreamWriter, _options.AmfVersion);

                input.MoveToContent();

                var headerCount = Convert.ToInt32(input.GetAttribute(AmfxContent.PacketHeaderCount));
                var bodyCount   = Convert.ToInt32(input.GetAttribute(AmfxContent.PacketBodyCount));

                while (input.Read())
                {
                    if (input.NodeType != XmlNodeType.Element)
                    {
                        continue;
                    }

                    if (headerCount != -1)
                    {
                        WriteHeaderCount(amfStreamWriter, headerCount);
                        headerCount = -1;
                    }

                    #region Read packet header
                    if (input.Name == AmfxContent.PacketHeader)
                    {
                        var header = new AmfHeaderDescriptor
                        {
                            Name           = input.GetAttribute(AmfxContent.PacketHeaderName),
                            MustUnderstand =
                                (input.GetAttribute(AmfxContent.PacketHeaderMustUnderstand) ==
                                 AmfxContent.True)
                        };

                        encoder.WritePacketHeader(stream, header);

                        while (input.Read())
                        {
                            //Skip until header content is found, if any
                            if (input.NodeType != XmlNodeType.Element || input.Name == AmfxContent.PacketHeader)
                            {
                                continue;
                            }

                            encoder.Encode(stream, input);
                            break;
                        }
                        continue;
                    }
                    #endregion

                    if (bodyCount != -1)
                    {
                        WriteMessageCount(amfStreamWriter, bodyCount);
                        bodyCount = -1;
                    }

                    #region Read packet body
                    if (input.Name == AmfxContent.PacketBody)
                    {
                        var message = new AmfMessageDescriptor
                        {
                            Target   = input.GetAttribute(AmfxContent.PacketBodyTarget),
                            Response = input.GetAttribute(AmfxContent.PacketBodyResponse)
                        };

                        encoder.WritePacketBody(stream, message);

                        while (input.Read())
                        {
                            //Skip until body content is found, if any
                            if (input.NodeType != XmlNodeType.Element || input.Name == AmfxContent.PacketBody)
                            {
                                continue;
                            }

                            encoder.Encode(stream, input);
                            break;
                        }
                        continue;
                    }
                    #endregion
                }
                #endregion
            }
            catch (Exception e)
            {
                throw new FormatException(Errors.AmfPacketReader_DecodingError, e);
            }
        }
コード例 #6
0
 /// <summary>
 /// Initializes a new instance of the <see cref="AmfMessage"/> class.
 /// </summary>
 /// <param name="descriptor">
 /// The descriptor.
 /// </param>
 public AmfMessage(AmfMessageDescriptor descriptor)
 {
     this.Target   = descriptor.Target;
     this.Response = descriptor.Response;
 }
コード例 #7
0
        /// <summary>
        /// Decode an AMF packet into an AMFX format.
        /// </summary>
        /// <param name="stream">
        /// The stream.
        /// </param>
        /// <param name="output">
        /// The output.
        /// </param>
        /// <exception cref="InvalidDataException">
        /// Error during decoding.
        /// </exception>
        public void Decode(Stream stream, XmlWriter output)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            if (!stream.CanRead)
            {
                throw new ArgumentException(Errors.AmfPacketReader_Read_StreamClosed, "stream");
            }

            if (output == null)
            {
                throw new ArgumentNullException("output");
            }

            try
            {
                var amfStreamReader = new AmfStreamReader(stream);

                AmfVersion  version = ReadPacketVersion(amfStreamReader);
                IAmfDecoder decoder = CreateDecoder(version, this._options);

                output.WriteStartDocument();
                output.WriteStartElement(AmfxContent.AmfxDocument, AmfxContent.Namespace);
                output.WriteAttributeString(AmfxContent.VersionAttribute, version.ToAmfxName());
                output.Flush();

                // Read headers
                uint headerCount = ReadDataCount(amfStreamReader);

                for (int i = 0; i < headerCount; i++)
                {
                    AmfHeaderDescriptor header = decoder.ReadPacketHeader(stream);

                    output.WriteStartElement(AmfxContent.PacketHeader);
                    output.WriteAttributeString(AmfxContent.PacketHeaderName, header.Name);
                    output.WriteAttributeString(
                        AmfxContent.PacketHeaderMustUnderstand,
                        header.MustUnderstand.ToString());
                    decoder.Decode(stream, output);
                    output.WriteEndElement();
                    output.Flush();
                }

                // Read messages
                uint messageCount = ReadDataCount(amfStreamReader);

                for (int i = 0; i < messageCount; i++)
                {
                    AmfMessageDescriptor body = decoder.ReadPacketBody(stream);

                    output.WriteStartElement(AmfxContent.PacketBody);
                    output.WriteAttributeString(AmfxContent.PacketBodyTarget, body.Target);
                    output.WriteAttributeString(AmfxContent.PacketBodyResponse, body.Response);
                    decoder.Decode(stream, output);
                    output.WriteEndElement();
                    output.Flush();
                }

                output.WriteEndElement();
                output.WriteEndDocument();
                output.Flush();
            }
            catch (Exception e)
            {
                output.Flush();
                throw new InvalidDataException(Errors.AmfPacketReader_DecodingError, e);
            }
        }