public async Task ShouldRegisterAllMethodsMarkedWithEventHandlerAttribute()
            {
                var attributedHandler1 = new TestAttributedEventHandler(_outputHelper);
                var attributedHandler2 = new TestAttributedEventHandler(_outputHelper);
                var attributedHandler3 = new TestAttributedEventHandler(_outputHelper);

                var registration = new MultiMessageHandlerRegistration();

                registration.RegisterEventHandlersByAttribute(() => attributedHandler1);
                registration.RegisterEventHandlersByAttribute(EventHandlerAttributeMethod.FromType <TestAttributedEventHandler>(() => attributedHandler2));
                registration.RegisterEventHandlersByAttribute(EventHandlerAttributeMethod.FromType(typeof(TestAttributedEventHandler), () => attributedHandler3));

                IMessageHandlerResolver resolver = registration.BuildMessageHandlerResolver();

                MessageHandlerDelegate eventHandlerDelegate = resolver.ResolveMessageHandler(typeof(TestEvent1));

                // Get all methods marked with [EventHandler] and receiving TestEvent as parameter.
                int eventHandler1MethodCount = TestAttributedEventHandler.GetEventHandlerAttributeCountFor <TestEvent1>();
                int eventHandler2MethodCount = TestAttributedEventHandler.GetEventHandlerAttributeCountFor <TestEvent1>();
                int eventHandler3MethodCount = TestAttributedEventHandler.GetEventHandlerAttributeCountFor <TestEvent1>();

                await eventHandlerDelegate.Invoke(new TestEvent1());

                int totalEventHandlerMethodCount = eventHandler1MethodCount + eventHandler2MethodCount + eventHandler3MethodCount;
                int totalEventsHandledCount      = attributedHandler1.HandledEvents.Count + attributedHandler2.HandledEvents.Count + attributedHandler3.HandledEvents.Count;

                totalEventsHandledCount.Should().Be(totalEventHandlerMethodCount);
            }
예제 #2
0
            public async Task ShouldRegisterAllEventHandlers()
            {
                // 6 handlers.
                var asyncHandler1 = new TestEventHandler(_outputHelper);
                var asyncHandler2 = new TestEventHandler(_outputHelper);
                var asyncHandler3 = new TestEventHandler(_outputHelper);
                var handler1      = new TestEventHandler(_outputHelper);
                var handler2      = new TestEventHandler(_outputHelper);
                var handler3      = new TestEventHandler(_outputHelper);

                var registration = new MultiMessageHandlerRegistration();

                registration.RegisterEventHandler <TestEvent1>(() => asyncHandler1);
                registration.RegisterEventHandler <TestEvent1>(() => asyncHandler2);
                registration.RegisterEventHandler <TestEvent1>(() => asyncHandler3);
                registration.RegisterEventHandler <TestEvent1>(() => handler1.AsEventSyncHandler <TestEvent1>());
                registration.RegisterEventHandler <TestEvent1>(() => handler2.AsEventSyncHandler <TestEvent1>());
                registration.RegisterEventHandler <TestEvent1>(() => handler3.AsEventSyncHandler <TestEvent1>());

                IMessageHandlerResolver resolver = registration.BuildMessageHandlerResolver();

                MessageHandlerDelegate eventHandlerDelegate = resolver.ResolveMessageHandler(typeof(TestEvent1));

                await eventHandlerDelegate.Invoke(new TestEvent1());

                int totalEventsHandledCount = asyncHandler1.HandledEvents.Count +
                                              asyncHandler2.HandledEvents.Count +
                                              asyncHandler3.HandledEvents.Count +
                                              handler1.HandledEvents.Count +
                                              handler2.HandledEvents.Count +
                                              handler3.HandledEvents.Count;

                totalEventsHandledCount.Should().Be(6);
            }
            public void ShouldResolveTheFirstAvaibleMessageHandlerFromListOfResolvers()
            {
                // Given
                int expectedNumberOfHandlers = 3;
                int actualNumberOfHandlers   = 0;

                IMessageHandlerResolver multiResolver = CreateMultiMessageHandlerResolver(multiRegistration =>
                {
                    // These should be invoked.
                    multiRegistration.Register <TestMessage>((message, ct) => { actualNumberOfHandlers++; return(Task.CompletedTask); });
                    multiRegistration.Register <TestMessage>((message, ct) => { actualNumberOfHandlers++; return(Task.CompletedTask); });
                    multiRegistration.Register <TestMessage>((message, ct) => { actualNumberOfHandlers++; return(Task.CompletedTask); });
                });

                // This should not be invoked.
                IMessageHandlerResolver singleResolver = CreateSingleMessageHandlerResolver(singleRegistration =>
                {
                    singleRegistration.Register <TestMessage>((message, ct) => { actualNumberOfHandlers++; return(Task.CompletedTask); });
                });

                CompositeMessageHandlerResolver compositeResolver = CreateCompositeMessageHandlerResolver(multiResolver, singleResolver);

                // When
                MessageHandlerDelegate handler = compositeResolver.ResolveMessageHandler(typeof(TestMessage));

                handler.Invoke(new TestMessage()); // Should invode the handlers registered in multi registration

                // Then
                actualNumberOfHandlers.ShouldBeEquivalentTo(expectedNumberOfHandlers);
            }
예제 #4
0
        protected void HandlePacket(ClientGamePacket packet)
        {
            IReadable message = MessageManager.GetMessage(packet.Opcode);

            if (message == null)
            {
                log.Warn($"Received unknown packet {packet.Opcode:X}");
                return;
            }

            MessageHandlerDelegate handlerInfo = MessageManager.GetMessageHandler(packet.Opcode);

            if (handlerInfo == null)
            {
                log.Warn($"Received unhandled packet {packet.Opcode}(0x{packet.Opcode:X}).");
                return;
            }

            if (packet.Opcode != GameMessageOpcode.ClientEncrypted &&
                packet.Opcode != GameMessageOpcode.ClientPacked &&
                packet.Opcode != GameMessageOpcode.ClientPackedWorld &&
                packet.Opcode != GameMessageOpcode.ClientEntityCommand)
            {
                log.Trace($"Received packet {packet.Opcode}(0x{packet.Opcode:X}).");
            }

            // FIXME workaround for now. possible performance impact.
            // ClientPing does not currently work and the session times out after 300s -> this keeps the session alive if -any- client packet is received
            Heartbeat.OnHeartbeat();

            using (var stream = new MemoryStream(packet.Data))
                using (var reader = new GamePacketReader(stream))
                {
                    message.Read(reader);
                    if (reader.BytesRemaining > 0)
                    {
                        log.Warn($"Failed to read entire contents of packet {packet.Opcode}");
                    }

                    try
                    {
                        handlerInfo.Invoke(this, message);
                    }
                    catch (InvalidPacketValueException exception)
                    {
                        log.Error(exception);
                        RequestedDisconnect = true;
                    }
                    catch (Exception exception)
                    {
                        log.Error(exception);
                    }
                }
        }
예제 #5
0
        private void HandlePacket(ClientGamePacket packet)
        {
            IReadable message = MessageManager.GetMessage(packet.Opcode);

            if (message == null)
            {
                log.Warn($"Received unknown packet {packet.Opcode:X}");
                return;
            }

            MessageHandlerDelegate handlerInfo = MessageManager.GetMessageHandler(packet.Opcode);

            if (handlerInfo == null)
            {
                log.Warn($"Received unhandled packet {packet.Opcode}(0x{packet.Opcode:X}).");
                return;
            }

            if (packet.Opcode != GameMessageOpcode.ClientEncrypted &&
                packet.Opcode != GameMessageOpcode.ClientPacked &&
                packet.Opcode != GameMessageOpcode.ClientEntityCommand)
            {
                log.Trace($"Received packet {packet.Opcode}(0x{packet.Opcode:X}).");
            }

            using (var stream = new MemoryStream(packet.Data))
                using (var reader = new GamePacketReader(stream))
                {
                    message.Read(reader);
                    if (reader.BytesRemaining > 0)
                    {
                        log.Warn($"Failed to read entire contents of packet {packet.Opcode}");
                    }

                    try
                    {
                        handlerInfo.Invoke(this, message);
                    }
                    catch (InvalidPacketValueException exception)
                    {
                        log.Error(exception);
                        OnDisconnect();
                    }
                    catch (Exception exception)
                    {
                        log.Error(exception);
                    }
                }
        }
        public void ShouldThrowIfUnexpectedMessageTypeIsPassedToTheSingleHandlerDelegate()
        {
            // Given
            IMessageHandlerResolver resolver = CreateSingleMessageHandlerResolver(registration =>
            {
                registration.Register <TestMessage>((message, ct) => Task.CompletedTask);
            });

            // When
            MessageHandlerDelegate handler = resolver.ResolveMessageHandler(typeof(TestMessage));

            // Expected TestMessage but was given a TestInvalidMessage.
            Action action = () => handler.Invoke(new TestInvalidMessage());

            // Then
            action.ShouldThrow <ArgumentException>("because MessageHandlerDelegate expects a TestMessage but was given a TestInvalidMessage.");
        }
예제 #7
0
            public void ShouldResolveAsyncCommandHandlerFromContainer()
            {
                var commandHandler = new TestCommandHandler(_outputHelper);

                IMessageHandlerResolver resolver = CreateContainerCommandAsyncHandlerResolver(container =>
                {
                    container.RegisterSingleton <ICommandAsyncHandler <TestCommand> >(commandHandler);
                });

                MessageHandlerDelegate commandHandlerDelegate = resolver.ResolveMessageHandler(typeof(TestCommand));

                // Delegate should invoke the actual command handler - TestCommandHandler.
                commandHandlerDelegate.Invoke(new TestCommand());

                commandHandler.HandledCommands.Should().HaveCount(1);
                commandHandler.HasHandledCommand <TestCommand>().Should().BeTrue();
            }
        /// <summary>
        /// Send message to a delegate with one/many handlers.
        /// </summary>
        /// <typeparam name="TMessage">Type of message.</typeparam>
        /// <param name="message">Message to send.</param>
        /// <param name="cancellationToken">Optional cancellation token to be passed to handlers.</param>
        /// <returns>Asynchronous task which can be awaited for completion.</returns>
        public Task SendAsync <TMessage>(TMessage message, CancellationToken cancellationToken = default(CancellationToken)) where TMessage : class
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            Type messageType = message.GetType();

            MessageHandlerDelegate messageHandler = _messageHandlerResolver.ResolveMessageHandler(messageType);

            if (messageHandler == null)
            {
                throw NoMessageHandlerResolvedException.WithMessageType(messageType);
            }

            return(messageHandler.Invoke(message, cancellationToken));
        }
        public void ShouldThrowIfNullIsPassedToTheMultiHandlerDelegate()
        {
            // Given
            IMessageHandlerResolver resolver = CreateMultiMessageHandlerResolver(registration =>
            {
                registration.Register <TestMessage>((message, ct) => Task.CompletedTask);
                registration.Register <TestMessage>((message, ct) => Task.CompletedTask);
                registration.Register <TestMessage>((message, ct) => Task.CompletedTask);
            });

            // When
            MessageHandlerDelegate handler = resolver.ResolveMessageHandler(typeof(TestMessage));

            // Expected TestMessage but was given a TestInvalidMessage.
            Action action = () => handler.Invoke(null);

            // Then
            action.ShouldThrow <ArgumentNullException>("because null was passed-in.");
        }
예제 #10
0
            public void ShouldRegisterCommandAsyncHandler()
            {
                var commandHandler = new TestCommandHandler(_outputHelper);

                var registration = new SingleMessageHandlerRegistration();

                registration.RegisterCommandHandler(() => commandHandler.AsCommandAsyncHandler <TestCommand>());

                IMessageHandlerResolver resolver = registration.BuildMessageHandlerResolver();

                MessageHandlerDelegate commandHandlerDelegate = resolver.ResolveMessageHandler(typeof(TestCommand));

                commandHandlerDelegate.Should().NotBeNull();

                // Delegate should invoke the actual command handler - TestCommandHandler.
                commandHandlerDelegate.Invoke(new TestCommand());

                commandHandler.HandledCommands.Should().HaveCount(1);
                commandHandler.HasHandledCommand <TestCommand>().Should().BeTrue();
            }
            public async Task ShouldRegisterAllMethodsOfTypeThatIsMarkedWithCommandHandlerAttribute()
            {
                var commandHandler = new TestAttributedCommandHandler(_outputHelper);

                var registration = new SingleMessageHandlerRegistration();

                registration.RegisterCommandHandlersByAttribute(() => commandHandler);

                IMessageHandlerResolver resolver = registration.BuildMessageHandlerResolver();

                MessageHandlerDelegate commandHandlerDelegate = resolver.ResolveMessageHandler(typeof(TestCommand));

                commandHandlerDelegate.Should().NotBeNull();

                // Delegate should invoke the actual command handler - TestAttributedCommandHandler.
                await commandHandlerDelegate.Invoke(new TestCommand());

                commandHandler.HandledCommands.Should().HaveCount(1);
                commandHandler.HasHandledCommand <TestCommand>().Should().BeTrue();
            }
예제 #12
0
            public void ShouldResolveCommandHandlerFromCompositeResolver()
            {
                var commandHandler = new TestCommandHandler(_outputHelper);
                var container      = new Container();

                container.RegisterSingleton <ICommandHandler <TestCommand> >(commandHandler);
                container.RegisterSingleton <ICommandAsyncHandler <CancellableTestCommand> >(commandHandler);

                // Exception handler will log and ignore exception.
                Func <Exception, bool> exceptionHandler = (ex) =>
                {
                    if (ex != null)
                    {
                        _outputHelper.WriteLine($"Ignoring encountered exception while trying to resolve command handler: {ex.Message}");

                        // Notify as handled if no command handler is resolved from other resolvers.
                        return(true);
                    }

                    return(false);
                };

                var containerAdapter = new SimpleInjectorContainerAdapter(container);
                var containerAsyncHandlerResolver = new ContainerCommandAsyncHandlerResolver(containerAdapter, exceptionHandler);
                var containerHandlerResolver      = new ContainerCommandHandlerResolver(containerAdapter, exceptionHandler);

                CompositeMessageHandlerResolver compositeResolver = CompositeMessageHandlerResolver.Compose(
                    containerAsyncHandlerResolver,
                    containerHandlerResolver);

                MessageHandlerDelegate testCommandHandlerDelegate            = compositeResolver.ResolveMessageHandler(typeof(TestCommand));
                MessageHandlerDelegate cancellableTestCommandHandlerDelegate = compositeResolver.ResolveMessageHandler(typeof(CancellableTestCommand));

                testCommandHandlerDelegate?.Invoke(new TestCommand());
                cancellableTestCommandHandlerDelegate?.Invoke(new CancellableTestCommand());

                commandHandler.HandledCommands.Should().HaveCount(2);
                commandHandler.HasHandledCommand <TestCommand>().Should().BeTrue();
                commandHandler.HasHandledCommand <CancellableTestCommand>().Should().BeTrue();
            }
예제 #13
0
        /// <summary>
        /// This handles incoming messages and delegates them to registered message handlers.
        /// </summary>
        /// <param name="msg">The incoming message.</param>
        override protected void HandleMessage(string msg)
        {
#if TEST_MODE
            Console.WriteLine("HandleMessage = " + msg);

            //using (StreamWriter sw = File.AppendText("D:\\muse_message_log.txt" ))
            //{
            //    var timestamp = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds();
            //    sw.WriteLine(string.Format("{0} {1}", timestamp, msg));
            //}
#endif

            Message message = JsonUtils.DeserializeObject <Message>(msg);

            if (message == null)
            {
                Console.Error.WriteLine("The received message can not be processed.");
            }

            MessageHandlerDelegate <Message> messageHandler = DefaultMessageHandler;

            if (MessageHandler.ContainsKey(message._type))
            {
                Type messageType = MessageHandlerType[message._type];

                object obj = Activator.CreateInstance(messageType);

                MethodInfo methodInfo = messageType.GetMethod("AsMessage");

                methodInfo = methodInfo.MakeGenericMethod(messageType);

                message = (Message)methodInfo.Invoke(obj, new object[] { msg });

                messageHandler = MessageHandler[message._type];
            }

            messageHandler?.Invoke(server, this, Session, message);
        }
            public async Task ShouldRegisterAllCommandHandlerAttributeMethods()
            {
                var commandHandler = new TestAttributedCommandHandler(_outputHelper);

                // Get methods marked with [CommandHandler] attribute.
                IEnumerable <CommandHandlerAttributeMethod> methods = CommandHandlerAttributeMethod.FromType(() => commandHandler);

                var registration = new SingleMessageHandlerRegistration();

                registration.RegisterCommandHandlersByAttribute(methods);

                IMessageHandlerResolver resolver = registration.BuildMessageHandlerResolver();

                MessageHandlerDelegate commandHandlerDelegate = resolver.ResolveMessageHandler(typeof(TestCommand));

                commandHandlerDelegate.Should().NotBeNull();

                // Delegate should invoke the actual command handler - TestAttributedCommandHandler.
                await commandHandlerDelegate.Invoke(new TestCommand());

                commandHandler.HandledCommands.Should().HaveCount(1);
                commandHandler.HasHandledCommand <TestCommand>().Should().BeTrue();
            }
            public async Task ShouldResolverAllEventHandlersFromContainer()
            {
                Assembly assembly = typeof(TestEvent1).Assembly;

                // Register all event handlers.
                var containerWithEventHandlers = new Container();

                containerWithEventHandlers.Register(() => _outputHelper);
                containerWithEventHandlers.RegisterCollection(typeof(IEventAsyncHandler <>), assembly);
                containerWithEventHandlers.RegisterCollection(typeof(IEventHandler <>), assembly);

                // Get all event handlers from above container and register in this new container as singletons.
                // Register event handlers as singletons so that we can verify HandledEvents property.
                var container = new Container();

                container.Register(() => _outputHelper);
                container.RegisterCollection <IEventAsyncHandler <TestEvent1> >(containerWithEventHandlers.GetAllInstances <IEventAsyncHandler <TestEvent1> >().ToList());
                container.RegisterCollection <IEventHandler <TestEvent1> >(containerWithEventHandlers.GetAllInstances <IEventHandler <TestEvent1> >().ToList());

                var eventHandlerResolver = new ContainerEventHandlerResolver(new SimpleInjectorContainerAdapter(container));

                MessageHandlerDelegate eventHandlerDelegate = eventHandlerResolver.ResolveMessageHandler(typeof(TestEvent1));

                await eventHandlerDelegate.Invoke(new TestEvent1());

                // Get all handlers in assembly.
                var eventAsyncHandlers = container.GetAllInstances <IEventAsyncHandler <TestEvent1> >().Cast <TestEventHandler>().ToList();
                var eventSyncHandlers  = container.GetAllInstances <IEventHandler <TestEvent1> >().Cast <TestEventHandler>().ToList();

                int totalEventHandlerCount = eventAsyncHandlers.Count + eventSyncHandlers.Count;

                int totalEventsHandledCount = eventAsyncHandlers.Sum(e => e.HandledEvents.Count) +
                                              eventSyncHandlers.Sum(e => e.HandledEvents.Count);

                // Important Note: This should equal all handlers in this assembly.
                totalEventHandlerCount.Should().Be(totalEventsHandledCount);
            }