static void TestDbusHelloImpl(Udbus.Serialization.UdbusMessageBuilderTracker builder, Udbus.Core.IUdbusMessageVisitor visitor, Udbus.Serialization.UdbusConnector connector,
            System.Threading.ManualResetEvent stop, PreNextActionDelegate preNextAction)
        {
            int result = -1;
            //Udbus.v4v.v4vConnection connection = new Udbus.v4v.v4vConnection(Console_io_debug);
            //Udbus.Serialization.ManagedDbusIo dbio;
            //connection.PopulateDbio(ref dbio);

            //Udbus.Core.UdbusFunctions.dbus_auth(ref dbio, "Dude");
            //Udbus.Serialization.UdbusConnector connector = Udbus.Serialization.UdbusConnector.CreateAuthorised(connection);

            // Visitors
            preNextAction("Hello");

            // Hello.
            using (var msg = builder.UdbusMethodMessage(
                                "org.freedesktop.DBus", "/org/freedesktop/DBus",
                                "org.freedesktop.DBus", "Hello").Message)
            {
                result = connector.Send(msg);
                Console.WriteLine("Hello send result: {0}", result);
                Udbus.Core.UdbusVisitorFunctions.Visit(result, msg, new Udbus.Core.UdbusMessageVisitorDumpConsole());
            }
            //var recv = connector.Receive(out result);
            Udbus.Serialization.NMessageStruct.UdbusMessageHandle recv;
            result = connector.ReceiveStruct(out recv);
            Console.WriteLine("Hello recv result: {0}. msg: {1}", result, recv);
        }
        static void TestDbusReadIcon(Udbus.Serialization.UdbusMessageBuilderTracker builder, Udbus.Core.IUdbusMessageVisitor visitor, Udbus.Serialization.UdbusConnector connector,
            System.Threading.ManualResetEvent stop, PreNextActionDelegate preNextAction)
        {
            int result = -1;
            //Udbus.v4v.v4vConnection connection = new Udbus.v4v.v4vConnection(Console_io_debug);
            //Udbus.Serialization.ManagedDbusIo dbio;
            //connection.PopulateDbio(ref dbio);

            //Udbus.Core.UdbusFunctions.dbus_auth(ref dbio, "Dude");
            //Udbus.Serialization.UdbusConnector connector = Udbus.Serialization.UdbusConnector.CreateAuthorised(connection);

            // Visitors
            preNextAction("readicon");

            // list_vms.
            using (var msg = builder.UdbusMethodMessage(
                "com.citrix.xenclient.xenmgr", "/",
                "com.citrix.xenclient.xenmgr",
                "list_vms").Message)
            {
                result = connector.Send(msg);
                Console.WriteLine("list_vms send result: {0}", result);
            }
            Udbus.Serialization.NMessageStruct.UdbusMessageHandle recv;
            using (var msg = Udbus.Core.UdbusVisitorFunctions.LoopUdbusFind(connector, visitor, Console.Out, stop).Handle)
            {
                msg.HandleToStructure(out recv);
                Console.WriteLine("list_vms recv result: {0}. msg: {1}", result, recv);

                Udbus.Serialization.UdbusMessageReader reader = new Udbus.Serialization.UdbusMessageReader(msg);
                uint counter = 0;

                foreach (Udbus.Serialization.UdbusMessageReader subreader in reader.ArrayReader(Udbus.Types.dbus_type.DBUS_OBJECTPATH))
                {
                    Udbus.Types.UdbusObjectPath op;
                    result = subreader.ReadObjectPath(out op);
                    if (result != 0)
                    {
                        Console.WriteLine("Error ! {0:d}/0x{0:x8}", result);
                    }
                    else
                    {
                        Console.WriteLine(" {0}", op);

                        using (var msgVm = builder.UdbusMethodMessage(
                            "com.citrix.xenclient.xenmgr", op.Path,
                            "com.citrix.xenclient.xenmgr.vm",
                            "read_icon").Message)
                        {
                            result = connector.Send(msgVm);
                            Console.WriteLine("read_icon send result: {0}", result);

                        }
                        if (result == 0)
                        {
                            Udbus.Serialization.NMessageStruct.UdbusMessageHandle recvVm;
                            using (var msgVm = Udbus.Core.UdbusVisitorFunctions.LoopUdbusFind(connector, visitor, Console.Out, stop).Handle)
                            {
                                if (msgVm == null)
                                {
                                    Console.WriteLine("read_icon failed");
                                }
                                else
                                {
                                    msgVm.HandleToStructure(out recvVm);
                                    Console.WriteLine("read_icon result: {0}. msg: {1}", result, recvVm);

                                    Udbus.Serialization.UdbusMessageReader readerVm = new Udbus.Serialization.UdbusMessageReader(msgVm);
                                    Console.WriteLine("Icon bytes:");
                                    uint byteCounter = 0;

                                    foreach (Udbus.Serialization.UdbusMessageReader subreaderIcon in readerVm.ArrayReader(Udbus.Types.dbus_type.DBUS_BYTE))
                                    {
                                        byte b;
                                        result = subreaderIcon.ReadByte(out b);
                                        if (result != 0)
                                        {
                                            Console.WriteLine("Error ! {0:d}/0x{0:x8}", result);
                                            break;
                                        }
                                        else
                                        {
                                            //Console.Write(b);
                                            ++byteCounter;
                                        }
                                    }
                                    Console.WriteLine("Total: {0} bytes", byteCounter);
                                    Console.WriteLine();

                                } // Ends else read_icon succeeded
                            } // Ends read_icon response
                        }
                    }
                    Console.WriteLine("Entry {0:d2}: {1}", counter++, op);
                }
            } // Ends using ListVms response
        }
        static void TestDbusGetPropertyImpl(Udbus.Serialization.UdbusMessageBuilderTracker builder, Udbus.Core.IUdbusMessageVisitor visitor, Udbus.Serialization.UdbusConnector connector,
            System.Threading.ManualResetEvent stop, PreNextActionDelegate preNextAction)
        {
            preNextAction("GetProperty");
            int result;
            Udbus.Serialization.NMessageStruct.UdbusMessageHandle recv;

            using (var msg = builder.UdbusMethodMessage(
                "com.citrix.xenclient.xenmgr", "/",
                "org.freedesktop.DBus.Properties", "Get").Message)
            {
                Udbus.Types.dbus_sig signature = Udbus.Types.dbus_sig.Initialiser;
                signature.a[0] = Udbus.Types.dbus_type.DBUS_STRING;
                signature.a[1] = Udbus.Types.dbus_type.DBUS_STRING;
                signature.a[2] = Udbus.Types.dbus_type.DBUS_INVALID;
                builder.SetSignature(ref signature)
                    .BodyAdd(4096)
                    .BodyAdd_String("com.citrix.xenclient.xenmgr.config")
                    .BodyAdd_String("iso-path")
                ;
                result = connector.Send(msg);
                Console.WriteLine("GetProperty send result: {0}", result);
                Udbus.Core.UdbusVisitorFunctions.Visit(result, msg, new Udbus.Core.UdbusMessageVisitorDumpConsole());
            }
            try
            {
                Udbus.Core.UdbusMessagePair recvData = Udbus.Core.UdbusVisitorFunctions.LoopUdbusFind(connector, visitor, Console.Out, stop);
                if (!recvData.QuEmpty)
                {
                    recv = recvData.Data;
                    if (recv.typefield.type != Udbus.Types.dbus_msg_type.DBUS_TYPE_ERROR)
                    {
                        Udbus.Serialization.UdbusMessageReader reader = new Udbus.Serialization.UdbusMessageReader(recvData.Handle);
                        do
                        {
            #if !READASVARIANT
                            var variant = reader.ReadVariantValue(out result);
                            Console.WriteLine("iso_path: {0}", variant.ToString());
                            //Udbus.Parsing.BuildContext context = new Udbus.Parsing.BuildContext(new Udbus.Parsing.CodeTypeNoOpHolder());
                            //Udbus.Serialization.Variant.UdbusVariantIn variantIn = new Udbus.Serialization.Variant.UdbusVariantIn(reader);
                            //Udbus.Parsing.IDLArgumentTypeNameBuilderBase nameBuilder = new Udbus.Parsing.IDLArgumentTypeNameBuilderNoOp();
                            //Udbus.Types.dbus_sig sig = Udbus.Types.dbus_sig.Initialiser;
                            //result = reader.ReadSignature(ref sig);
                            //Udbus.Parsing.CodeBuilderHelper.BuildCodeParamType(variantIn, nameBuilder, sig.a, context);
                            //Console.WriteLine("iso_path: {0}", variantIn.Variant.ToString());

            #else // !READASVARIANT
                        Udbus.Types.dbus_sig sig = Udbus.Types.dbus_sig.Initialiser;
                        result = reader.ReadSignature(ref sig);
                        if (result != 0)
                        {
                            Console.WriteLine("Error reading property variant signature: {0}", result);
                            break;
                        }
                        string isopath;
                        result = reader.ReadString(out isopath);
                        if (result != 0)
                        {
                            Console.WriteLine("Error reading property variant string: {0}", result);
                            break;
                        }

                        Console.WriteLine("iso_path: {0}", isopath);
            #endif // READASVARIANT
                        } while (false);
                    }
                }
                else
                {
                    Console.WriteLine("Getting the property failed. Boooooo ");
                }
            }
            catch (System.Runtime.InteropServices.SEHException seh)
            {
                Console.WriteLine("Error: " + seh.ToString());
            }
        }