Esempio n. 1
0
        public UnsubscribeAction Connect(IInboundPipelineConfigurator configurator, object instance)
        {
            var consumer = instance as TConsumer;

            if (consumer == null)
            {
                throw new NullReferenceException("The consumer instance cannot be null.");
            }

            var correlatedConfigurator = new InboundCorrelatedMessageRouterConfigurator(configurator.Pipeline);

            CorrelatedMessageRouter <IConsumeContext <TMessage>, TMessage, TKey> router =
                correlatedConfigurator.FindOrCreate <TMessage, TKey>();

            var sink = new InstanceMessageSink <TMessage>(MultipleHandlerSelector.ForHandler(
                                                              HandlerSelector.ForHandler <TMessage>(consumer.Consume)));

            TKey correlationId = consumer.CorrelationId;

            UnsubscribeAction result = router.Connect(correlationId, sink);

            UnsubscribeAction remove = configurator.SubscribedTo <TMessage, TKey>(correlationId);

            return(() => result() && (router.SinkCount(correlationId) == 0) && remove());
        }
Esempio n. 2
0
        private static void AddHandlers(this IServiceCollection services)
        {
            var packetTypeToHandlersMap = new Dictionary <Type, Type>()
            {
                { typeof(IAttackInfo), typeof(AttackHandler) },
                { typeof(IAutoMovementInfo), typeof(AutoMoveHandler) },
                { typeof(IActionWithoutContentInfo), typeof(ActionWithoutContentHandler) },
                { typeof(IBytesInfo), typeof(DefaultRequestHandler) },
                { typeof(IFollowInfo), typeof(FollowHandler) },
                { typeof(IGameLogInInfo), typeof(GameLogInHandler) },
                { typeof(IGatewayLoginInfo), typeof(GatewayLogInHandler) },
                { typeof(ILookAtInfo), typeof(LookAtHandler) },
                { typeof(IModesInfo), typeof(ModesHandler) },
                { typeof(ISpeechInfo), typeof(SpeechHandler) },
                { typeof(ITurnOnDemandInfo), typeof(TurnOnDemandHandler) },
                { typeof(IWalkOnDemandInfo), typeof(WalkOnDemandHandler) },
            };

            foreach (var(packetType, type) in packetTypeToHandlersMap)
            {
                services.TryAddSingleton(type);
            }

            services.AddSingleton <IHandlerSelector>(s =>
            {
                var handlerSelector = new HandlerSelector(s.GetRequiredService <Microsoft.Extensions.Logging.ILogger <HandlerSelector> >());

                foreach (var(packetType, type) in packetTypeToHandlersMap)
                {
                    handlerSelector.RegisterForPacketType(packetType, s.GetRequiredService(type) as ITcpRequestHandler);
                }

                return(handlerSelector);
            });
        }
Esempio n. 3
0
        UnsubscribeAction IResponseHandler.Connect(IInboundPipelineConfigurator configurator)
        {
            var connector = new RequestHandlerSubscriptionConnector <TResponse>();

            HandlerSelector <TResponse> handler = HandlerSelector.ForContextHandler <TResponse>(HandleResponse);

            return(connector.Connect(configurator, _requestId, handler));
        }
Esempio n. 4
0
        public HandlerSubscriptionBuilder(HandlerSelector <TMessage> handler,
                                          ReferenceFactory referenceFactory)
        {
            _handler          = handler;
            _referenceFactory = referenceFactory;

            _connector = new HandlerSubscriptionConnector <TMessage>();
        }
Esempio n. 5
0
        public HandlerSubscriptionBuilder(HandlerSelector <TMessage> handler,
                                          Func <UnsubscribeAction, ISubscriptionReference> referenceFactory)
        {
            _handler          = handler;
            _referenceFactory = referenceFactory;

            _connector = new HandlerSubscriptionConnector <TMessage>();
        }
Esempio n. 6
0
        private static MessageRouter <IConsumeContext <PingMessage> > SetupRouterOnly()
        {
            _consumer = new PerformantConsumer <PingMessage>(1500000);

            var messageSink = new InstanceMessageSink <PingMessage>(MultipleHandlerSelector.ForHandler(
                                                                        HandlerSelector.ForHandler <PingMessage>(_consumer.Consume)));

            var router = new MessageRouter <IConsumeContext <PingMessage> >();

            router.Connect(messageSink);
            return(router);
        }
        public UnsubscribeAction Connect(IInboundPipelineConfigurator configurator, HandlerSelector <TMessage> handler)
        {
            var routerConfigurator = new InboundMessageRouterConfigurator(configurator.Pipeline);

            MessageRouter <IConsumeContext <TMessage> > router = routerConfigurator.FindOrCreate <TMessage>();

            var sink = new InstanceMessageSink <TMessage>(MultipleHandlerSelector.ForHandler(handler));

            UnsubscribeAction result = router.Connect(sink);

            UnsubscribeAction remove = configurator.SubscribedTo <TMessage>();

            return(() => result() && (router.SinkCount == 0) && remove());
        }
Esempio n. 8
0
        public UnsubscribeAction Connect(IInboundPipelineConfigurator configurator, object instance)
        {
            var consumer = instance as TConsumer;

            if (consumer == null)
            {
                throw new NullReferenceException("The consumer instance cannot be null.");
            }

            var sink = new InstanceMessageSink <TMessage>(MultipleHandlerSelector.ForHandler(
                                                              HandlerSelector.ForSelectiveHandler <TMessage>(consumer.Accept, consumer.Consume)));

            return(configurator.Pipeline.ConnectToRouter(sink, () => configurator.SubscribedTo <TMessage>()));
        }
        public UnsubscribeAction Connect(IInboundPipelineConfigurator configurator, object instance)
        {
            var worker = instance as IWorker <TMessage>;

            if (worker == null)
            {
                throw new ConfigurationException("The instance is not a distributor worker");
            }

            var sink = new WorkerMessageSink <Distributed <TMessage> >(MultipleHandlerSelector.ForHandler(
                                                                           HandlerSelector.ForSelectiveHandler <Distributed <TMessage> >(worker.Accept, worker.Consume)));

            return(configurator.Pipeline.ConnectToRouter(sink, () => configurator.SubscribedTo <Distributed <TMessage> >()));
        }
Esempio n. 10
0
        public void How_many_messages_can_the_pipe_send_per_second()
        {
            long count  = 0;
            long count2 = 0;
            long limit  = 2500000;

            var messageSink = new InstanceMessageSink <ClaimModified>(MultipleHandlerSelector.ForHandler(
                                                                          HandlerSelector.ForHandler <ClaimModified>(m => { count++; })));

            var messageSink2 = new InstanceMessageSink <ClaimModified>(MultipleHandlerSelector.ForHandler(
                                                                           HandlerSelector.ForHandler <ClaimModified>(m => { count2++; })));

            var router = new MessageRouter <IConsumeContext <ClaimModified> >();

            router.Connect(messageSink);
            router.Connect(messageSink2);


            var translater   = new InboundConvertMessageSink <ClaimModified>(router);
            var objectRouter = new MessageRouter <IConsumeContext>();

            objectRouter.Connect(translater);
            var pipeline = new InboundMessagePipeline(objectRouter, MockRepository.GenerateMock <IInboundPipelineConfigurator>());

            var message = new ClaimModified();
            var context = new ConsumeContext <ClaimModified>(ReceiveContext.Empty(), message);

            for (int i = 0; i < 100; i++)
            {
                pipeline.Dispatch(context);
            }

            count  = 0;
            count2 = 0;

            Stopwatch timer = Stopwatch.StartNew();

            for (int i = 0; i < limit; i++)
            {
                pipeline.Dispatch(context);
            }

            timer.Stop();

            Trace.WriteLine("Received: " + (count + count2) + ", expected " + limit * 2);
            Trace.WriteLine("Elapsed Time: " + timer.ElapsedMilliseconds + "ms");
            Trace.WriteLine("Messages Per Second: " + limit * 1000 / timer.ElapsedMilliseconds);
        }
Esempio n. 11
0
        public ServiceBusSubscription(IServiceBus bus, IObserver <T> observer, Predicate <T> condition)
        {
            _observer = Observer.Synchronize(observer);

            _unsubscribeAction = bus.SubscribeHandlerSelector(HandlerSelector.ForSelectiveHandler(condition, m =>
            {
                try
                {
                    observer.OnNext(m);
                }
                catch (Exception ex)
                {
                    observer.OnError(ex);
                }
            }));
        }
Esempio n. 12
0
        private static MessageRouter <IConsumeContext> SetupTwoRoutersOnly()
        {
            _consumer = new PerformantConsumer <PingMessage>(1500000);

            var messageSink = new InstanceMessageSink <PingMessage>(MultipleHandlerSelector.ForHandler(
                                                                        HandlerSelector.ForHandler <PingMessage>(_consumer.Consume)));

            var router = new MessageRouter <IConsumeContext <PingMessage> >();

            router.Connect(messageSink);

            var translater = new InboundConvertMessageSink <PingMessage>(router);

            var nextRouter = new MessageRouter <IConsumeContext>();

            nextRouter.Connect(translater);

            return(nextRouter);
        }
        public UnsubscribeAction Connect(IInboundPipelineConfigurator configurator, string requestId,
                                         HandlerSelector <TMessage> handler)
        {
            if (handler == null)
            {
                throw new ArgumentNullException("handler", "The handler cannot be null.");
            }

            var correlatedConfigurator = new InboundRequestMessageRouterConfigurator(configurator.Pipeline);

            RequestMessageRouter <IConsumeContext <TMessage>, TMessage> router =
                correlatedConfigurator.FindOrCreate <TMessage>();

            var sink = new InstanceMessageSink <TMessage>(MultipleHandlerSelector.ForHandler(handler));

            UnsubscribeAction result = router.Connect(requestId, sink);

            return(() => result() && (router.SinkCount(requestId) == 0)); // && remove();
        }
        public void Handle <TResponse>(Action <TResponse> handler)
            where TResponse : class
        {
            var connector = new RequestHandlerSubscriptionConnector <TResponse>();

            Action <TResponse> responseHandler = message =>
            {
                try
                {
                    handler(message);

                    _request.Complete(message);
                }
                catch (Exception ex)
                {
                    var exception = new RequestException("The response handler threw an exception", ex, message);
                    _request.Fail(exception);
                }
            };

            _handlers.Add(
                x => { return(connector.Connect(x, _requestId, HandlerSelector.ForHandler(responseHandler))); });
        }
 public HandlerWorkerConnector(HandlerSelector <TMessage> handler,
                               ReferenceFactory referenceFactory)
 {
     _handler          = handler;
     _referenceFactory = referenceFactory;
 }
Esempio n. 16
0
        public UnsubscribeAction Connect(IInboundPipelineConfigurator configurator, string requestId, HandlerSelector <TMessage> handler)
        {
            if (handler == null)
            {
                throw new NullReferenceException("The handler cannot be null.");
            }

            var correlatedConfigurator = new InboundRequestMessageRouterConfigurator(configurator.Pipeline);

            RequestMessageRouter <IConsumeContext <TMessage>, TMessage> router =
                correlatedConfigurator.FindOrCreate <TMessage>();

            var sink = new InstanceMessageSink <TMessage>(MultipleHandlerSelector.ForHandler(handler));

            UnsubscribeAction result = router.Connect(requestId, sink);

            // TODO this should NOT publish a correlated subscription, in fact, do we even need to subscribe?
            //UnsubscribeAction remove = configurator.SubscribedTo<TMessage>();

            return(() => result() && (router.SinkCount(requestId) == 0));// && remove();
        }
Esempio n. 17
0
 protected ResponseHandlerBase(string requestId, Action <TResponse> handler)
     : this(requestId)
 {
     _handler = HandlerSelector.ForHandler(handler);
 }
        /// <summary>
        /// Adds a message handler to the service bus for handling a specific type of message
        /// </summary>
        /// <typeparam name="T">The message type to handle, often inferred from the callback specified</typeparam>
        /// <param name="bus"></param>
        /// <param name="handler">The callback to invoke when messages of the specified type arrive on the service bus</param>
        public static UnsubscribeAction SubscribeHandlerSelector <T>(this IServiceBus bus, HandlerSelector <T> handler)
            where T : class
        {
            var connector = new HandlerSubscriptionConnector <T>();

            return(bus.Configure(x => connector.Connect(x, handler)));
        }
 /// <summary>
 /// Adds a message handler to the service bus for handling a specific type of message
 /// </summary>
 /// <typeparam name="T">The message type to handle, often inferred from the callback specified</typeparam>
 /// <param name="bus"></param>
 /// <param name="handler">The callback to invoke when messages of the specified type arrive on the service bus</param>
 /// <param name="condition"></param>
 public static UnsubscribeAction SubscribeHandler <T>(this IServiceBus bus, Action <T> handler, Predicate <T> condition)
     where T : class
 {
     return(SubscribeHandlerSelector(bus, HandlerSelector.ForSelectiveHandler(condition, handler)));
 }
 /// <summary>
 /// Adds a message handler to the service bus for handling a specific type of message
 /// </summary>
 /// <typeparam name="T">The message type to handle, often inferred from the callback specified</typeparam>
 /// <param name="bus"></param>
 /// <param name="handler">The callback to invoke when messages of the specified type arrive on the service bus</param>
 public static UnsubscribeAction SubscribeContextHandler <T>(this IServiceBus bus, Action <IConsumeContext <T> > handler)
     where T : class
 {
     return(SubscribeHandlerSelector(bus, HandlerSelector.ForContextHandler(handler)));
 }
Esempio n. 21
0
        public HandlerSubscriptionConfigurator <TMessage> Where(Predicate <TMessage> condition)
        {
            _handler = HandlerSelector.ForCondition(_handler, condition);

            return(this);
        }
Esempio n. 22
0
 public HandlerSubscriptionConfiguratorImpl(Action <IConsumeContext <TMessage>, TMessage> handler)
 {
     _handler = x => context => handler(context, context.Message);
 }
Esempio n. 23
0
 public HandlerSubscriptionConfiguratorImpl(Action <TMessage> handler)
 {
     _handler = HandlerSelector.ForHandler(handler);
 }
Esempio n. 24
0
 protected ResponseHandlerBase(string requestId, SynchronizationContext synchronizationContext,
                               Action <TResponse> handler)
     : this(requestId, synchronizationContext)
 {
     _handler = HandlerSelector.ForHandler(handler);
 }
Esempio n. 25
0
 public HandlerWorkerConfiguratorImpl(Action <TMessage> handler)
 {
     _handler = HandlerSelector.ForHandler(handler);
 }