| 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.Text; |
| 7 |
using System.Collections.Generic; |
| 8 |
using System.IO; |
| 9 |
using System.Reflection; |
| 10 |
|
| 11 |
namespace NDesk.DBus |
| 12 |
{ |
| 13 |
class MessageWriter |
| 14 |
{ |
| 15 |
protected EndianFlag endianness; |
| 16 |
protected MemoryStream stream; |
| 17 |
|
| 18 |
public Connection connection; |
| 19 |
|
| 20 |
//a default constructor is a bad idea for now as we want to make sure the header and content-type match |
| 21 |
public MessageWriter () : this (Connection.NativeEndianness) {} |
| 22 |
|
| 23 |
public MessageWriter (EndianFlag endianness) |
| 24 |
{ |
| 25 |
this.endianness = endianness; |
| 26 |
stream = new MemoryStream (); |
| 27 |
} |
| 28 |
|
| 29 |
public byte[] ToArray () |
| 30 |
{ |
| 31 |
//TODO: mark the writer locked or something here |
| 32 |
return stream.ToArray (); |
| 33 |
} |
| 34 |
|
| 35 |
public void CloseWrite () |
| 36 |
{ |
| 37 |
int needed = Protocol.PadNeeded ((int)stream.Position, 8); |
| 38 |
for (int i = 0 ; i != needed ; i++) |
| 39 |
stream.WriteByte (0); |
| 40 |
} |
| 41 |
|
| 42 |
public void Write (byte val) |
| 43 |
{ |
| 44 |
stream.WriteByte (val); |
| 45 |
} |
| 46 |
|
| 47 |
public void Write (bool val) |
| 48 |
{ |
| 49 |
Write ((uint) (val ? 1 : 0)); |
| 50 |
} |
| 51 |
|
| 52 |
unsafe protected void MarshalUShort (byte *data) |
| 53 |
{ |
| 54 |
WritePad (2); |
| 55 |
byte[] dst = new byte[2]; |
| 56 |
|
| 57 |
if (endianness == Connection.NativeEndianness) { |
| 58 |
dst[0] = data[0]; |
| 59 |
dst[1] = data[1]; |
| 60 |
} else { |
| 61 |
dst[0] = data[1]; |
| 62 |
dst[1] = data[0]; |
| 63 |
} |
| 64 |
|
| 65 |
stream.Write (dst, 0, 2); |
| 66 |
} |
| 67 |
|
| 68 |
unsafe public void Write (short val) |
| 69 |
{ |
| 70 |
MarshalUShort ((byte*)&val); |
| 71 |
} |
| 72 |
|
| 73 |
unsafe public void Write (ushort val) |
| 74 |
{ |
| 75 |
MarshalUShort ((byte*)&val); |
| 76 |
} |
| 77 |
|
| 78 |
unsafe protected void MarshalUInt (byte *data) |
| 79 |
{ |
| 80 |
WritePad (4); |
| 81 |
byte[] dst = new byte[4]; |
| 82 |
|
| 83 |
if (endianness == Connection.NativeEndianness) { |
| 84 |
dst[0] = data[0]; |
| 85 |
dst[1] = data[1]; |
| 86 |
dst[2] = data[2]; |
| 87 |
dst[3] = data[3]; |
| 88 |
} else { |
| 89 |
dst[0] = data[3]; |
| 90 |
dst[1] = data[2]; |
| 91 |
dst[2] = data[1]; |
| 92 |
dst[3] = data[0]; |
| 93 |
} |
| 94 |
|
| 95 |
stream.Write (dst, 0, 4); |
| 96 |
} |
| 97 |
|
| 98 |
unsafe public void Write (int val) |
| 99 |
{ |
| 100 |
MarshalUInt ((byte*)&val); |
| 101 |
} |
| 102 |
|
| 103 |
unsafe public void Write (uint val) |
| 104 |
{ |
| 105 |
MarshalUInt ((byte*)&val); |
| 106 |
} |
| 107 |
|
| 108 |
unsafe protected void MarshalULong (byte *data) |
| 109 |
{ |
| 110 |
WritePad (8); |
| 111 |
byte[] dst = new byte[8]; |
| 112 |
|
| 113 |
if (endianness == Connection.NativeEndianness) { |
| 114 |
for (int i = 0; i < 8; ++i) |
| 115 |
dst[i] = data[i]; |
| 116 |
} else { |
| 117 |
for (int i = 0; i < 8; ++i) |
| 118 |
dst[i] = data[7 - i]; |
| 119 |
} |
| 120 |
|
| 121 |
stream.Write (dst, 0, 8); |
| 122 |
} |
| 123 |
|
| 124 |
unsafe public void Write (long val) |
| 125 |
{ |
| 126 |
MarshalULong ((byte*)&val); |
| 127 |
} |
| 128 |
|
| 129 |
unsafe public void Write (ulong val) |
| 130 |
{ |
| 131 |
MarshalULong ((byte*)&val); |
| 132 |
} |
| 133 |
|
| 134 |
#if !DISABLE_SINGLE |
| 135 |
unsafe public void Write (float val) |
| 136 |
{ |
| 137 |
MarshalUInt ((byte*)&val); |
| 138 |
} |
| 139 |
#endif |
| 140 |
|
| 141 |
unsafe public void Write (double val) |
| 142 |
{ |
| 143 |
MarshalULong ((byte*)&val); |
| 144 |
} |
| 145 |
|
| 146 |
public void Write (string val) |
| 147 |
{ |
| 148 |
byte[] utf8_data = Encoding.UTF8.GetBytes (val); |
| 149 |
Write ((uint)utf8_data.Length); |
| 150 |
stream.Write (utf8_data, 0, utf8_data.Length); |
| 151 |
WriteNull (); |
| 152 |
} |
| 153 |
|
| 154 |
public void Write (ObjectPath val) |
| 155 |
{ |
| 156 |
Write (val.Value); |
| 157 |
} |
| 158 |
|
| 159 |
public void Write (Signature val) |
| 160 |
{ |
| 161 |
byte[] ascii_data = val.GetBuffer (); |
| 162 |
|
| 163 |
if (ascii_data.Length > Protocol.MaxSignatureLength) |
| 164 |
throw new Exception ("Signature length " + ascii_data.Length + " exceeds maximum allowed " + Protocol.MaxSignatureLength + " bytes"); |
| 165 |
|
| 166 |
Write ((byte)ascii_data.Length); |
| 167 |
stream.Write (ascii_data, 0, ascii_data.Length); |
| 168 |
WriteNull (); |
| 169 |
} |
| 170 |
|
| 171 |
public void WriteComplex (object val, Type type) |
| 172 |
{ |
| 173 |
if (type == typeof (void)) |
| 174 |
return; |
| 175 |
|
| 176 |
if (type.IsArray) { |
| 177 |
WriteArray (val, type.GetElementType ()); |
| 178 |
} else if (type.IsGenericType && (type.GetGenericTypeDefinition () == typeof (IDictionary<,>) || type.GetGenericTypeDefinition () == typeof (Dictionary<,>))) { |
| 179 |
Type[] genArgs = type.GetGenericArguments (); |
| 180 |
System.Collections.IDictionary idict = (System.Collections.IDictionary)val; |
| 181 |
WriteFromDict (genArgs[0], genArgs[1], idict); |
| 182 |
} else if (Mapper.IsPublic (type)) { |
| 183 |
WriteObject (type, val); |
| 184 |
} else if (!type.IsPrimitive && !type.IsEnum) { |
| 185 |
WriteValueType (val, type); |
| 186 |
/* |
| 187 |
} else if (type.IsGenericType && type.GetGenericTypeDefinition () == typeof (Nullable<>)) { |
| 188 |
//is it possible to support nullable types? |
| 189 |
Type[] genArgs = type.GetGenericArguments (); |
| 190 |
WriteVariant (genArgs[0], val); |
| 191 |
*/ |
| 192 |
} else { |
| 193 |
throw new Exception ("Can't write"); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
public void Write (Type type, object val) |
| 198 |
{ |
| 199 |
if (type == typeof (void)) |
| 200 |
return; |
| 201 |
|
| 202 |
if (type.IsArray) { |
| 203 |
WriteArray (val, type.GetElementType ()); |
| 204 |
} else if (type == typeof (ObjectPath)) { |
| 205 |
Write ((ObjectPath)val); |
| 206 |
} else if (type == typeof (Signature)) { |
| 207 |
Write ((Signature)val); |
| 208 |
} else if (type == typeof (object)) { |
| 209 |
Write (val); |
| 210 |
} else if (type == typeof (string)) { |
| 211 |
Write ((string)val); |
| 212 |
} else if (type.IsGenericType && (type.GetGenericTypeDefinition () == typeof (IDictionary<,>) || type.GetGenericTypeDefinition () == typeof (Dictionary<,>))) { |
| 213 |
Type[] genArgs = type.GetGenericArguments (); |
| 214 |
System.Collections.IDictionary idict = (System.Collections.IDictionary)val; |
| 215 |
WriteFromDict (genArgs[0], genArgs[1], idict); |
| 216 |
} else if (Mapper.IsPublic (type)) { |
| 217 |
WriteObject (type, val); |
| 218 |
} else if (!type.IsPrimitive && !type.IsEnum) { |
| 219 |
WriteValueType (val, type); |
| 220 |
} else { |
| 221 |
Write (Signature.TypeToDType (type), val); |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
//helper method, should not be used as it boxes needlessly |
| 226 |
public void Write (DType dtype, object val) |
| 227 |
{ |
| 228 |
switch (dtype) |
| 229 |
{ |
| 230 |
case DType.Byte: |
| 231 |
{ |
| 232 |
Write ((byte)val); |
| 233 |
} |
| 234 |
break; |
| 235 |
case DType.Boolean: |
| 236 |
{ |
| 237 |
Write ((bool)val); |
| 238 |
} |
| 239 |
break; |
| 240 |
case DType.Int16: |
| 241 |
{ |
| 242 |
Write ((short)val); |
| 243 |
} |
| 244 |
break; |
| 245 |
case DType.UInt16: |
| 246 |
{ |
| 247 |
Write ((ushort)val); |
| 248 |
} |
| 249 |
break; |
| 250 |
case DType.Int32: |
| 251 |
{ |
| 252 |
Write ((int)val); |
| 253 |
} |
| 254 |
break; |
| 255 |
case DType.UInt32: |
| 256 |
{ |
| 257 |
Write ((uint)val); |
| 258 |
} |
| 259 |
break; |
| 260 |
case DType.Int64: |
| 261 |
{ |
| 262 |
Write ((long)val); |
| 263 |
} |
| 264 |
break; |
| 265 |
case DType.UInt64: |
| 266 |
{ |
| 267 |
Write ((ulong)val); |
| 268 |
} |
| 269 |
break; |
| 270 |
#if !DISABLE_SINGLE |
| 271 |
case DType.Single: |
| 272 |
{ |
| 273 |
Write ((float)val); |
| 274 |
} |
| 275 |
break; |
| 276 |
#endif |
| 277 |
case DType.Double: |
| 278 |
{ |
| 279 |
Write ((double)val); |
| 280 |
} |
| 281 |
break; |
| 282 |
case DType.String: |
| 283 |
{ |
| 284 |
Write ((string)val); |
| 285 |
} |
| 286 |
break; |
| 287 |
case DType.ObjectPath: |
| 288 |
{ |
| 289 |
Write ((ObjectPath)val); |
| 290 |
} |
| 291 |
break; |
| 292 |
case DType.Signature: |
| 293 |
{ |
| 294 |
Write ((Signature)val); |
| 295 |
} |
| 296 |
break; |
| 297 |
case DType.Variant: |
| 298 |
{ |
| 299 |
Write ((object)val); |
| 300 |
} |
| 301 |
break; |
| 302 |
default: |
| 303 |
throw new Exception ("Unhandled D-Bus type: " + dtype); |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
public void WriteObject (Type type, object val) |
| 308 |
{ |
| 309 |
ObjectPath path; |
| 310 |
|
| 311 |
BusObject bobj = val as BusObject; |
| 312 |
|
| 313 |
if (bobj == null && val is MarshalByRefObject) { |
| 314 |
bobj = ((MarshalByRefObject)val).GetLifetimeService () as BusObject; |
| 315 |
} |
| 316 |
|
| 317 |
if (bobj == null) |
| 318 |
throw new Exception ("No object reference to write"); |
| 319 |
|
| 320 |
path = bobj.Path; |
| 321 |
|
| 322 |
Write (path); |
| 323 |
} |
| 324 |
|
| 325 |
//variant |
| 326 |
public void Write (object val) |
| 327 |
{ |
| 328 |
//TODO: maybe support sending null variants |
| 329 |
|
| 330 |
if (val == null) |
| 331 |
throw new NotSupportedException ("Cannot send null variant"); |
| 332 |
|
| 333 |
Type type = val.GetType (); |
| 334 |
|
| 335 |
WriteVariant (type, val); |
| 336 |
} |
| 337 |
|
| 338 |
public void WriteVariant (Type type, object val) |
| 339 |
{ |
| 340 |
Signature sig = Signature.GetSig (type); |
| 341 |
|
| 342 |
Write (sig); |
| 343 |
Write (type, val); |
| 344 |
} |
| 345 |
|
| 346 |
//this requires a seekable stream for now |
| 347 |
public void WriteArray (object obj, Type elemType) |
| 348 |
{ |
| 349 |
Array val = (Array)obj; |
| 350 |
|
| 351 |
//TODO: more fast paths for primitive arrays |
| 352 |
if (elemType == typeof (byte)) { |
| 353 |
if (val.Length > Protocol.MaxArrayLength) |
| 354 |
throw new Exception ("Array length " + val.Length + " exceeds maximum allowed " + Protocol.MaxArrayLength + " bytes"); |
| 355 |
|
| 356 |
Write ((uint)val.Length); |
| 357 |
stream.Write ((byte[])val, 0, val.Length); |
| 358 |
return; |
| 359 |
} |
| 360 |
|
| 361 |
long origPos = stream.Position; |
| 362 |
Write ((uint)0); |
| 363 |
|
| 364 |
//advance to the alignment of the element |
| 365 |
WritePad (Protocol.GetAlignment (Signature.TypeToDType (elemType))); |
| 366 |
|
| 367 |
long startPos = stream.Position; |
| 368 |
|
| 369 |
foreach (object elem in val) |
| 370 |
Write (elemType, elem); |
| 371 |
|
| 372 |
long endPos = stream.Position; |
| 373 |
uint ln = (uint)(endPos - startPos); |
| 374 |
stream.Position = origPos; |
| 375 |
|
| 376 |
if (ln > Protocol.MaxArrayLength) |
| 377 |
throw new Exception ("Array length " + ln + " exceeds maximum allowed " + Protocol.MaxArrayLength + " bytes"); |
| 378 |
|
| 379 |
Write (ln); |
| 380 |
stream.Position = endPos; |
| 381 |
} |
| 382 |
|
| 383 |
public void WriteFromDict (Type keyType, Type valType, System.Collections.IDictionary val) |
| 384 |
{ |
| 385 |
long origPos = stream.Position; |
| 386 |
Write ((uint)0); |
| 387 |
|
| 388 |
//advance to the alignment of the element |
| 389 |
//WritePad (Protocol.GetAlignment (Signature.TypeToDType (type))); |
| 390 |
WritePad (8); |
| 391 |
|
| 392 |
long startPos = stream.Position; |
| 393 |
|
| 394 |
foreach (System.Collections.DictionaryEntry entry in val) |
| 395 |
{ |
| 396 |
WritePad (8); |
| 397 |
|
| 398 |
Write (keyType, entry.Key); |
| 399 |
Write (valType, entry.Value); |
| 400 |
} |
| 401 |
|
| 402 |
long endPos = stream.Position; |
| 403 |
uint ln = (uint)(endPos - startPos); |
| 404 |
stream.Position = origPos; |
| 405 |
|
| 406 |
if (ln > Protocol.MaxArrayLength) |
| 407 |
throw new Exception ("Dict length " + ln + " exceeds maximum allowed " + Protocol.MaxArrayLength + " bytes"); |
| 408 |
|
| 409 |
Write (ln); |
| 410 |
stream.Position = endPos; |
| 411 |
} |
| 412 |
|
| 413 |
public void WriteValueType (object val, Type type) |
| 414 |
{ |
| 415 |
MethodInfo mi = TypeImplementer.GetWriteMethod (type); |
| 416 |
mi.Invoke (null, new object[] {this, val}); |
| 417 |
} |
| 418 |
|
| 419 |
/* |
| 420 |
public void WriteValueTypeOld (object val, Type type) |
| 421 |
{ |
| 422 |
WritePad (8); |
| 423 |
|
| 424 |
if (type.IsGenericType && type.GetGenericTypeDefinition () == typeof (KeyValuePair<,>)) { |
| 425 |
System.Reflection.PropertyInfo key_prop = type.GetProperty ("Key"); |
| 426 |
Write (key_prop.PropertyType, key_prop.GetValue (val, null)); |
| 427 |
|
| 428 |
System.Reflection.PropertyInfo val_prop = type.GetProperty ("Value"); |
| 429 |
Write (val_prop.PropertyType, val_prop.GetValue (val, null)); |
| 430 |
|
| 431 |
return; |
| 432 |
} |
| 433 |
|
| 434 |
FieldInfo[] fis = type.GetFields (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); |
| 435 |
|
| 436 |
foreach (System.Reflection.FieldInfo fi in fis) { |
| 437 |
object elem; |
| 438 |
elem = fi.GetValue (val); |
| 439 |
Write (fi.FieldType, elem); |
| 440 |
} |
| 441 |
} |
| 442 |
*/ |
| 443 |
|
| 444 |
public void WriteNull () |
| 445 |
{ |
| 446 |
stream.WriteByte (0); |
| 447 |
} |
| 448 |
|
| 449 |
public void WritePad (int alignment) |
| 450 |
{ |
| 451 |
stream.Position = Protocol.Padded ((int)stream.Position, alignment); |
| 452 |
} |
| 453 |
} |
| 454 |
} |