Esempio n. 1
0
        public async Task Check_SimpleUdpConnection()
        {
            var testingPort1 = TestUtility.GetFreeTcpPort();
            var testingPort2 = TestUtility.GetFreeTcpPort();

            var receiveTaskOnPassiveChannel = new TaskCompletionSource <string>();
            var receiveTaskOnActiveChannel  = new TaskCompletionSource <string>();

            // Define channels
            var passiveChannel = new MessageChannel(
                new UdpByteStreamHandlerSettings(testingPort1, IPAddress.Loopback, testingPort2),
                new DefaultMessageRecognizerSettings(Encoding.UTF8),
                (msg) =>
            {
                receiveTaskOnPassiveChannel.SetResult(msg.ToString());
                msg.ReturnToPool();
            });
            var activeChannel = new MessageChannel(
                new UdpByteStreamHandlerSettings(testingPort2, IPAddress.Loopback, testingPort1),
                new DefaultMessageRecognizerSettings(Encoding.UTF8),
                (msg) =>
            {
                receiveTaskOnActiveChannel.SetResult(msg.ToString());
                msg.ReturnToPool();
            });

            try
            {
                // Start both channels
                await Task.WhenAll(
                    passiveChannel.StartAsync(),
                    activeChannel.StartAsync());

                // Wait for connection
                var timeoutTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(5.0));
                await Task.WhenAll(
                    passiveChannel.WaitForConnectionAsync(timeoutTokenSource.Token),
                    activeChannel.WaitForConnectionAsync(timeoutTokenSource.Token));

                Assert.AreEqual(ConnectionState.Connected, passiveChannel.State, $"Unable to connect on port {testingPort2}");
                Assert.AreEqual(ConnectionState.Connected, activeChannel.State, $"Unable to connect on port {testingPort1}");

                // Send/Receive some messages in both directions
                await passiveChannel.SendAsync("Message from passive endpoint");

                await activeChannel.SendAsync("Message from active endpoint");

                // Check for received messages
                var receivedOnPassiveChannel = await receiveTaskOnPassiveChannel.Task;
                var receivedOnActiveChannel  = await receiveTaskOnActiveChannel.Task;
                Assert.IsTrue(receivedOnPassiveChannel == "Message from active endpoint");
                Assert.IsTrue(receivedOnActiveChannel == "Message from passive endpoint");
            }
            finally
            {
                await Task.WhenAll(
                    passiveChannel.StopAsync(),
                    activeChannel.StopAsync());
            }
        }
Esempio n. 2
0
        public async Task Check_SerialConnection_Send()
        {
            var encoding = Encoding.UTF8;

            // Fake serial port api and listen for Write calls
            var receivedString = string.Empty;

            SerialPortByteStreamHandler.SerialPortFactory = () =>
            {
                var isOpened = false;

                var fakedSerialPort = A.Fake <ISerialPort>();
                A.CallTo(() => fakedSerialPort.IsOpen).ReturnsLazily(call => isOpened);
                A.CallTo(() => fakedSerialPort.Open()).Invokes(call => isOpened  = true);
                A.CallTo(() => fakedSerialPort.Close()).Invokes(call => isOpened = false);
                A.CallTo(fakedSerialPort)
                .Where(call => call.Method.Name == nameof(ISerialPort.Write))
                .Invokes(call =>
                {
                    var buffer = (byte[])call.Arguments[0] !;
                    var offset = (int)call.Arguments[1] !;
                    var count  = (int)call.Arguments[2] !;

                    receivedString = encoding.GetString(buffer, offset, count);
                });

                return(fakedSerialPort);
            };

            // Define channels
            var serialChannel = new MessageChannel(
                new SerialPortByteStreamHandlerSettings("COM1"),
                new StartAndEndSymbolsRecognizerSettings(encoding, "<", ">"));

            try
            {
                // Start both channels
                await serialChannel.StartAsync();

                // Wait for connection
                var timeoutTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(5.0));
                await serialChannel.WaitForConnectionAsync(timeoutTokenSource.Token);

                Assert.AreEqual(serialChannel.State, ConnectionState.Connected);

                // Send/Receive some messages in both directions
                await serialChannel.SendAsync("Message sent through serial channel");

                // Check for written message
                Assert.AreEqual(receivedString, "<Message sent through serial channel>");
            }
            finally
            {
                await serialChannel.StopAsync();
            }
        }
        public async Task ChangeParametersAsync(ConnectionParameters newConnParameters)
        {
            var prefWasRunning = false;

            if (_messageChannel.IsRunning)
            {
                await _messageChannel.StopAsync();

                prefWasRunning = true;
            }

            this.Parameters = newConnParameters;
            _messageChannel = SetupMessageChannel(newConnParameters, this, this);

            if (prefWasRunning)
            {
                await _messageChannel.StartAsync();
            }
        }
        public static async Task Main(string[] args)
        {
            // Create and start receive channel
            var passiveTcpChannel = new MessageChannel(
                new TcpPassiveByteStreamHandlerSettings(IPAddress.Loopback, 12000),
                new EndSymbolsMessageRecognizerSettings(Encoding.UTF8, "##"),
                (message) =>
            {
                Console.WriteLine($"Received message on passive channel: {message}");
            });
            await passiveTcpChannel.StartAsync();

            // Create and start send channel
            var activeTcpChannel = new MessageChannel(
                new TcpActiveByteStreamHandlerSettings(IPAddress.Loopback, 12000),
                new EndSymbolsMessageRecognizerSettings(Encoding.UTF8, "##"),
                (message) =>
            {
                Console.WriteLine($"Received message on active channel: {message}");
            });
            await activeTcpChannel.StartAsync();

            // Wait until both channels are connected
            while (passiveTcpChannel.State != ConnectionState.Connected)
            {
                await Task.Delay(500);
            }

            // Send some messages (active -> passive)
            await activeTcpChannel.SendAsync("Message 1 from active to passive...");

            await activeTcpChannel.SendAsync("Message 2 from active to passive...");

            await activeTcpChannel.SendAsync("Message 3 from active to passive...");

            // Send some messages (passive -> active)
            await passiveTcpChannel.SendAsync("Message 1 from active to passive...");

            await passiveTcpChannel.SendAsync("Message 2 from active to passive...");

            await passiveTcpChannel.SendAsync("Message 3 from active to passive...");

            // Wait
            Console.ReadLine();

            // Stop both channels
            await activeTcpChannel.StopAsync();

            await passiveTcpChannel.StopAsync();
        }
Esempio n. 5
0
        public async Task Check_SerialConnection_Receive()
        {
            var encoding = Encoding.UTF8;

            // Fake serial port api and listen for Write calls
            byte[]? fakeReceive = null;
            ISerialPort?fakedSerialPort = null;

            SerialPortByteStreamHandler.SerialPortFactory = () =>
            {
                var isOpened = false;

                fakedSerialPort = A.Fake <ISerialPort>();
                A.CallTo(() => fakedSerialPort.IsOpen).ReturnsLazily(call => isOpened);
                A.CallTo(() => fakedSerialPort.Open()).Invokes(call => isOpened  = true);
                A.CallTo(() => fakedSerialPort.Close()).Invokes(call => isOpened = false);
                A.CallTo(() => fakedSerialPort.BytesToRead).ReturnsLazily(call => fakeReceive?.Length ?? 0);
                A.CallTo(fakedSerialPort)
                .Where(call => call.Method.Name == nameof(ISerialPort.Read))
                .WithReturnType <int>()
                .Invokes(call =>
                {
                    var fakeReceiveInner = fakeReceive;
                    if (fakeReceiveInner == null)
                    {
                        return;
                    }

                    var buffer = (byte[])call.Arguments[0] !;
                    Array.Copy(fakeReceiveInner, buffer, fakeReceiveInner.Length);
                })
                .ReturnsLazily(call =>
                {
                    var result  = fakeReceive !.Length;
                    fakeReceive = null;
                    return(result);
                });

                return(fakedSerialPort);
            };

            // Define channels
            var receiveMessageTask = new TaskCompletionSource <string>();
            var serialChannel      = new MessageChannel(
                new SerialPortByteStreamHandlerSettings("COM1"),
                new StartAndEndSymbolsRecognizerSettings(encoding, "<", ">"),
                (msg) =>
            {
                receiveMessageTask.SetResult(msg.ToString());
                msg.ReturnToPool();
            });

            try
            {
                // Start both channels
                await serialChannel.StartAsync();

                // Wait for connection
                var timeoutTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(5.0));
                await serialChannel.WaitForConnectionAsync(timeoutTokenSource.Token);

                Assert.AreEqual(serialChannel.State, ConnectionState.Connected);

                var constructor = typeof(SerialDataReceivedEventArgs)
                                  .GetConstructor(
                    BindingFlags.NonPublic | BindingFlags.CreateInstance | BindingFlags.Instance,
                    null, CallingConventions.Any,
                    new[] { typeof(SerialData) }, null);
                Assert.IsNotNull(constructor, $"Constructor for {nameof(SerialDataReceivedEventArgs)} not found!");

                // Trigger receive data and event
                fakeReceive = encoding.GetBytes("<Message received through serial channel>");
                fakedSerialPort !.DataReceived += Raise.FreeForm <SerialDataReceivedEventHandler>
                                                  .With(
                    fakedSerialPort,
                    constructor !.Invoke(new object[] { SerialData.Chars }));

                // Process received data
                await Task.WhenAny(
                    receiveMessageTask.Task,
                    Task.Delay(4000));

                Assert.IsTrue(receiveMessageTask.Task.IsCompleted);
                Assert.AreEqual(receiveMessageTask.Task.Result, "Message received through serial channel");
            }
            finally
            {
                await serialChannel.StopAsync();
            }

            Assert.IsFalse(fakedSerialPort.IsOpen);
        }