Exemple #1
0
        static void Main(string[] args)
        {
            Console.Title = "Resonance Shared Memory Demo";

            Console.WriteLine("Resonance Shared Memory Demo.");
            Console.WriteLine();

            Console.Write("Adapter Address: ");
            String address = Console.ReadLine();

            Console.WriteLine("Connecting...");

            IResonanceTransporter transporter = ResonanceTransporter.Builder
                                                .Create()
                                                .WithSharedMemoryAdapter()
                                                .WithAddress(address)
                                                .WithJsonTranscoding()
                                                .Build();

            transporter.StateChanged += (_, e) =>
            {
                if (e.NewState == ResonanceComponentState.Failed)
                {
                    Console.WriteLine();
                    Console.WriteLine($"Transporter Disconnected.\n{transporter.FailedStateException.Message}");
                }
            };

            transporter.RegisterMessageHandler <SharedMemoryMessage>((t, msg) =>
            {
                Console.WriteLine();
                Console.WriteLine($"Message Received: {msg.Object.Text}");
                Console.Write("Send Message: ");
            });

            transporter.Connect();

            Console.WriteLine("Connected.");

            while (true)
            {
                Console.Write("Send Message: ");
                String text = Console.ReadLine();

                if (text.ToLower() == "exit")
                {
                    Console.WriteLine("Disconnecting...");
                    transporter.Disconnect();
                    Console.ReadLine();
                    return;
                }

                transporter.Send(new SharedMemoryMessage()
                {
                    Text = text
                });
            }
        }
Exemple #2
0
        public void Transporters_Router_Standard_Propagates_Disconnection()
        {
            IResonanceTransporter receiver1 = null;
            IResonanceTransporter client1   = null;
            IResonanceTransporter receiver2 = null;
            IResonanceTransporter client2   = null;
            TransporterRouter     router    = null;

            ResonanceTcpServer server = new ResonanceTcpServer(1333);

            server.ConnectionRequest += (x, e) =>
            {
                if (receiver1 == null)
                {
                    receiver1 = ResonanceTransporter.Builder.Create()
                                .WithAdapter(e.Accept())
                                .WithJsonTranscoding()
                                .Build();

                    receiver1.Connect();
                }
                else
                {
                    receiver2 = ResonanceTransporter.Builder.Create()
                                .WithAdapter(e.Accept())
                                .WithJsonTranscoding()
                                .Build();

                    receiver2.Connect();

                    router = new TransporterRouter(receiver1, receiver2, RoutingMode.TwoWay, WritingMode.Standard);
                    router.Bind();
                }
            };
            server.Start();


            client1 = ResonanceTransporter.Builder.Create()
                      .WithTcpAdapter()
                      .WithAddress("127.0.0.1")
                      .WithPort(1333)
                      .WithJsonTranscoding()
                      .Build();

            client1.Connect();

            client2 = ResonanceTransporter.Builder.Create()
                      .WithTcpAdapter()
                      .WithAddress("127.0.0.1")
                      .WithPort(1333)
                      .WithJsonTranscoding()
                      .Build();

            client2.Connect();

            Thread.Sleep(1000);

            client1.Disconnect();

            Thread.Sleep(500);

            Assert.IsTrue(client1.State == ResonanceComponentState.Disconnected);
            Assert.IsTrue(receiver1.State == ResonanceComponentState.Failed);
            Assert.IsTrue(receiver2.State == ResonanceComponentState.Disconnected);
            Assert.IsTrue(client2.State == ResonanceComponentState.Failed);


            client1.Dispose();
            client2.Dispose();
            receiver1.Dispose();
            receiver2.Dispose();
            server.Dispose();
            router.Dispose();
        }
Exemple #3
0
        public void Transporters_Router_Standard_Propagates_Connection_Loss()
        {
            IResonanceTransporter receiver1 = null;
            IResonanceTransporter client1   = null;
            IResonanceTransporter receiver2 = null;
            IResonanceTransporter client2   = null;
            TransporterRouter     router    = null;

            ResonanceTcpServer server = new ResonanceTcpServer(1333);

            server.ConnectionRequest += (x, e) =>
            {
                if (receiver1 == null)
                {
                    receiver1 = ResonanceTransporter.Builder.Create()
                                .WithAdapter(e.Accept())
                                .WithJsonTranscoding()
                                .WithKeepAlive(TimeSpan.FromSeconds(1), 1)
                                .Build();

                    receiver1.Connect();
                }
                else
                {
                    receiver2 = ResonanceTransporter.Builder.Create()
                                .WithAdapter(e.Accept())
                                .WithJsonTranscoding()
                                .WithKeepAlive()
                                .Build();

                    receiver2.Connect();

                    router = new TransporterRouter(receiver1, receiver2, RoutingMode.TwoWay, WritingMode.Standard);
                    router.Bind();
                }
            };
            server.Start();


            client1 = ResonanceTransporter.Builder.Create()
                      .WithTcpAdapter()
                      .WithAddress("127.0.0.1")
                      .WithPort(1333)
                      .WithJsonTranscoding()
                      .Build();

            client1.NotifyOnDisconnect = false;
            client1.KeepAliveConfiguration.EnableAutoResponse = false;

            client1.Connect();

            client2 = ResonanceTransporter.Builder.Create()
                      .WithTcpAdapter()
                      .WithAddress("127.0.0.1")
                      .WithPort(1333)
                      .WithJsonTranscoding()
                      .WithKeepAlive()
                      .Build();

            client2.Connect();

            Thread.Sleep(15000);

            client1.Disconnect();

            Assert.IsTrue(client1.State == ResonanceComponentState.Disconnected);
            Assert.IsTrue(receiver1.State == ResonanceComponentState.Failed);
            Assert.IsTrue(receiver2.State == ResonanceComponentState.Disconnected);
            Assert.IsTrue(client2.State == ResonanceComponentState.Failed);


            client1.Dispose();
            client2.Dispose();
            receiver1.Dispose();
            receiver2.Dispose();
            server.Dispose();
            router.Dispose();
        }
        private void DoRouting(ResonancePreviewDecodingInfoEventArgs e, IResonanceTransporter transporter)
        {
            try
            {
                if (transporter.State == ResonanceComponentState.Connected)
                {
                    if (RoutingMode == RoutingMode.TwoWay ||
                        (transporter == SourceTransporter && RoutingMode == RoutingMode.OneWayToSource) ||
                        (transporter == TargetTransporter && RoutingMode == RoutingMode.OneWayToTarget))
                    {
                        if (e.DecodingInformation.Type == ResonanceTranscodingInformationType.KeepAliveRequest)
                        {
                            return;
                        }
                        if (e.DecodingInformation.Type == ResonanceTranscodingInformationType.KeepAliveResponse)
                        {
                            return;
                        }

                        if (e.DecodingInformation.Type != ResonanceTranscodingInformationType.Disconnect)
                        {
                            e.Handled = true;

                            var args = new ResonancePreviewDecodingInfoEventArgs();
                            args.DecodingInformation = e.DecodingInformation;
                            args.RawData             = e.RawData;

                            if (transporter == SourceTransporter)
                            {
                                OnPreviewSourceDecodingInformation(args);
                            }
                            else
                            {
                                OnPreviewTargetDecodingInformation(args);
                            }

                            if (args.Handled)
                            {
                                return;
                            }

                            if (WritingMode == WritingMode.Standard)
                            {
                                ResonanceEncodingInformation info = new ResonanceEncodingInformation();
                                info.Completed    = e.DecodingInformation.Completed;
                                info.ErrorMessage = e.DecodingInformation.ErrorMessage;
                                info.HasError     = e.DecodingInformation.HasError;
                                info.IsCompressed = e.DecodingInformation.IsCompressed;
                                info.Message      = e.DecodingInformation.Message;
                                info.RPCSignature = e.DecodingInformation.RPCSignature;
                                info.Timeout      = e.DecodingInformation.Timeout;
                                info.Token        = e.DecodingInformation.Token;
                                info.Transcoding  = e.DecodingInformation.Transcoding;
                                info.Type         = e.DecodingInformation.Type;

                                transporter.SubmitEncodingInformation(info);
                            }
                            else
                            {
                                transporter.Adapter.Write(e.RawData);
                            }
                        }
                        else if (RouteDisconnection)
                        {
                            Task.Factory.StartNew(() =>
                            {
                                try
                                {
                                    if (transporter == SourceTransporter)
                                    {
                                        Logger.LogInformation($"Disconnection notification was received by source transporter. disconnecting target transporter...");
                                    }
                                    else
                                    {
                                        Logger.LogInformation($"Disconnection notification was received by target transporter. disconnecting source transporter...");
                                    }

                                    transporter.Disconnect();
                                }
                                catch (Exception ex)
                                {
                                    Logger.LogError(ex, "Error occurred while trying to disconnect the transporter.");
                                }
                            });
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, "Unexpected routing error has occurred.");
            }
        }