1 // Copyright 2006 Alp Toker <alp@atoker.com>
2 // This software is made available under the MIT License
3 // See COPYING for details
6 using System
.Collections
.Generic
;
15 public Connection Connection
;
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
31 if (Header
.Fields
.TryGetValue(FieldCode
.Signature
, out o
))
34 return Signature
.Empty
;
37 if (value == Signature
.Empty
)
38 Header
.Fields
.Remove(FieldCode
.Signature
);
40 Header
.Fields
[FieldCode
.Signature
] = value;
44 public bool ReplyExpected
47 return (Header
.Flags
& HeaderFlag
.NoReplyExpected
)
52 Header
.Flags
&= ~HeaderFlag
.NoReplyExpected
;
54 Header
.Flags
|= HeaderFlag
.NoReplyExpected
;
60 public FieldCode Code
;
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
);
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();
86 // Header format is: yyyyuua{yv}
87 public byte[] GetHeaderData()
90 Header
.Length
= (uint)Body
.Length
;
92 MessageWriter writer
= new MessageWriter(Header
.Endianness
);
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
;
120 wv
.print("Decoding message:\n{0}\n", wv
.hexdump(data
));
122 return new WvDBusIter(conv
, Header
.Signature
,
123 data
, 0, data
.Length
);