Exemplo n.º 1
0
        internal Task __QueueUnbind(string queue, string exchange, string routingKey,
                                    IDictionary <string, object> arguments)
        {
            var tcs = new TaskCompletionSource <bool>(TaskCreationOptions.RunContinuationsAsynchronously);

            var writer = AmqpChannelLevelFrameWriter.QueueUnbind(queue, exchange, routingKey, arguments);

            _connectionIo.SendCommand(_channelNum,
                                      Amqp.Channel.Queue.ClassId, Amqp.Channel.Queue.Methods.QueueUnbind,
                                      writer,
                                      reply: (channel, classMethodId, error) =>
            {
                if (classMethodId == AmqpClassMethodChannelLevelConstants.QueueUnbindOk)
                {
                    if (LogAdapter.ProtocolLevelLogEnabled)
                    {
                        LogAdapter.LogDebug(LogSource, "< QueueUnbindOk " + queue);
                    }

                    tcs.SetResult(true);
                }
                else
                {
                    AmqpIOBase.SetException(tcs, error, classMethodId);
                }
            },
                                      expectsReply: true);

            return(tcs.Task);
        }
Exemplo n.º 2
0
        internal Task __ExchangeDelete(string exchange, bool waitConfirmation)
        {
            var tcs = new TaskCompletionSource <bool>(TaskCreationOptions.RunContinuationsAsynchronously);

            var writer = AmqpChannelLevelFrameWriter.ExchangeDelete(exchange, waitConfirmation);

            _connectionIo.SendCommand(_channelNum, Amqp.Channel.Exchange.ClassId, Amqp.Channel.Exchange.Methods.ExchangeDelete,
                                      writer,
                                      reply: (channel, classMethodId, error) =>
            {
                if (!waitConfirmation || classMethodId == AmqpClassMethodChannelLevelConstants.ExchangeDeleteOk)
                {
                    if (LogAdapter.ProtocolLevelLogEnabled)
                    {
                        LogAdapter.LogDebug(LogSource, "< ExchangeDeleteOk " + exchange);
                    }

                    tcs.SetResult(true);
                }
                else
                {
                    AmqpIOBase.SetException(tcs, error, classMethodId);
                }
            },
                                      expectsReply: waitConfirmation);

            return(tcs.Task);
        }
Exemplo n.º 3
0
        private Task __SendConnectionClose(ushort replyCode, string message)
        {
            if (LogAdapter.ProtocolLevelLogEnabled)
            {
                LogAdapter.LogDebug(LogSource, "__SendConnectionClose >");
            }

            var tcs = new TaskCompletionSource <bool>();

            SendCommand(0, Amqp.Connection.ClassId, 50, AmqpConnectionFrameWriter.ConnectionClose,
                        reply: (channel, classMethodId, error) =>
            {
                if (classMethodId == AmqpClassMethodConnectionLevelConstants.ConnectionCloseOk)
                {
                    if (LogAdapter.ProtocolLevelLogEnabled)
                    {
                        LogAdapter.LogDebug(LogSource, "__SendConnectionClose enabled");
                    }

                    tcs.SetResult(true);
                }
                else
                {
                    AmqpIOBase.SetException(tcs, error, classMethodId);
                }
            },
                        expectsReply: true,
                        optArg: new FrameParameters.CloseParams()
            {
                replyCode = replyCode,
                replyText = message
            }, immediately: true);

            return(tcs.Task);
        }
Exemplo n.º 4
0
        internal Task __SendChannelClose(ushort replyCode, string message)
        {
            var tcs = new TaskCompletionSource <bool>();

            _connectionIo.SendCommand(_channelNum, 20, 40,
                                      AmqpChannelLevelFrameWriter.ChannelClose,
                                      reply: (channel, classMethodId, error) =>
            {
                if (classMethodId == AmqpClassMethodChannelLevelConstants.ChannelCloseOk)
                {
                    tcs.SetResult(true);
                }
                else
                {
                    AmqpIOBase.SetException(tcs, error, classMethodId);
                }
            },
                                      expectsReply: true,
                                      optArg: new FrameParameters.CloseParams()
            {
                replyCode = replyCode,
                replyText = message
            },
                                      immediately: true);

            return(tcs.Task);
        }
Exemplo n.º 5
0
        private Task __SendConnectionClose(ushort replyCode, string message)
        {
            var tcs = new TaskCompletionSource <bool>();

            SendCommand(0, 10, 50, AmqpConnectionFrameWriter.ConnectionClose,
                        reply: (channel, classMethodId, error) =>
            {
                if (classMethodId == AmqpClassMethodConnectionLevelConstants.ConnectionCloseOk)
                {
                    // _frameReader.Read_ConnectionCloseOk(() =>
                    {
                        tcs.SetResult(true);
                    }                            //);
                }
                else
                {
                    AmqpIOBase.SetException(tcs, error, classMethodId);
                }
                return(Task.CompletedTask);
            },
                        expectsReply: true,
                        optArg: new FrameParameters.CloseParams()
            {
                replyCode = replyCode,
                replyText = message
            });

            return(tcs.Task);
        }
Exemplo n.º 6
0
        internal Task Open()
        {
            var tcs = new TaskCompletionSource <bool>();

            var writer = AmqpChannelLevelFrameWriter.ChannelOpen();

            _connectionIo.SendCommand(_channelNum, 20, 10,
                                      writer,
                                      reply: (channel, classMethodId, error) =>
            {
                if (classMethodId == AmqpClassMethodChannelLevelConstants.ChannelOpenOk)
                {
                    _connectionIo._frameReader.Read_ChannelOpenOk((reserved) =>
                    {
                        tcs.SetResult(true);
                    });
                }
                else
                {
                    AmqpIOBase.SetException(tcs, error, classMethodId);
                }
            }, expectsReply: true);

            return(tcs.Task);
        }
Exemplo n.º 7
0
        private Task __SendConnectionStartOk(string username, string password)
        {
            var tcs = new TaskCompletionSource <bool>();

            // Only supports PLAIN authentication for now

            var auth   = Encoding.UTF8.GetBytes("\0" + username + "\0" + password);
            var writer = AmqpConnectionFrameWriter.ConnectionStartOk(Protocol.ClientProperties, "PLAIN", auth, "en_US");

            SendCommand(0, 10, 30,
                        writer,
                        reply: (channel, classMethodId, error) =>
            {
                if (classMethodId == AmqpClassMethodConnectionLevelConstants.ConnectionTune)
                {
                    _frameReader.Read_ConnectionTune((channelMax, frameMax, heartbeat) =>
                    {
                        this._channelMax = channelMax;
                        this._heartbeat  = heartbeat;
                        this._frameMax   = frameMax;

                        tcs.SetResult(true);
                    });
                }
                else
                {
                    AmqpIOBase.SetException(tcs, error, classMethodId);
                }
                return(Task.CompletedTask);
            }, expectsReply: true);

            return(tcs.Task);
        }
Exemplo n.º 8
0
        internal Task __ExchangeDeclare(string exchange, string type, bool durable, bool autoDelete,
                                        IDictionary <string, object> arguments, bool waitConfirmation)
        {
            var tcs = new TaskCompletionSource <bool>(TaskCreationOptions.RunContinuationsAsynchronously);

            var writer = AmqpChannelLevelFrameWriter.ExchangeDeclare(exchange, type, durable, autoDelete,
                                                                     arguments, false, false, waitConfirmation);

            _connectionIo.SendCommand(_channelNum, 40, 10,
                                      writer,
                                      reply: (channel, classMethodId, error) =>
            {
                if (!waitConfirmation || classMethodId == AmqpClassMethodChannelLevelConstants.ExchangeDeclareOk)
                {
                    Console.WriteLine("< ExchangeDeclareOk " + exchange);

                    tcs.SetResult(true);
                }
                else
                {
                    AmqpIOBase.SetException(tcs, error, classMethodId);
                }
                return(Task.CompletedTask);
            },
                                      expectsReply: waitConfirmation);

            return(tcs.Task);
        }
Exemplo n.º 9
0
        public Task __ExchangeUnbind(string source, string destination, string routingKey, IDictionary <string, object> arguments, bool waitConfirmation)
        {
            var tcs = new TaskCompletionSource <bool>(TaskCreationOptions.RunContinuationsAsynchronously);

            var writer = AmqpChannelLevelFrameWriter.ExchangeUnbind(source, destination, routingKey, arguments, waitConfirmation);

            _connectionIo.SendCommand(_channelNum,
                                      Amqp.Channel.Exchange.ClassId, Amqp.Channel.Exchange.Methods.ExchangeUnBind,
                                      writer,
                                      reply: (channel, classMethodId, error) =>
            {
                if (!waitConfirmation || classMethodId == AmqpClassMethodChannelLevelConstants.ExchangeUnbindOk)
                {
                    if (LogAdapter.ProtocolLevelLogEnabled)
                    {
                        LogAdapter.LogDebug(LogSource, "< ExchangeUnbindOk " + source);
                    }

                    tcs.SetResult(true);
                }
                else
                {
                    AmqpIOBase.SetException(tcs, error, classMethodId);
                }
            },
                                      expectsReply: waitConfirmation);

            return(tcs.Task);
        }
Exemplo n.º 10
0
        internal Task __BasicCancel(string consumerTag, bool waitConfirmation)
        {
            var tcs = new TaskCompletionSource <bool>(TaskCreationOptions.RunContinuationsAsynchronously);

            _connectionIo.SendCommand(_channelNum, 60, 30,
                                      AmqpChannelLevelFrameWriter.BasicCancel(consumerTag, waitConfirmation),
                                      reply: (channel, classMethodId, error) =>
            {
                if (waitConfirmation && classMethodId == AmqpClassMethodChannelLevelConstants.CancelOk)
                {
                    _connectionIo._frameReader.Read_CancelOk(_ =>
                    {
                        tcs.SetResult(true);
                    });
                }
                else if (!waitConfirmation)
                {
                    tcs.SetResult(true);
                }
                else
                {
                    AmqpIOBase.SetException(tcs, error, classMethodId);
                }
            },
                                      expectsReply: waitConfirmation);

            return(tcs.Task);
        }
Exemplo n.º 11
0
        private Task <string> __SendConnectionOpen(string vhost)
        {
            if (LogAdapter.ProtocolLevelLogEnabled)
            {
                LogAdapter.LogDebug("ConnectionIO", "__SendConnectionOpen > vhost " + vhost);
            }

            var tcs    = new TaskCompletionSource <string>();
            var writer = AmqpConnectionFrameWriter.ConnectionOpen(vhost, string.Empty, false);

            SendCommand(0, 10, 40, writer,
                        reply: (channel, classMethodId, error) =>
            {
                if (classMethodId == AmqpClassMethodConnectionLevelConstants.ConnectionOpenOk)
                {
                    _frameReader.Read_ConnectionOpenOk((knowHosts) =>
                    {
                        if (LogAdapter.ProtocolLevelLogEnabled)
                        {
                            LogAdapter.LogDebug("ConnectionIO", "__SendConnectionOpen completed for vhost " + vhost);
                        }

                        tcs.SetResult(knowHosts);
                    });
                }
                else
                {
                    AmqpIOBase.SetException(tcs, error, classMethodId);
                }
                return(Task.CompletedTask);
            }, expectsReply: true);

            return(tcs.Task);
        }
Exemplo n.º 12
0
        private Task __SendConnectionStartOk(string username, string password, string connectionName)
        {
            if (LogAdapter.ProtocolLevelLogEnabled)
            {
                LogAdapter.LogDebug(LogSource, "__SendConnectionStartOk >");
            }

            var tcs = new TaskCompletionSource <bool>();

            // Only supports PLAIN authentication for now

            var clientProperties = Protocol.ClientProperties;

            if (!string.IsNullOrEmpty(connectionName))
            {
                clientProperties = new Dictionary <string, object>(clientProperties);
                clientProperties["connection_name"] = connectionName;
            }

            var auth   = Encoding.UTF8.GetBytes("\0" + username + "\0" + password);
            var writer = AmqpConnectionFrameWriter.ConnectionStartOk(clientProperties, "PLAIN", auth, "en_US");

            SendCommand(0, Amqp.Connection.ClassId, 30,
                        writer,
                        reply: (channel, classMethodId, error) =>
            {
                if (classMethodId == AmqpClassMethodConnectionLevelConstants.ConnectionTune)
                {
                    _frameReader.Read_ConnectionTune((channelMax, frameMax, heartbeat) =>
                    {
                        this._channelMax = channelMax;
                        this._frameMax   = frameMax;
                        this.Heartbeat   = heartbeat;

                        if (LogAdapter.ProtocolLevelLogEnabled)
                        {
                            LogAdapter.LogDebug(LogSource, "__SendConnectionStartOk completed.");
                        }

                        if (LogAdapter.IsDebugEnabled)
                        {
                            LogAdapter.LogDebug(LogSource, "Tune results: Channel max: " + channel + " Frame max size: " + frameMax + " heartbeat: " + heartbeat);
                        }

                        tcs.SetResult(true);
                    });
                }
                else
                {
                    AmqpIOBase.SetException(tcs, error, classMethodId);
                }
            }, expectsReply: true, immediately: true);

            return(tcs.Task);
        }
Exemplo n.º 13
0
 private void SetErrorResultIfErrorPending(bool expectsReply, Func <ushort, int, AmqpError, Task> replyFn,
                                           TaskCompletionSource <bool> tcs, TaskSlim taskSlim)
 {
     if (expectsReply)
     {
         replyFn(0, 0, _lastError);
     }
     else
     {
         AmqpIOBase.SetException(tcs, _lastError, 0);
         AmqpIOBase.SetException(taskSlim, _lastError, 0);
     }
 }
Exemplo n.º 14
0
        internal Task <string> __BasicConsume(ConsumeMode mode, string queue, string consumerTag, bool withoutAcks,
                                              bool exclusive, IDictionary <string, object> arguments, bool waitConfirmation, Action <string> confirmConsumerTag)
        {
            var tcs = new TaskCompletionSource <string>(            // TaskCreationOptions.AttachedToParent |
                mode == ConsumeMode.ParallelWithBufferCopy || mode == ConsumeMode.SerializedWithBufferCopy
                                ? TaskCreationOptions.RunContinuationsAsynchronously : TaskCreationOptions.None);

            var writer = AmqpChannelLevelFrameWriter.BasicConsume(
                queue, consumerTag, withoutAcks, exclusive, arguments, waitConfirmation);

            _connectionIo.SendCommand(_channelNum, 60, 20,
                                      writer,
                                      reply: (channel, classMethodId, error) =>
            {
                if (LogAdapter.ProtocolLevelLogEnabled)
                {
                    LogAdapter.LogDebug("ChannelIO", "< BasicConsumeOk for queue " + queue);
                }

                if (waitConfirmation && classMethodId == AmqpClassMethodChannelLevelConstants.BasicConsumeOk)
                {
                    _connectionIo._frameReader.Read_BasicConsumeOk((consumerTag2) =>
                    {
                        if (string.IsNullOrEmpty(consumerTag))
                        {
                            if (LogAdapter.ProtocolLevelLogEnabled)
                            {
                                LogAdapter.LogDebug("ChannelIO", "< BasicConsumeOk consumerTag " + consumerTag);
                            }

                            confirmConsumerTag(consumerTag2);
                        }

                        tcs.SetResult(consumerTag2);
                    });
                }
                else if (!waitConfirmation)
                {
                    tcs.SetResult(consumerTag);
                }
                else
                {
                    AmqpIOBase.SetException(tcs, error, classMethodId);
                }

                return(Task.CompletedTask);
            }, expectsReply: waitConfirmation);

            return(tcs.Task);
        }
Exemplo n.º 15
0
        internal Task __BasicPublishConfirm(string exchange, string routingKey, bool mandatory,
                                            BasicProperties properties, ArraySegment <byte> buffer)
        {
            if (properties == null)
            {
                properties = BasicProperties.Empty;
            }

            var confirmationKeeper = _channel._confirmationKeeper;

            var tcs = new TaskCompletionSource <bool>(TaskCreationOptions.RunContinuationsAsynchronously);

            confirmationKeeper.WaitForSemaphore();             // make sure we're not over the limit

            var args = _basicPubArgsPool.GetObject();

            args.exchange   = exchange;
            args.routingKey = routingKey;
            args.mandatory  = mandatory;
            args.properties = properties;
            args.buffer     = buffer;

            _connectionIo.SendCommand(_channelNum, 60, 40,
                                      null, // AmqpChannelLevelFrameWriter.InternalBasicPublish,
                                      reply: (channel, classMethodId, error) =>
            {
                if (properties.IsReusable)
                {
                    _channel.Return(properties);                             // the tcs is left for the confirmation keeper
                }

                if (error != null)
                {
                    AmqpIOBase.SetException(tcs, error, classMethodId);
                }
            },
                                      expectsReply: false,
//				tcsL: null,
                                      optArg: args,
                                      prepare: () => _channel._confirmationKeeper.Add(tcs));

            return(tcs.Task);
        }
Exemplo n.º 16
0
        internal Task __BasicPublishTask(string exchange, string routingKey, bool mandatory,
                                         BasicProperties properties, ArraySegment <byte> buffer)
        {
            if (properties == null)
            {
                properties = BasicProperties.Empty;
            }

            var tcs  = new TaskCompletionSource <bool>();
            var args = _basicPubArgsPool.GetObject();

            args.exchange   = exchange;
            args.routingKey = routingKey;
            args.mandatory  = mandatory;
            args.properties = properties;
            args.buffer     = buffer;

            _connectionIo.SendCommand(_channelNum, 60, 40,
                                      null, // AmqpChannelLevelFrameWriter.InternalBasicPublish,
                                      reply: (channel, classMethodId, error) =>
            {
                if (properties.IsReusable)
                {
                    _channel.Return(properties);                             // the tcs is left for the confirmation keeper
                }

                if (error == null)
                {
                    tcs.TrySetResult(true);
                }
                else
                {
                    AmqpIOBase.SetException(tcs, error, classMethodId);
                }
            },
                                      expectsReply: false,
//				tcsL: null,
                                      optArg: args);

            return(tcs.Task);
        }
Exemplo n.º 17
0
        internal Task <AmqpQueueInfo> __QueueDeclare(string queue, bool passive, bool durable, bool exclusive, bool autoDelete,
                                                     IDictionary <string, object> arguments, bool waitConfirmation)
        {
            var tcs = new TaskCompletionSource <AmqpQueueInfo>(TaskCreationOptions.RunContinuationsAsynchronously);

            var writer = AmqpChannelLevelFrameWriter.QueueDeclare(queue, passive, durable,
                                                                  exclusive, autoDelete, arguments, waitConfirmation);

            _connectionIo.SendCommand(_channelNum, 50, 10,
                                      writer,
                                      reply: async(channel, classMethodId, error) =>
            {
                if (waitConfirmation && classMethodId == AmqpClassMethodChannelLevelConstants.QueueDeclareOk)
                {
                    await _connectionIo._frameReader.Read_QueueDeclareOk((queueName, messageCount, consumerCount) =>
                    {
                        tcs.SetResult(new AmqpQueueInfo()
                        {
                            Name      = queueName,
                            Consumers = consumerCount,
                            Messages  = messageCount
                        });

                        return(Task.CompletedTask);
                    });
                }
                else if (!waitConfirmation)
                {
                    tcs.SetResult(new AmqpQueueInfo {
                        Name = queue
                    });
                }
                else
                {
                    AmqpIOBase.SetException(tcs, error, classMethodId);
                }
            }, expectsReply: waitConfirmation);

            return(tcs.Task);
        }
Exemplo n.º 18
0
        internal Task __BasicQos(uint prefetchSize, ushort prefetchCount, bool global)
        {
            var tcs = new TaskCompletionSource <bool>(TaskCreationOptions.RunContinuationsAsynchronously);

            var writer = AmqpChannelLevelFrameWriter.BasicQos(prefetchSize, prefetchCount, global);

            _connectionIo.SendCommand(_channelNum, 60, 10,
                                      writer,
                                      reply: (channel, classMethodId, error) =>
            {
                if (classMethodId == AmqpClassMethodChannelLevelConstants.BasicQosOk)
                {
                    tcs.SetResult(true);
                }
                else
                {
                    AmqpIOBase.SetException(tcs, error, classMethodId);
                }
            }, expectsReply: true);

            return(tcs.Task);
        }
Exemplo n.º 19
0
        public Task __SendConfirmSelect(bool noWait)
        {
            var tcs = new TaskCompletionSource <bool>(TaskCreationOptions.RunContinuationsAsynchronously);

            _connectionIo.SendCommand(_channelNum, 85, 10,
                                      AmqpChannelLevelFrameWriter.ConfirmSelect(noWait),
                                      reply: (channel, classMethodId, error) =>
            {
                if (classMethodId == AmqpClassMethodChannelLevelConstants.ConfirmSelectOk)
                {
                    tcs.SetResult(true);
                }
                else
                {
                    AmqpIOBase.SetException(tcs, error, classMethodId);
                }
            },
                                      expectsReply: true,
                                      tcs: tcs);

            return(tcs.Task);
        }
Exemplo n.º 20
0
        internal Task __QueueBind(string queue, string exchange, string routingKey, IDictionary <string, object> arguments, bool waitConfirmation)
        {
            var tcs = new TaskCompletionSource <bool>(TaskCreationOptions.RunContinuationsAsynchronously);

            var writer = AmqpChannelLevelFrameWriter.QueueBind(queue, exchange, routingKey, arguments, waitConfirmation);

            _connectionIo.SendCommand(_channelNum, 50, 20,
                                      writer,
                                      reply: (channel, classMethodId, error) =>
            {
                if (!waitConfirmation || (classMethodId == AmqpClassMethodChannelLevelConstants.QueueBindOk))
                {
                    tcs.SetResult(true);
                }
                else
                {
                    AmqpIOBase.SetException(tcs, error, classMethodId);
                }
            }, expectsReply: waitConfirmation);

            return(tcs.Task);
        }
Exemplo n.º 21
0
        internal Task __BasicRecover(bool requeue)
        {
            var tcs = new TaskCompletionSource <bool>(TaskCreationOptions.RunContinuationsAsynchronously);

            _connectionIo.SendCommand(_channelNum, 60, 110,
                                      AmqpChannelLevelFrameWriter.Recover(requeue),
                                      reply: (channel, classMethodId, error) =>
            {
                if (classMethodId == AmqpClassMethodChannelLevelConstants.RecoverOk)
                {
                    tcs.SetResult(true);
                }
                else
                {
                    AmqpIOBase.SetException(tcs, error, classMethodId);
                }
            },
                                      expectsReply: true,
                                      tcs: tcs);

            return(tcs.Task);
        }
Exemplo n.º 22
0
        internal Task <uint> __QueuePurge(string queue, bool waitConfirmation)
        {
            var tcs = new TaskCompletionSource <uint>(TaskCreationOptions.RunContinuationsAsynchronously);

            var writer = AmqpChannelLevelFrameWriter.QueuePurge(queue, waitConfirmation);

            _connectionIo.SendCommand(_channelNum,
                                      Amqp.Channel.Queue.ClassId, Amqp.Channel.Queue.Methods.QueuePurge,
                                      writer,
                                      reply: async(channel, classMethodId, error) =>
            {
                if (waitConfirmation && classMethodId == AmqpClassMethodChannelLevelConstants.QueuePurgeOk)
                {
                    await _connectionIo._frameReader.Read_GenericMessageCount(count =>
                    {
                        if (LogAdapter.ProtocolLevelLogEnabled)
                        {
                            LogAdapter.LogDebug("ChannelIO", "< QueuePurgeOk " + queue);
                        }

                        tcs.SetResult(count);
                        return(Task.CompletedTask);
                    }).ConfigureAwait(false);
                }
                else if (!waitConfirmation)
                {
                    tcs.SetResult(0);
                }
                else
                {
                    AmqpIOBase.SetException(tcs, error, classMethodId);
                }
            },
                                      expectsReply: waitConfirmation);

            return(tcs.Task);
        }