Пример #1
0
        /// <summary>
        /// Sends a request to target with a JSON model, waits response
        /// </summary>
        public async Task <HorseResult <TResponse> > RequestJson <TResponse>(string target, ushort?contentType, object model,
                                                                             IEnumerable <KeyValuePair <string, string> > messageHeaders = null)
        {
            TypeDeliveryDescriptor descriptor = _client.DeliveryContainer.GetDescriptor(model.GetType());
            HorseMessage           message    = descriptor.CreateMessage(MessageType.DirectMessage, target, contentType);

            message.Serialize(model, _client.JsonSerializer);

            if (messageHeaders != null)
            {
                foreach (KeyValuePair <string, string> pair in messageHeaders)
                {
                    message.AddHeader(pair.Key, pair.Value);
                }
            }

            HorseMessage responseMessage = await _client.Request(message);

            if (responseMessage.ContentType == 0)
            {
                TResponse response = responseMessage.Deserialize <TResponse>(_client.JsonSerializer);
                return(new HorseResult <TResponse>(response, message, HorseResultCode.Ok));
            }

            return(new HorseResult <TResponse>(default, responseMessage, (HorseResultCode)responseMessage.ContentType));
Пример #2
0
        internal Task TriggerEvents(HorseClient client, HorseMessage message)
        {
            string eventName = message.Source;
            string queue     = message.Target;

            List <EventDescriptor> list = _descriptors[eventName];
            EventDescriptor        descriptor;

            lock (list)
                descriptor = list.FirstOrDefault(x => x.Queue == queue);

            if (descriptor == null)
            {
                return(Task.CompletedTask);
            }

            List <Delegate> methods;

            lock (descriptor.Methods)
                methods = new List <Delegate>(descriptor.Methods);

            object param = message.Deserialize(descriptor.ParameterType, client.JsonSerializer);

            foreach (var m in methods)
            {
                m.DynamicInvoke(param);
            }

            return(Task.CompletedTask);
        }
Пример #3
0
        /// <summary>
        /// Creates new binding for a router
        /// </summary>
        private async Task CreateRouterBinding(MqClient client, HorseMessage message)
        {
            IRouter router = _server.FindRouter(message.Target);

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

                return;
            }

            BindingInformation info = message.Deserialize <BindingInformation>(new NewtonsoftContentSerializer());

            //check create queue access
            foreach (IClientAuthorization authorization in _server.Authorizations)
            {
                bool grant = await authorization.CanCreateBinding(client, router, info);

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

                    return;
                }
            }

            switch (info.BindingType)
            {
            case BindingType.Direct:
                router.AddBinding(new DirectBinding(info.Name, info.Target, info.ContentType, info.Priority, info.Interaction, info.Method));
                break;

            case BindingType.Queue:
                router.AddBinding(new QueueBinding(info.Name, info.Target, info.Priority, info.Interaction));
                break;

            case BindingType.Http:
                router.AddBinding(new HttpBinding(info.Name, info.Target, (HttpBindingMethod)(info.ContentType ?? 0), info.Priority, info.Interaction));
                break;

            case BindingType.Topic:
                router.AddBinding(new TopicBinding(info.Name, info.Target, info.ContentType ?? 0, info.Priority, info.Interaction, info.Method));
                break;
            }

            await client.SendAsync(message.CreateResponse(HorseResultCode.Ok));
        }