示例#1
0
        public void PortAvailability()
        {
            IDuplexInputChannel anInputChannel1 = MessagingSystemFactory.CreateDuplexInputChannel("tcp://[::1]:8044/");
            IDuplexInputChannel anInputChannel2 = MessagingSystemFactory.CreateDuplexInputChannel("tcp://127.0.0.1:8044/");

            try
            {
                anInputChannel1.StartListening();
                anInputChannel2.StartListening();

                Console.WriteLine("Available IP addresses:");
                string[] anAvailableIpAddresses = TcpMessagingSystemFactory.GetAvailableIpAddresses();
                foreach (string anIpAddress in anAvailableIpAddresses)
                {
                    Console.WriteLine(anIpAddress);
                }

                bool aResult = TcpMessagingSystemFactory.IsPortAvailableForTcpListening("tcp://[::1]:8044/");
                Assert.IsFalse(aResult);

                aResult = TcpMessagingSystemFactory.IsPortAvailableForTcpListening("tcp://0.0.0.0:8044/");
                Assert.IsTrue(aResult);
            }
            finally
            {
                anInputChannel1.StopListening();
                anInputChannel2.StopListening();
            }
        }
示例#2
0
        public virtual void Duplex_07_OpenConnection_if_InputChannelNotStarted()
        {
            IDuplexOutputChannel anOutputChannel = MessagingSystemFactory.CreateDuplexOutputChannel(ChannelId);

            try
            {
                anOutputChannel.OpenConnection();
            }
            catch
            {
            }

            Assert.IsFalse(anOutputChannel.IsConnected);
        }
        public void B01_Pinging_StopListening()
        {
            IDuplexInputChannel aDuplexInputChannel = MessagingSystemFactory.CreateDuplexInputChannel(ChannelId);

            IDuplexOutputChannel aDuplexOutputChannel = MessagingSystemFactory.CreateDuplexOutputChannel(ChannelId);

            AutoResetEvent aDisconnectedEvent = new AutoResetEvent(false);

            bool aDisconnectedFlag = false;

            aDuplexOutputChannel.ConnectionClosed += (x, y) =>
            {
                aDisconnectedFlag = true;
                aDisconnectedEvent.Set();
            };

            try
            {
                // Start listening.
                aDuplexInputChannel.StartListening();

                // Start pinging and wait 5 seconds.
                aDuplexOutputChannel.OpenConnection();
                Thread.Sleep(5000);

                Assert.IsFalse(aDisconnectedFlag);

                // Stop listener, therefore the ping response will not come and the channel should indicate the disconnection.
                aDuplexInputChannel.StopListening();

                aDisconnectedEvent.WaitOne();

                Assert.IsTrue(aDisconnectedFlag);
                Assert.IsFalse(aDuplexOutputChannel.IsConnected);
            }
            finally
            {
                aDuplexOutputChannel.CloseConnection();
                aDuplexInputChannel.StopListening();
            }
        }
        private IDuplexOutputChannel GetAssociatedDuplexOutputChannel(string duplexInputChannelId, string responseReceiverId, string duplexOutputChannelId)
        {
            using (EneterTrace.Entering())
            {
                using (ThreadLock.Lock(myDuplexInputChannelContextManipulatorLock))
                {
                    TDuplexInputChannelContext aDuplexInputChannelContext = myDuplexInputChannelContexts.FirstOrDefault(x => x.AttachedDuplexInputChannel.ChannelId == duplexInputChannelId);
                    if (aDuplexInputChannelContext == null)
                    {
                        string anError = TracedObject + "failed to return the duplex output channel associated with the duplex input channel '" + duplexInputChannelId + "' because the duplex input channel was not attached.";
                        EneterTrace.Error(anError);
                        throw new InvalidOperationException(anError);
                    }

                    TConnection aConnection = aDuplexInputChannelContext.OpenConnections.FirstOrDefault(x => x.ResponseReceiverId == responseReceiverId && x.ConnectedDuplexOutputChannel.ChannelId == duplexOutputChannelId);
                    if (aConnection == null)
                    {
                        IDuplexOutputChannel anAssociatedDuplexOutputChannel = MessagingSystemFactory.CreateDuplexOutputChannel(duplexOutputChannelId);

                        try
                        {
                            anAssociatedDuplexOutputChannel.ResponseMessageReceived += OnResponseMessageReceived;
                            anAssociatedDuplexOutputChannel.OpenConnection();
                        }
                        catch (Exception err)
                        {
                            anAssociatedDuplexOutputChannel.ResponseMessageReceived -= OnResponseMessageReceived;

                            EneterTrace.Error(TracedObject + "failed to open connection for the duplex output channel '" + duplexOutputChannelId + "'.", err);
                            throw;
                        }


                        aConnection = new TConnection(responseReceiverId, anAssociatedDuplexOutputChannel);
                        aDuplexInputChannelContext.OpenConnections.Add(aConnection);
                    }

                    return(aConnection.ConnectedDuplexOutputChannel);
                }
            }
        }
        public void B02_Pinging_DisconnectResponseReceiver()
        {
            IDuplexInputChannel  aDuplexInputChannel  = MessagingSystemFactory.CreateDuplexInputChannel(ChannelId);
            IDuplexOutputChannel aDuplexOutputChannel = MessagingSystemFactory.CreateDuplexOutputChannel(ChannelId);

            AutoResetEvent aDisconnectedEvent = new AutoResetEvent(false);

            bool aDisconnectedFlag = false;

            aDuplexOutputChannel.ConnectionClosed += (x, y) =>
            {
                aDisconnectedFlag = true;
                aDisconnectedEvent.Set();
            };

            try
            {
                // Start listening.
                aDuplexInputChannel.StartListening();

                // Allow some time for pinging.
                aDuplexOutputChannel.OpenConnection();
                Thread.Sleep(2000);

                Assert.IsFalse(aDisconnectedFlag);

                // Disconnect the duplex output channel.
                aDuplexInputChannel.DisconnectResponseReceiver(aDuplexOutputChannel.ResponseReceiverId);

                aDisconnectedEvent.WaitOne();

                Assert.IsTrue(aDisconnectedFlag);
                Assert.IsFalse(aDuplexOutputChannel.IsConnected);
            }
            finally
            {
                aDuplexOutputChannel.CloseConnection();
                aDuplexInputChannel.StopListening();
            }
        }
        public void B04_Pinging_CloseConnection()
        {
            IDuplexInputChannel aDuplexInputChannel = MessagingSystemFactory.CreateDuplexInputChannel(ChannelId);

            IDuplexOutputChannel aDuplexOutputChannel = MessagingSystemFactory.CreateDuplexOutputChannel(ChannelId);

            AutoResetEvent aConnectionClosedEvent    = new AutoResetEvent(false);
            string         aClosedResponseReceiverId = "";

            aDuplexInputChannel.ResponseReceiverDisconnected += (x, y) =>
            {
                aClosedResponseReceiverId = y.ResponseReceiverId;
                aConnectionClosedEvent.Set();
            };

            try
            {
                // Start listening.
                aDuplexInputChannel.StartListening();

                // Allow some time for pinging.
                aDuplexOutputChannel.OpenConnection();
                Thread.Sleep(2000);

                Assert.AreEqual("", aClosedResponseReceiverId);

                // Close connection. Therefore the duplex input channel will not get any pings anymore.
                aDuplexOutputChannel.CloseConnection();

                aConnectionClosedEvent.WaitOne();

                Assert.AreEqual(aDuplexOutputChannel.ResponseReceiverId, aClosedResponseReceiverId);
                Assert.IsFalse(aDuplexOutputChannel.IsConnected);
            }
            finally
            {
                aDuplexOutputChannel.CloseConnection();
                aDuplexInputChannel.StopListening();
            }
        }
        public virtual void ConnectionNotGranted()
        {
            IDuplexInputChannel  anInputChannel  = MessagingSystemFactory.CreateDuplexInputChannel(ChannelId);
            IDuplexOutputChannel anOutputChannel = MessagingSystemFactory.CreateDuplexOutputChannel(ChannelId);

            try
            {
                myConnectionNotGranted = true;

                anInputChannel.StartListening();

                // Client opens the connection.
                Assert.Throws <InvalidOperationException>(() => anOutputChannel.OpenConnection());
            }
            finally
            {
                myConnectionNotGranted = false;

                anOutputChannel.CloseConnection();
                anInputChannel.StopListening();
            }
        }
        public virtual void AuthenticationTimeout()
        {
            IDuplexInputChannel  anInputChannel  = MessagingSystemFactory.CreateDuplexInputChannel(ChannelId);
            IDuplexOutputChannel anOutputChannel = MessagingSystemFactory.CreateDuplexOutputChannel(ChannelId);

            try
            {
                myAuthenticationSleep = TimeSpan.FromMilliseconds(3000);

                anInputChannel.StartListening();

                // Client opens the connection.
                Assert.Throws <TimeoutException>(() => anOutputChannel.OpenConnection());
            }
            finally
            {
                myAuthenticationSleep = TimeSpan.FromMilliseconds(0);

                anOutputChannel.CloseConnection();
                anInputChannel.StopListening();
            }
        }
        public void TestPortAvailability()
        {
            IDuplexInputChannel anInputChannel1 = MessagingSystemFactory.CreateDuplexInputChannel("udp://[::1]:8045/");
            IDuplexInputChannel anInputChannel2 = MessagingSystemFactory.CreateDuplexInputChannel("udp://127.0.0.1:8045/");

            try
            {
                anInputChannel1.StartListening();
                anInputChannel2.StartListening();

                bool aResult = UdpMessagingSystemFactory.IsPortAvailableForUdpListening("tcp://[::1]:8045/");
                Assert.IsFalse(aResult);

                aResult = UdpMessagingSystemFactory.IsPortAvailableForUdpListening("tcp://0.0.0.0:8046/");
                Assert.IsTrue(aResult);
            }
            finally
            {
                anInputChannel1.StopListening();
                anInputChannel2.StopListening();
            }
        }
        public void B03_Pinging_NoResponseForPing()
        {
            // Create duplex input channel which will not send ping messages.
            IDuplexInputChannel aDuplexInputChannel = UnderlyingMessaging.CreateDuplexInputChannel(ChannelId);

            IDuplexOutputChannel aDuplexOutputChannel = MessagingSystemFactory.CreateDuplexOutputChannel(ChannelId);

            AutoResetEvent aDisconnectedEvent = new AutoResetEvent(false);

            bool aDisconnectedFlag = false;

            aDuplexOutputChannel.ConnectionClosed += (x, y) =>
            {
                aDisconnectedFlag = true;
                aDisconnectedEvent.Set();
            };

            try
            {
                // Start listening.
                aDuplexInputChannel.StartListening();

                // Allow some time for pinging.
                aDuplexOutputChannel.OpenConnection();
                Assert.IsTrue(aDuplexOutputChannel.IsConnected);

                Thread.Sleep(2000);

                aDisconnectedEvent.WaitOne();

                Assert.IsTrue(aDisconnectedFlag);
                Assert.IsFalse(aDuplexOutputChannel.IsConnected);
            }
            finally
            {
                aDuplexOutputChannel.CloseConnection();
                aDuplexInputChannel.StopListening();
            }
        }
        public virtual void AuthenticationCancelledByClient()
        {
            IDuplexInputChannel  anInputChannel  = MessagingSystemFactory.CreateDuplexInputChannel(ChannelId);
            IDuplexOutputChannel anOutputChannel = MessagingSystemFactory.CreateDuplexOutputChannel(ChannelId);

            try
            {
                myClientCancelAuthentication = true;

                anInputChannel.StartListening();

                Exception anException = null;
                try
                {
                    // Client opens the connection.
                    anOutputChannel.OpenConnection();
                }
                catch (Exception err)
                {
                    anException = err;
                }

                Assert.IsInstanceOf <InvalidOperationException>(anException);

                // Check that the AuthenticationCancelled calleback was called.
                Assert.IsTrue(myAuthenticationCancelled);
            }
            finally
            {
                myClientCancelAuthentication = false;
                myAuthenticationCancelled    = false;

                anOutputChannel.CloseConnection();
                anInputChannel.StopListening();
            }
        }
        public void SendReceive_Message_PerClientSerializer()
        {
            string aClient1Id = null;

            IMultiTypedMessagesFactory aSender1Factory = new MultiTypedMessagesFactory()
            {
                SerializerProvider = x => new XmlStringSerializer()
            };
            IMultiTypedMessagesFactory aSender2Factory = new MultiTypedMessagesFactory()
            {
                SerializerProvider = x => new BinarySerializer()
            };

            IMultiTypedMessagesFactory aReceiverFactory = new MultiTypedMessagesFactory()
            {
                SerializerProvider = x => (x == aClient1Id) ? (ISerializer) new XmlStringSerializer() : (ISerializer) new BinarySerializer()
            };

            IMultiTypedMessageSender   aSender1  = aSender1Factory.CreateMultiTypedMessageSender();
            IMultiTypedMessageSender   aSender2  = aSender2Factory.CreateMultiTypedMessageSender();
            IMultiTypedMessageReceiver aReceiver = aReceiverFactory.CreateMultiTypedMessageReceiver();

            aReceiver.ResponseReceiverConnected += (x, y) => aClient1Id = aClient1Id ?? y.ResponseReceiverId;

            int aReceivedMessage1 = 0;

            aReceiver.RegisterRequestMessageReceiver <int>((x, y) =>
            {
                aReceivedMessage1 = y.RequestMessage;

                // Send the response
                aReceiver.SendResponseMessage <string>(y.ResponseReceiverId, "hello");
            });

            CustomClass aReceivedMessage2 = null;

            aReceiver.RegisterRequestMessageReceiver <CustomClass>((x, y) =>
            {
                aReceivedMessage2 = y.RequestMessage;

                // Send the response
                CustomClass aResponse = new CustomClass();
                aResponse.Name        = "Car";
                aResponse.Count       = 100;

                aReceiver.SendResponseMessage <CustomClass>(y.ResponseReceiverId, aResponse);
            });

            aReceiver.AttachDuplexInputChannel(DuplexInputChannel);

            string aSender1ReceivedResponse1 = "";

            aSender1.RegisterResponseMessageReceiver <string>((x, y) =>
            {
                aSender1ReceivedResponse1 = y.ResponseMessage;
            });

            AutoResetEvent aSender1MessagesReceivedEvent = new AutoResetEvent(false);
            CustomClass    aSender1ReceivedResponse2     = null;

            aSender1.RegisterResponseMessageReceiver <CustomClass>((x, y) =>
            {
                aSender1ReceivedResponse2 = y.ResponseMessage;

                // Signal that the response message was received -> the loop is closed.
                aSender1MessagesReceivedEvent.Set();
            });
            aSender1.AttachDuplexOutputChannel(MessagingSystemFactory.CreateDuplexOutputChannel(DuplexInputChannel.ChannelId));


            string aSender2ReceivedResponse1 = "";

            aSender2.RegisterResponseMessageReceiver <string>((x, y) =>
            {
                aSender2ReceivedResponse1 = y.ResponseMessage;
            });

            AutoResetEvent aSender2MessagesReceivedEvent = new AutoResetEvent(false);
            CustomClass    aSender2ReceivedResponse2     = null;

            aSender2.RegisterResponseMessageReceiver <CustomClass>((x, y) =>
            {
                aSender2ReceivedResponse2 = y.ResponseMessage;

                // Signal that the response message was received -> the loop is closed.
                aSender2MessagesReceivedEvent.Set();
            });
            aSender2.AttachDuplexOutputChannel(MessagingSystemFactory.CreateDuplexOutputChannel(DuplexInputChannel.ChannelId));


            try
            {
                aSender1.SendRequestMessage <int>(1000);

                CustomClass aCustomRequest = new CustomClass();
                aCustomRequest.Name  = "House";
                aCustomRequest.Count = 1000;
                aSender1.SendRequestMessage <CustomClass>(aCustomRequest);

                aSender2.SendRequestMessage <int>(1000);
                aSender2.SendRequestMessage <CustomClass>(aCustomRequest);

                // Wait for the signal that the message is received.
                aSender1MessagesReceivedEvent.WaitIfNotDebugging(2000);
                aSender2MessagesReceivedEvent.WaitIfNotDebugging(2000);
            }
            finally
            {
                aSender1.DetachDuplexOutputChannel();
                aSender2.DetachDuplexOutputChannel();
                aReceiver.DetachDuplexInputChannel();
            }

            // Check received values
            Assert.AreEqual(1000, aReceivedMessage1);
            Assert.AreEqual("hello", aSender1ReceivedResponse1);
            Assert.AreEqual("hello", aSender2ReceivedResponse1);

            Assert.IsNotNull(aReceivedMessage2);
            Assert.AreEqual("House", aReceivedMessage2.Name);
            Assert.AreEqual(1000, aReceivedMessage2.Count);

            Assert.IsNotNull(aSender1ReceivedResponse2);
            Assert.AreEqual("Car", aSender1ReceivedResponse2.Name);
            Assert.AreEqual(100, aSender1ReceivedResponse2.Count);

            Assert.IsNotNull(aSender2ReceivedResponse2);
            Assert.AreEqual("Car", aSender2ReceivedResponse2.Name);
            Assert.AreEqual(100, aSender2ReceivedResponse2.Count);
        }
示例#13
0
        public void SendReceive_1Message_PerClientSerializer()
        {
            string aClient1Id = null;

            IDuplexTypedMessagesFactory aSender1Factory  = new DuplexTypedMessagesFactory(new XmlStringSerializer());
            IDuplexTypedMessagesFactory aSender2Factory  = new DuplexTypedMessagesFactory(new BinarySerializer());
            IDuplexTypedMessagesFactory aReceiverFactory = new DuplexTypedMessagesFactory()
            {
                SerializerProvider = x => (x == aClient1Id) ? (ISerializer) new XmlStringSerializer() : (ISerializer) new BinarySerializer()
            };

            IDuplexTypedMessageSender <int, int>   aSender1  = aSender1Factory.CreateDuplexTypedMessageSender <int, int>();
            IDuplexTypedMessageSender <int, int>   aSender2  = aSender2Factory.CreateDuplexTypedMessageSender <int, int>();
            IDuplexTypedMessageReceiver <int, int> aReceiver = aReceiverFactory.CreateDuplexTypedMessageReceiver <int, int>();

            aReceiver.ResponseReceiverConnected += (x, y) => aClient1Id = aClient1Id ?? y.ResponseReceiverId;


            int aReceivedMessage = 0;

            aReceiver.MessageReceived += (x, y) =>
            {
                aReceivedMessage = y.RequestMessage;

                // Send the response
                aReceiver.SendResponseMessage(y.ResponseReceiverId, 1000);
            };

            AutoResetEvent aSender1MessageReceivedEvent = new AutoResetEvent(false);
            int            aSender1ReceivedResponse     = 0;

            aSender1.ResponseReceived += (x, y) =>
            {
                aSender1ReceivedResponse = y.ResponseMessage;

                // Signal that the response message was received -> the loop is closed.
                aSender1MessageReceivedEvent.Set();
            };


            AutoResetEvent aSender2MessageReceivedEvent = new AutoResetEvent(false);
            int            aSender2ReceivedResponse     = 0;

            aSender2.ResponseReceived += (x, y) =>
            {
                aSender2ReceivedResponse = y.ResponseMessage;

                // Signal that the response message was received -> the loop is closed.
                aSender2MessageReceivedEvent.Set();
            };

            try
            {
                aReceiver.AttachDuplexInputChannel(DuplexInputChannel);
                aSender1.AttachDuplexOutputChannel(MessagingSystemFactory.CreateDuplexOutputChannel(DuplexInputChannel.ChannelId));
                aSender2.AttachDuplexOutputChannel(MessagingSystemFactory.CreateDuplexOutputChannel(DuplexInputChannel.ChannelId));

                aSender1.SendRequestMessage(2000);
                aSender2.SendRequestMessage(2000);

                // Wait for the signal that the message is received.
                aSender1MessageReceivedEvent.WaitIfNotDebugging(2000);
                aSender2MessageReceivedEvent.WaitIfNotDebugging(2000);
                //Assert.IsTrue(aMessageReceivedEvent.WaitOne(2000));
            }
            finally
            {
                aSender1.DetachDuplexOutputChannel();
                aSender2.DetachDuplexOutputChannel();
                aReceiver.DetachDuplexInputChannel();

                // Give some time to complete tracing.
                Thread.Sleep(300);
            }

            // Check received values
            Assert.AreEqual(2000, aReceivedMessage);
            Assert.AreEqual(1000, aSender1ReceivedResponse);
            Assert.AreEqual(1000, aSender2ReceivedResponse);
        }
示例#14
0
        public void A01_Reconnect_DisconnectReceiver()
        {
            IDuplexInputChannel  aDuplexInputChannel  = MessagingSystemFactory.CreateDuplexInputChannel(ChannelId);
            IDuplexOutputChannel aDuplexOutputChannel = MessagingSystemFactory.CreateDuplexOutputChannel(ChannelId);

            Reconnecter aReconnecter = new Reconnecter(aDuplexOutputChannel, TimeSpan.FromMilliseconds(300), -1);

            AutoResetEvent         aReconnectEvent     = new AutoResetEvent(false);
            DuplexChannelEventArgs aReconnectEventArgs = null;

            aReconnecter.ConnectionOpened += (x, y) =>
            {
                aReconnectEventArgs = y;
                aReconnectEvent.Set();
            };

            AutoResetEvent         aDisconnectEvent     = new AutoResetEvent(false);
            DuplexChannelEventArgs aDisconnectEventArgs = null;

            aReconnecter.ConnectionClosed += (x, y) =>
            {
                aDisconnectEventArgs = y;
                aDisconnectEvent.Set();
            };


            AutoResetEvent aConnectionOpenedEvent = new AutoResetEvent(false);

            aDuplexOutputChannel.ConnectionOpened += (x, y) =>
            {
                aConnectionOpenedEvent.Set();
            };

            AutoResetEvent aConnectionClosedEvent = new AutoResetEvent(false);

            aDuplexOutputChannel.ConnectionClosed += (x, y) =>
            {
                aConnectionClosedEvent.Set();
            };

            try
            {
                aReconnecter.EnableReconnecting();

                // Start listening.
                aDuplexInputChannel.StartListening();
                aDuplexOutputChannel.OpenConnection();
                Thread.Sleep(2000);

                Assert.IsTrue(aDuplexOutputChannel.IsConnected);
                aConnectionOpenedEvent.WaitOne();

                // Disconnect the duplexoutput channel.
                aDuplexInputChannel.DisconnectResponseReceiver(aDuplexOutputChannel.ResponseReceiverId);

                // Wait until the disconnect is notified.
                aDisconnectEvent.WaitOne();
                aConnectionClosedEvent.WaitOne();

                Assert.AreEqual(aDuplexOutputChannel.ChannelId, aDisconnectEventArgs.ChannelId);
                Assert.AreEqual(aDuplexOutputChannel.ResponseReceiverId, aDisconnectEventArgs.ResponseReceiverId);

                // Wait until the reconnecter opens connection again.
                aReconnectEvent.WaitOne();
                Assert.AreEqual(aDuplexOutputChannel.ChannelId, aReconnectEventArgs.ChannelId);
                Assert.AreEqual(aDuplexOutputChannel.ResponseReceiverId, aReconnectEventArgs.ResponseReceiverId);

                Assert.IsTrue(aDuplexOutputChannel.IsConnected);

                aConnectionOpenedEvent.WaitOne();
            }
            finally
            {
                aReconnecter.DisableReconnecting();
                aDuplexOutputChannel.CloseConnection();
                aDuplexInputChannel.StopListening();
            }
        }