Пример #1
0
        internal TaskSlim __BasicPublishTask(string exchange, string routingKey, bool mandatory, bool immediate,
                                             BasicProperties properties, ArraySegment <byte> buffer)
        {
            if (properties == null)
            {
                properties = BasicProperties.Empty;
            }

            TaskSlim tcs  = _taskLightPool.GetObject();
            var      args = _basicPubArgsPool.GetObject();

            args.exchange   = exchange;
            args.immediate  = immediate;
            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
                }
                tcs.SetCompleted();
                return(Task.CompletedTask);
            },
                                      expectsReply: false,
                                      tcsL: null,
                                      optArg: args);

            return(tcs);
        }
Пример #2
0
        public async Task RunReplyAction(ushort channel, int classMethodId, AmqpError error)
        {
#if DEBUG
            if (classMethodId != 0)
            {
                // Confirm reply
                var classId  = classMethodId >> 16;
                var methodId = classMethodId & 0x0000FFFF;

                var matchesClass  = (ClassId == classId);
                var matchesMethod = MethodId == (methodId - 1);

                if (!matchesClass || !matchesMethod)
                {
                    Console.WriteLine("[channel " + channel + "] Command for " + ClassId + "|" + MethodId + " did not match reply " + classId + "|" + methodId);
                }
            }
#endif

            if (this.ReplyAction != null)
            {
                await this.ReplyAction(channel, classMethodId, error);
            }
            else
            {
                if (error != null)
                {
                    AmqpIOBase.SetException(Tcs, error, classMethodId);
                    AmqpIOBase.SetException(TcsSlim, error, classMethodId);
                }
                else
                {
                    if (Tcs != null)
                    {
                        Tcs.SetResult(true);
                    }

                    if (TcsSlim != null)
                    {
                        TcsSlim.SetCompleted();
                    }
                }
            }

            if (_recycler != null)
            {
                _recycler(this);
            }
        }
        public void SetCompleted_Should_setCompletedFlags2()
        {
            var taskSlim = new TaskSlim(null);

            taskSlim.IsCompleted.Should().BeFalse();
            taskSlim.HasContinuation.Should().BeFalse();
            taskSlim.HasException.Should().BeFalse();
            taskSlim.RunContinuationAsync.Should().BeFalse();

            taskSlim.SetCompleted(runContinuationAsync: true);

            taskSlim.IsCompleted.Should().BeTrue();
            taskSlim.HasContinuation.Should().BeFalse();
            taskSlim.HasException.Should().BeFalse();
            taskSlim.RunContinuationAsync.Should().BeTrue();
        }
        public void SetComplete_Should_RunContinuationIfOneIsPresent()
        {
            var taskSlim = new TaskSlim(null);

            taskSlim.IsCompleted.Should().BeFalse();
            taskSlim.HasContinuation.Should().BeFalse();
            taskSlim.HasException.Should().BeFalse();
            taskSlim.RunContinuationAsync.Should().BeFalse();

            var runCont = false;

            taskSlim.OnCompleted(() =>
            {
                runCont = true;
            });

            taskSlim.SetCompleted(runContinuationAsync: false);

            runCont.Should().BeTrue();
        }
        public void OnCompleted_Should_RunContinuationIfCompleted()
        {
            var taskSlim = new TaskSlim(null);

            taskSlim.IsCompleted.Should().BeFalse();
            taskSlim.HasContinuation.Should().BeFalse();
            taskSlim.HasException.Should().BeFalse();
            taskSlim.RunContinuationAsync.Should().BeFalse();

            taskSlim.SetCompleted(runContinuationAsync: false);

            var runCont = false;
            taskSlim.OnCompleted(() =>
            {
                runCont = true;
                taskSlim.GetResult();
            });

            runCont.Should().BeTrue();
        }
Пример #6
0
        public async Task RunReplyAction(ushort channel, int classMethodId, AmqpError error)
        {
            AssertCanBeUsed();

#if DEBUG
            if (classMethodId != 0)
            {
                // Confirm reply
                var classId  = classMethodId >> 16;
                var methodId = classMethodId & 0x0000FFFF;

                var matchesClass  = (ClassId == classId);
                var matchesMethod = MethodId == (methodId - 1);

                if (!matchesClass || !matchesMethod)
                {
                    if (LogAdapter.ExtendedLogEnabled)
                    {
                        LogAdapter.LogDebug("CommandToSend", "[channel " + channel + "] Command for " + ClassId + "|" + MethodId + " did not match reply " + classId + "|" + methodId);
                    }
                }
            }
#endif

            // Allows more commands to be sent. This contention is sadly required by the amqp/rabbitmq
            if (_whenReplyReceived != null)
            {
                _whenReplyReceived.Set();
            }
            // ---

            if (this.ReplyAction != null)
            {
                try
                {
                    await this.ReplyAction(channel, classMethodId, error).ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    error = new AmqpError
                    {
                        ReplyText = ex.Message
                    };
                    AmqpIOBase.SetException(Tcs, error, classMethodId);
                    AmqpIOBase.SetException(TcsSlim, error, classMethodId);

                    throw;
                }
            }
            else
            {
                if (error != null)
                {
                    AmqpIOBase.SetException(Tcs, error, classMethodId);
                    AmqpIOBase.SetException(TcsSlim, error, classMethodId);
                }
                else
                {
                    if (Tcs != null)
                    {
                        Tcs.SetResult(true);
                    }

                    if (TcsSlim != null)
                    {
                        TcsSlim.SetCompleted();
                    }
                }
            }

            if (_recycler != null)
            {
                _recycler(this);
            }
        }
        public void SetCompleted_Should_setCompletedFlags()
        {
            var taskSlim = new TaskSlim(null);

            taskSlim.IsCompleted.Should().BeFalse();
            taskSlim.HasContinuation.Should().BeFalse();
            taskSlim.HasException.Should().BeFalse();
            taskSlim.RunContinuationAsync.Should().BeFalse();

            taskSlim.SetCompleted(runContinuationAsync: false);

            taskSlim.IsCompleted.Should().BeTrue();
            taskSlim.HasContinuation.Should().BeFalse();
            taskSlim.HasException.Should().BeFalse();
            taskSlim.RunContinuationAsync.Should().BeFalse();
        }