예제 #1
0
        public void TestStringValue()
        {
            byte[] rawData = new byte[]
            {
                0x05, (byte)'h', (byte)'e', (byte)'l', (byte)'l', (byte)'o',
                0x03, (byte)'b', (byte)'y', (byte)'e', 0x55,
            };
            WireDecoder d   = new WireDecoder(rawData);
            var         val = d.ReadValue(NtType.String);

            Assert.That(val.Type, Is.EqualTo(NtType.String));
            Assert.That(val.Value, Is.EqualTo(v_string.Value));

            var vFalse = RpcValue.MakeString("bye");

            val = d.ReadValue(NtType.String);
            Assert.That(val.Type, Is.EqualTo(NtType.String));
            Assert.That(val.Value, Is.EqualTo(vFalse.Value));

            byte b = 0;

            Assert.That(d.Read8(ref b));
            Assert.That(b, Is.EqualTo(0x55));

            Assert.That(d.ReadValue(NtType.String), Is.Null);
            Assert.That(d.Error, Is.Null);
        }
예제 #2
0
        public void TestRpcAllTypes()
        {
            CoreMethods.SetLogger((level, file, line, message) =>
            {
                //Console.Error.WriteLine(message);
            }, 0);

            var def = new NtRpcDefinition(1, "myfunc1", new[]
            {
                new NtRpcParamDef("param1", RpcValue.MakeBoolean(true)),
                new NtRpcParamDef("param2", RpcValue.MakeBooleanArray(new[] { true, false })),
                new NtRpcParamDef("param3", RpcValue.MakeDouble(0.0)),
                new NtRpcParamDef("param4", RpcValue.MakeDoubleArray(new[] { 2.8, 6.87 })),
                new NtRpcParamDef("param5", RpcValue.MakeRaw(new byte[] { 52, 0, 89, 0, 0, 98 })),
                new NtRpcParamDef("param6", RpcValue.MakeString("NewString")),
                new NtRpcParamDef("param7", RpcValue.MakeStringArray(new[] { "String1", "String2" })),
            }, new[]
            {
                new NtRpcResultDef("result1", NtType.Boolean),
                new NtRpcResultDef("result2", NtType.BooleanArray),
                new NtRpcResultDef("result3", NtType.Double),
                new NtRpcResultDef("result4", NtType.DoubleArray),
                new NtRpcResultDef("result5", NtType.Raw),
                new NtRpcResultDef("result6", NtType.String),
                new NtRpcResultDef("result7", NtType.StringArray),
            });

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

            Console.WriteLine("Calling RPC");

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

            Console.WriteLine("Waiting for RPC Result");
            byte[] result      = RemoteProcedureCall.GetRpcResult(true, call1Uid);
            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());
        }
예제 #3
0
        private 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(RpcValue.MakeBoolean(true), RpcValue.MakeBooleanArray(new[] { true, false }), RpcValue.MakeDouble(2.2), RpcValue.MakeDoubleArray(new[] { 2.8, 6.876 }),
                                                     RpcValue.MakeRaw(new byte[] { 52, 0, 89, 0, 0, 98 }), RpcValue.MakeString("NewString"), RpcValue.MakeStringArray(new[] { "String1", "String2" })));
        }
예제 #4
0
        public RpcValue ReadValue(NtType type)
        {
            byte size = 0;

            byte[] buf;
            Error = null;
            switch (type)
            {
            case NtType.Boolean:
                byte vB = 0;
                return(!Read8(ref vB) ? null : RpcValue.MakeBoolean(vB != 0));

            case NtType.Double:
                double vD = 0;
                return(!ReadDouble(ref vD) ? null : RpcValue.MakeDouble(vD));

            case NtType.Raw:
                byte[] vRa = null;
                return(!ReadRaw(ref vRa) ? null : RpcValue.MakeRaw(vRa));

            case NtType.Rpc:
            case NtType.String:
                string vS = "";
                return(!ReadString(ref vS) ? null : RpcValue.MakeString(vS));

            case NtType.BooleanArray:
                if (!Read8(ref size))
                {
                    return(null);
                }
                buf = ReadArray(size);
                if (buf == null)
                {
                    return(null);
                }
                bool[] bBuf = new bool[buf.Length];
                for (int i = 0; i < buf.Length; i++)
                {
                    bBuf[i] = buf[i] != 0;
                }
                return(RpcValue.MakeBooleanArray(bBuf));

            case NtType.DoubleArray:
                if (!Read8(ref size))
                {
                    return(null);
                }
                buf = ReadArray(size * 8);
                if (buf == null)
                {
                    return(null);
                }
                double[] dBuf = new double[size];
                for (int i = 0; i < size; i++)
                {
                    dBuf[i] = ReadDouble(buf, i * 8);
                }
                return(RpcValue.MakeDoubleArray(dBuf));

            case NtType.StringArray:
                if (!Read8(ref size))
                {
                    return(null);
                }
                string[] sBuf = new string[size];
                for (int i = 0; i < size; i++)
                {
                    if (!ReadString(ref sBuf[i]))
                    {
                        return(null);
                    }
                }
                return(RpcValue.MakeStringArray(sBuf));

            default:
                Error = "invalid type when trying to read value";
                Console.WriteLine("invalid type when trying to read value");
                return(null);
            }
        }