Наследование: IDisposable
Пример #1
0
        public virtual void ProcessFrame(Frame frame)
        {
            Performative command = frame.Command;

            try
            {
                AmqpDebug.Log(this, false, command);

                if (command.DescriptorCode == Begin.Code)
                {
                    this.OnReceiveBegin((Begin)command);
                }
                else if (command.DescriptorCode == End.Code)
                {
                    this.OnReceiveEnd((End)command);
                }
                else if (command.DescriptorCode == Disposition.Code)
                {
                    this.OnReceiveDisposition((Disposition)command);
                }
                else if (command.DescriptorCode == Flow.Code)
                {
                    this.OnReceiveFlow((Flow)command);
                }
                else
                {
                    this.OnReceiveLinkFrame(frame);
                }
            }
            catch (Exception exception)
            {
                if (Fx.IsFatal(exception))
                {
                    throw;
                }

                AmqpTrace.Provider.AmqpLogError(this, "ProcessFrame", exception.Message);
                this.SafeClose(exception);
            }
        }
Пример #2
0
        void OnReceiveLinkFrame(Frame frame)
        {
            AmqpLink link = null;
            Performative command = frame.Command;
            if (command.DescriptorCode == Attach.Code)
            {
                Attach attach = (Attach)command;
                lock (this.ThisLock)
                {
                    this.links.TryGetValue(attach.LinkName, out link);
                }

                if (link == null)
                {
                    if (!this.TryCreateRemoteLink(attach, out link))
                    {
                        return;
                    }
                }
                else
                {
                    lock (this.ThisLock)
                    {
                        link.RemoteHandle = attach.Handle;
                        this.linksByRemoteHandle.Add(attach.Handle.Value, link);
                    }
                }
            }
            else
            {
                LinkPerformative linkBody = (LinkPerformative)command;
                if (!this.linksByRemoteHandle.TryGetObject(linkBody.Handle.Value, out link))
                {
                    if (this.Settings.IgnoreMissingLinks)
                    {
                        AmqpTrace.Provider.AmqpMissingHandle(this, "link", linkBody.Handle.Value);
                        return;
                    }

                    if (linkBody.DescriptorCode != Detach.Code)
                    {
                        this.SafeClose(new AmqpException(AmqpErrorCode.UnattachedHandle, AmqpResources.GetString(AmqpResources.AmqpHandleNotFound, linkBody.Handle.Value, this)));
                    }

                    return;
                }
            }

            link.ProcessFrame(frame);
        }
Пример #3
0
        public void SendCommand(Performative command, ushort channel, ArraySegment<byte>[] payload)
        {
#if DEBUG
            Frame frame = new Frame();
            frame.Channel = channel;
            frame.Command = command;
            frame.Trace(true, this, channel, command, -1);
            AmqpTrace.Provider.AmqpLogOperationVerbose(this, TraceOperation.Send, frame);
#endif

            int frameSize = 0;
            if (payload == null)
            {
                // The frame buffer is disposed when the write completes
                ByteBuffer buffer = Frame.EncodeCommand(FrameType.Amqp, channel, command, 0);
                frameSize = buffer.Length;
                this.SendBuffer(buffer);
            }
            else
            {
                ByteBuffer[] buffers = new ByteBuffer[1 + payload.Length];
                int payloadSize = 0;
                for (int i = 0; i < payload.Length; ++i)
                {
                    ArraySegment<byte> segment = payload[i];
                    payloadSize += segment.Count;
                    buffers[i + 1] = new ByteBuffer(segment);
                }

                // The frame buffer is disposed when the write completes
                ByteBuffer cmdBuffer = Frame.EncodeCommand(FrameType.Amqp, channel, command, payloadSize);
                frameSize = cmdBuffer.Length + payloadSize;
                buffers[0] = cmdBuffer;
                this.SendBuffers(buffers);
            }

            this.heartBeat.OnSend();
            if (this.UsageMeter != null)
            {
                this.UsageMeter.OnWrite(this, command == null ? 0 : command.DescriptorCode, frameSize);
            }
        }
Пример #4
0
 protected override void OnProcessTransfer(Delivery delivery, Transfer transfer, Frame rawFrame)
 {
     throw new NotImplementedException();
 }
Пример #5
0
        void OnReceiveSessionFrame(Frame frame)
        {
            AmqpSession session = null;
            Performative command = frame.Command;
            ushort channel = frame.Channel;

            if (command.DescriptorCode == Begin.Code)
            {
                Begin begin = (Begin)command;
                if (begin.RemoteChannel.HasValue)
                {
                    // reply to begin
                    lock (this.ThisLock)
                    {
                        if (!this.sessionsByLocalHandle.TryGetObject(begin.RemoteChannel.Value, out session))
                        {
                            throw new AmqpException(AmqpErrorCode.NotFound, AmqpResources.GetString(AmqpResources.AmqpChannelNotFound, begin.RemoteChannel.Value, this));
                        }

                        session.RemoteChannel = channel;
                        this.sessionsByRemoteHandle.Add(channel, session);
                    }
                }
                else
                {
                    // new begin request
                    AmqpSessionSettings settings = AmqpSessionSettings.Create(begin);
                    settings.RemoteChannel = channel;
                    session = this.SessionFactory.CreateSession(this, settings);
                    this.AddSession(session, channel);
                }
            }
            else
            {
                if (!this.sessionsByRemoteHandle.TryGetObject((uint)channel, out session))
                {
                    if (command.DescriptorCode == End.Code ||
                        command.DescriptorCode == Detach.Code ||
                        this.Settings.IgnoreMissingSessions)
                    {
                        // The session close may timed out already
                        AmqpTrace.Provider.AmqpMissingHandle(this, "session", channel);
                        return;
                    }

                    throw new AmqpException(AmqpErrorCode.NotFound, AmqpResources.GetString(AmqpResources.AmqpChannelNotFound, channel, this));
                }
                else if (command.DescriptorCode == End.Code)
                {
                    this.sessionsByRemoteHandle.Remove((uint)channel);
                    session.RemoteChannel = null;
                }
            }

            session.ProcessFrame(frame);
        }
Пример #6
0
        void ProcessFrame(Frame frame)
        {
            Performative command = frame.Command;
            Fx.Assert(command != null, "Must have a valid command");

            if (command.DescriptorCode == OpenCommand.Code)
            {
                this.OnReceiveOpen((Open)frame.Command);
            }
            else if (command.DescriptorCode == CloseCommand.Code)
            {
                this.OnReceiveClose((Close)frame.Command);
            }
            else
            {
                this.OnReceiveSessionFrame(frame);
            }
        }
Пример #7
0
        protected override void OnFrameBuffer(ByteBuffer buffer)
        {
            if (this.State == AmqpObjectState.End)
            {
                buffer.Dispose();
                return;
            }

            using (Frame frame = new Frame())
            {
                frame.Decode(buffer);
#if DEBUG
                frame.Trace(false, this, frame.Channel, frame.Command, frame.Payload.Count);
                AmqpTrace.Provider.AmqpLogOperationVerbose(this, TraceOperation.Receive, frame);
#endif

                this.heartBeat.OnReceive();
                if (this.UsageMeter != null)
                {
                    this.UsageMeter.OnRead(this, frame.Command != null ? frame.Command.DescriptorCode : 0, buffer.Length);
                }

                if (frame.Command != null)
                {
                    this.ProcessFrame(frame);
                }
            }
        }
Пример #8
0
        public void WriteFrame(Performative command, bool needReply)
        {
            try
            {
#if DEBUG
                Frame frame = new Frame(FrameType.Sasl) { Command = command };
                frame.Trace(true);
                AmqpTrace.Provider.AmqpLogOperationVerbose(this, TraceOperation.Send, frame);
#endif

                ByteBuffer buffer = Frame.EncodeCommand(FrameType.Sasl, 0, command, 0);
                TransportAsyncCallbackArgs args = new TransportAsyncCallbackArgs();
                args.SetBuffer(buffer);
                args.CompletedCallback = onWriteFrameComplete;
                args.UserToken = this;
                args.UserToken2 = needReply;
                this.writer.WriteBuffer(args);
            }
            catch (Exception exception)
            {
                if (Fx.IsFatal(exception))
                {
                    throw;
                }

                this.HandleException("WriteFrame", exception);
            }
        }
Пример #9
0
        void IIoHandler.OnReceiveBuffer(ByteBuffer buffer)
        {
            Frame frame = new Frame();
            try
            {
                frame.Decode(buffer);
#if DEBUG
                frame.Trace(false);
                AmqpTrace.Provider.AmqpLogOperationVerbose(this, TraceOperation.Receive, frame);
#endif

                if (frame.Type != FrameType.Sasl)
                {
                    throw new AmqpException(AmqpErrorCode.InvalidField, "sasl-frame-type");
                }

                if (frame.Command == null)
                {
                    throw new AmqpException(AmqpErrorCode.InvalidField, "sasl-frame-body");
                }
            }
            catch (Exception exp)
            {
                if (Fx.IsFatal(exp))
                {
                    throw;
                }

                AmqpTrace.Provider.AmqpLogError(this, "SaslDecode", exp.Message);
                this.CompleteNegotiation(SaslCode.Sys, exp);
                return;
            }

            try
            {
                this.HandleSaslCommand(frame.Command);
            }
            catch (UnauthorizedAccessException authzExp)
            {
                AmqpTrace.Provider.AmqpLogError(this, "Authorize", authzExp.Message);
                this.CompleteNegotiation(SaslCode.Auth, authzExp);
            }
            catch (Exception exp)
            {
                if (Fx.IsFatal(exp))
                {
                    throw;
                }

                AmqpTrace.Provider.AmqpLogError(this, "HandleSaslCommand", exp.Message);
                this.CompleteNegotiation(SaslCode.Sys, exp);
            }
        }