Ejemplo n.º 1
0
        public void TestRpcLocalAsync()
        {
            Console.WriteLine("LocalAsync");
            var def = new RpcDefinition(1, "myfunc1", new List <RpcParamDef> {
                new RpcParamDef("param1", Value.MakeDouble(0.0))
            }, new List <RpcResultsDef> {
                new RpcResultsDef("result1", NtType.Double)
            });

            RemoteProcedureCall.CreateRpc("func1", RemoteProcedureCall.PackRpcDefinition(def), Callback1);

            Console.WriteLine("Calling RPC");

            long call1Uid = RemoteProcedureCall.CallRpc("func1", RemoteProcedureCall.PackRpcValues(new [] { Value.MakeDouble(2.0) }));

            Console.WriteLine("Waiting for RPC Result");
            CancellationTokenSource source = new CancellationTokenSource();
            var task      = RemoteProcedureCall.GetRpcResultAsync(call1Uid, source.Token);
            var completed = task.Wait(TimeSpan.FromSeconds(1));

            if (!completed)
            {
                source.Cancel();
            }
            Assert.That(completed, Is.True);
            Assert.That(task.IsCompleted);
            var call1Result = RemoteProcedureCall.UnpackRpcValues(task.Result, new [] { NtType.Double });

            Assert.AreNotEqual(0, call1Result.Count, "RPC Result empty");

            Console.WriteLine(call1Result[0].ToString());
        }
Ejemplo n.º 2
0
        public void TestRpcSpeedAsync()
        {
            Console.WriteLine("SpeedAsync");
            var def = new RpcDefinition(1, "myfunc1", new List <RpcParamDef> {
                new RpcParamDef("param1", Value.MakeDouble(0.0))
            }, new List <RpcResultsDef> {
                new RpcResultsDef("result1", NtType.Double)
            });

            RemoteProcedureCall.CreateRpc("func1", RemoteProcedureCall.PackRpcDefinition(def), Callback1);
            Stopwatch sw = new Stopwatch();

            sw.Start();
            for (int i = 0; i < 10000; ++i)
            {
                long call1Uid = RemoteProcedureCall.CallRpc("func1", RemoteProcedureCall.PackRpcValues(new [] { Value.MakeDouble(i) }));
                CancellationTokenSource source = new CancellationTokenSource();
                var task      = RemoteProcedureCall.GetRpcResultAsync(call1Uid, source.Token);
                var completed = task.Wait(TimeSpan.FromSeconds(1));
                if (!completed)
                {
                    source.Cancel();
                }
                Assert.That(completed, Is.True);
                Assert.That(task.IsCompleted);
                var res = RemoteProcedureCall.UnpackRpcValues(task.Result, new [] { NtType.Double });
                Assert.AreNotEqual(0, res.Count, "RPC Result empty");
            }
            sw.Stop();
            Console.WriteLine(sw.Elapsed);
        }
Ejemplo n.º 3
0
        public void TestRpcSpeedTimeoutSuccess()
        {
            Console.WriteLine("SpeedTimeoutSuccess");
            var def = new RpcDefinition(1, "myfunc1", new List <RpcParamDef> {
                new RpcParamDef("param1", Value.MakeDouble(0.0))
            }, new List <RpcResultsDef> {
                new RpcResultsDef("result1", NtType.Double)
            });

            RemoteProcedureCall.CreateRpc("func1", RemoteProcedureCall.PackRpcDefinition(def), Callback1);
            Stopwatch sw = new Stopwatch();

            sw.Start();
            for (int i = 0; i < 10000; ++i)
            {
                long   call1Uid    = RemoteProcedureCall.CallRpc("func1", RemoteProcedureCall.PackRpcValues(new [] { Value.MakeDouble(i) }));
                byte[] call1Result = null;
                RemoteProcedureCall.GetRpcResult(true, call1Uid, TimeSpan.FromSeconds(1), out call1Result);
                Assert.That(call1Result, Is.Not.Null);
                var res = RemoteProcedureCall.UnpackRpcValues(call1Result, new [] { NtType.Double });
                Assert.AreNotEqual(0, res.Count, "RPC Result empty");
            }
            sw.Stop();
            Console.WriteLine(sw.Elapsed);
        }
Ejemplo n.º 4
0
        public void TestRpcLocalTimeoutSuccess()
        {
            Console.WriteLine("LocalTimeoutSuccess");
            var def = new RpcDefinition(1, "myfunc1", new List <RpcParamDef> {
                new RpcParamDef("param1", Value.MakeDouble(0.0))
            }, new List <RpcResultsDef> {
                new RpcResultsDef("result1", NtType.Double)
            });

            RemoteProcedureCall.CreateRpc("func1", RemoteProcedureCall.PackRpcDefinition(def), Callback1);

            Console.WriteLine("Calling RPC");

            long call1Uid = RemoteProcedureCall.CallRpc("func1", RemoteProcedureCall.PackRpcValues(new [] { Value.MakeDouble(2.0) }));

            Console.WriteLine("Waiting for RPC Result");
            byte[] result = null;
            RemoteProcedureCall.GetRpcResult(true, call1Uid, TimeSpan.FromSeconds(1), out result);
            Assert.That(result, Is.Not.Null);
            var call1Result = RemoteProcedureCall.UnpackRpcValues(result, new [] { NtType.Double });

            Assert.AreNotEqual(0, call1Result.Count, "RPC Result empty");

            Console.WriteLine(call1Result[0].ToString());
        }
Ejemplo n.º 5
0
        public void TestPolledRpc()
        {
            Console.WriteLine("TestPolledRpc");
            var def = new RpcDefinition(1, "myfunc1", new List <RpcParamDef> {
                new RpcParamDef("param1", Value.MakeDouble(0.0))
            }, new List <RpcResultsDef> {
                new RpcResultsDef("result1", NtType.Double)
            });

            RemoteProcedureCall.CreatePolledRpc("func1", def);

            Console.WriteLine("Calling RPC");

            long call1Uid = RemoteProcedureCall.CallRpc("func1", RemoteProcedureCall.PackRpcValues(new [] { Value.MakeDouble(2.0) }));

            RpcCallInfo info;
            bool        polled = RemoteProcedureCall.PollRpc(true, TimeSpan.FromSeconds(1), out info);

            Assert.That(polled, Is.True);

            IList <byte> toSendBack = Callback1(info.Name, info.Params, new ConnectionInfo());

            Assert.That(toSendBack.Count, Is.Not.EqualTo(0));

            RemoteProcedureCall.PostRpcResponse(info.RpcId, info.CallUid, toSendBack);

            Console.WriteLine("Waiting for RPC Result");
            byte[] result = null;
            RemoteProcedureCall.GetRpcResult(true, call1Uid, out result);
            var call1Result = RemoteProcedureCall.UnpackRpcValues(result, new [] { NtType.Double });

            Assert.AreNotEqual(0, call1Result.Count, "RPC Result empty");

            Console.WriteLine(call1Result[0].ToString());
        }
Ejemplo n.º 6
0
        private static byte[] Callback2(string names, byte[] paramsStr)
        {
            var param = RemoteProcedureCall.UnpackRpcValues(paramsStr, NtType.Boolean, NtType.BooleanArray, NtType.Double, NtType.DoubleArray, NtType.Raw, NtType.String, NtType.StringArray);

            if (param.Count == 0)
            {
                Console.Error.WriteLine("Empty Params?");
                return(new byte[] { 0 });
            }

            return(RemoteProcedureCall.PackRpcValues(Value.MakeBoolean(true), Value.MakeBooleanArray(new[] { true, false }), Value.MakeDouble(2.2), Value.MakeDoubleArray(new[] { 2.8, 6.876 }),
                                                     Value.MakeRaw(new byte[] { 52, 0, 89, 0, 0, 98 }), Value.MakeString("NewString"), Value.MakeStringArray(new[] { "String1", "String2" })));
        }
Ejemplo n.º 7
0
        private static byte[] Callback1(string names, byte[] paramsStr)
        {
            var param = RemoteProcedureCall.UnpackRpcValues(paramsStr, NtType.Double);

            if (param.Count == 0)
            {
                Console.Error.WriteLine("Empty Params?");
                return(new byte[] { 0 });
            }
            double val = param[0].GetDouble();

            return(RemoteProcedureCall.PackRpcValues(Value.MakeDouble(val + 1.2)));
        }
Ejemplo n.º 8
0
        private static IList <byte> Callback1(string names, byte[] paramsStr, ConnectionInfo connInfo)
        {
            var param = RemoteProcedureCall.UnpackRpcValues(paramsStr, new[] { NtType.Double });

            if (param.Count == 0)
            {
                Console.Error.WriteLine("Empty Params?");
                return(new byte[] { 0 });
            }
            double val = param[0].GetDouble();

            return(RemoteProcedureCall.PackRpcValues(new[] { Value.MakeDouble(val + 1.2) }));
        }
Ejemplo n.º 9
0
        public void TestPolledRpcAsync()
        {
            Console.WriteLine("TestPolledRpcAsync");
            var def = new RpcDefinition(1, "myfunc1", new List <RpcParamDef> {
                new RpcParamDef("param1", Value.MakeDouble(0.0))
            }, new List <RpcResultsDef> {
                new RpcResultsDef("result1", NtType.Double)
            });

            serverRemoteProcedureCall.CreatePolledRpc("func1", def);

            Console.WriteLine("Calling RPC");

            long call1Uid = clientRemoteProcedureCall.CallRpc("func1", RemoteProcedureCall.PackRpcValues(new[] { Value.MakeDouble(2.0) }));

            CancellationTokenSource source = new CancellationTokenSource();
            var  task      = serverRemoteProcedureCall.PollRpcAsync(source.Token);
            bool completed = task.Wait(TimeSpan.FromSeconds(1));

            if (!completed)
            {
                source.Cancel();
            }
            Assert.That(completed, Is.True);
            Assert.That(task.IsCompleted, Is.True);
            Assert.That(task.Result, Is.Not.Null);
            Assert.That(task.Result.HasValue);

            IList <byte> toSendBack = Callback1(task.Result.Value.Name, task.Result.Value.Params, new ConnectionInfo());

            Assert.That(toSendBack.Count, Is.Not.EqualTo(0));

            serverRemoteProcedureCall.PostRpcResponse(task.Result.Value.RpcId, task.Result.Value.CallUid, toSendBack);

            Console.WriteLine("Waiting for RPC Result");
            byte[] result = null;
            clientRemoteProcedureCall.GetRpcResult(true, call1Uid, out result);
            var call1Result = clientRemoteProcedureCall.UnpackRpcValues(result, new[] { NtType.Double });

            Assert.AreNotEqual(0, call1Result.Count, "RPC Result empty");

            Console.WriteLine(call1Result[0].ToString());
        }
Ejemplo n.º 10
0
        public void TestRpcAllTypes()
        {
            Console.WriteLine("AllTypes");
            var def = new RpcDefinition(1, "myfunc1", new List <RpcParamDef>
            {
                new RpcParamDef("param1", Value.MakeBoolean(true)),
                new RpcParamDef("param2", Value.MakeBooleanArray(new[] { true, false })),
                new RpcParamDef("param3", Value.MakeDouble(0.0)),
                new RpcParamDef("param4", Value.MakeDoubleArray(new[] { 2.8, 6.87 })),
                new RpcParamDef("param5", Value.MakeRaw(new byte[] { 52, 0, 89, 0, 0, 98 })),
                new RpcParamDef("param6", Value.MakeString("NewString")),
                new RpcParamDef("param7", Value.MakeStringArray(new[] { "String1", "String2" })),
            }, new List <RpcResultsDef>
            {
                new RpcResultsDef("result1", NtType.Boolean),
                new RpcResultsDef("result2", NtType.BooleanArray),
                new RpcResultsDef("result3", NtType.Double),
                new RpcResultsDef("result4", NtType.DoubleArray),
                new RpcResultsDef("result5", NtType.Raw),
                new RpcResultsDef("result6", NtType.String),
                new RpcResultsDef("result7", NtType.StringArray),
            });

            RemoteProcedureCall.CreateRpc("func1", RemoteProcedureCall.PackRpcDefinition(def), Callback2);

            Console.WriteLine("Calling RPC");

            long call1Uid = RemoteProcedureCall.CallRpc("func1", RemoteProcedureCall.PackRpcValues(Value.MakeBoolean(true), Value.MakeBooleanArray(new[] { true, false }), Value.MakeDouble(2.2), Value.MakeDoubleArray(new[] { 2.8, 6.87 }),
                                                                                                   Value.MakeRaw(new byte[] { 52, 0, 89, 0, 0, 98 }), Value.MakeString("NewString"), Value.MakeStringArray(new[] { "String1", "String2" })));

            Console.WriteLine("Waiting for RPC Result");
            byte[] result = null;
            RemoteProcedureCall.GetRpcResult(true, call1Uid, out result);
            var call1Result = RemoteProcedureCall.UnpackRpcValues(result, NtType.Boolean, NtType.BooleanArray, NtType.Double, NtType.DoubleArray, NtType.Raw, NtType.String, NtType.StringArray);

            Assert.AreNotEqual(0, call1Result.Count, "RPC Result empty");

            Console.WriteLine(call1Result[0].ToString());
        }
 /// <inheritdoc cref="RemoteProcedureCall.PackRpcValues(List{Value})"/>
 public byte[] PackRpcValues(List <Value> values)
 {
     return(RemoteProcedureCall.PackRpcValues(values));
 }
 /// <inheritdoc cref="RemoteProcedureCall.PackRpcValues(Value[])"/>
 public byte[] PackRpcValues(params Value[] values)
 {
     return(RemoteProcedureCall.PackRpcValues(values));
 }