1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
|
// Copyright 2006 Alp Toker <alp@atoker.com>
// This software is made available under the MIT License
// See COPYING for details
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Reflection;
namespace NDesk.DBus
{
using Authentication;
using Transports;
public partial class Connection
{
//TODO: reconsider this field
Stream ns = null;
Transport transport;
internal Transport Transport {
get {
return transport;
} set {
transport = value;
}
}
protected Connection () {}
internal Connection (Transport transport)
{
this.transport = transport;
transport.Connection = this;
//TODO: clean this bit up
ns = transport.Stream;
}
//should this be public?
internal Connection (string address)
{
OpenPrivate (address);
Authenticate ();
}
/*
bool isConnected = false;
public bool IsConnected
{
get {
return isConnected;
}
}
*/
//should we do connection sharing here?
public static Connection Open (string address)
{
Connection conn = new Connection ();
conn.OpenPrivate (address);
conn.Authenticate ();
return conn;
}
internal void OpenPrivate (string address)
{
if (address == null)
throw new ArgumentNullException ("address");
AddressEntry[] entries = Address.Parse (address);
if (entries.Length == 0)
throw new Exception ("No addresses were found");
//TODO: try alternative addresses if needed
AddressEntry entry = entries[0];
transport = Transport.Create (entry);
//TODO: clean this bit up
ns = transport.Stream;
}
void Authenticate ()
{
if (transport != null)
transport.WriteCred ();
SaslClient auth = new SaslClient (this);
auth.Run ();
isAuthenticated = true;
}
bool isAuthenticated = false;
internal bool IsAuthenticated
{
get {
return isAuthenticated;
}
}
//Interlocked.Increment() handles the overflow condition for uint correctly, so it's ok to store the value as an int but cast it to uint
int serial = 0;
uint GenerateSerial ()
{
//return ++serial;
return (uint)Interlocked.Increment (ref serial);
}
internal Message SendWithReplyAndBlock (Message msg)
{
PendingCall pending = SendWithReply (msg);
return pending.Reply;
}
internal PendingCall SendWithReply (Message msg)
{
msg.ReplyExpected = true;
msg.Header.Serial = GenerateSerial ();
//TODO: throttle the maximum number of concurrent PendingCalls
PendingCall pending = new PendingCall (this);
pendingCalls[msg.Header.Serial] = pending;
WriteMessage (msg);
return pending;
}
internal uint Send (Message msg)
{
msg.Header.Serial = GenerateSerial ();
WriteMessage (msg);
//Outbound.Enqueue (msg);
//temporary
//Flush ();
return msg.Header.Serial;
}
object writeLock = new object ();
internal void WriteMessage (Message msg)
{
byte[] HeaderData = msg.GetHeaderData ();
long msgLength = HeaderData.Length + (msg.Body != null ? msg.Body.Length : 0);
if (msgLength > Protocol.MaxMessageLength)
throw new Exception ("Message length " + msgLength + " exceeds maximum allowed " + Protocol.MaxMessageLength + " bytes");
lock (writeLock) {
ns.Write (HeaderData, 0, HeaderData.Length);
if (msg.Body != null && msg.Body.Length != 0)
ns.Write (msg.Body, 0, msg.Body.Length);
}
}
Queue<Message> Inbound = new Queue<Message> ();
/*
Queue<Message> Outbound = new Queue<Message> ();
public void Flush ()
{
//should just iterate the enumerator here
while (Outbound.Count != 0) {
Message msg = Outbound.Dequeue ();
WriteMessage (msg);
}
}
public bool ReadWrite (int timeout_milliseconds)
{
//TODO
return true;
}
public bool ReadWrite ()
{
return ReadWrite (-1);
}
public bool Dispatch ()
{
//TODO
Message msg = Inbound.Dequeue ();
//HandleMessage (msg);
return true;
}
public bool ReadWriteDispatch (int timeout_milliseconds)
{
//TODO
return Dispatch ();
}
public bool ReadWriteDispatch ()
{
return ReadWriteDispatch (-1);
}
*/
internal Message ReadMessage ()
{
byte[] header;
byte[] body = null;
int read;
//16 bytes is the size of the fixed part of the header
byte[] hbuf = new byte[16];
read = ns.Read (hbuf, 0, 16);
if (read == 0)
return null;
if (read != 16)
throw new Exception ("Header read length mismatch: " + read + " of expected " + "16");
EndianFlag endianness = (EndianFlag)hbuf[0];
MessageReader reader = new MessageReader (endianness, hbuf);
//discard the endian byte as we've already read it
reader.ReadByte ();
//discard message type and flags, which we don't care about here
reader.ReadByte ();
reader.ReadByte ();
byte version = reader.ReadByte ();
if (version < Protocol.MinVersion || version > Protocol.MaxVersion)
throw new NotSupportedException ("Protocol version '" + version.ToString () + "' is not supported");
if (Protocol.Verbose)
if (version != Protocol.Version)
Console.Error.WriteLine ("Warning: Protocol version '" + version.ToString () + "' is not explicitly supported but may be compatible");
uint bodyLength = reader.ReadUInt32 ();
//discard serial
reader.ReadUInt32 ();
uint headerLength = reader.ReadUInt32 ();
//this check may become relevant if a future version of the protocol allows larger messages
/*
if (bodyLength > Int32.MaxValue || headerLength > Int32.MaxValue)
throw new NotImplementedException ("Long messages are not yet supported");
*/
int bodyLen = (int)bodyLength;
int toRead = (int)headerLength;
//we fixup to include the padding following the header
toRead = Protocol.Padded (toRead, 8);
long msgLength = toRead + bodyLen;
if (msgLength > Protocol.MaxMessageLength)
throw new Exception ("Message length " + msgLength + " exceeds maximum allowed " + Protocol.MaxMessageLength + " bytes");
header = new byte[16 + toRead];
Array.Copy (hbuf, header, 16);
read = ns.Read (header, 16, toRead);
if (read != toRead)
throw new Exception ("Message header length mismatch: " + read + " of expected " + toRead);
//read the body
if (bodyLen != 0) {
body = new byte[bodyLen];
read = ns.Read (body, 0, bodyLen);
if (read != bodyLen)
throw new Exception ("Message body length mismatch: " + read + " of expected " + bodyLen);
}
Message msg = new Message ();
msg.Connection = this;
msg.Body = body;
msg.SetHeaderData (header);
return msg;
}
//temporary hack
internal void DispatchSignals ()
{
lock (Inbound) {
while (Inbound.Count != 0) {
Message msg = Inbound.Dequeue ();
HandleSignal (msg);
}
}
}
internal Thread mainThread = Thread.CurrentThread;
//temporary hack
public void Iterate ()
{
mainThread = Thread.CurrentThread;
//Message msg = Inbound.Dequeue ();
Message msg = ReadMessage ();
HandleMessage (msg);
DispatchSignals ();
}
internal void HandleMessage (Message msg)
{
//TODO: support disconnection situations properly and move this check elsewhere
if (msg == null)
throw new ArgumentNullException ("msg", "Cannot handle a null message; maybe the bus was disconnected");
{
object field_value;
if (msg.Header.Fields.TryGetValue (FieldCode.ReplySerial, out field_value)) {
uint reply_serial = (uint)field_value;
PendingCall pending;
if (pendingCalls.TryGetValue (reply_serial, out pending)) {
if (pendingCalls.Remove (reply_serial))
pending.Reply = msg;
return;
}
//we discard reply messages with no corresponding PendingCall
if (Protocol.Verbose)
Console.Error.WriteLine ("Unexpected reply message received: MessageType='" + msg.Header.MessageType + "', ReplySerial=" + reply_serial);
return;
}
}
switch (msg.Header.MessageType) {
case MessageType.MethodCall:
MethodCall method_call = new MethodCall (msg);
HandleMethodCall (method_call);
break;
case MessageType.Signal:
//HandleSignal (msg);
lock (Inbound)
Inbound.Enqueue (msg);
break;
case MessageType.Error:
//TODO: better exception handling
Error error = new Error (msg);
string errMsg = String.Empty;
if (msg.Signature.Value.StartsWith ("s")) {
MessageReader reader = new MessageReader (msg);
errMsg = reader.ReadString ();
}
//throw new Exception ("Remote Error: Signature='" + msg.Signature.Value + "' " + error.ErrorName + ": " + errMsg);
//if (Protocol.Verbose)
Console.Error.WriteLine ("Remote Error: Signature='" + msg.Signature.Value + "' " + error.ErrorName + ": " + errMsg);
break;
case MessageType.Invalid:
default:
throw new Exception ("Invalid message received: MessageType='" + msg.Header.MessageType + "'");
}
}
Dictionary<uint,PendingCall> pendingCalls = new Dictionary<uint,PendingCall> ();
//this might need reworking with MulticastDelegate
internal void HandleSignal (Message msg)
{
Signal signal = new Signal (msg);
//TODO: this is a hack, not necessary when MatchRule is complete
MatchRule rule = new MatchRule ();
rule.MessageType = MessageType.Signal;
rule.Interface = signal.Interface;
rule.Member = signal.Member;
rule.Path = signal.Path;
Delegate dlg;
if (Handlers.TryGetValue (rule, out dlg)) {
//dlg.DynamicInvoke (GetDynamicValues (msg));
MethodInfo mi = dlg.Method;
//signals have no return value
dlg.DynamicInvoke (MessageHelper.GetDynamicValues (msg, mi.GetParameters ()));
} else {
//TODO: how should we handle this condition? sending an Error may not be appropriate in this case
if (Protocol.Verbose)
Console.Error.WriteLine ("Warning: No signal handler for " + signal.Member);
}
}
internal Dictionary<MatchRule,Delegate> Handlers = new Dictionary<MatchRule,Delegate> ();
//very messy
internal void MaybeSendUnknownMethodError (MethodCall method_call)
{
Message msg = MessageHelper.CreateUnknownMethodError (method_call);
if (msg != null)
Send (msg);
}
//not particularly efficient and needs to be generalized
internal void HandleMethodCall (MethodCall method_call)
{
//TODO: Ping and Introspect need to be abstracted and moved somewhere more appropriate once message filter infrastructure is complete
//FIXME: these special cases are slightly broken for the case where the member but not the interface is specified in the message
if (method_call.Interface == "org.freedesktop.DBus.Peer" && method_call.Member == "Ping") {
Message reply = MessageHelper.ConstructReply (method_call);
Send (reply);
return;
}
if (method_call.Interface == "org.freedesktop.DBus.Introspectable" && method_call.Member == "Introspect") {
Introspector intro = new Introspector ();
intro.root_path = method_call.Path;
intro.WriteStart ();
//FIXME: do this properly
//this is messy and inefficient
List<string> linkNodes = new List<string> ();
int depth = method_call.Path.Decomposed.Length;
foreach (ObjectPath pth in RegisteredObjects.Keys) {
if (pth.Value == (method_call.Path.Value)) {
ExportObject exo = (ExportObject)RegisteredObjects[pth];
intro.WriteType (exo.obj.GetType ());
} else {
for (ObjectPath cur = pth ; cur != null ; cur = cur.Parent) {
if (cur.Value == method_call.Path.Value) {
string linkNode = pth.Decomposed[depth];
if (!linkNodes.Contains (linkNode)) {
intro.WriteNode (linkNode);
linkNodes.Add (linkNode);
}
}
}
}
}
intro.WriteEnd ();
Message reply = MessageHelper.ConstructReply (method_call, intro.xml);
Send (reply);
return;
}
BusObject bo;
if (RegisteredObjects.TryGetValue (method_call.Path, out bo)) {
ExportObject eo = (ExportObject)bo;
eo.HandleMethodCall (method_call);
} else {
MaybeSendUnknownMethodError (method_call);
}
}
Dictionary<ObjectPath,BusObject> RegisteredObjects = new Dictionary<ObjectPath,BusObject> ();
//FIXME: this shouldn't be part of the core API
//that also applies to much of the other object mapping code
public object GetObject (Type type, string bus_name, ObjectPath path)
{
//if (type == null)
// return GetObject (bus_name, path);
//if the requested type is an interface, we can implement it efficiently
//otherwise we fall back to using a transparent proxy
if (type.IsInterface) {
return BusObject.GetObject (this, bus_name, path, type);
} else {
if (Protocol.Verbose)
Console.Error.WriteLine ("Warning: Note that MarshalByRefObject use is not recommended; for best performance, define interfaces");
BusObject busObject = new BusObject (this, bus_name, path);
DProxy prox = new DProxy (busObject, type);
return prox.GetTransparentProxy ();
}
}
public T GetObject<T> (string bus_name, ObjectPath path)
{
return (T)GetObject (typeof (T), bus_name, path);
}
[Obsolete ("Use the overload of Register() which does not take a bus_name parameter")]
public void Register (string bus_name, ObjectPath path, object obj)
{
Register (path, obj);
}
[Obsolete ("Use the overload of Unregister() which does not take a bus_name parameter")]
public object Unregister (string bus_name, ObjectPath path)
{
return Unregister (path);
}
public void Register (ObjectPath path, object obj)
{
ExportObject eo = new ExportObject (this, path, obj);
eo.Registered = true;
//TODO: implement some kind of tree data structure or internal object hierarchy. right now we are ignoring the name and putting all object paths in one namespace, which is bad
RegisteredObjects[path] = eo;
}
public object Unregister (ObjectPath path)
{
BusObject bo;
if (!RegisteredObjects.TryGetValue (path, out bo))
throw new Exception ("Cannot unregister " + path + " as it isn't registered");
RegisteredObjects.Remove (path);
ExportObject eo = (ExportObject)bo;
eo.Registered = false;
return eo.obj;
}
//these look out of place, but are useful
internal protected virtual void AddMatch (string rule)
{
}
internal protected virtual void RemoveMatch (string rule)
{
}
static Connection ()
{
if (BitConverter.IsLittleEndian)
NativeEndianness = EndianFlag.Little;
else
NativeEndianness = EndianFlag.Big;
}
internal static readonly EndianFlag NativeEndianness;
}
}
|