Debian Codegraph @

lib/csharp/MonoExample/NDesk-dbus/Protocol.cs

raw
1 // Copyright 2006 Alp Toker <[email protected]>
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
8 namespace NDesk.DBus
9 {
10 //yyyyuua{yv}
11 struct Header
12 {
13 public EndianFlag Endianness;
14 public MessageType MessageType;
15 public HeaderFlag Flags;
16 public byte MajorVersion;
17 public uint Length;
18 public uint Serial;
19 //public HeaderField[] Fields;
20 public IDictionary<FieldCode,object> Fields;
21
22 /*
23 public static DType TypeForField (FieldCode f)
24 {
25 switch (f) {
26 case FieldCode.Invalid:
27 return DType.Invalid;
28 case FieldCode.Path:
29 return DType.ObjectPath;
30 case FieldCode.Interface:
31 return DType.String;
32 case FieldCode.Member:
33 return DType.String;
34 case FieldCode.ErrorName:
35 return DType.String;
36 case FieldCode.ReplySerial:
37 return DType.UInt32;
38 case FieldCode.Destination:
39 return DType.String;
40 case FieldCode.Sender:
41 return DType.String;
42 case FieldCode.Signature:
43 return DType.Signature;
44 #if PROTO_REPLY_SIGNATURE
45 case FieldCode.ReplySignature: //note: not supported in dbus
46 return DType.Signature;
47 #endif
48 default:
49 return DType.Invalid;
50 }
51 }
52 */
53 }
54
55 /*
56 public struct HeaderField
57 {
58 //public HeaderField (FieldCode code, object value)
59 //{
60 // this.Code = code;
61 // this.Value = value;
62 //}
63
64 public static HeaderField Create (FieldCode code, object value)
65 {
66 HeaderField hf;
67
68 hf.Code = code;
69 hf.Value = value;
70
71 return hf;
72 }
73
74 public FieldCode Code;
75 public object Value;
76 }
77 */
78
79 enum MessageType : byte
80 {
81 //This is an invalid type.
82 Invalid,
83 //Method call.
84 MethodCall,
85 //Method reply with returned data.
86 MethodReturn,
87 //Error reply. If the first argument exists and is a string, it is an error message.
88 Error,
89 //Signal emission.
90 Signal,
91 }
92
93 enum FieldCode : byte
94 {
95 Invalid,
96 Path,
97 Interface,
98 Member,
99 ErrorName,
100 ReplySerial,
101 Destination,
102 Sender,
103 Signature,
104 #if PROTO_REPLY_SIGNATURE
105 ReplySignature, //note: not supported in dbus
106 #endif
107 }
108
109 enum EndianFlag : byte
110 {
111 Little = (byte)'l',
112 Big = (byte)'B',
113 }
114
115 [Flags]
116 enum HeaderFlag : byte
117 {
118 None = 0,
119 NoReplyExpected = 0x1,
120 NoAutoStart = 0x2,
121 }
122
123 public sealed class ObjectPath //: IComparable, IComparable<ObjectPath>, IEquatable<ObjectPath>
124 {
125 public static readonly ObjectPath Root = new ObjectPath ("/");
126
127 internal readonly string Value;
128
129 public ObjectPath (string value)
130 {
131 if (value == null)
132 throw new ArgumentNullException ("value");
133
134 this.Value = value;
135 }
136
137 public override bool Equals (object o)
138 {
139 ObjectPath b = o as ObjectPath;
140
141 if (b == null)
142 return false;
143
144 return Value.Equals (b.Value);
145 }
146
147 public override int GetHashCode ()
148 {
149 return Value.GetHashCode ();
150 }
151
152 public override string ToString ()
153 {
154 return Value;
155 }
156
157 //this may or may not prove useful
158 internal string[] Decomposed
159 {
160 get {
161 return Value.Split (new char[] {'/'}, StringSplitOptions.RemoveEmptyEntries);
162 /*
163 } set {
164 Value = String.Join ("/", value);
165 */
166 }
167 }
168
169 internal ObjectPath Parent
170 {
171 get {
172 if (Value == Root.Value)
173 return null;
174
175 string par = Value.Substring (0, Value.LastIndexOf ('/'));
176 if (par == String.Empty)
177 par = "/";
178
179 return new ObjectPath (par);
180 }
181 }
182
183 /*
184 public int CompareTo (object value)
185 {
186 return 1;
187 }
188
189 public int CompareTo (ObjectPath value)
190 {
191 return 1;
192 }
193
194 public bool Equals (ObjectPath value)
195 {
196 return false;
197 }
198 */
199 }
200
201 static class Protocol
202 {
203 //protocol versions that we support
204 public const byte MinVersion = 0;
205 public const byte Version = 1;
206 public const byte MaxVersion = Version;
207
208 public const uint MaxMessageLength = 134217728; //2 to the 27th power
209 public const uint MaxArrayLength = 67108864; //2 to the 26th power
210 public const uint MaxSignatureLength = 255;
211 public const uint MaxArrayDepth = 32;
212 public const uint MaxStructDepth = 32;
213
214 //this is not strictly related to Protocol since names are passed around as strings
215 internal const uint MaxNameLength = 255;
216
217 public static int PadNeeded (int pos, int alignment)
218 {
219 int pad = pos % alignment;
220 pad = pad == 0 ? 0 : alignment - pad;
221
222 return pad;
223 }
224
225 public static int Padded (int pos, int alignment)
226 {
227 int pad = pos % alignment;
228 if (pad != 0)
229 pos += alignment - pad;
230
231 return pos;
232 }
233
234 public static int GetAlignment (DType dtype)
235 {
236 switch (dtype) {
237 case DType.Byte:
238 return 1;
239 case DType.Boolean:
240 return 4;
241 case DType.Int16:
242 case DType.UInt16:
243 return 2;
244 case DType.Int32:
245 case DType.UInt32:
246 return 4;
247 case DType.Int64:
248 case DType.UInt64:
249 return 8;
250 #if !DISABLE_SINGLE
251 case DType.Single: //Not yet supported!
252 return 4;
253 #endif
254 case DType.Double:
255 return 8;
256 case DType.String:
257 return 4;
258 case DType.ObjectPath:
259 return 4;
260 case DType.Signature:
261 return 1;
262 case DType.Array:
263 return 4;
264 case DType.Struct:
265 return 8;
266 case DType.Variant:
267 return 1;
268 case DType.DictEntry:
269 return 8;
270 case DType.Invalid:
271 default:
272 throw new Exception ("Cannot determine alignment of " + dtype);
273 }
274 }
275
276 //this class may not be the best place for Verbose
277 public readonly static bool Verbose;
278
279 static Protocol ()
280 {
281 Verbose = !String.IsNullOrEmpty (Environment.GetEnvironmentVariable ("DBUS_VERBOSE"));
282 }
283 }
284
285 #if UNDOCUMENTED_IN_SPEC
286 /*
287 "org.freedesktop.DBus.Error.Failed"
288 "org.freedesktop.DBus.Error.NoMemory"
289 "org.freedesktop.DBus.Error.ServiceUnknown"
290 "org.freedesktop.DBus.Error.NameHasNoOwner"
291 "org.freedesktop.DBus.Error.NoReply"
292 "org.freedesktop.DBus.Error.IOError"
293 "org.freedesktop.DBus.Error.BadAddress"
294 "org.freedesktop.DBus.Error.NotSupported"
295 "org.freedesktop.DBus.Error.LimitsExceeded"
296 "org.freedesktop.DBus.Error.AccessDenied"
297 "org.freedesktop.DBus.Error.AuthFailed"
298 "org.freedesktop.DBus.Error.NoServer"
299 "org.freedesktop.DBus.Error.Timeout"
300 "org.freedesktop.DBus.Error.NoNetwork"
301 "org.freedesktop.DBus.Error.AddressInUse"
302 "org.freedesktop.DBus.Error.Disconnected"
303 "org.freedesktop.DBus.Error.InvalidArgs"
304 "org.freedesktop.DBus.Error.FileNotFound"
305 "org.freedesktop.DBus.Error.UnknownMethod"
306 "org.freedesktop.DBus.Error.TimedOut"
307 "org.freedesktop.DBus.Error.MatchRuleNotFound"
308 "org.freedesktop.DBus.Error.MatchRuleInvalid"
309 "org.freedesktop.DBus.Error.Spawn.ExecFailed"
310 "org.freedesktop.DBus.Error.Spawn.ForkFailed"
311 "org.freedesktop.DBus.Error.Spawn.ChildExited"
312 "org.freedesktop.DBus.Error.Spawn.ChildSignaled"
313 "org.freedesktop.DBus.Error.Spawn.Failed"
314 "org.freedesktop.DBus.Error.UnixProcessIdUnknown"
315 "org.freedesktop.DBus.Error.InvalidSignature"
316 "org.freedesktop.DBus.Error.SELinuxSecurityContextUnknown"
317 */
318 #endif
319 }