示例#1
0
        public void RemoteReferences()
        {
            using (var setup = new ClientServerTester(FailMethod, FailMethod))
            {
                var client = new RPCPeer(setup.Client);
                var server = new RPCPeer(setup.Server, typeof(RemoteInvokeAble));

                // Let the client create proxy objects
                client.AddProxyGenerator((peer, type, handle) => new RemoteInvokeAbleProxy(peer, handle));

                // Let the server pass the object by reference
                server.TypeSerializer.RegisterSerializationAction(typeof(RemoteInvokeAble), SerializationAction.Reference);

                // Run the tests
                using (var proxy = client.InvokeRemoteMethodAsync <IRemoteInvokeAbleProxy>(0, typeof(RemoteInvokeAble).GetConstructor(new Type[0]), null, false).Result)
                {
                    if (proxy.Echo("test message") != "Echo : test message")
                    {
                        throw new Exception("Expected echo message back");
                    }

                    if (proxy.ID != 42)
                    {
                        throw new Exception("Expected 42 as the ID");
                    }

                    proxy.ID = 45;
                    if (proxy.ID != 45)
                    {
                        throw new Exception("Expected 45 as the ID");
                    }

                    if (proxy.CreateTime == 0)
                    {
                        throw new Exception("Expected a proxy creation time");
                    }

                    proxy.CreateTime = 0;
                    if (proxy.CreateTime != 0)
                    {
                        throw new Exception("Expected no proxy creation time");
                    }
                }
            }
        }
示例#2
0
        public void RemoteInvoke()
        {
            using (var setup = new ClientServerTester(FailMethod, FailMethod))
            {
                var client = new RPCPeer(setup.Client);
                var server = new RPCPeer(setup.Server, new Type[] { typeof(System.IO.Directory) }, (m, a) => m.Name == nameof(System.IO.Directory.GetCurrentDirectory));

                var result = client.InvokeRemoteMethodAsync <string>(0L, typeof(System.IO.Directory).GetMethod(nameof(System.IO.Directory.GetCurrentDirectory)), null, false).Result;
                if (result != System.IO.Directory.GetCurrentDirectory())
                {
                    throw new Exception("Failed to get the correct ");
                }

                Exception res = null;
                // Try unsupported type:
                try
                {
                    client.InvokeRemoteMethodAsync(0, typeof(System.IO.File).GetMethod(nameof(System.IO.File.Create)), new object[] { "test.txt" }, false).Wait();
                }
                catch (Exception ex)
                {
                    res = ex;
                }

                if (res == null)
                {
                    throw new Exception("Unwanted access to other type");
                }

                // Try other method
                res = null;
                try
                {
                    client.InvokeRemoteMethodAsync(0, typeof(System.IO.Directory).GetMethod(nameof(System.IO.Directory.CreateDirectory)), new object[] { "testdir" }, false).Wait();
                }
                catch (Exception ex)
                {
                    res = ex;
                }

                if (res == null)
                {
                    throw new Exception("Unwanted access to other method");
                }

                // Try invoking on the client
                res = null;
                try
                {
                    server.InvokeRemoteMethodAsync <string>(0, typeof(System.IO.Directory).GetMethod(nameof(System.IO.Directory.GetCurrentDirectory)), null, false).Wait();
                }
                catch (Exception ex)
                {
                    res = ex;
                }

                if (res == null)
                {
                    throw new Exception("Unwanted access to client method");
                }
            }
        }