示例#1
0
        static void Main(string[] args)
        {
            //Create chat server instance
            var chatServer = new ChatServer();

            //Remote Hub Switch
            remoteHubSwitch = new RemoteHubSwitch();
            remoteHubSwitch.ConnectionErrorOccurred += RemoteHubSwitch_ConnectionErrorOccurred;
            remoteHubSwitch.AdapterRemoved          += RemoteHubSwitch_AdapterRemoved;

            //Tcp server
            ClientEntity.InitializeServerCertificate(serverIP, "TestServer");
            tcpListener = new TcpListener(serverIP, serverPort);
            tcpListener.Start();
            var accepting = Task.Run(() => TcpServerAcceptLinks(shutdownSignal.Token));

            //Local client on RemoteHub
            var siteId = ServerId.SiteId;

            localClient = new RemoteHubSwitchDirect <string>(siteId, OnMessageReceivedFromRemoteHub);
            localClient.RemoteClientRemoved += RemoteHubClient_RemoteClientRemoved;
            remoteHubSwitch.AddAdapter(localClient);

            //Remote Agency
            remoteAgency = new RemoteAgencyManagerEncapsulated(false, true, siteId);
            remoteAgency.MessageForSendingPrepared += OnMessageForSendingPrepared;
            remoteAgency.AddServiceWrapper <IChatServer>(chatServer, ServerId.ServiceId);

            //localClient.Start(); //started already. when SwitchDirect is added to switch, it will be started automatically.
            remoteAgency.Connect();

            Console.WriteLine("Chat server is started. Press any key to quit.");
            Console.ReadKey(true);

            shutdownSignal.Cancel();
            tcpListener.Stop();
            remoteAgency.Disconnect(false);
            remoteAgency.Dispose();
            localClient.Stop();
            localClient.Dispose();
            remoteHubSwitch.RemoveAllAdapters();
            foreach (var client in clients)
            {
                client.Dispose();
            }
            clients.Clear();
        }
        static void VirtualHostTest(RemoteHubSwitch remoteHubSwitch1, StreamAdapter <byte[]> streamAdaptersOnSwitch1, IRemoteHub <string> sender)
        {
            sender.RemoteClientUpdated += Sender_RemoteClientUpdated;

            //reg
            Console.WriteLine("Please wait for several seconds and press any key to reg virtual hosts...");
            Console.ReadKey(true);

            Guid virtualHostId = Guid.NewGuid();

            foreach (var client in clients)
            {
                client.ApplyVirtualHosts(new KeyValuePair <Guid, VirtualHostSetting>(virtualHostId, new VirtualHostSetting(0, 1)));
            }
            //clients[0].ApplyVirtualHosts(new KeyValuePair<Guid, VirtualHostSetting>(virtualHostId, new VirtualHostSetting(1, 1))); //this command will add higher setting on only clients[0] which will suppress all other clients on the same virtual host.

            Console.WriteLine("Please wait for several seconds and press any key to continue sending test...");
            Console.ReadKey(true);

            //send
            for (int i = 0; i < 100; i++)
            {
                if (sender.TryResolveVirtualHost(virtualHostId, out var hostId))
                {
                    string testMessage = string.Format("<-- {0:D2}: To Virtual Host on {1} -->", i, clientNames[hostId]);
                    waitingTexts.Add(testMessage);
                    sender.SendMessage(hostId, testMessage);
                }
            }

            Console.WriteLine("Press any key to continue...");
            Console.ReadKey(true);
            if (waitingTexts.Count > 0)
            {
                foreach (var text in waitingTexts)
                {
                    Console.WriteLine("Missing message: " + text);
                }
                waitingTexts.Clear();
            }
            else
            {
                Console.WriteLine("All message received.");
            }

            //disconnect switch
            Console.WriteLine("Disconnecting...");
            remoteHubSwitch1.RemoveAdapter(streamAdaptersOnSwitch1);

            //add switch direct link
            Console.WriteLine("Adding client...");
            RemoteHubSwitchDirect <string> clientDirect = new RemoteHubSwitchDirect <string>(Guid.NewGuid(), Received);

            adapterNamesForSwitch1[clientDirect] = "To SwitchDirect"; //name the new created adapter as To SwitchDirect
            clients.Add(clientDirect);
            clientNames.Add(clientDirect.ClientId, "SwitchDirect");   //name the client as SwitchDirect
            remoteHubSwitch1.AddAdapter(clientDirect);

            //set another virtual host
            clientDirect.ApplyVirtualHosts(new KeyValuePair <Guid, VirtualHostSetting>(virtualHostId, new VirtualHostSetting(0, 3)));
            Console.WriteLine("Please wait a while for client (SwitchDirect) discovery and press any key to continue...");

            //resume switch connection
            Console.WriteLine("Reconnecting...");
            remoteHubSwitch1.AddAdapter(streamAdaptersOnSwitch1);

            Console.ReadKey(true);
            //send
            for (int i = 0; i < 100; i++)
            {
                if (sender.TryResolveVirtualHost(virtualHostId, out var hostId))
                {
                    string testMessage = string.Format("<-- {0:D2}: To Virtual Host on {1} -->", i, clientNames[hostId]);
                    waitingTexts.Add(testMessage);
                    sender.SendMessage(hostId, testMessage);
                }
            }

            Console.WriteLine("Press any key to continue...");
            Console.ReadKey(true);

            if (waitingTexts.Count > 0)
            {
                foreach (var text in waitingTexts)
                {
                    Console.WriteLine("Missing message: " + text);
                }
                waitingTexts.Clear();
            }
            else
            {
                Console.WriteLine("All message received.");
            }
        }