コード例 #1
0
        /// <summary>
        /// Finds and joins to channel and sends response
        /// </summary>
        private async Task JoinChannel(MqClient client, TmqMessage message)
        {
            Channel channel = _server.FindChannel(message.Target);

            //if auto creation active, try to create channel
            if (channel == null && _server.Options.AutoChannelCreation)
            {
                channel = _server.FindOrCreateChannel(message.Target);
            }

            if (channel == null)
            {
                if (message.ResponseRequired)
                {
                    await client.SendAsync(MessageBuilder.ResponseStatus(message, KnownContentTypes.NotFound));
                }

                return;
            }

            ChannelClient found = channel.FindClient(client.UniqueId);

            if (found != null)
            {
                if (message.ResponseRequired)
                {
                    await client.SendAsync(MessageBuilder.ResponseStatus(message, KnownContentTypes.Ok));
                }

                return;
            }

            ClientJoinResult result = await channel.AddClient(client);

            if (message.ResponseRequired)
            {
                switch (result)
                {
                case ClientJoinResult.Success:
                    await client.SendAsync(MessageBuilder.ResponseStatus(message, KnownContentTypes.Ok));

                    break;

                case ClientJoinResult.Unauthorized:
                    await client.SendAsync(MessageBuilder.ResponseStatus(message, KnownContentTypes.Unauthorized));

                    break;

                case ClientJoinResult.Full:
                    await client.SendAsync(MessageBuilder.ResponseStatus(message, KnownContentTypes.LimitExceeded));

                    break;
                }
            }
        }
コード例 #2
0
        public async Task Handle(MqClient client, TmqMessage message)
        {
            //find channel and queue
            Channel channel = _server.FindChannel(message.Target);

            //if auto creation active, try to create channel
            if (channel == null && _server.Options.AutoChannelCreation)
            {
                channel = _server.FindOrCreateChannel(message.Target);
            }

            if (channel == null)
            {
                if (!string.IsNullOrEmpty(message.MessageId))
                {
                    await client.SendAsync(MessageBuilder.ResponseStatus(message, KnownContentTypes.NotFound));
                }
                return;
            }

            ChannelQueue queue = channel.FindQueue(message.ContentType);

            //if auto creation active, try to create queue
            if (queue == null && _server.Options.AutoQueueCreation)
            {
                queue = await channel.FindOrCreateQueue(message.ContentType);
            }

            if (queue == null)
            {
                if (!string.IsNullOrEmpty(message.MessageId))
                {
                    await client.SendAsync(MessageBuilder.ResponseStatus(message, KnownContentTypes.NotFound));
                }
                return;
            }

            //consumer is trying to pull from the queue
            //in false cases, we won't send any response, cuz client is pending only queue messages, not response messages
            if (message.Length == 0 && message.ResponseRequired)
            {
                await HandlePullRequest(client, message, channel, queue);
            }

            //message have a content, this is the real message from producer to the queue
            else
            {
                await HandlePush(client, message, queue);
            }
        }
コード例 #3
0
        public async Task Handle(MqClient client, TmqMessage message)
        {
            //priority has no role in ack message.
            //we are using priority for helping receiver type recognization for better performance
            if (message.HighPriority)
            {
                //target should be client
                MqClient target = _server.FindClient(message.Target);
                if (target != null)
                {
                    await target.SendAsync(message);

                    return;
                }
            }

            //find channel and queue
            Channel channel = _server.FindChannel(message.Target);

            if (channel == null)
            {
                //if high prio, dont try to find client again
                if (!message.HighPriority)
                {
                    //target should be client
                    MqClient target = _server.FindClient(message.Target);
                    if (target != null)
                    {
                        await target.SendAsync(message);
                    }
                }

                return;
            }

            ChannelQueue queue = channel.FindQueue(message.ContentType);

            if (queue == null)
            {
                return;
            }

            await queue.AcknowledgeDelivered(client, message);
        }