Пример #1
0
        /// <summary>
        /// Server receives the client init that may contain the initial response message.
        /// </summary>
        void OnSaslInit(SaslInit init)
        {
            if (this.state != SaslState.WaitingForInit)
            {
                throw new AmqpException(AmqpErrorCode.IllegalState, AmqpResources.GetString(AmqpResources.AmqpIllegalOperationState, "R:SASL-INIT", this.state));
            }

            this.state       = SaslState.Negotiating;
            this.saslHandler = this.provider.GetHandler(init.Mechanism.Value, true);
            this.saslHandler.Start(this, init, false);
        }
Пример #2
0
        public static object DecodeObject(ByteBuffer buffer, FormatCode formatCode)
        {
            EncodingBase encoding;

            if (encodingsByCode.TryGetValue(formatCode, out encoding))
            {
                return(encoding.DecodeObject(buffer, formatCode));
            }

            throw GetEncodingException(AmqpResources.GetString(AmqpResources.AmqpInvalidFormatCode, formatCode, buffer.Offset));
        }
Пример #3
0
        protected override void EnsureRequired()
        {
            if (!this.Role.HasValue)
            {
                throw AmqpEncoding.GetEncodingException(AmqpResources.GetString(AmqpResources.AmqpRequiredFieldNotSet, "role", Name));
            }

            if (!this.First.HasValue)
            {
                throw AmqpEncoding.GetEncodingException(AmqpResources.GetString(AmqpResources.AmqpRequiredFieldNotSet, "first", Name));
            }
        }
Пример #4
0
        static DescribedType Decode(ByteBuffer buffer, FormatCode formatCode)
        {
            if (formatCode != FormatCode.Described)
            {
                throw AmqpEncoding.GetEncodingException(AmqpResources.GetString(AmqpResources.AmqpInvalidFormatCode, formatCode, buffer.Offset));
            }

            object descriptor = AmqpEncoding.DecodeObject(buffer);
            object value      = AmqpEncoding.DecodeObject(buffer);

            return(new DescribedType(descriptor, value));
        }
Пример #5
0
            protected override void Initialize(ByteBuffer buffer, FormatCode formatCode,
                                               out int size, out int count, out int encodeWidth, out Collection effectiveType)
            {
                if (formatCode != FormatCode.Map32 && formatCode != FormatCode.Map8)
                {
                    throw new AmqpException(AmqpErrorCode.InvalidField, AmqpResources.GetString(AmqpResources.AmqpInvalidFormatCode, formatCode, buffer.Offset));
                }

                encodeWidth = formatCode == FormatCode.Map8 ? FixedWidth.UByte : FixedWidth.UInt;
                AmqpEncoding.ReadSizeAndCount(buffer, formatCode, FormatCode.Map8, FormatCode.Map32, out size, out count);
                effectiveType = this;
            }
Пример #6
0
        internal static Error FromException(Exception exception, bool includeErrorDetails = false)
        {
            AmqpException amqpException = exception as AmqpException;

            if (amqpException != null)
            {
                return(amqpException.Error);
            }

            Error error = new Error();

            error.Description = exception.Message;
            if (exception is UnauthorizedAccessException)
            {
                error.Condition = AmqpErrorCode.UnauthorizedAccess;
            }
            else if (exception is InvalidOperationException)
            {
                error.Condition = AmqpErrorCode.NotAllowed;
            }
            else if (exception is System.Transactions.TransactionAbortedException)
            {
                error.Condition = AmqpErrorCode.TransactionRollback;
            }
            else if (exception is NotImplementedException)
            {
                error.Condition = AmqpErrorCode.NotImplemented;
            }
            else
            {
                error.Condition   = AmqpErrorCode.InternalError;
                error.Description = includeErrorDetails ?
                                    exception.Message :
                                    AmqpResources.GetString(AmqpResources.AmqpErrorOccurred, AmqpErrorCode.InternalError);
            }

#if DEBUG
            error.Info = new Fields();

            const int MaxSizeInInfoMap = 8 * 1024;
            // Limit the size of the exception string as it may exceed the connection max frame size
            string exceptionString = exception.ToString();
            if (exceptionString.Length > MaxSizeInInfoMap)
            {
                exceptionString = exceptionString.Substring(0, MaxSizeInInfoMap);
            }

            error.Info.Add("exception", exceptionString);
#endif

            return(error);
        }
Пример #7
0
        protected override TransportBase OnCreateTransport(TransportBase innerTransport, bool isInitiator)
        {
#if !PCL
            if (innerTransport.GetType() != typeof(TcpTransport))
            {
                throw new InvalidOperationException(AmqpResources.GetString(AmqpResources.AmqpTransportUpgradeNotAllowed,
                                                                            innerTransport.GetType().Name, typeof(TlsTransport).Name));
            }

            return(new TlsTransport(innerTransport, this.tlsSettings));
#else
            throw new NotImplementedException(Microsoft.Azure.Amqp.PCL.Resources.ReferenceAssemblyInvalidUse);
#endif
        }
Пример #8
0
 public static void ReadCount(ByteBuffer buffer, FormatCode formatCode, FormatCode formatCode8, FormatCode formatCode32, out int count)
 {
     if (formatCode == formatCode8)
     {
         count = AmqpBitConverter.ReadUByte(buffer);
     }
     else if (formatCode == formatCode32)
     {
         count = (int)AmqpBitConverter.ReadUInt(buffer);
     }
     else
     {
         throw GetEncodingException(AmqpResources.GetString(AmqpResources.AmqpInvalidFormatCode, formatCode, buffer.Offset));
     }
 }
Пример #9
0
        void OnReadHeaderComplete(TransportAsyncCallbackArgs args)
        {
            if (args.Exception != null)
            {
                AmqpTrace.Provider.AmqpLogError(this, "ReadHeader", args.Exception.Message);
                this.Complete(args);
                return;
            }

            try
            {
                ProtocolHeader receivedHeader = new ProtocolHeader();
                receivedHeader.Decode(new ByteBuffer(args.Buffer, args.Offset, args.Count));
#if DEBUG
                receivedHeader.Trace(false);
                AmqpTrace.Provider.AmqpLogOperationVerbose(this, TraceOperation.Receive, receivedHeader);
#endif

                if (!receivedHeader.Equals(this.sentHeader))
                {
                    // TODO: need to reconnect with the reply version if supported
                    throw new AmqpException(AmqpErrorCode.NotImplemented, AmqpResources.GetString(AmqpResources.AmqpProtocolVersionNotSupported, this.sentHeader, receivedHeader));
                }

                // upgrade transport
                TransportBase secureTransport = this.settings.TransportProviders[this.providerIndex].CreateTransport(args.Transport, true);
                AmqpTrace.Provider.AmqpUpgradeTransport(this, args.Transport, secureTransport);
                args.Transport = secureTransport;
                IAsyncResult result = args.Transport.BeginOpen(this.timeoutHelper.RemainingTime(), this.OnTransportOpenCompete, args);
                if (result.CompletedSynchronously)
                {
                    this.HandleTransportOpened(result);
                }
            }
            catch (Exception exp)
            {
                if (Fx.IsFatal(exp))
                {
                    throw;
                }

                AmqpTrace.Provider.AmqpLogError(this, "OnProtocolHeader", exp.Message);
                args.Exception = exp;
                this.Complete(args);
            }
        }
Пример #10
0
        public void Decode(ByteBuffer buffer)
        {
            if (buffer.Length < this.EncodeSize)
            {
                throw new AmqpException(AmqpErrorCode.DecodeError, AmqpResources.GetString(AmqpResources.AmqpInsufficientBufferSize, this.EncodeSize, buffer.Length));
            }

            uint prefix = AmqpBitConverter.ReadUInt(buffer);

            if (prefix != ProtocolHeader.AmqpPrefix)
            {
                throw new AmqpException(AmqpErrorCode.DecodeError, "ProtocolName" + prefix.ToString("X8"));
            }

            this.protocolId = (ProtocolId)AmqpBitConverter.ReadUByte(buffer);

            this.version = new AmqpVersion(
                AmqpBitConverter.ReadUByte(buffer),
                AmqpBitConverter.ReadUByte(buffer),
                AmqpBitConverter.ReadUByte(buffer));
        }
Пример #11
0
        internal override void EnsureRequired()
        {
            if (this.LinkName == null)
            {
                throw AmqpEncoding.GetEncodingException(AmqpResources.GetString(AmqpResources.AmqpRequiredFieldNotSet, "name", Name));
            }

            if (!this.Handle.HasValue)
            {
                throw AmqpEncoding.GetEncodingException(AmqpResources.GetString(AmqpResources.AmqpRequiredFieldNotSet, "handle", Name));
            }

            if (!this.Role.HasValue)
            {
                throw AmqpEncoding.GetEncodingException(AmqpResources.GetString(AmqpResources.AmqpRequiredFieldNotSet, "role", Name));
            }

            ////if (!this.Role.Value && this.InitialDeliveryCount == null)
            ////{
            ////    throw AmqpEncoding.GetEncodingException("attach.initial-delivery-count");
            ////}
        }
Пример #12
0
        public static EncodingBase GetEncoding(Type type)
        {
            EncodingBase encoding = null;

            if (encodingsByType.TryGetValue(type, out encoding))
            {
                return(encoding);
            }
            else if (type.IsArray)
            {
                return(arrayEncoding);
            }
            else if (typeof(IList).IsAssignableFrom(type))
            {
                return(listEncoding);
            }
            else if (typeof(DescribedType).IsAssignableFrom(type))
            {
                return(describedTypeEncoding);
            }
            throw new NotSupportedException(AmqpResources.GetString(AmqpResources.AmqpInvalidType, type.ToString()));
        }
Пример #13
0
        /// <summary>
        /// Client receives the announced server mechanisms.
        /// </summary>
        void OnSaslServerMechanisms(SaslMechanisms mechanisms)
        {
            if (this.state != SaslState.WaitingForServerMechanisms)
            {
                throw new AmqpException(AmqpErrorCode.IllegalState, AmqpResources.GetString(AmqpResources.AmqpIllegalOperationState, "R:SASL-MECH", this.state));
            }

            string mechanismToUse = null;

            foreach (string mechanism in this.provider.Mechanisms)
            {
                if (mechanisms.SaslServerMechanisms.Contains(new AmqpSymbol(mechanism)))
                {
                    mechanismToUse = mechanism;
                    break;
                }

                if (mechanismToUse != null)
                {
                    break;
                }
            }

            if (mechanismToUse == null)
            {
                throw new AmqpException(
                          AmqpErrorCode.NotFound,
                          AmqpResources.GetString(AmqpResources.AmqpNotSupportMechanism, mechanisms.SaslServerMechanisms.ToString(), string.Join(",", this.provider.Mechanisms)));
            }

            this.state       = SaslState.Negotiating;
            this.saslHandler = this.provider.GetHandler(mechanismToUse, true);
            SaslInit init = new SaslInit();

            init.Mechanism = mechanismToUse;
            this.saslHandler.Start(this, init, true);
        }
Пример #14
0
        public static EncodingBase GetEncoding(object value)
        {
            EncodingBase encoding = null;
            Type         type     = value.GetType();

            if (encodingsByType.TryGetValue(type, out encoding))
            {
                return(encoding);
            }
            else if (type.IsArray)
            {
                return(arrayEncoding);
            }
            else if (value is IList)
            {
                return(listEncoding);
            }
            else if (value is DescribedType)
            {
                return(describedTypeEncoding);
            }

            throw new NotSupportedException(AmqpResources.GetString(AmqpResources.AmqpInvalidType, type.ToString()));
        }
Пример #15
0
        internal static decimal DecodeValue(ByteBuffer buffer, FormatCode formatCode)
        {
            decimal value = 0;

            switch (formatCode)
            {
            case FormatCode.Decimal32:
                value = DecimalEncoding.DecodeDecimal32(buffer);
                break;

            case FormatCode.Decimal64:
                value = DecimalEncoding.DecodeDecimal64(buffer);
                break;

            case FormatCode.Decimal128:
                value = DecimalEncoding.DecodeDecimal128(buffer);
                break;

            default:
                throw AmqpEncoding.GetEncodingException(AmqpResources.GetString(AmqpResources.AmqpInvalidFormatCode, formatCode, buffer.Offset));
            }

            return(value);
        }
Пример #16
0
        SerializableType CompileType(Type type, bool describedOnly)
        {
            var typeAttributes = type.GetTypeInfo().GetCustomAttributes(typeof(AmqpContractAttribute), false);

            if (!typeAttributes.Any())
            {
                if (describedOnly)
                {
                    return(null);
                }
                else
                {
                    return(CompileNonContractTypes(type));
                }
            }

            AmqpContractAttribute contractAttribute = (AmqpContractAttribute)typeAttributes.First();
            SerializableType      baseType          = null;

            if (type.GetTypeInfo().BaseType != typeof(object))
            {
                baseType = this.CompileType(type.GetTypeInfo().BaseType, true);
                if (baseType != null)
                {
                    if (baseType.Encoding != contractAttribute.Encoding)
                    {
                        throw new SerializationException(AmqpResources.GetString(AmqpResources.AmqpEncodingTypeMismatch, type.Name, contractAttribute.Encoding, type.GetTypeInfo().BaseType.Name, baseType.Encoding));
                    }

                    this.customTypeCache.TryAdd(type.GetTypeInfo().BaseType, baseType);
                }
            }

            string descriptorName = contractAttribute.Name;
            ulong? descriptorCode = contractAttribute.InternalCode;

            if (descriptorName == null && descriptorCode == null)
            {
                descriptorName = type.FullName;
            }

            List <SerialiableMember> memberList = new List <SerialiableMember>();

            if (contractAttribute.Encoding == EncodingType.List && baseType != null)
            {
                memberList.AddRange(baseType.Members);
            }

            int lastOrder = memberList.Count + 1;

            MemberInfo[]   memberInfos    = type.GetMembers(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
            MethodAccessor onDeserialized = null;

            foreach (MemberInfo memberInfo in memberInfos)
            {
                if (memberInfo.DeclaringType != type)
                {
                    continue;
                }

                if (memberInfo is FieldInfo ||
                    memberInfo is PropertyInfo)
                {
                    var memberAttributes = memberInfo.GetCustomAttributes(typeof(AmqpMemberAttribute), true);
                    if (memberAttributes.Count() != 1)
                    {
                        continue;
                    }

                    AmqpMemberAttribute attribute = (AmqpMemberAttribute)memberAttributes.First();

                    SerialiableMember member = new SerialiableMember();
                    member.Name      = attribute.Name ?? memberInfo.Name;
                    member.Order     = attribute.InternalOrder ?? lastOrder++;
                    member.Mandatory = attribute.Mandatory;
                    member.Accessor  = MemberAccessor.Create(memberInfo, true);

                    // This will recursively resolve member types
                    Type memberType = memberInfo is FieldInfo ? ((FieldInfo)memberInfo).FieldType : ((PropertyInfo)memberInfo).PropertyType;
                    member.Type = GetType(memberType);

                    memberList.Add(member);
                }
                else if (memberInfo is MethodInfo)
                {
                    var memberAttributes = memberInfo.GetCustomAttributes(typeof(OnDeserializedAttribute), false);
                    if (memberAttributes.Count() == 1)
                    {
                        onDeserialized = MethodAccessor.Create((MethodInfo)memberInfo);
                    }
                }
            }

            if (contractAttribute.Encoding == EncodingType.List)
            {
                memberList.Sort(MemberOrderComparer.Instance);
                int order = -1;
                foreach (SerialiableMember member in memberList)
                {
                    if (order > 0 && member.Order == order)
                    {
                        throw new SerializationException(AmqpResources.GetString(AmqpResources.AmqpDuplicateMemberOrder, order, type.Name));
                    }

                    order = member.Order;
                }
            }

            SerialiableMember[] members = memberList.ToArray();

            Dictionary <Type, SerializableType> knownTypes = null;

            foreach (object o in type.GetTypeInfo().GetCustomAttributes(typeof(KnownTypeAttribute), false))
            {
                KnownTypeAttribute knownAttribute = (KnownTypeAttribute)o;
                if (knownAttribute.Type.GetTypeInfo().GetCustomAttributes(typeof(AmqpContractAttribute), false).Any())
                {
                    if (knownTypes == null)
                    {
                        knownTypes = new Dictionary <Type, SerializableType>();
                    }

                    // KnownType compilation is delayed and non-recursive to avoid circular references
                    knownTypes.Add(knownAttribute.Type, null);
                }
            }

            if (contractAttribute.Encoding == EncodingType.List)
            {
                return(SerializableType.CreateDescribedListType(this, type, baseType, descriptorName,
                                                                descriptorCode, members, knownTypes, onDeserialized));
            }
            else if (contractAttribute.Encoding == EncodingType.Map)
            {
                return(SerializableType.CreateDescribedMapType(this, type, baseType, descriptorName,
                                                               descriptorCode, members, knownTypes, onDeserialized));
            }
            else
            {
                throw new NotSupportedException(contractAttribute.Encoding.ToString());
            }
        }
Пример #17
0
            protected override void Initialize(ByteBuffer buffer, FormatCode formatCode,
                                               out int size, out int count, out int encodeWidth, out Collection effectiveType)
            {
                if (formatCode != FormatCode.Described)
                {
                    throw new AmqpException(AmqpErrorCode.InvalidField, AmqpResources.GetString(AmqpResources.AmqpInvalidFormatCode, formatCode, buffer.Offset));
                }

                effectiveType = null;
                formatCode    = AmqpEncoding.ReadFormatCode(buffer);
                ulong?     code   = null;
                AmqpSymbol symbol = default(AmqpSymbol);

                if (formatCode == FormatCode.ULong0)
                {
                    code = 0;
                }
                else if (formatCode == FormatCode.ULong || formatCode == FormatCode.SmallULong)
                {
                    code = ULongEncoding.Decode(buffer, formatCode);
                }
                else if (formatCode == FormatCode.Symbol8 || formatCode == FormatCode.Symbol32)
                {
                    symbol = SymbolEncoding.Decode(buffer, formatCode);
                }

                if (this.AreEqual(this.descriptorCode, this.descriptorName, code, symbol))
                {
                    effectiveType = this;
                }
                else if (this.knownTypes != null)
                {
                    for (int i = 0; i < this.knownTypes.Count; ++i)
                    {
                        Composite knownType = (Composite)this.serializer.GetType(this.knownTypes[i]);
                        if (this.AreEqual(knownType.descriptorCode, knownType.descriptorName, code, symbol))
                        {
                            effectiveType = knownType;
                            break;
                        }
                    }
                }

                if (effectiveType == null)
                {
                    throw new SerializationException(AmqpResources.GetString(AmqpResources.AmqpUnknownDescriptor, code ?? (object)symbol.Value, this.type.Name));
                }

                formatCode = AmqpEncoding.ReadFormatCode(buffer);
                if (this.Code == FormatCode.List32)
                {
                    if (formatCode == FormatCode.List0)
                    {
                        size = count = encodeWidth = 0;
                    }
                    else
                    {
                        encodeWidth = formatCode == FormatCode.List8 ? FixedWidth.UByte : FixedWidth.UInt;
                        AmqpEncoding.ReadSizeAndCount(buffer, formatCode, FormatCode.List8,
                                                      FormatCode.List32, out size, out count);
                    }
                }
                else
                {
                    encodeWidth = formatCode == FormatCode.Map8 ? FixedWidth.UByte : FixedWidth.UInt;
                    AmqpEncoding.ReadSizeAndCount(buffer, formatCode, FormatCode.Map8,
                                                  FormatCode.Map32, out size, out count);
                }
            }
Пример #18
0
 static void ThrowInvalidFormatCodeException(FormatCode formatCode, int offset)
 {
     throw AmqpEncoding.GetEncodingException(AmqpResources.GetString(AmqpResources.AmqpInvalidFormatCode, formatCode, offset));
 }