Rename Message.open() to Message.iter(), and getnext() to pop().
[versaplex.git] / dbus-sharp / Message.cs
blobb1672ae1e546bb297425eb1ba3fd51f00b85cfbd
1 // Copyright 2006 Alp Toker <alp@atoker.com>
2 // This software is made available under the MIT License
3 // See COPYING for details
4 //
5 using System;
6 using System.Collections.Generic;
7 using System.IO;
8 using Mono;
10 namespace Wv
12 public class Message
14 public Header Header;
15 public Connection Connection;
16 public byte[] Body;
18 public Message()
20 Header.Endianness = Connection.NativeEndianness;
21 Header.MessageType = MessageType.MethodCall;
22 Header.Flags = HeaderFlag.NoReplyExpected;
23 Header.MajorVersion = Protocol.Version;
24 Header.Fields = new Dictionary<FieldCode,object>();
27 public Signature Signature
29 get {
30 object o;
31 if (Header.Fields.TryGetValue(FieldCode.Signature, out o))
32 return (Signature)o;
33 else
34 return Signature.Empty;
36 set {
37 if (value == Signature.Empty)
38 Header.Fields.Remove(FieldCode.Signature);
39 else
40 Header.Fields[FieldCode.Signature] = value;
44 public bool ReplyExpected
46 get {
47 return (Header.Flags & HeaderFlag.NoReplyExpected)
48 == HeaderFlag.None;
50 set {
51 if (value)
52 Header.Flags &= ~HeaderFlag.NoReplyExpected;
53 else
54 Header.Flags |= HeaderFlag.NoReplyExpected;
58 struct SillyType
60 public FieldCode Code;
61 public object Value;
64 // Header format is: yyyyuua{yv}
65 public void SetHeaderData(byte[] data)
67 EndianFlag endianness = (EndianFlag)data[0];
68 MessageReader reader = new MessageReader(endianness, data);
70 Header h;
71 h.Endianness = (EndianFlag)reader.ReadByte();
72 h.MessageType = (MessageType)reader.ReadByte();
73 h.Flags = (HeaderFlag)reader.ReadByte();
74 h.MajorVersion = reader.ReadByte();
75 h.Length = reader.ReadUInt32();
76 h.Serial = reader.ReadUInt32();
77 h.Fields = new Dictionary<FieldCode,object>();
78 reader.ReadArrayFunc(8, (r) => {
79 FieldCode c = (FieldCode)r.ReadByte();
80 object o = r.ReadVariant();
81 h.Fields[c] = o;
82 });
83 Header = h;
86 // Header format is: yyyyuua{yv}
87 public byte[] GetHeaderData()
89 if (Body != null)
90 Header.Length = (uint)Body.Length;
92 MessageWriter writer = new MessageWriter(Header.Endianness);
94 Header h = Header;
95 writer.Write((byte)h.Endianness);
96 writer.Write((byte)h.MessageType);
97 writer.Write((byte)h.Flags);
98 writer.Write((byte)h.MajorVersion);
99 writer.Write((uint)h.Length);
100 writer.Write((uint)h.Serial);
102 writer.WriteArray(8, h.Fields, (w2, i) => {
103 w2.Write((byte)i.Key);
104 w2.WriteVariant(i.Value.GetType(), i.Value);
107 writer.WritePad(8); // the header is *always* a multiple of 8
108 return writer.ToArray();
111 // FIXME: this whole Message class is junk, so this will presumably
112 // migrate elsewhere eventually.
113 public WvDBusIter iter()
115 DataConverter conv = Header.Endianness==EndianFlag.Little
116 ? DataConverter.LittleEndian : DataConverter.BigEndian;
118 byte[] data = Body;
120 wv.print("Decoding message:\n{0}\n", wv.hexdump(data));
122 return new WvDBusIter(conv, Header.Signature,
123 data, 0, data.Length);