Example #1
0
        private ErrorResponseBody GetErrorBody(AMFBody amfBody, Exception exception)
        {
            ErrorResponseBody errorResponseBody = null;

            try
            {
                object content = amfBody.Content;
                if (content is IList)
                {
                    content = (content as IList)[0];
                }
                if (content is SolidSoft.AMFCore.Messaging.Messages.IMessage)
                {
                    errorResponseBody = new ErrorResponseBody(amfBody, content as SolidSoft.AMFCore.Messaging.Messages.IMessage, exception);
                }
                else
                {
                    errorResponseBody = new ErrorResponseBody(amfBody, exception);
                }
            }
            catch
            {
                errorResponseBody = new ErrorResponseBody(amfBody, exception);
            }
            return(errorResponseBody);
        }
Example #2
0
        private AMFBody ReadBody()
        {
            this.Reset();
            string target = base.ReadString();

            // Response that the client understands.
            string response = base.ReadString();
            int    length   = base.ReadInt32();

            if (base.BaseStream.CanSeek)
            {
                long position = base.BaseStream.Position;
                // Read content.
                try
                {
                    object    content   = base.ReadData();
                    AMFBody   amfBody   = new AMFBody(target, response, content);
                    Exception exception = this.LastError;
                    if (exception != null)
                    {
                        ErrorResponseBody errorResponseBody = GetErrorBody(amfBody, exception);
                        _failedAMFBodies.Add(errorResponseBody);
                        return(null);
                    }
                    return(amfBody);
                }
                catch (Exception exception)
                {
                    base.BaseStream.Position = position + length;
                    //Try to build a valid response from partialy deserialized amf body
                    AMFBody           amfBody           = new AMFBody(target, response, null);
                    ErrorResponseBody errorResponseBody = GetErrorBody(amfBody, exception);
                    _failedAMFBodies.Add(errorResponseBody);
                    return(null);
                }
            }
            else
            {
                try
                {
                    object    content   = base.ReadData();
                    AMFBody   amfBody   = new AMFBody(target, response, content);
                    Exception exception = this.LastError;
                    if (exception != null)
                    {
                        ErrorResponseBody errorResponseBody = GetErrorBody(amfBody, exception);
                        _failedAMFBodies.Add(errorResponseBody);
                        return(null);
                    }
                    return(amfBody);
                }
                catch (Exception exception)
                {
                    //Try to build a valid response from partialy deserialized amf body
                    AMFBody           amfBody           = new AMFBody(target, response, null);
                    ErrorResponseBody errorResponseBody = GetErrorBody(amfBody, exception);
                    _failedAMFBodies.Add(errorResponseBody);
                    throw;
                }
            }
        }
Example #3
0
        /// <summary>
        /// This method supports the AMFCore infrastructure and is not intended to be used directly from your code.
        /// </summary>
        /// <param name="amfMessage"></param>

        public void WriteMessage(AMFMessage amfMessage)
        {
            try
            {
                base.WriteShort(amfMessage.Version);
                int headerCount = amfMessage.HeaderCount;
                base.WriteShort(headerCount);
                for (int i = 0; i < headerCount; i++)
                {
                    this.WriteHeader(amfMessage.GetHeaderAt(i), ObjectEncoding.AMF0);
                }
                int bodyCount = amfMessage.BodyCount;
                base.WriteShort(bodyCount);
                for (int i = 0; i < bodyCount; i++)
                {
                    ResponseBody responseBody = amfMessage.GetBodyAt(i) as ResponseBody;
                    if (responseBody != null && !responseBody.IgnoreResults)
                    {
                        //Try to catch serialization errors
                        if (this.BaseStream.CanSeek)
                        {
                            long position = this.BaseStream.Position;

                            try
                            {
                                responseBody.WriteBody(amfMessage.ObjectEncoding, this);
                            }
                            catch (Exception exception)
                            {
                                this.BaseStream.Seek(position, SeekOrigin.Begin);

                                ErrorResponseBody errorResponseBody;
                                if (responseBody.RequestBody.IsEmptyTarget)
                                {
                                    object content = responseBody.RequestBody.Content;
                                    if (content is IList)
                                    {
                                        content = (content as IList)[0];
                                    }
                                    IMessage         message          = content as IMessage;
                                    MessageException messageException = new MessageException(exception);
                                    messageException.FaultCode = __Res.GetString(__Res.Amf_SerializationFail);
                                    errorResponseBody          = new ErrorResponseBody(responseBody.RequestBody, message, messageException);
                                }
                                else
                                {
                                    errorResponseBody = new ErrorResponseBody(responseBody.RequestBody, exception);
                                }

                                try
                                {
                                    errorResponseBody.WriteBody(amfMessage.ObjectEncoding, this);
                                }
                                catch (Exception)
                                {
                                    throw;
                                }
                            }
                        }
                        else
                        {
                            responseBody.WriteBody(amfMessage.ObjectEncoding, this);
                        }
                    }
                    else
                    {
                        AMFBody amfBody = amfMessage.GetBodyAt(i);
                        SolidSoft.AMFCore.Util.ValidationUtils.ObjectNotNull(amfBody, "amfBody");
                        amfBody.WriteBody(amfMessage.ObjectEncoding, this);
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
        }