示例#1
0
        public async Task MultipleClientsWork(ChannelType type)
        {
            var proxy = await Init <ITestObject>(new TestObject(), type);

            ITestObject proxy2 = null;

            if (type == ChannelType.Tcp)
            {
                var client2 = new TcpRpcClientChannel(
                    new BinaryRpcSerializer(),
                    new RpcMessageFactory(),
                    IPAddress.Loopback,
                    11234);

                await client2.ConnectAsync();

                proxy2 = await client2.GetServerObjectAsync <ITestObject>();
            }
            else if (type == ChannelType.NamedPipe)
            {
                var client2 = new NamedPipeRpcClientChannel(
                    new BinaryRpcSerializer(),
                    new RpcMessageFactory(),
                    _pipeName);

                await client2.ConnectAsync();

                proxy2 = await client2.GetServerObjectAsync <ITestObject>();
            }



            var threads = new List <Task <bool> >();

            for (int i = 0; i < 10; i++)
            {
                int mi = i;
                threads.Add(new Task <bool>(delegate
                {
                    ITestObject pr = mi % 2 == 0 ? proxy : proxy2;

                    for (int j = 0; j < 100; j++)
                    {
                        if (pr.SimpleCalc(mi * 100, j) != mi * 100 + j)
                        {
                            return(false);
                        }
                    }
                    return(true);
                }));
            }

            foreach (var t in threads)
            {
                t.Start();
            }

            Assert.IsTrue(Task.WaitAll(threads.ToArray(), 20000));
            Assert.IsTrue(threads.TrueForAll(t => t.Result));
        }
示例#2
0
        public async Task NamedPipeRpcClientRaisesEventOnDisconnectWhenServerShutdown()
        {
            var name = Guid.NewGuid().ToString();

            using (var server = new NamedPipeRpcServerChannel(
                       new BinaryRpcSerializer(),
                       new RpcMessageFactory(),
                       name))
            {
                await server.ListenAsync();

                using (var client = new NamedPipeRpcClientChannel(new BinaryRpcSerializer(),
                                                                  new RpcMessageFactory(), name))
                {
                    await client.ConnectAsync();

                    var wait = new ManualResetEventSlim(false);
                    client.Disconnected += (s, e) =>
                    {
                        Assert.AreSame(client, e.TransportChannel.Channel);
                        wait.Set();
                    };

                    server.Dispose();

                    Assert.IsTrue(wait.Wait(1000));
                }
            }
        }
示例#3
0
        static async Task Main(string[] args)
        {
            /*
             * var server = new TcpRpcServerChannel(
             *  new JsonRpcSerializer(),
             *  new RpcMessageFactory(),
             *  IPAddress.Loopback,
             *  11234);
             * server.ObjectRepository.RegisterSingleton(new TestObject());
             * await server.ListenAsync();*/


            /*var client = new TcpRpcClientChannel(
             *  new JsonRpcSerializer(),
             *  new RpcMessageFactory(),
             *  IPAddress.Loopback,
             *  11234);*/

            const LogLevel logLevel = LogLevel.Error;

            var client = new NamedPipeRpcClientChannel(
                new BinaryRpcSerializer(),
                new RpcMessageFactory(),
                "test",
                TokenImpersonationLevel.Impersonation,
                loggerFactory: LoggerFactory.Create(builder =>
                                                    builder
                                                    .AddFilter("AdvancedRpcLib", logLevel)
                                                    .AddConsole(o => o.LogToStandardErrorThreshold = logLevel)));

            await client.ConnectAsync(TimeSpan.FromSeconds(5));

            var testObj = await client.GetServerObjectAsync <ITestObject>();

            Console.WriteLine($"Remote user: {testObj.Username}");

            Console.WriteLine(testObj.SimpleCall());

            var sw = new Stopwatch();

            sw.Start();
            int j = 0;

            for (int i = 0; i < 1000; i++)
            {
                j += testObj.Calculate(2, 8);
            }
            sw.Stop();
            Console.WriteLine(sw.Elapsed);

            Console.ReadLine();
        }
示例#4
0
        private async Task <IRpcClientChannel> CreateClient(ChannelType channelType, IRpcSerializer serializer = null,
                                                            TokenImpersonationLevel tokenImpersonationLevel    = TokenImpersonationLevel.None,
                                                            IRpcObjectRepository localRepository = null, Func <IRpcObjectRepository> remoteRepository = null)
        {
            if (serializer == null)
            {
                serializer = new BinaryRpcSerializer();
            }
            switch (channelType)
            {
            case ChannelType.Tcp:
            {
                var client = new TcpRpcClientChannel(
                    serializer,
                    new RpcMessageFactory(),
                    IPAddress.Loopback,
                    11234,
                    localRepository,
                    remoteRepository);

                await client.ConnectAsync();

                return(client);
            }

            case ChannelType.NamedPipe:
            {
                var client = new NamedPipeRpcClientChannel(
                    serializer,
                    new RpcMessageFactory(),
                    _pipeName,
                    tokenImpersonationLevel,
                    localRepository,
                    remoteRepository);

                await client.ConnectAsync();

                return(client);
            }

            default:
                throw new NotSupportedException();
            }
        }