Пример #1
0
        public async Task SendMessageOneWay_FromHost_1000x()
        {
            TestMessageProcessor.Count = 0;

            using (var listener = new MessageListener(registry)) {
                using (var client = new MessageClient(registry)) {
                    listener.ConnectionReceived += (sender, e) => {
                        for (var i = 0; i < 1000; i++)
                        {
                            e.Host.SendOneWay(new TestRequestOneWayMessage());
                        }

                        e.Host.Stop();
                    };

                    listener.Listen(IPAddress.Loopback, 10934);
                    await client.ConnectAsync("localhost", 10934, CancellationToken.None);

                    await Task.Delay(200);

                    client.Disconnect();
                }

                Assert.That(TestMessageProcessor.Count, Is.EqualTo(1_000));

                listener.Stop();
            }
        }
Пример #2
0
        public void SendMessage_SingleMessageIsSentSuccessfully_ReceivedMessageIsCorrect()
        {
            var port            = 15000;
            var messageListener = new MessageListener(port);

            messageListener.Listen();

            var server = new StreamingServer(port);

            server.SendMessage("Hello");
            Thread.Sleep(2000);

            var receivedMessages = messageListener.ReceivedMessages;

            receivedMessages.Should().BeEquivalentTo("Hello");
        }
Пример #3
0
        /// <summary>
        /// Raised when a message is received.
        /// <para>
        /// Listens to all messages with <see cref="MessageListener"/> and determines if messages are commands.
        /// </para>
        /// </summary>
        /// <param name="messageParam">The message recieved.</param>
        /// <returns>No object or value is returned by this method when it completes.</returns>
        private async Task MessageReceivedEventHandler(SocketMessage messageParam)
        {
            // Ignores system messages.
            if (!(messageParam is SocketUserMessage message))
            {
                return;
            }

            var argPos = 0; // Integer used to track where the prefix ends and the command begins.

            // Determines if the message is a command based on if it starts with the prefix character or a mention prefix.
            if (message.HasCharPrefix(COMMAND_PREFIX, ref argPos) || message.HasMentionPrefix(_client.CurrentUser, ref argPos))
            {
                await ProcessCommandAsync(message, argPos);
            }

            Task _ = _messageListener.Listen(messageParam); // Fired and forgotten.
        }
Пример #4
0
        public void Start()
        {
            if (isStarted)
            {
                throw new Exception("Agent has already been started!");
            }
            isStarted = true;

            Log.Debug("Starting Agent...");

            // Load existing or default agent configuration
            Definition = ParseAgentDefinition() ?? new AgentDefinition {
                Http =
                {
                    Host = "localhost",
                    Port =            8082,
                    Path = "/photon/agent",
                },
            };

            if (!IPAddress.TryParse(Definition.Tcp.Host, out var _address))
            {
                throw new Exception($"Invalid TCP Host '{Definition.Tcp.Host}'!");
            }

            MessageRegistry.Scan(Assembly.GetExecutingAssembly());
            MessageRegistry.Scan(typeof(ILibraryAssembly).Assembly);
            MessageRegistry.Scan(typeof(IFrameworkAssembly).Assembly);
            messageListener.Listen(_address, Definition.Tcp.Port);

            Sessions.Start();

            var taskVariables    = Task.Run(() => Variables.Load(Configuration.VariablesDirectory));
            var taskRepositories = Task.Run(() => RepositorySources.Initialize());
            var taskHttp         = Task.Run(() => StartHttpServer());

            Task.WaitAll(
                taskVariables,
                taskRepositories,
                taskHttp);

            Log.Info("Agent started.");
        }
Пример #5
0
        public async Task SendMessageOneWay_ToHost_1000x()
        {
            TestMessageProcessor.Count = 0;

            using (var listener = new MessageListener(registry)) {
                using (var client = new MessageClient(registry)) {
                    listener.Listen(IPAddress.Any, 10934);
                    await client.ConnectAsync("localhost", 10934, CancellationToken.None);

                    for (var i = 0; i < 1000; i++)
                    {
                        client.SendOneWay(new TestRequestOneWayMessage());
                    }

                    client.Disconnect();
                }

                Assert.That(TestMessageProcessor.Count, Is.EqualTo(1_000));

                listener.Stop();
            }
        }
Пример #6
0
        public async Task ClientDisconnectWaitsForMessages()
        {
            var registry = new MessageProcessorRegistry();

            registry.Register(typeof(DelayedTestProcessor));

            using (var listener = new MessageListener(registry))
                using (var client = new MessageClient(registry)) {
                    listener.Listen(IPAddress.Loopback, Port);
                    await client.ConnectAsync(Host, Port, CancellationToken.None);

                    DelayedTestProcessor.Complete = false;
                    var message = new DelayedTestRequest();
                    var _       = client.Send(message).GetResponseAsync <DelayedTestResponse>();

                    client.Disconnect();
                    //await task;

                    Assert.That(DelayedTestProcessor.Complete, Is.True);

                    listener.Stop();
                }
        }