public static INodeEndpointProtocolServer WaitForServer <T>(
            this INodeEndpointProtocolServerListener serverListener,
            string address,
            IDuplexNodeEndpoint <T> endpoint,
            int timeout = DefaultTimeout
            )
            where T : INodeEndpointClient
        {
            INodeEndpointProtocolServer server = WaitForServerBase(serverListener, address, endpoint, timeout);

            if (server != null)
            {
                if (server.EnableDuplex)
                {
                    INodeEndpointClientProvider provider = new ProtocolEnabledClientProvider();
                    provider.Protocol = server;
                    T endpointInterface = StrongTypedNodeEndpointClientBuilder.Create <T>(provider);
                    endpoint.Callback = endpointInterface;
                }
                else
                {
                    server.Disconnect();
                    throw new InvalidOperationException("The protocol does not support duplex communication.");
                }
                server.BeginListen();
            }
            return(server);
        }
示例#2
0
        static void Main(string[] args)
        {
            INodeEndpointProtocolFactory        factory  = new NamedPipeProtocolFactory();
            INodeEndpointProtocolServerListener listener = factory.CreateServerListener();

            Thread serverThread = new Thread(() =>
            {
                while (true)
                {
                    CommunicationService service       = new CommunicationService();
                    INodeEndpointProtocolServer server = listener.WaitForServer("VczhCommunication", service);
                    lock (loggedInServices)
                    {
                        loggedInServices.Add(Tuple.Create(server, service));
                    }
                    SendMessageToAll("Someone logged in");
                }
            });

            serverThread.Start();

            Console.WriteLine("Press [ENTER] to exit.");
            Console.Read();

            serverThread.Abort();
        }
        public static INodeEndpointProtocolServer WaitForServer(
            this INodeEndpointProtocolServerListener serverListener,
            string address,
            INodeEndpoint endpoint,
            int timeout = DefaultTimeout
            )
        {
            INodeEndpointProtocolServer server = WaitForServerBase(serverListener, address, endpoint, timeout);

            if (server != null)
            {
                server.BeginListen();
            }
            return(server);
        }
        public static void TestProtocolAsync(INodeEndpointProtocolFactory serverFactory, INodeEndpointProtocolFactory clientFactory, string serverAddress, string clientAddress)
        {
            INodeEndpointProtocolServer server = null;

            Thread serverThread = new Thread(() =>
            {
                INodeEndpointProtocolServerListener serverListener = serverFactory.CreateServerListener();
                server = serverListener.WaitForServer(serverAddress, new CalculationEndpoint(true));
            });

            serverThread.Start();

            ICalculationEndpointAsync client = clientFactory.WaitForClient <ICalculationEndpointAsync>(clientAddress, "Calculation");

            Assert.IsNotNull(client);

            Assert.AreEqual(3, client.Add(2, 1).Result);
            Assert.AreEqual(1, client.Sub(2, 1).Result);
            Assert.AreEqual(2, client.Mul(2, 1).Result);
            Assert.AreEqual(2, client.Div(2, 1).Result);

            Point point = client.Swap(new Point {
                X = 1, Y = 2
            }).Result;

            Assert.AreEqual(2, point.X);
            Assert.AreEqual(1, point.Y);

            Cat cat = (Cat)client.CopyAnimal(new Cat {
                name = "cat", catName = "bigcat"
            }).Result;

            Assert.AreEqual("cat", cat.name);
            Assert.AreEqual("bigcat", cat.catName);

            Dog dog = (Dog)client.CopyAnimal(new Dog {
                name = "dog", dogName = "bigdog"
            }).Result;

            Assert.AreEqual("dog", dog.name);
            Assert.AreEqual("bigdog", dog.dogName);

            client.SendMessage("Vczh is a genius!").Wait();
            Assert.AreEqual("Vczh is a genius!", client.ReceiveMessage().Result);
        }
        public static void TestProtocolDuplex(INodeEndpointProtocolFactory serverFactory, INodeEndpointProtocolFactory clientFactory, string serverAddress, string clientAddress)
        {
            INodeEndpointProtocolServer server = null;

            Thread serverThread = new Thread(() =>
            {
                INodeEndpointProtocolServerListener listener = serverFactory.CreateServerListener();
                server = listener.WaitForServer(serverAddress, new DuplexServer());
            });

            serverThread.Start();

            IDuplexServer client = clientFactory.WaitForClient <IDuplexServer, DuplexCallback>(clientAddress, "DuplexServer", new DuplexCallback());

            Assert.IsNotNull(client);

            Assert.AreEqual(15, client.Add(5));
        }
        private static INodeEndpointProtocolServer WaitForServerBase(
            this INodeEndpointProtocolServerListener serverListener,
            string address,
            INodeEndpoint endpoint,
            int timeout
            )
        {
            if (!serverListener.Connected)
            {
                serverListener.Connect(address, endpoint.EndpointName);
            }
            INodeEndpointProtocolServer server = serverListener.Listen(timeout);

            if (server != null)
            {
                INodeEndpointProtocolRequestListener endpointListener = new ProtocolEnabledRequestListener(endpoint);
                server.AddListener(endpointListener);
            }
            return(server);
        }
示例#7
0
        private static void PortListeningThreadProc(int port, INodeEndpointProtocolServerListener serverListener)
        {
            List <INodeEndpointProtocolServer> servers = new List <INodeEndpointProtocolServer>();

            try
            {
                while (true)
                {
                    if (!serverListener.Connected)
                    {
                        serverListener.Connect(port.ToString(), TcpShareProviderServiceConfiguration.EndpointName);
                    }
                    if (!serverListener.Connected)
                    {
                        lock (portListeningThreads)
                        {
                            serverListener.Disconnect();
                            portListeningThreads.Remove(port);
                            break;
                        }
                    }

                    INodeEndpointProtocolServer server = serverListener.Listen(NodeEndpointProtocolFactoryExtension.DefaultTimeout);
                    if (server != null)
                    {
                        TcpShareProviderServiceRedirector redirector = new TcpShareProviderServiceRedirector(port, outFactory, server);
                        server.AddListener(redirector);
                        server.BeginListen();
                        servers.Add(server);
                    }

                    ClearServices(servers, true);
                }
            }
            catch (ThreadAbortException)
            {
            }
            ClearServices(servers, false);
        }
示例#8
0
 public StreamServerProtocol(INodeEndpointProtocolServerListener serverListener)
     : base(serverListener.Factory)
 {
     this.serverListener = serverListener;
 }
示例#9
0
 public Server(Socket acceptedSocket, INodeEndpointProtocolServerListener serverListener)
     : base(serverListener)
 {
     this.acceptedSocket = acceptedSocket;
     this.Stream         = new NetworkStream(this.acceptedSocket, FileAccess.ReadWrite, false);
 }
示例#10
0
 public Server(NamedPipeServerStream stream, INodeEndpointProtocolServerListener serverListener)
     : base(serverListener)
 {
     this.Stream = stream;
 }
示例#11
0
        public static void TestProtocol(INodeEndpointProtocolFactory serverFactory, INodeEndpointProtocolFactory clientFactory, string serverAddress, string clientAddress)
        {
            INodeEndpointProtocolServer server = null;

            Thread serverThread = new Thread(() =>
            {
                INodeEndpointProtocolServerListener serverListener = serverFactory.CreateServerListener();
                server = serverListener.WaitForServer(serverAddress, new CalculationEndpoint(true));
            });

            serverThread.Start();

            ICalculationEndpoint client = clientFactory.WaitForClient <ICalculationEndpoint>(clientAddress, "Calculation");

            Assert.IsNotNull(client);

            Assert.AreEqual(3, client.Add(2, 1));
            Assert.AreEqual(1, client.Sub(2, 1));
            Assert.AreEqual(2, client.Mul(2, 1));
            Assert.AreEqual(2, client.Div(2, 1));

            Point point = client.Swap(new Point {
                X = 1, Y = 2
            });

            Assert.AreEqual(2, point.X);
            Assert.AreEqual(1, point.Y);

            Cat cat = (Cat)client.CopyAnimal(new Cat {
                name = "cat", catName = "bigcat"
            });

            Assert.AreEqual("cat", cat.name);
            Assert.AreEqual("bigcat", cat.catName);

            Dog dog = (Dog)client.CopyAnimal(new Dog {
                name = "dog", dogName = "bigdog"
            });

            Assert.AreEqual("dog", dog.name);
            Assert.AreEqual("bigdog", dog.dogName);

            client.SendMessage("Vczh is a genius!");
            Assert.AreEqual("Vczh is a genius!", client.ReceiveMessage());

            AssertCollection(client.CopyArray(Enumerable.Range(0, 10).ToArray()));
            AssertCollection(client.CopyList(new List <int>(Enumerable.Range(0, 10))));
            AssertCollection(client.CopyHashSet(new HashSet <int>(Enumerable.Range(0, 10))));
            AssertCollection(client.CopyLinkedList(new LinkedList <int>(Enumerable.Range(0, 10))));
            AssertCollection(client.CopyQueue(new Queue <int>(Enumerable.Range(0, 10))));
            AssertCollection(client.CopySortedSet(new SortedSet <int>(Enumerable.Range(0, 10))));
            AssertCollection(client.CopyStack(new Stack <int>(Enumerable.Range(0, 10).Reverse())));

            Dictionary <int, int> dictionary = Enumerable.Range(0, 10).ToDictionary(i => i);

            AssertCollection(client.CopyDictionary(dictionary));
            AssertCollection(client.CopySortedDictionary(new SortedDictionary <int, int>(dictionary)));
            AssertCollection(client.CopySortedList(new SortedList <int, int>(dictionary)));

            byte[] bytes = new byte[] { 1, 2, 3, 4, 5 };
            using (Stream stream = client.CopyStream(bytes))
            {
                byte[] copied = stream.ReadAllBytes();
                Assert.AreEqual("[1][2][3][4][5]", bytes.Select(b => "[" + b.ToString() + "]").Aggregate("", (a, b) => a + b));
            }
        }
示例#12
0
 public ServerListener(INodeEndpointProtocolServerListener outerServerListener, ITranslatorProtocolHandlerFactory handlerFactory, TranslatorProtocolFactory factory)
 {
     this.factory             = factory;
     this.outerServerListener = outerServerListener;
     this.handlerFactory      = handlerFactory;
 }