public Form1() { InitializeComponent(); // Create ProtoBuf serializer. mySerializer = new ProtoBufSerializer(); // Create the broker client that will receive notification messages. IDuplexBrokerFactory aBrokerFactory = new DuplexBrokerFactory(mySerializer); myBrokerClient = aBrokerFactory.CreateBrokerClient(); myBrokerClient.BrokerMessageReceived += OnNotificationMessageReceived; // Create the Tcp messaging for the communication with the publisher. // Note: For the interprocess communication you can use: Tcp, NamedPipes and Http. IMessagingSystemFactory aMessagingFactory = new TcpMessagingSystemFactory(); // Create duplex output channel for the communication with the publisher. // Note: The duplex output channel can send requests and receive responses. // In our case, the broker client will send requests to subscribe/unsubscribe // and receive notifications as response messages. myOutputChannel = aMessagingFactory.CreateDuplexOutputChannel("tcp://127.0.0.1:7091/"); // Attach the output channel to the broker client myBrokerClient.AttachDuplexOutputChannel(myOutputChannel); }
public ChannelWrapper() { IMessagingSystemFactory anInternalMessaging = new SynchronousMessagingSystemFactory(); // The service receives messages via one channel (i.e. it listens on one address). // The incoming messages are unwrapped on the server side. // Therefore the client must use wrapper to send messages via one channel. IChannelWrapperFactory aChannelWrapperFactory = new ChannelWrapperFactory(); myDuplexChannelWrapper = aChannelWrapperFactory.CreateDuplexChannelWrapper(); // To connect message senders and the wrapper with duplex channels we can use the following helper class. IConnectionProviderFactory aConnectionProviderFactory = new ConnectionProviderFactory(); IConnectionProvider aConnectionProvider = aConnectionProviderFactory.CreateConnectionProvider(anInternalMessaging); // Factory to create message senders. // Sent messages will be serialized in Xml. IDuplexTypedMessagesFactory aCommandsFactory = new DuplexTypedMessagesFactory(); plusSender = aCommandsFactory.CreateDuplexTypedMessageSender <TestOutput, TestInput>(); plusSender.ResponseReceived += (s, e) => { Console.WriteLine("CW.V+: " + e.ResponseMessage.Value); }; // attach method handling the request aConnectionProvider.Connect(myDuplexChannelWrapper, plusSender, "plus"); // attach the input channel to get messages from unwrapper minusSender = aCommandsFactory.CreateDuplexTypedMessageSender <TestOutput, TestInput>(); minusSender.ResponseReceived += (s, e) => { Console.WriteLine("CW.V-: " + e.ResponseMessage.Value); }; // attach method handling the request aConnectionProvider.Connect(myDuplexChannelWrapper, minusSender, "minus"); // attach the input channel to get messages from unwrapper dotSender = aCommandsFactory.CreateDuplexTypedMessageSender <TestOutput, TestInput>(); dotSender.ResponseReceived += (s, e) => { Console.WriteLine("CW.V.: " + e.ResponseMessage.Value); }; // attach method handling the request aConnectionProvider.Connect(myDuplexChannelWrapper, dotSender, "dot"); // attach the input channel to get messages from unwrapper IMessagingSystemFactory aTcpMessagingSystem = new TcpMessagingSystemFactory(); // Create output channel to send requests to the service. IDuplexOutputChannel anOutputChannel = aTcpMessagingSystem.CreateDuplexOutputChannel("tcp://127.0.0.1:8091/"); // Attach the output channel to the wrapper - so that we are able to send messages // and receive response messages. // Note: The service has the coresponding unwrapper. myDuplexChannelWrapper.AttachDuplexOutputChannel(anOutputChannel); }
public virtual void A15_ResponseReceiverReconnects_AfterStopListening() { IDuplexOutputChannel aDuplexOutputChannel = MessagingSystem.CreateDuplexOutputChannel(ChannelId); IDuplexInputChannel aDuplexInputChannel = MessagingSystem.CreateDuplexInputChannel(ChannelId); AutoResetEvent aConnectionsCompletedEvent = new AutoResetEvent(false); List <string> anOpenConnections = new List <string>(); aDuplexInputChannel.ResponseReceiverConnected += (x, y) => { lock (anOpenConnections) { anOpenConnections.Add(y.ResponseReceiverId); aConnectionsCompletedEvent.Set(); } }; try { aDuplexInputChannel.StartListening(); aDuplexOutputChannel.OpenConnection(); // Wait until the client is connected. aConnectionsCompletedEvent.WaitOne(); // Stop listenig. aDuplexInputChannel.StopListening(); // Give some time to stop. Thread.Sleep(700); // Start listening again. aDuplexInputChannel.StartListening(); // The duplex output channel will try to connect again, therefore wait until connected. aConnectionsCompletedEvent.WaitOne(); Assert.IsTrue(aDuplexOutputChannel.IsConnected); } finally { aDuplexOutputChannel.CloseConnection(); aDuplexInputChannel.StopListening(); } Assert.AreEqual(2, anOpenConnections.Count); // Both connections should be same. Assert.AreEqual(aDuplexOutputChannel.ResponseReceiverId, anOpenConnections[0]); Assert.AreEqual(aDuplexOutputChannel.ResponseReceiverId, anOpenConnections[1]); }
public void Attach(IDuplexOutputChannel duplexOutputChannel) { lock (this) { if (AttachedDuplexOutputChannel != null) { throw new InvalidOperationException("The duplex output channel is already attached."); } AttachedDuplexOutputChannel = duplexOutputChannel; AttachedDuplexOutputChannel.ResponseMessageReceived += OnResponseMessageReceived; } }
public void DoNotNotifyPublisher() { // Create channels IMessagingSystemFactory aMessagingSystem = new SynchronousMessagingSystemFactory(); IDuplexInputChannel aBrokerInputChannel = aMessagingSystem.CreateDuplexInputChannel("BrokerChannel"); IDuplexOutputChannel aClient1OutputChannel = aMessagingSystem.CreateDuplexOutputChannel("BrokerChannel"); IDuplexOutputChannel aClient2OutputChannel = aMessagingSystem.CreateDuplexOutputChannel("BrokerChannel"); // Specify in the factory that the publisher shall not be notified from its own published events. IDuplexBrokerFactory aBrokerFactory = new DuplexBrokerFactory() { IsPublisherNotified = false }; IDuplexBroker aBroker = aBrokerFactory.CreateBroker(); aBroker.AttachDuplexInputChannel(aBrokerInputChannel); IDuplexBrokerClient aClient1 = aBrokerFactory.CreateBrokerClient(); List <BrokerMessageReceivedEventArgs> aClient1ReceivedMessage = new List <BrokerMessageReceivedEventArgs>(); aClient1.BrokerMessageReceived += (x, y) => { aClient1ReceivedMessage.Add(y); }; aClient1.AttachDuplexOutputChannel(aClient1OutputChannel); IDuplexBrokerClient aClient2 = aBrokerFactory.CreateBrokerClient(); List <BrokerMessageReceivedEventArgs> aClient2ReceivedMessage = new List <BrokerMessageReceivedEventArgs>(); aClient2.BrokerMessageReceived += (x, y) => { aClient2ReceivedMessage.Add(y); }; aClient2.AttachDuplexOutputChannel(aClient2OutputChannel); aClient1.Subscribe("TypeA"); aClient2.Subscribe("TypeA"); // Notify the message. aClient2.SendMessage("TypeA", "Message A"); // Client 2 should not get the notification. Assert.AreEqual(1, aClient1ReceivedMessage.Count); Assert.AreEqual(0, aClient2ReceivedMessage.Count); Assert.AreEqual("TypeA", aClient1ReceivedMessage[0].MessageTypeId); Assert.AreEqual("Message A", (String)aClient1ReceivedMessage[0].Message); }
// When some output connection was closed/broken. private void OnConnectionClosed(object sender, DuplexChannelEventArgs e) { using (EneterTrace.Entering()) { using (ThreadLock.Lock(myOutputConnectionLock)) { IDuplexOutputChannel anOutputChannel = (IDuplexOutputChannel)sender; anOutputChannel.ConnectionClosed -= OnConnectionClosed; anOutputChannel.ResponseMessageReceived -= OnResponseMessageReceived; myOpenOutputConnections.Remove(anOutputChannel); } } }
public void Openconnection() { // Create message sender sending request messages of type Person and receiving responses of type string. IDuplexTypedMessagesFactory aTypedMessagesFactory = new DuplexTypedMessagesFactory(); myMessageSender = aTypedMessagesFactory.CreateDuplexTypedMessageSender <string, Person>(); myMessageSender.ResponseReceived += OnResponseReceived; // Create messaging based on TCP. IMessagingSystemFactory aMessagingSystemFactory = new TcpMessagingSystemFactory(); IDuplexOutputChannel anOutputChannel = aMessagingSystemFactory.CreateDuplexOutputChannel("tcp://127.0.0.1:8094/"); // Attach output channel and be able to send messages and receive response messages. myMessageSender.AttachDuplexOutputChannel(anOutputChannel); }
public virtual void Duplex_07_OpenConnection_if_InputChannelNotStarted() { IDuplexOutputChannel anOutputChannel = MessagingSystemFactory.CreateDuplexOutputChannel(ChannelId); try { anOutputChannel.OpenConnection(); } catch { } Assert.IsFalse(anOutputChannel.IsConnected); }
public YZXMessagingClient(string partner, YZXMessagesFactoryType messagetype = YZXMessagesFactoryType.Duplex) { Partner = partner; DSender = DSenderFactory.CreateDuplexTypedMessageSender <ResponseType, RequestType>(); anOutputChannel = UnderlyingMessaging.CreateDuplexOutputChannel(Partner); ConfigSender(); AttachOutputChannel(); }
/// <summary> /// Constructs the reconnecter from specified parameters. /// </summary> /// <param name="duplexOutputChannel">observed duplex output channel</param> /// <param name="reconnectFrequency">how often the reconnect attempt shall be performed (in case of the disconnection)</param> /// <param name="maxReconnectAttempts">max amounts of reconnect attempts. If exceeded, the ReconnectingFailed is invoked.</param> public Reconnecter(IDuplexOutputChannel duplexOutputChannel, TimeSpan reconnectFrequency, int maxReconnectAttempts) { using (EneterTrace.Entering()) { if (duplexOutputChannel == null) { EneterTrace.Error(TracedObject + "detected null input parameter 'duplexOutputChannel'."); throw new ArgumentNullException("The input parameter 'duplexOutputChannel' is null."); } myDuplexOutputChannel = duplexOutputChannel; myReconnecFrequency = reconnectFrequency; myMaxReconnectAttempts = maxReconnectAttempts; } }
public void OpenConnection() { // Establish connection with load scheduling module IMessagingSystemFactory myMessaging = new TcpMessagingSystemFactory(); IDuplexOutputChannel anOutputChannel = myMessaging.CreateDuplexOutputChannel("tcp://127.0.0.1:8060/"); IDuplexTypedMessagesFactory aSenderFactory = new DuplexTypedMessagesFactory(); mySender = aSenderFactory.CreateDuplexTypedMessageSender <double, Range>(); // Event handler on receipt of response mySender.ResponseReceived += OnResponseReceived; // Attach the output channel to send messages and receive responses mySender.AttachDuplexOutputChannel(anOutputChannel); }
/// <summary> /// The configure receiver. /// </summary> private void ConfigureReceiver() { // Create reliable message receiver. var messagesFactory = new ReliableTypedMessagesFactory(); _messageSender = messagesFactory.CreateReliableDuplexTypedMessageSender <bool?, NotificationMessage>(); _messageSender.MessageDelivered += OnMessageDelivered; _messageSender.MessageNotDelivered += OnMessageNotDelivered; _messageSender.ConnectionClosed += OnConnectionClosed; // Create Named Pipe based messaging to communicate with clients. IMessagingSystemFactory messageFactory = new NamedPipeMessagingSystemFactory(); _channel = messageFactory.CreateDuplexOutputChannel(Constants.ChannelId); }
// The method is called when the button to send message is clicked. private void SendMessage_Click(object sender, RoutedEventArgs e) { // Create message sender sending request messages of type Person and receiving responses of type string. IDuplexTypedMessagesFactory aTypedMessagesFactory = new DuplexTypedMessagesFactory(); myMessageSender = aTypedMessagesFactory.CreateDuplexTypedMessageSender <byte[], byte[]>(); myMessageSender.ResponseReceived += ResponseReceived; // Create messaging based on TCP. IMessagingSystemFactory aMessagingSystemFactory = new TcpMessagingSystemFactory(); IDuplexOutputChannel aDuplexOutputChannel = aMessagingSystemFactory.CreateDuplexOutputChannel("tcp://127.0.0.1:4502/"); // Attach output channel and be able to send messages and receive response messages. myMessageSender.AttachDuplexOutputChannel(aDuplexOutputChannel); myMessageSender.SendRequestMessage(GetBytes(textBox1.Text)); }
public void A14_ResponseReceiverReconnects_AfterDisconnect() { // Duplex output channel without queue - it will not try to reconnect. IDuplexOutputChannel aDuplexOutputChannel = MessagingSystem.CreateDuplexOutputChannel(ChannelId); IDuplexInputChannel aDuplexInputChannel = MessagingSystem.CreateDuplexInputChannel(ChannelId); AutoResetEvent aConnectionsCompletedEvent = new AutoResetEvent(false); List <string> anOpenConnections = new List <string>(); aDuplexInputChannel.ResponseReceiverConnected += (x, y) => { lock (anOpenConnections) { anOpenConnections.Add(y.ResponseReceiverId); aConnectionsCompletedEvent.Set(); } }; try { aDuplexInputChannel.StartListening(); aDuplexOutputChannel.OpenConnection(); // Wait until the connection is open. aConnectionsCompletedEvent.WaitOne(); // Disconnect the response receiver. aDuplexInputChannel.DisconnectResponseReceiver(aDuplexOutputChannel.ResponseReceiverId); // The duplex output channel will try to connect again, therefore wait until connected. aConnectionsCompletedEvent.WaitOne(); } finally { aDuplexOutputChannel.CloseConnection(); aDuplexInputChannel.StopListening(); } Assert.AreEqual(2, anOpenConnections.Count); // Both connections should be same. Assert.AreEqual(aDuplexOutputChannel.ResponseReceiverId, anOpenConnections[0]); Assert.AreEqual(aDuplexOutputChannel.ResponseReceiverId, anOpenConnections[1]); }
public TalkFlow() { IRpcFactory factory = new RpcFactory(); var client = factory.CreateClient <ITalk>(); TcpMessagingSystemFactory messaging = new TcpMessagingSystemFactory(); IDuplexOutputChannel anOutputChannel = messaging.CreateDuplexOutputChannel("tcp://127.0.0.1:8045/"); client.AttachDuplexOutputChannel(anOutputChannel); string sentence = ""; while ((sentence = Console.ReadLine()) != "stop") { client.Proxy.Talk("Client A", sentence); } }
public Form1() { InitializeComponent(); // Create ProtoBuf serializer. mySerializer = new ProtoBufSerializer(); // Create broker client responsible for sending messages to the broker. IDuplexBrokerFactory aBrokerFactory = new DuplexBrokerFactory(mySerializer); myBrokerClient = aBrokerFactory.CreateBrokerClient(); // Create output channel to send messages via Tcp. IMessagingSystemFactory aMessaging = new TcpMessagingSystemFactory(); myOutputChannel = aMessaging.CreateDuplexOutputChannel("tcp://127.0.0.1:7091/"); // Attach the output channel to the broker client to be able to send messages. myBrokerClient.AttachDuplexOutputChannel(myOutputChannel); }
public MainWindow() { InitializeComponent(); System.Windows.Forms.Panel aVideoPanel = new System.Windows.Forms.Panel(); aVideoPanel.BackColor = System.Drawing.Color.Black; VideoWindow.Child = aVideoPanel; // If not installed in Provide path to your VLC. myVlcInstance = new VlcInstance(@"c:\Program Files\VideoLAN\VLC\"); // Use TCP messaging. // You can try to use UDP or WebSockets too. myVideoChannel = new TcpMessagingSystemFactory() //myVideoChannel = new UdpMessagingSystemFactory() // Note: Provide address of your service here. .CreateDuplexOutputChannel("tcp://172.20.10.9:8093/"); myVideoChannel.ResponseMessageReceived += OnResponseMessageReceived; }
public void MaxAmountOfConnections() { IMessagingSystemFactory aMessaging = new UdpMessagingSystemFactory() { MaxAmountOfConnections = 2 }; IDuplexOutputChannel anOutputChannel1 = aMessaging.CreateDuplexOutputChannel("udp://127.0.0.1:8049/"); IDuplexOutputChannel anOutputChannel2 = aMessaging.CreateDuplexOutputChannel("udp://127.0.0.1:8049/"); IDuplexOutputChannel anOutputChannel3 = aMessaging.CreateDuplexOutputChannel("udp://127.0.0.1:8049/"); IDuplexInputChannel anInputChannel = aMessaging.CreateDuplexInputChannel("udp://127.0.0.1:8049/"); try { ManualResetEvent aConnectionClosed = new ManualResetEvent(false); anOutputChannel3.ConnectionClosed += (x, y) => { EneterTrace.Info("Connection closed."); aConnectionClosed.Set(); }; anInputChannel.StartListening(); anOutputChannel1.OpenConnection(); anOutputChannel2.OpenConnection(); anOutputChannel3.OpenConnection(); if (!aConnectionClosed.WaitOne(1000)) { Assert.Fail("Third connection was not closed."); } Assert.IsTrue(anOutputChannel1.IsConnected); Assert.IsTrue(anOutputChannel2.IsConnected); Assert.IsFalse(anOutputChannel3.IsConnected); } finally { anOutputChannel1.CloseConnection(); anOutputChannel2.CloseConnection(); anOutputChannel3.CloseConnection(); anInputChannel.StopListening(); } }
public void OpenConnection() { // Create TCP messaging for the communication. // Note: Requests are sent to the balancer that will forward them // to available services. IMessagingSystemFactory myMessaging = new TcpMessagingSystemFactory(); IDuplexOutputChannel anOutputChannel = myMessaging.CreateDuplexOutputChannel("tcp://127.0.0.1:8060/"); // Create sender to send requests. IDuplexTypedMessagesFactory aSenderFactory = new DuplexTypedMessagesFactory(); mySender = aSenderFactory.CreateDuplexTypedMessageSender <double, Range>(); // Subscribe to receive response messages. mySender.ResponseReceived += OnResponseReceived; // Attach the output channel and be able to send messages and receive responses. mySender.AttachDuplexOutputChannel(anOutputChannel); }
public AuthenticatedDuplexOutputChannel(IDuplexOutputChannel underlyingOutputChannel, GetLoginMessage getLoginMessageCallback, GetHandshakeResponseMessage getHandshakeResponseMessageCallback, TimeSpan authenticationTimeout, IThreadDispatcher threadDispatcher) { using (EneterTrace.Entering()) { myUnderlyingOutputChannel = underlyingOutputChannel; myGetLoginMessageCallback = getLoginMessageCallback; myGetHandshakeResponseMessageCallback = getHandshakeResponseMessageCallback; myAuthenticationTimeout = authenticationTimeout; myUnderlyingOutputChannel.ConnectionClosed += OnConnectionClosed; myUnderlyingOutputChannel.ResponseMessageReceived += OnResponseMessageReceived; myThreadDispatcher = threadDispatcher; } }
public void SubscribeSameMessageTwice() { // Create channels IMessagingSystemFactory aMessagingSystem = new SynchronousMessagingSystemFactory(); IDuplexInputChannel aBrokerInputChannel = aMessagingSystem.CreateDuplexInputChannel("BrokerChannel"); IDuplexOutputChannel aSubscriberClientOutputChannel = aMessagingSystem.CreateDuplexOutputChannel("BrokerChannel"); IDuplexOutputChannel aPublisherClientOutputChannel = aMessagingSystem.CreateDuplexOutputChannel("BrokerChannel"); IDuplexBrokerFactory aBrokerFactory = new DuplexBrokerFactory(); IDuplexBroker aBroker = aBrokerFactory.CreateBroker(); aBroker.AttachDuplexInputChannel(aBrokerInputChannel); IDuplexBrokerClient aSubscriber = aBrokerFactory.CreateBrokerClient(); List <BrokerMessageReceivedEventArgs> aClient1ReceivedMessage = new List <BrokerMessageReceivedEventArgs>(); aSubscriber.BrokerMessageReceived += (x, y) => { aClient1ReceivedMessage.Add(y); }; aSubscriber.AttachDuplexOutputChannel(aSubscriberClientOutputChannel); IDuplexBrokerClient aPublisher = aBrokerFactory.CreateBrokerClient(); aPublisher.AttachDuplexOutputChannel(aPublisherClientOutputChannel); // Subscribe the 1st time. aSubscriber.Subscribe("TypeA"); // Subscribe the 2nd time. aSubscriber.Subscribe("TypeA"); // Notify the message. aPublisher.SendMessage("TypeA", "Message A"); // Although the client is subscribed twice, the message shall be notified once. Assert.AreEqual(1, aClient1ReceivedMessage.Count); Assert.AreEqual("TypeA", aClient1ReceivedMessage[0].MessageTypeId); Assert.AreEqual("Message A", (String)aClient1ReceivedMessage[0].Message); }
protected void SendMessage(string duplexInputChannelId, string duplexInputChannelResponseReceiverId, string duplexOutputChannelId, object message) { using (EneterTrace.Entering()) { using (ThreadLock.Lock(myDuplexInputChannelContextManipulatorLock)) { try { // Get (or create) the duplex output channel that will be used IDuplexOutputChannel aDuplexOutputChannel = GetAssociatedDuplexOutputChannel(duplexInputChannelId, duplexInputChannelResponseReceiverId, duplexOutputChannelId); aDuplexOutputChannel.SendMessage(message); } catch (Exception err) { EneterTrace.Error(TracedObject + "failed to send the message to the duplex output channel '" + duplexOutputChannelId + "'.", err); throw; } } } }
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 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(); } }
public void ConnectionTimeout() { IMessagingSystemFactory aMessaging = new WebSocketMessagingSystemFactory() { ConnectTimeout = TimeSpan.FromMilliseconds(1000) }; // Nobody is listening on this address. IDuplexOutputChannel anOutputChannel = aMessaging.CreateDuplexOutputChannel("ws://109.74.151.135:8045/"); ManualResetEvent aConnectionCompleted = new ManualResetEvent(false); try { // Start opening in another thread to be able to measure // if the timeout occured with the specified time. Exception anException = null; ThreadPool.QueueUserWorkItem(x => { try { anOutputChannel.OpenConnection(); } catch (Exception err) { anException = err; } aConnectionCompleted.Set(); }); if (aConnectionCompleted.WaitOne(1500)) { } Assert.AreEqual(typeof(TimeoutException), anException); } finally { anOutputChannel.CloseConnection(); } }
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 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 void ServiceReceiveTimeout() { IMessagingSystemFactory aMessaging = new WebSocketMessagingSystemFactory() { ReceiveTimeout = TimeSpan.FromMilliseconds(1000) }; IDuplexOutputChannel anOutputChannel = aMessaging.CreateDuplexOutputChannel("ws://127.0.0.1:8046/"); IDuplexInputChannel anInputChannel = aMessaging.CreateDuplexInputChannel("ws://127.0.0.1:8046/"); try { ManualResetEvent aConnectionClosed = new ManualResetEvent(false); anInputChannel.ResponseReceiverDisconnected += (x, y) => { EneterTrace.Info("Response Receiver Disconnected: " + y.ResponseReceiverId); }; anOutputChannel.ConnectionClosed += (x, y) => { EneterTrace.Info("Connection closed."); aConnectionClosed.Set(); }; anInputChannel.StartListening(); anOutputChannel.OpenConnection(); EneterTrace.Info("Connection opened: " + anOutputChannel.ResponseReceiverId); // According to set receive timeout the client should get disconnected within 1 second. //aConnectionClosed.WaitOne(); Assert.IsTrue(aConnectionClosed.WaitOne(3000)); } finally { anOutputChannel.CloseConnection(); anInputChannel.StopListening(); } }
public MainWindow() { InitializeComponent(); IDuplexTypedMessagesFactory aTypedMessagesFactory = new DuplexTypedMessagesFactory(); mySender = aTypedMessagesFactory.CreateDuplexTypedMessageSender <MyResponse, MyRequest>(); mySender.ResponseReceived += OnResponseReceived; // Create messaging based on TCP. IMessagingSystemFactory aMessagingSystemFactory = new TcpMessagingSystemFactory(); IDuplexOutputChannel anOutputChannel = aMessagingSystemFactory.CreateDuplexOutputChannel("tcp://192.168.2.9:8060/"); // Attach output channel and be able to send messages and receive response messages. mySender.AttachDuplexOutputChannel(anOutputChannel); MyRequest test = new MyRequest { side = "L", strength = 10 }; mySender.SendRequestMessage(test); MyRequest reset = new MyRequest { side = "L", strength = 0 }; mySender.SendRequestMessage(reset); try { USBInterface usb = new USBInterface("vid_044f", "pid_b108"); usb.Connect(); usb.enableUsbBufferEvent(new System.EventHandler(myEventCacher)); usb.startRead(); Console.ReadLine(); } catch (Exception ex) { Console.WriteLine(ex); } }
private bool Worker4504_Initialize() { // Create TCP messaging IMessagingSystemFactory aMessaging = new TcpMessagingSystemFactory(); Worker4504_OutputChannel = aMessaging.CreateDuplexOutputChannel("tcp://" + serverIP.Text + ":4504/"); // Subscribe to response messages. Worker4504_OutputChannel.ConnectionClosed += Worker4504_ConnectionClosed; Worker4504_OutputChannel.ConnectionOpened += Worker4504_ConnectionOpened; Worker4504_OutputChannel.ResponseMessageReceived += Worker4504_ResponseMessageReceived; // Open connection and be able to send messages and receive response messages. Worker4504_OutputChannel.OpenConnection(); if (Worker4504_OutputChannel.IsConnected) { return true; } else { return false; } }
private bool InitializeWorkerConnection() { // Create TCP messaging IMessagingSystemFactory aMessaging = new TcpMessagingSystemFactory(); Worker4504OutputChannel = aMessaging.CreateDuplexOutputChannel("tcp://127.0.0.1:4504/"); // Subscribe to response messages. Worker4504OutputChannel.ConnectionClosed += Worker4504OutputChannel_ConnectionClosed; Worker4504OutputChannel.ConnectionOpened += Worker4504OutputChannel_ConnectionOpened; Worker4504OutputChannel.ResponseMessageReceived += Worker4504OutputChannel_ResponseMessageReceived; // Open connection and be able to send messages and receive response messages. Worker4504OutputChannel.OpenConnection(); Log("Channel id : " + Worker4504OutputChannel.ChannelId); // Send a message. byte[] data = new byte[1048576]; // initialize 1MB data //byte[] data = new byte[10]; // initialize 1MB data Random random = new Random(); random.NextBytes(data); Worker4504OutputChannel.SendMessage(data); Log("Sent data length : " + data.Length); // Close connection. //Worker4504OutputChannel.CloseConnection(); return true; }