コード例 #1
0
        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
ファイル: Program.cs プロジェクト: vczh-codeplex/vlpp
        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();
        }
コード例 #3
0
 public Server(INodeEndpointProtocolServer outerProtocol, ITranslatorProtocolHandlerFactory handlerFactory, ServerListener serverListener)
     : base((TranslatorProtocolFactory)serverListener.Factory)
 {
     this.serverListener = serverListener;
     this.serverHandler  = handlerFactory.CreateServerHandler();
     SetOuterProtocol(outerProtocol);
     this.Handler = this.serverHandler;
     this.serverHandler.AttachServer(this);
 }
コード例 #4
0
 protected virtual void InstallServerTracer(INodeEndpointProtocolServer server)
 {
     foreach (var listener in server.GetListeners())
     {
         var acceptableListener = listener as INodeEndpointProtocolRequestTracableListener;
         if (acceptableListener != null)
         {
             acceptableListener.AddTracer(this.tracer);
         }
     }
 }
コード例 #5
0
            public INodeEndpointProtocolServer Listen(int timeout)
            {
                INodeEndpointProtocolServer server = this.outerServerListener.Listen(timeout);

                if (server == null)
                {
                    return(null);
                }
                else
                {
                    return(new Server(server, this.handlerFactory, this));
                }
            }
コード例 #6
0
 public void SetOuterProtocol(INodeEndpointProtocolServer protocol)
 {
     if (this.outerProtocol != null)
     {
         this.outerProtocol.OnInnerProtocolSet(null);
     }
     this.outerProtocol  = protocol;
     this.ParentProtocol = protocol;
     if (this.outerProtocol != null)
     {
         this.outerProtocol.OnInnerProtocolSet(this);
     }
 }
コード例 #7
0
        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);
        }
コード例 #8
0
ファイル: Program.cs プロジェクト: vczh-codeplex/vlpp
        private static void ClearServices(List <INodeEndpointProtocolServer> servers, bool disconnectedOnly)
        {
            lock (servers)
            {
                for (int i = servers.Count - 1; i >= 0; i--)
                {
                    INodeEndpointProtocolServer server = servers[i];

                    if (!disconnectedOnly || !server.Connected)
                    {
                        server.Disconnect();
                        servers.RemoveAt(i);
                    }
                }
            }
        }
コード例 #9
0
        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);
        }
コード例 #10
0
        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));
        }
コード例 #11
0
        private void ClearServices(bool disconnectedOnly)
        {
            lock (this.runningServices)
            {
                for (int i = this.runningServices.Count - 1; i >= 0; i--)
                {
                    var pair     = this.runningServices[i];
                    T   endpoint = pair.Item1;
                    INodeEndpointProtocolServer server = pair.Item2;

                    if (!disconnectedOnly || !server.Connected)
                    {
                        this.callback.OnEndpointStopped(endpoint, server);
                        server.Disconnect();
                        endpoint.Dispose();
                        this.runningServices.RemoveAt(i);
                    }
                }
            }
        }
コード例 #12
0
        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);
        }
コード例 #13
0
ファイル: Program.cs プロジェクト: vczh-codeplex/vlpp
        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);
        }
コード例 #14
0
 private void ServerListenerThreadProc()
 {
     while (!this.needToStop)
     {
         T endpoint = this.callback.CreateEndpoint();
         INodeEndpointProtocolServer server = CreateProtocolServer(endpoint, NodeEndpointProtocolFactoryExtension.DefaultTimeout);
         if (server == null)
         {
             endpoint.Dispose();
         }
         else
         {
             InstallServerTracer(server);
             lock (this.runningServices)
             {
                 this.runningServices.Add(Tuple.Create(endpoint, server));
                 this.callback.OnEndpointStart(endpoint, server);
             }
         }
         ClearServices(true);
     }
 }
コード例 #15
0
 public virtual void SetOuterProtocol(INodeEndpointProtocolServer protocol)
 {
     throw new InvalidOperationException("The protocol server cannot have a outer protocol.");
 }
コード例 #16
0
 public void OnEndpointStopped(Endpoint endpoint, INodeEndpointProtocolServer protocolServer)
 {
     Interlocked.Increment(ref this.endpointStoppedCounter);
 }
コード例 #17
0
ファイル: Program.cs プロジェクト: vczh-codeplex/vlpp
 public void OnEndpointStopped(ExpressionService endpoint, INodeEndpointProtocolServer protocolServer)
 {
 }
コード例 #18
0
ファイル: Program.cs プロジェクト: vczh-codeplex/vlpp
 public TcpShareProviderServiceRedirector(int port, NamedPipeProtocolFactory protocolFactory, INodeEndpointProtocolServer server)
 {
     this.protocolFactory = protocolFactory;
     this.server          = server;
     this.port            = port;
 }
コード例 #19
0
ファイル: Program.cs プロジェクト: vczh-codeplex/vlpp
 public ResponseListener(INodeEndpointProtocolServer server)
 {
     this.server = server;
 }
コード例 #20
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));
            }
        }
コード例 #21
0
 public void OnEndpointStopped(GuardService endpoint, INodeEndpointProtocolServer protocolServer)
 {
     this.sharedData.Unregister(endpoint);
 }
コード例 #22
0
 public void OnEndpointStart(GuardService endpoint, INodeEndpointProtocolServer protocolServer)
 {
 }
コード例 #23
0
 public virtual void OnInnerProtocolSet(INodeEndpointProtocolServer protocol)
 {
     this.innerProtocol = protocol;
 }
コード例 #24
0
 public void AttachServer(INodeEndpointProtocolServer server)
 {
 }
コード例 #25
0
 public void AttachServer(INodeEndpointProtocolServer server)
 {
     this.server = server;
 }
コード例 #26
0
 public void OnEndpointStopped(CalculationService endpoint, INodeEndpointProtocolServer protocolServer)
 {
 }