/// <summary>
        /// Processes the client message which has single receiver (message by unique id)
        /// </summary>
        private async Task ProcessSingleReceiverClientMessage(MqClient client, HorseMessage message)
        {
            //find the receiver
            MqClient other = _server.FindClient(message.Target);

            if (other == null)
            {
                await client.SendAsync(message.CreateResponse(HorseResultCode.NotFound));

                return;
            }

            //check sending message authority
            foreach (IClientAuthorization authorization in _server.Authorizations)
            {
                bool grant = await authorization.CanDirectMessage(client, message, other);

                if (!grant)
                {
                    await client.SendAsync(message.CreateResponse(HorseResultCode.Unauthorized));

                    return;
                }
            }

            //send the message
            await other.SendAsync(message);
        }
        public async Task Handle(MqClient sender, HorseMessage message, bool fromNode)
        {
            //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 queue
            HorseQueue queue = _server.FindQueue(message.Target);

            if (queue != null)
            {
                await queue.AcknowledgeDelivered(sender, message);

                return;
            }

            //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);
                }
            }
        }
Example #3
0
        /// <summary>
        /// Called when a new client is connected via HMQ protocol
        /// </summary>
        public async Task <HorseServerSocket> Connected(IHorseServer server, IConnectionInfo connection, ConnectionData data)
        {
            string clientId;
            bool   found = data.Properties.TryGetValue(HorseHeaders.CLIENT_ID, out clientId);

            if (!found || string.IsNullOrEmpty(clientId))
            {
                clientId = _server.ClientIdGenerator.Create();
            }

            //if another client with same unique id is online, do not accept new client
            MqClient foundClient = _server.FindClient(clientId);

            if (foundClient != null)
            {
                await connection.Socket.SendAsync(HmqWriter.Create(MessageBuilder.Busy()));

                return(null);
            }

            if (_server.Options.ClientLimit > 0 && _server.GetOnlineClients() >= _server.Options.ClientLimit)
            {
                return(null);
            }

            //creates new mq client object
            MqClient client = new MqClient(_server, connection, _server.MessageIdGenerator, _server.Options.UseMessageId);

            client.Data     = data;
            client.UniqueId = clientId.Trim();
            client.Token    = data.Properties.GetStringValue(HorseHeaders.CLIENT_TOKEN);
            client.Name     = data.Properties.GetStringValue(HorseHeaders.CLIENT_NAME);
            client.Type     = data.Properties.GetStringValue(HorseHeaders.CLIENT_TYPE);

            //authenticates client
            foreach (IClientAuthenticator authenticator in _server.Authenticators)
            {
                client.IsAuthenticated = await authenticator.Authenticate(_server, client);

                if (!client.IsAuthenticated)
                {
                    await client.SendAsync(MessageBuilder.Unauthorized());

                    return(null);
                }
            }

            //client authenticated, add it into the connected clients list
            _server.AddClient(client);

            //send response message to the client, client should check unique id,
            //if client's unique id isn't permitted, server will create new id for client and send it as response
            await client.SendAsync(MessageBuilder.Accepted(client.UniqueId));

            foreach (IClientHandler handler in _server.ClientHandlers)
            {
                await handler.Connected(_server, client);
            }

            return(client);
        }