コード例 #1
0
        public TCPManager(string ipAddress)
        {
            IPAddress = ipAddress;
            // Create message receiver receiving 'MyRequest' and receiving 'MyResponse'.
            aReceiverFactory = new DuplexTypedMessagesFactory();
            myReceiver       = aReceiverFactory.CreateDuplexTypedMessageReceiver <String, String>();

            // Subscribe to handle messages.
            myReceiver.MessageReceived += OnMessageReceived;

            // Create TCP messaging.
            // Note: 192.168.0.100 is the IP from the wireless router (no internet)
            // and 8800 is the socket.
            aMessaging     = new TcpMessagingSystemFactory();
            anInputChannel = aMessaging.CreateDuplexInputChannel(IPAddress);

            // Attach the input channel and start to listen to messages.
            try
            {
                myReceiver.AttachDuplexInputChannel(anInputChannel);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
コード例 #2
0
        static void Main(string[] args)
        {
            string aServiceAddress = GetMyAddress();

            if (aServiceAddress == "")
            {
                Console.WriteLine("The service could not start because all possible ports are occupied");
                return;
            }

            // TCP message protocol for receiving requests
            IMessagingSystemFactory aMessaging     = new TcpMessagingSystemFactory();
            IDuplexInputChannel     anInputChannel = aMessaging.CreateDuplexInputChannel(aServiceAddress);

            IDuplexTypedMessagesFactory aReceiverFactory = new DuplexTypedMessagesFactory();
            IDuplexTypedMessageReceiver <double, Range> aReceiver
                = aReceiverFactory.CreateDuplexTypedMessageReceiver <double, Range>();

            aReceiver.MessageReceived += OnMessageReceived;

            aReceiver.AttachDuplexInputChannel(anInputChannel);

            Console.WriteLine("Root Square Calculator listening to " + aServiceAddress +
                              " is running.\r\n Press ENTER to stop.");
            Console.ReadLine();

            aReceiver.DetachDuplexInputChannel();
        }
コード例 #3
0
        public static void run()
        {
            // Create message receiver receiving 'MyRequest' and receiving 'MyResponse'.
            IDuplexTypedMessagesFactory aReceiverFactory = new DuplexTypedMessagesFactory();

            myReceiver = aReceiverFactory.CreateDuplexTypedMessageReceiver <MyResponse, MyRequest>();

            // Subscribe to handle messages.
            myReceiver.MessageReceived += OnMessageReceived;

            // Create TCP messaging.
            IMessagingSystemFactory aMessaging = new TcpMessagingSystemFactory();
            IDuplexInputChannel     anInputChannel
                = aMessaging.CreateDuplexInputChannel("tcp://192.168.43.167:8067/");

            //= aMessaging.CreateDuplexInputChannel("tcp://192.168.173.1:8060/");

            // Attach the input channel and start to listen to messages.
            myReceiver.AttachDuplexInputChannel(anInputChannel);

            Console.WriteLine("The service is running. To stop press enter.");
            Console.ReadLine();

            // Detach the input channel and stop listening.
            // It releases the thread listening to messages.
            myReceiver.DetachDuplexInputChannel();
        }
コード例 #4
0
        private void StartServiceBtn_Click(object sender, EventArgs e)
        {
            if (myReceiver.IsDuplexInputChannelAttached)
            {
                // The channel is already attached so nothing to do.
                return;
            }

            // Use TCP communication
            IMessagingSystemFactory aMessaging = new TcpMessagingSystemFactory()
            {
                // Set to receive messages in the main UI thread.
                // Note: if this is not set then methods OnMessageReceived, OnClientConnected
                //       and OnClientDisconnected would not be called from main UI thread
                //       but from a listener thread.
                MaxAmountOfConnections = 5,
                InputChannelThreading  = new WinFormsDispatching(this)
            };

            // Create input channel.
            IDuplexInputChannel anInputChannel = aMessaging.CreateDuplexInputChannel("tcp://127.0.0.1:8060/");

            // Attach the input channel and be able to receive messages
            // and send back response messages.
            myReceiver.AttachDuplexInputChannel(anInputChannel);

            ServiceStatusLabel.Text = "Service Running";
        }
コード例 #5
0
        public void ConnectionClosedDuringWaitingForResponse()
        {
            IDuplexTypedMessageReceiver <int, int> aReceiver = DuplexTypedMessagesFactory.CreateDuplexTypedMessageReceiver <int, int>();

            aReceiver.MessageReceived += (x, y) =>
            {
                // Disconnect the cient.
                aReceiver.AttachedDuplexInputChannel.DisconnectResponseReceiver(y.ResponseReceiverId);
            };

            ISyncDuplexTypedMessageSender <int, int> aSender = DuplexTypedMessagesFactory.CreateSyncDuplexTypedMessageSender <int, int>();

            try
            {
                aReceiver.AttachDuplexInputChannel(InputChannel);
                aSender.AttachDuplexOutputChannel(OutputChannel);

                Assert.Throws <InvalidOperationException>(() => aSender.SendRequestMessage(100));
            }
            finally
            {
                aSender.DetachDuplexOutputChannel();
                aReceiver.DetachDuplexInputChannel();
            }
        }
コード例 #6
0
        public void SyncRequestResponse()
        {
            IDuplexTypedMessageReceiver <string, int> aReceiver = DuplexTypedMessagesFactory.CreateDuplexTypedMessageReceiver <string, int>();

            aReceiver.MessageReceived += (x, y) =>
            {
                aReceiver.SendResponseMessage(y.ResponseReceiverId, (y.RequestMessage * 10).ToString());
            };

            ISyncDuplexTypedMessageSender <string, int> aSender = DuplexTypedMessagesFactory.CreateSyncDuplexTypedMessageSender <string, int>();

            try
            {
                aReceiver.AttachDuplexInputChannel(InputChannel);
                aSender.AttachDuplexOutputChannel(OutputChannel);

                string aResult = aSender.SendRequestMessage(100);

                Assert.AreEqual("1000", aResult);
            }
            finally
            {
                aSender.DetachDuplexOutputChannel();
                aReceiver.DetachDuplexInputChannel();
            }
        }
コード例 #7
0
        public void Start()
        {
            if (myReceiver.IsDuplexInputChannelAttached)
            {
                // The channel is already attached so nothing to do.
                return;
            }

            // Use TCP communication
            IMessagingSystemFactory aMessaging = new TcpMessagingSystemFactory()
            {
                // Set to receive messages in the main UI thread.
                // Note: if this is not set then methods OnMessageReceived, OnClientConnected
                //       and OnClientDisconnected would not be called from main UI thread
                //       but from a listener thread.
                MaxAmountOfConnections = 5,
                //InputChannelThreading = new AsyncDispatching();
                //TODO
                // InputChannelThreading = new WinFormsDispatching(this)
            };

            // Create input channel.
            IDuplexInputChannel anInputChannel = aMessaging.CreateDuplexInputChannel(ServerIp);

            // Attach the input channel and be able to receive messages
            // and send back response messages.
            myReceiver.AttachDuplexInputChannel(anInputChannel);
        }
コード例 #8
0
ファイル: Program.cs プロジェクト: tvinod81/Load_Balancer
        static void Main(string[] args)
        {
            string aServiceAddress = GetMyAddress();

            if (aServiceAddress == "")
            {
                Console.WriteLine("The service could not start because all possible ports are occupied.");
                return;
            }

            // Create TCP messaging for receiving requests.
            IMessagingSystemFactory aMessaging     = new TcpMessagingSystemFactory();
            IDuplexInputChannel     anInputChannel = aMessaging.CreateDuplexInputChannel(aServiceAddress);

            // Create typed message receiver to receive requests.
            // It receives request messages of type Range and sends back
            // response messages of type double.
            IDuplexTypedMessagesFactory aReceiverFactory = new DuplexTypedMessagesFactory();
            IDuplexTypedMessageReceiver <double, Range> aReceiver
                = aReceiverFactory.CreateDuplexTypedMessageReceiver <double, Range>();

            // Subscribre to messages.
            aReceiver.MessageReceived += OnMessageReceived;

            // Attach the input channel and start listening.
            aReceiver.AttachDuplexInputChannel(anInputChannel);

            Console.WriteLine("Root Square Calculator listening to " + aServiceAddress +
                              " is running.\r\n Press ENTER to stop.");
            Console.ReadLine();

            // Detach the input channel and stop listening.
            aReceiver.DetachDuplexInputChannel();
        }
コード例 #9
0
        static void Main(string[] args)
        {
            // Instantiate Protocol Buffer based serializer.
            ISerializer aSerializer = new ProtoBufSerializer();

            // Create message receiver receiving 'MyRequest' and receiving 'MyResponse'.
            // The receiver will use Protocol Buffers to serialize/deserialize messages.
            IDuplexTypedMessagesFactory aReceiverFactory = new DuplexTypedMessagesFactory(aSerializer);
            myReceiver = aReceiverFactory.CreateDuplexTypedMessageReceiver<MyResponse, MyRequest>();

            // Subscribe to handle messages.
            myReceiver.MessageReceived += OnMessageReceived;

            // Create TCP messaging.
            IMessagingSystemFactory aMessaging = new TcpMessagingSystemFactory();

            IDuplexInputChannel anInputChannel
                = aMessaging.CreateDuplexInputChannel("tcp://127.0.0.1:8060/");

            // Attach the input channel and start to listen to messages.
            myReceiver.AttachDuplexInputChannel(anInputChannel);

            Console.WriteLine("The service is running. To stop press enter.");
            Console.ReadLine();

            // Detach the input channel and stop listening.
            // It releases the thread listening to messages.
            myReceiver.DetachDuplexInputChannel();
        }
コード例 #10
0
        public void ThreadDispatching()
        {
            IDuplexTypedMessageReceiver <string, int> aReceiver = DuplexTypedMessagesFactory.CreateDuplexTypedMessageReceiver <string, int>();

            aReceiver.MessageReceived += (x, y) =>
            {
                aReceiver.SendResponseMessage(y.ResponseReceiverId, (y.RequestMessage * 10).ToString());
            };

            // Set windows working thread dispatcher.
            Dispatcher         aWindowsDispatcher = WindowsDispatching.StartNewWindowsDispatcher();
            WindowsDispatching aThreadDispatching = new WindowsDispatching(aWindowsDispatcher);

            ((DuplexTypedMessagesFactory)DuplexTypedMessagesFactory).SyncDuplexTypedSenderThreadMode = aThreadDispatching;

            ISyncDuplexTypedMessageSender <string, int> aSender = DuplexTypedMessagesFactory.CreateSyncDuplexTypedMessageSender <string, int>();

            int aOpenConnectionThreadId = 0;

            aSender.ConnectionOpened += (x, y) =>
            {
                aOpenConnectionThreadId = Thread.CurrentThread.ManagedThreadId;
            };

            ManualResetEvent aConnectionClosedEvent = new ManualResetEvent(false);
            int aCloseConnectionThreadId            = 0;

            aSender.ConnectionClosed += (x, y) =>
            {
                aCloseConnectionThreadId = Thread.CurrentThread.ManagedThreadId;
                aConnectionClosedEvent.Set();
            };

            try
            {
                aReceiver.AttachDuplexInputChannel(InputChannel);
                aSender.AttachDuplexOutputChannel(OutputChannel);

                string aResult = aSender.SendRequestMessage(100);

                aReceiver.AttachedDuplexInputChannel.DisconnectResponseReceiver(aSender.AttachedDuplexOutputChannel.ResponseReceiverId);
                aConnectionClosedEvent.WaitOne();

                Assert.AreEqual("1000", aResult);
                Assert.AreEqual(aWindowsDispatcher.Thread.ManagedThreadId, aOpenConnectionThreadId);
                Assert.AreEqual(aWindowsDispatcher.Thread.ManagedThreadId, aCloseConnectionThreadId);
            }
            finally
            {
                aSender.DetachDuplexOutputChannel();
                aReceiver.DetachDuplexInputChannel();

                if (aWindowsDispatcher != null)
                {
                    WindowsDispatching.StopWindowsDispatcher(aWindowsDispatcher);
                }
            }
        }
コード例 #11
0
        public void AttachInputChannel()
        {
            anInputChannel = UnderlyingMessaging.CreateDuplexInputChannel(Self);

            switch (MessagesFactoryType)
            {
            case YZXMessagesFactoryType.Duplex:
                DReceiver.AttachDuplexInputChannel(anInputChannel);
                break;
            }
        }
コード例 #12
0
        public void DetachOutputChannelDuringWaitingForResponse()
        {
            IDuplexTypedMessageReceiver <int, int> aReceiver = DuplexTypedMessagesFactory.CreateDuplexTypedMessageReceiver <int, int>();

            ISyncDuplexTypedMessageSender <int, int> aSender = DuplexTypedMessagesFactory.CreateSyncDuplexTypedMessageSender <int, int>();

            try
            {
                aReceiver.AttachDuplexInputChannel(InputChannel);
                aSender.AttachDuplexOutputChannel(OutputChannel);

                // Send the request from a different thread.
                AutoResetEvent aWaitingInterrupted = new AutoResetEvent(false);
                Exception      aCaughtException    = null;
                ThreadPool.QueueUserWorkItem(x =>
                {
                    try
                    {
                        aSender.SendRequestMessage(100);
                    }
                    catch (Exception err)
                    {
                        aCaughtException = err;
                    }

                    aWaitingInterrupted.Set();
                });

                Thread.Sleep(100);

                // Detach the output channel while other thread waits for the response.
                aSender.DetachDuplexOutputChannel();

                Assert.IsTrue(aWaitingInterrupted.WaitOne(50000));

                Assert.IsTrue(aCaughtException != null && aCaughtException is InvalidOperationException);
            }
            finally
            {
                aSender.DetachDuplexOutputChannel();
                aReceiver.DetachDuplexInputChannel();
            }
        }
コード例 #13
0
        // Connect
        public void Connect(string channelID)
        {
            // Create message receiver receiving 'MyRequest' and receiving 'MyResponse'.
            IDuplexTypedMessagesFactory aReceiverFactory = new DuplexTypedMessagesFactory();

            myReceiver = aReceiverFactory.CreateDuplexTypedMessageReceiver <MyResponse, MyRequest>();

            // Subscribe to handle messages.
            myReceiver.MessageReceived += OnMessageReceived;

            // Create TCP messaging.
            IMessagingSystemFactory aMessaging     = new TcpMessagingSystemFactory();
            IDuplexInputChannel     anInputChannel =
                aMessaging.CreateDuplexInputChannel(channelID);

            // Attach the input channel and start to listen to messages.
            myReceiver.AttachDuplexInputChannel(anInputChannel);

            isConnected = true;
        }
コード例 #14
0
        public void WaitingForResponseTimeouted()
        {
            IDuplexTypedMessageReceiver <int, int> aReceiver = DuplexTypedMessagesFactory.CreateDuplexTypedMessageReceiver <int, int>();

            // Create sender expecting the response within 500 ms.
            IDuplexTypedMessagesFactory aSenderFactory       = new DuplexTypedMessagesFactory(TimeSpan.FromMilliseconds(500));
            ISyncDuplexTypedMessageSender <int, int> aSender = aSenderFactory.CreateSyncDuplexTypedMessageSender <int, int>();

            try
            {
                aReceiver.AttachDuplexInputChannel(InputChannel);
                aSender.AttachDuplexOutputChannel(OutputChannel);

                Assert.Throws <InvalidOperationException>(() => aSender.SendRequestMessage(100));
            }
            finally
            {
                aSender.DetachDuplexOutputChannel();
                aReceiver.DetachDuplexInputChannel();
            }
        }
コード例 #15
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);
        }
コード例 #16
0
        public void OpenServer()
        {
            IsClose = true;
            IsOpen = false;
            _receiver = _receiverFactory.Value.CreateDuplexTypedMessageReceiver<ChatMessage, ChatMessage>();
            _receiver.MessageReceived += OnMessageReceived;

            _receiver.ResponseReceiverConnected += OnClientConnected;
            _receiver.ResponseReceiverDisconnected += OnClientDisconnected;

            // Use webSocket for the communication
            IMessagingSystemFactory messaging = new WebSocketMessagingSystemFactory();

            // Attach input channel and be able to receive request messages and send back response messages.
            IDuplexInputChannel inputChannel = messaging.CreateDuplexInputChannel("ws://192.168.15.124:8091/");
            _receiver.AttachDuplexInputChannel(inputChannel);
        }