Ejemplo n.º 1
0
        /// <summary>
        /// Writes an AMF header.
        /// </summary>
        /// <param name="output">The output stream</param>
        /// <param name="header">The header to write</param>
        private static void WriteAMFHeader(AMFDataOutput output, AMFHeader header)
        {
            output.WriteShortString(header.Name);
            output.WriteBoolean(header.MustUnderstand);
            output.WriteUnsignedInt(0xffffffff); // header length ignored

            WriteAMFContent(output, header.Content);
        }
Ejemplo n.º 2
0
        public override void SetUp()
        {
            base.SetUp();

            stream = new MemoryStream();
            serializer = Mocks.CreateMock<IActionScriptSerializer>();
            output = new AMFDataOutput(stream, serializer);
        }
Ejemplo n.º 3
0
        public AMF3ObjectWriter(AMFDataOutput output)
        {
            this.output = output;

            classDefinitionCache = new Dictionary<ASClass, int>(ReferenceEqualityComparer<ASClass>.Instance);
            objectReferenceCache = new Dictionary<IASValue, CacheItem>(ReferenceEqualityComparer<IASValue>.Instance);
            stringReferenceCache = new Dictionary<string, int>(StringComparer.Ordinal);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Writes an AMF body.
        /// </summary>
        /// <param name="output">The output stream</param>
        /// <param name="body">The body to write</param>
        private static void WriteAMFBody(AMFDataOutput output, AMFBody body)
        {
            output.WriteShortString(body.RequestTarget);
            output.WriteShortString(body.ResponseTarget);
            output.WriteUnsignedInt(0xffffffff); // body length ignored

            WriteAMFContent(output, body.Content);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Writes an AMF header.
        /// </summary>
        /// <param name="output">The output stream</param>
        /// <param name="header">The header to write</param>
        private static void WriteAMFHeader(AMFDataOutput output, AMFHeader header)
        {
            output.WriteShortString(header.Name);
            output.WriteBoolean(header.MustUnderstand);
            output.WriteUnsignedInt(0xffffffff); // header length ignored

            WriteAMFContent(output, header.Content);
        }
Ejemplo n.º 6
0
        public AMF3ObjectWriter(AMFDataOutput output)
        {
            this.output = output;

            classDefinitionCache = new Dictionary <ASClass, int>(ReferenceEqualityComparer <ASClass> .Instance);
            objectReferenceCache = new Dictionary <IASValue, CacheItem>(ReferenceEqualityComparer <IASValue> .Instance);
            stringReferenceCache = new Dictionary <string, int>(StringComparer.Ordinal);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Writes an AMF message to an output stream and bubbles up exceptions.
        /// </summary>
        /// <param name="output">The output stream</param>
        /// <param name="message">The message to write</param>
        private static void UncheckedWriteAMFMessage(AMFDataOutput output, AMFMessage message)
        {
            output.WriteUnsignedShort(message.Version);

            output.WriteUnsignedShort((ushort)message.Headers.Count);
            foreach (AMFHeader header in message.Headers)
                WriteAMFHeader(output, header);

            output.WriteUnsignedShort((ushort)message.Bodies.Count);
            foreach (AMFBody body in message.Bodies)
                WriteAMFBody(output, body);
        }
Ejemplo n.º 8
0
 /// <summary>
 /// Writes AMF content.
 /// </summary>
 /// <param name="output">The output stream</param>
 /// <param name="content">The content to write</param>
 private static void WriteAMFContent(AMFDataOutput output, IASValue content)
 {
     output.BeginObjectStream();
     try
     {
         output.WriteObject(content);
     }
     finally
     {
         output.EndObjectStream();
     }
 }
Ejemplo n.º 9
0
        /// <summary>
        /// Writes an AMF message to an output stream.
        /// </summary>
        /// <param name="output">The output stream</param>
        /// <param name="message">The message to write</param>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="output"/> or
        /// <paramref name="message"/> is null</exception>
        /// <exception cref="AMFException">Thrown if an exception occurred while writing the message</exception>
        public static void WriteAMFMessage(AMFDataOutput output, AMFMessage message)
        {
            if (output == null)
                throw new ArgumentNullException("output");
            if (message == null)
                throw new ArgumentNullException("message");

            try
            {
                UncheckedWriteAMFMessage(output, message);
            }
            catch (Exception ex)
            {
                throw new AMFException("An exception occurred while writing an AMF message to the stream.", ex);
            }
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Writes an AMF message to an output stream and bubbles up exceptions.
        /// </summary>
        /// <param name="output">The output stream</param>
        /// <param name="message">The message to write</param>
        private static void UncheckedWriteAMFMessage(AMFDataOutput output, AMFMessage message)
        {
            output.WriteUnsignedShort(message.Version);

            output.WriteUnsignedShort((ushort)message.Headers.Count);
            foreach (AMFHeader header in message.Headers)
            {
                WriteAMFHeader(output, header);
            }

            output.WriteUnsignedShort((ushort)message.Bodies.Count);
            foreach (AMFBody body in message.Bodies)
            {
                WriteAMFBody(output, body);
            }
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Writes an AMF message to an output stream.
        /// </summary>
        /// <param name="output">The output stream</param>
        /// <param name="message">The message to write</param>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="output"/> or
        /// <paramref name="message"/> is null</exception>
        /// <exception cref="AMFException">Thrown if an exception occurred while writing the message</exception>
        public static void WriteAMFMessage(AMFDataOutput output, AMFMessage message)
        {
            if (output == null)
            {
                throw new ArgumentNullException("output");
            }
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }

            try
            {
                UncheckedWriteAMFMessage(output, message);
            }
            catch (Exception ex)
            {
                throw new AMFException("An exception occurred while writing an AMF message to the stream.", ex);
            }
        }
Ejemplo n.º 12
0
            protected override IEnumerable<Step> GetSteps()
            {
                // Set properties from context.
                context.Request.IsSecureConnection = context.HttpContext.Request.IsSecureConnection;

                // Read the AMF message and prepare the response context.
                AMFDataInput dataInput = new AMFDataInput(context.HttpContext.Request.InputStream, context.Serializer);
                AMFMessage amfRequestMessage = AMFMessageReader.ReadAMFMessage(dataInput);
                context.Request.SetMessage(amfRequestMessage);
                context.Response.PrepareResponseMessage(amfRequestMessage);

                // Process all message bodies.
                foreach (AMFBody requestBody in amfRequestMessage.Bodies)
                {
                    object nativeContent;
                    try
                    {
                        nativeContent = context.Serializer.ToNative(requestBody.Content, null);
                    }
                    catch (Exception ex)
                    {
                        context.Response.AddErrorResponse(requestBody.ResponseTarget,
                            "An error occurred while deserializing the request body.", ex);
                        continue;
                    }

                    if (requestBody.RequestTarget == "null")
                    {
                        IMessage requestMessage = nativeContent as IMessage;
                        if (requestMessage == null)
                        {
                            object[] array = nativeContent as object[];
                            if (array != null)
                                requestMessage = array[0] as IMessage;

                            if (requestMessage != null)
                            {
                                IAsyncResult result;
                                try
                                {
                                    result = messageBroker.BeginProcessRequest(context, requestMessage, OnAsyncResultCallbackResume, null);
                                }
                                catch (Exception ex)
                                {
                                    ErrorMessage errorMessage = ErrorMessage.CreateErrorResponse(requestMessage,
                                        "An error occurred while the message broker was processing the message.",
                                        "Gateway.MessageBroker.BeginProcessRequestFailed",
                                        ex);

                                    context.Response.AddErrorResponse(requestBody.ResponseTarget, errorMessage);
                                    continue;
                                }

                                yield return new Step("Waiting for message broker.");

                                try
                                {
                                    IMessage responseMessage = messageBroker.EndProcessRequest(result);
                                    context.Response.AddResultResponse(requestBody.ResponseTarget, responseMessage);
                                }
                                catch (Exception ex)
                                {
                                    ErrorMessage errorMessage = ErrorMessage.CreateErrorResponse(requestMessage,
                                        "An error occurred while the message broker was processing the message.",
                                        "Gateway.MessageBroker.EndProcessRequestFailed",
                                        ex);

                                    context.Response.AddErrorResponse(requestBody.ResponseTarget, errorMessage);
                                }

                                continue;
                            }
                        }
                    }

                    // Don't know what to do with this message.
                    context.Response.AddErrorResponse(requestBody.ResponseTarget,
                        String.Format(CultureInfo.CurrentCulture,
                        "Received a message for unsupported request target '{0}'.", requestBody.RequestTarget), null);
                }

                // Generate the response.
                AMFDataOutput dataOutput = new AMFDataOutput(context.HttpContext.Response.OutputStream, context.Serializer);

                AMFMessageWriter.WriteAMFMessage(dataOutput, context.Response.Message);

                context.HttpContext.Response.ContentType = AMFContentType;
                context.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                yield break;
            }
Ejemplo n.º 13
0
        /// <summary>
        /// Writes an AMF body.
        /// </summary>
        /// <param name="output">The output stream</param>
        /// <param name="body">The body to write</param>
        private static void WriteAMFBody(AMFDataOutput output, AMFBody body)
        {
            output.WriteShortString(body.RequestTarget);
            output.WriteShortString(body.ResponseTarget);
            output.WriteUnsignedInt(0xffffffff); // body length ignored

            WriteAMFContent(output, body.Content);
        }
Ejemplo n.º 14
0
        private byte[] ToAMF(object nativeValue)
        {
            IActionScriptSerializer serializer = serializerFactory.CreateSerializer();
            IASValue asValue = serializer.ToASValue(nativeValue);
            MemoryStream stream = new MemoryStream();
            AMFDataOutput dataOutput = new AMFDataOutput(stream, serializer);
            dataOutput.ObjectEncoding = AMFObjectEncoding.AMF3;
            dataOutput.BeginObjectStream();
            dataOutput.WriteObject(asValue);
            dataOutput.EndObjectStream();

            return stream.ToArray();
        }
Ejemplo n.º 15
0
 /// <summary>
 /// Writes AMF content.
 /// </summary>
 /// <param name="output">The output stream</param>
 /// <param name="content">The content to write</param>
 private static void WriteAMFContent(AMFDataOutput output, IASValue content)
 {
     output.BeginObjectStream();
     try
     {
         output.WriteObject(content);
     }
     finally
     {
         output.EndObjectStream();
     }
 }
Ejemplo n.º 16
0
        public AMF0ObjectWriter(AMFDataOutput output)
        {
            this.output = output;

            referenceCache = new Dictionary <IASValue, int>(ReferenceEqualityComparer <IASValue> .Instance);
        }
Ejemplo n.º 17
0
        public AMF0ObjectWriter(AMFDataOutput output)
        {
            this.output = output;

            referenceCache = new Dictionary<IASValue, int>(ReferenceEqualityComparer<IASValue>.Instance);
        }