public static T WaitForClient <T, U>(
            this INodeEndpointProtocolFactory protocolFactory,
            string address,
            string endpointName,
            U callback,
            int timeout = 5000
            )
            where T : IDuplexNodeEndpointClient <U>
            where U : INodeEndpoint
        {
            T client = WaitForClient <T>(protocolFactory, address, endpointName, timeout);

            if (!object.ReferenceEquals(client, default(T)))
            {
                if (client.Provider.Protocol.EnableDuplex)
                {
                    INodeEndpointProtocolRequestListener endpointListener = new ProtocolEnabledRequestListener(callback);
                    client.Provider.Protocol.AddListener(endpointListener);
                    client.Callback = callback;
                }
                else
                {
                    client.Dispose();
                    throw new InvalidOperationException("The protocol does not support duplex communication.");
                }
            }
            return(client);
        }
Ejemplo n.º 2
0
        public ServiceReflector(XElement serviceDescription)
        {
            this.factory         = BuildFactory(serviceDescription.Element("Protocol"), out this.outestFactory);
            this.protocolAddress = serviceDescription.Element("Address").Element("ProtocolAddress").Value;
            this.endpointName    = serviceDescription.Element("Address").Element("EndpointName").Value;

            this.methods = serviceDescription
                           .Element("Contract")
                           .Element("Methods")
                           .Elements("Method")
                           .Select(m => new MethodContract(m))
                           .ToArray();
        }
Ejemplo n.º 3
0
        private static INodeEndpointProtocolFactory BuildFactory(XElement protocolDescription, out INodeEndpointProtocolFactory outestFactory)
        {
            outestFactory = null;
            INodeEndpointProtocolFactory factory = null;

            foreach (var factoryDescription in protocolDescription.Elements().Reverse())
            {
                factory = BuildFactory(factoryDescription, factory);
                if (outestFactory == null)
                {
                    outestFactory = factory;
                }
            }
            return(factory);
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
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));
        }
        public static T WaitForClient <T>(
            this INodeEndpointProtocolFactory protocolFactory,
            string address,
            string endpointName,
            int timeout = DefaultTimeout
            )
            where T : INodeEndpointClient
        {
            INodeEndpointProtocolClient client = protocolFactory.CreateClient();

            if (client.Connect(address, endpointName, timeout))
            {
                INodeEndpointClientProvider provider = new ProtocolEnabledClientProvider();
                provider.Protocol = client;
                T endpointInterface = StrongTypedNodeEndpointClientBuilder.Create <T>(provider);
                client.BeginListen();
                return(endpointInterface);
            }
            else
            {
                return(default(T));
            }
        }
 public static void LaunchService(string executablePath, string arguments, string name, INodeEndpointProtocolFactory protocolFactory, string protocolAddress, string endpointName)
 {
     DuplexGuardServiceStarterInternal <TService, TCallback, GuardServiceStarterServerCallback <TService> > .LaunchService(
         executablePath,
         arguments,
         name,
         new GuardServiceStarterServerCallback <TService>(protocolFactory, protocolAddress, endpointName)
         );
 }
 public GuardServiceStarterServerCallback(INodeEndpointProtocolFactory protocolFactory, string protocolAddress, string endpointName)
 {
     this.protocolFactory = protocolFactory;
     this.protocolAddress = protocolAddress;
     this.endpointName    = endpointName;
 }
Ejemplo n.º 9
0
 public Client(INodeEndpointProtocolFactory factory)
     : base(factory)
 {
 }
Ejemplo n.º 10
0
 public ServerListener(INodeEndpointProtocolFactory factory)
 {
     this.factory = factory;
 }
Ejemplo n.º 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));
            }
        }
Ejemplo n.º 12
0
 public static void TestProtocolDuplex(INodeEndpointProtocolFactory factory, string serverAddress, string clientAddress)
 {
     TestProtocolDuplex(factory, factory, serverAddress, clientAddress);
 }
Ejemplo n.º 13
0
 public ServerListener(INodeEndpointProtocolFactory factory)
     : base(factory)
 {
 }
Ejemplo n.º 14
0
 public StreamClientProtocol(INodeEndpointProtocolFactory factory)
     : base(factory)
 {
 }
Ejemplo n.º 15
0
 public Client(AddressFamily addressFamily, ProtocolType protocolType, INodeEndpointProtocolFactory factory)
     : base(factory)
 {
     this.addressFamily = addressFamily;
     this.protocolType  = protocolType;
 }
 public static INodeEndpointProtocolFactory With(this INodeEndpointProtocolFactory factory, ITranslatorProtocolHandlerSimple handler)
 {
     return(factory.With(new TranslatorProtocolHandlerFactorySimple(handler)));
 }
 public static INodeEndpointProtocolFactory With(this INodeEndpointProtocolFactory factory, ITranslatorProtocolHandlerFactory handlerFactory)
 {
     return(new TranslatorProtocolFactory(factory, handlerFactory));
 }
Ejemplo n.º 18
0
 public ServerListener(AddressFamily addressFamily, ProtocolType protocolType, INodeEndpointProtocolFactory factory)
 {
     this.addressFamily = addressFamily;
     this.protocolType  = protocolType;
     this.factory       = factory;
 }
Ejemplo n.º 19
0
 public MainForm()
 {
     this.protocolFactory = MachineInfoServiceConfiguration.CreateFactory();
     InitializeComponent();
 }
Ejemplo n.º 20
0
 public StreamProtocol(INodeEndpointProtocolFactory factory)
 {
     this.factory = factory;
 }
Ejemplo n.º 21
0
        private static INodeEndpointProtocolFactory BuildFactory(XElement factoryDescription, INodeEndpointProtocolFactory previousFactory)
        {
            string factoryName = factoryDescription.Name.LocalName;

            switch (factoryName)
            {
            case "NamedPipeProtocolFactory":
            {
                if (previousFactory != null)
                {
                    throw new ArgumentException(factoryName + " should be the outest protocol.");
                }
                return(new NamedPipeProtocolFactory());
            }

            case "TcpProtocolFactory":
            {
                if (previousFactory != null)
                {
                    throw new ArgumentException(factoryName + " should be the outest protocol.");
                }
                TcpProtocolFactory factory = new TcpProtocolFactory();
                factory.AddressFamily = (AddressFamily)typeof(AddressFamily)
                                        .GetField(factoryDescription.Element("AddressFamily").Value, BindingFlags.Public | BindingFlags.Static)
                                        .GetValue(null);
                factory.ProtocolType = (ProtocolType)typeof(ProtocolType)
                                       .GetField(factoryDescription.Element("ProtocolType").Value, BindingFlags.Public | BindingFlags.Static)
                                       .GetValue(null);
                return(factory);
            }

            case "HttpProtocolFactory":
            {
                if (previousFactory != null)
                {
                    throw new ArgumentException(factoryName + " should be the outest protocol.");
                }
                return(new HttpProtocolFactory());
            }

            case "TcpShareProtocolFactory":
            {
                if (previousFactory != null)
                {
                    throw new ArgumentException(factoryName + " should be the outest protocol.");
                }
                return(new TcpShareProtocolFactory());
            }

            case "TranslatorProtocolFactory":
            {
                if (previousFactory == null)
                {
                    throw new ArgumentException(factoryName + " cannot be the outest protocol.");
                }
                XElement handlerDescription = (XElement)factoryDescription.FirstNode;
                string   handlerName        = handlerDescription.Name.LocalName;
                switch (handlerName)
                {
                case "TranslatorProtocolHandlerFactorySimple":
                {
                    XElement handlerSimpleDescription = (XElement)handlerDescription.FirstNode;
                    string   handlerSimpleName        = handlerSimpleDescription.Name.LocalName;
                    switch (handlerSimpleName)
                    {
                    case "GzipProtocolHandler":
                        return(previousFactory.With(new GzipProtocolHandler()));

                    default:
                        throw new ArgumentException("Don't know how to deal with " + handlerSimpleName + ".");
                    }
                }

                default:
                    throw new ArgumentException("Don't know how to deal with " + handlerName + ".");
                }
            }

            default:
                throw new ArgumentException("Don't know how to deal with " + factoryName + ".");
            }
        }
Ejemplo n.º 22
0
 public Client(INodeEndpointProtocolFactory factory)
     : base(AddressFamily.InterNetwork, ProtocolType.Tcp, factory)
 {
 }
Ejemplo n.º 23
0
 public TranslatorProtocolFactory(INodeEndpointProtocolFactory outerFactory, ITranslatorProtocolHandlerFactory handlerFactory)
 {
     this.OuterFactory   = outerFactory;
     this.HandlerFactory = handlerFactory;
 }