public override void WritePacketBody(Stream stream, AmfMessageDescriptor descriptor) { var writer = new AmfStreamWriter(stream); WriteUtf8(writer, descriptor.Target); WriteUtf8(writer, descriptor.Response); writer.Write(-1); }
public override sealed AmfMessageDescriptor ReadPacketBody(Stream stream) { var reader = new AmfStreamReader(stream); var context = 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); } }
/// <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); } }
public abstract void WritePacketBody(Stream stream, AmfMessageDescriptor descriptor);