summaryrefslogtreecommitdiffstats
path: root/mono/Message.cs
blob: 54472d1efdaa8d7e05584e60151a19a6404899f6 (plain)
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
namespace DBus 
{
  
  using System;
  using System.Runtime.InteropServices;
  using System.Diagnostics;
  using System.Collections;
  
  public class Message 
  {
    
    /// <summary>
    /// A pointer to the underlying Message structure
    /// </summary>
    private IntPtr rawMessage;
    
    /// <summary>
    /// The current slot number
    /// </summary>
    private static int slot = -1;
    
    // Keep in sync with C
    public enum MessageType 
    {
      Invalid = 0,
      MethodCall = 1,
      MethodReturn = 2,
      Error = 3,
      Signal = 4
    }

    private Arguments arguments = null;

    protected Service service = null;
    protected string pathName = null;
    protected string interfaceName = null;
    protected string name = null;    
    private string key= null;

    protected Message()
    {
      // An empty constructor for the sake of sub-classes which know how to construct theirselves.
    }
    
    protected Message(IntPtr rawMessage, Service service)
    {
      RawMessage = rawMessage;
      this.service = service;
    }
    
    protected Message(MessageType messageType) 
    {
      // the assignment bumps the refcount
      RawMessage = dbus_message_new((int) messageType);
      
      if (RawMessage == IntPtr.Zero) {
	throw new OutOfMemoryException();
      }
      
      dbus_message_unref(RawMessage);
    }
    
    protected Message(MessageType messageType, Service service) : this(messageType) 
    {
      this.service = service;
    }
    
    ~Message() 
    {
      RawMessage = IntPtr.Zero; // free the native object
    }
    
    public static Message Wrap(IntPtr rawMessage, Service service) 
    {
      if (slot > -1) {
	// If we already have a Message object associated with this rawMessage then return it
	IntPtr rawThis = dbus_message_get_data(rawMessage, slot);
	if (rawThis != IntPtr.Zero)
	  return (DBus.Message) ((GCHandle)rawThis).Target;
      } 
      // If it doesn't exist then create a new Message around it
      Message message = null;
      MessageType messageType = (MessageType) dbus_message_get_type(rawMessage);
      
      switch (messageType) {
      case MessageType.Signal:
	message = new Signal(rawMessage, service);
	break;
      case MessageType.MethodCall:
	message = new MethodCall(rawMessage, service);
	break;
      case MessageType.MethodReturn:
	message = new MethodReturn(rawMessage, service);
	break;
      case MessageType.Error:
	message = new ErrorMessage(rawMessage, service);
	break;
      default:
	throw new ApplicationException("Unknown message type to wrap: " + messageType);
      }

      return message;
    }
    
    internal IntPtr RawMessage 
    {
      get 
	{
	  return rawMessage;
	}
      set 
	{
	  if (value == rawMessage) 
	    return;
	  
	  if (rawMessage != IntPtr.Zero) 
	    {
	      // Get the reference to this
	      IntPtr rawThis = dbus_message_get_data(rawMessage, Slot);
	      Debug.Assert (rawThis != IntPtr.Zero);
	      
	      // Blank over the reference
	      dbus_message_set_data(rawMessage, Slot, IntPtr.Zero, IntPtr.Zero);
	      
	      // Free the reference
	      ((GCHandle) rawThis).Free();
	      
	      // Unref the connection
	      dbus_message_unref(rawMessage);
	    }
	  
	  this.rawMessage = value;
	  
	  if (rawMessage != IntPtr.Zero) 
	    {
	      GCHandle rawThis;
	      
	      dbus_message_ref(rawMessage);
	      
	      // We store a weak reference to the C# object on the C object
	      rawThis = GCHandle.Alloc(this, GCHandleType.WeakTrackResurrection);
	      
	      dbus_message_set_data(rawMessage, Slot, (IntPtr) rawThis, IntPtr.Zero);
	    }
	}
    }
    
    public void Send(ref int serial) 
    {
      if (!dbus_connection_send (Service.Connection.RawConnection, RawMessage, ref serial))
	throw new OutOfMemoryException ();

      Service.Connection.Flush();
    }
    
    public void Send() 
    {
      int ignored = 0;
      Send(ref ignored);
    }

    public void SendWithReply() 
    {
      IntPtr rawPendingCall = IntPtr.Zero;
      
      if (!dbus_connection_send_with_reply (Service.Connection.RawConnection, RawMessage, rawPendingCall, Service.Connection.Timeout))
	throw new OutOfMemoryException();
    }
     
    public MethodReturn SendWithReplyAndBlock()
    {
      Error error = new Error();
      error.Init();

      IntPtr rawMessage = dbus_connection_send_with_reply_and_block(Service.Connection.RawConnection, 
								    RawMessage, 
								    Service.Connection.Timeout, 
								    ref error);

      if (rawMessage != IntPtr.Zero) {
	MethodReturn methodReturn = new MethodReturn(rawMessage, Service);
	return methodReturn;
      } else {
	throw new DBusException(error);
      }
    }

    public MessageType Type
    {
      get 
	{
	  return (MessageType) dbus_message_get_type(RawMessage);
	}
    }
    
    public Service Service
    {
      set 
	{
	  if (this.service != null && (value.Name != this.service.Name)) {
	    if (!dbus_message_set_destination(RawMessage, value.Name)) {
	      throw new OutOfMemoryException();
	    }
	  }
	  
	  this.service = value;
	}
      get 
	{
	  return this.service;
	}
    }
    
    protected virtual string PathName
    {
      set 
	{
	  if (value != this.pathName) 
	    {
	      if (!dbus_message_set_path(RawMessage, value)) {
		throw new OutOfMemoryException();
	      }
	      
	      this.pathName = value;
	    }
	}
      get 
	{
	  if (this.pathName == null) {
	    this.pathName = Marshal.PtrToStringAnsi(dbus_message_get_path(RawMessage));
	  }
	  
	  return this.pathName;
	}
    }
    
    protected virtual string InterfaceName
    {
      set 
	{
	  if (value != this.interfaceName)
	    {
	      dbus_message_set_interface (RawMessage, value);
	      this.interfaceName = value;
	    }
	}
      get 
	{
	  if (this.interfaceName == null) {
	    this.interfaceName = Marshal.PtrToStringAnsi(dbus_message_get_interface(RawMessage));
	  }

	  return this.interfaceName;
	}
    }
    
    protected virtual string Name
    {
      set {
	if (value != this.name) {
	  dbus_message_set_member(RawMessage, value);
	  this.name = value;
	}
      }
      get {
	if (this.name == null) {
	  this.name = Marshal.PtrToStringAnsi(dbus_message_get_member(RawMessage));
	}

	return this.name;
      }
    }

    public string Key
    {
      get {
	if (this.key == null) {
	  this.key = Name + " " + Arguments;
	}
	
	return this.key;
      }
    }

    public Arguments Arguments
    {
      get 
	{
	  if (this.arguments == null) {
	    this.arguments = new Arguments(this);
	  }
	  
	  return this.arguments;
	}
    }

    protected int Slot
    {
      get 
	{
	  if (slot == -1) 
	    {
	      // We need to initialize the slot
	      if (!dbus_message_allocate_data_slot (ref slot))
		throw new OutOfMemoryException ();
	      
	      Debug.Assert (slot >= 0);
	    }
	  
	  return slot;
	}
    }
    
    [DllImport ("dbus-1", EntryPoint="dbus_message_new")]
    protected extern static IntPtr dbus_message_new (int messageType);
    
    [DllImport ("dbus-1", EntryPoint="dbus_message_unref")]
    protected extern static void dbus_message_unref (IntPtr ptr);
    
    [DllImport ("dbus-1", EntryPoint="dbus_message_ref")]
    protected extern static void dbus_message_ref (IntPtr ptr);
    
    [DllImport ("dbus-1", EntryPoint="dbus_message_allocate_data_slot")]
    protected extern static bool dbus_message_allocate_data_slot (ref int slot);
    
    [DllImport ("dbus-1", EntryPoint="dbus_message_free_data_slot")]
    protected extern static void dbus_message_free_data_slot (ref int slot);
    
    [DllImport ("dbus-1", EntryPoint="dbus_message_set_data")]
    protected extern static bool dbus_message_set_data (IntPtr ptr,
							int    slot,
							IntPtr data,
							IntPtr free_data_func);
    
    [DllImport ("dbus-1", EntryPoint="dbus_message_get_data")]
    protected extern static IntPtr dbus_message_get_data (IntPtr ptr,
							  int    slot);
    
    [DllImport ("dbus-1", EntryPoint="dbus_connection_send")]
    private extern static bool dbus_connection_send (IntPtr  ptr,
						     IntPtr  message,
						     ref int client_serial);

    [DllImport ("dbus-1", EntryPoint="dbus_connection_send_with_reply")]
    private extern static bool dbus_connection_send_with_reply (IntPtr rawConnection, IntPtr rawMessage, IntPtr rawPendingCall, int timeout);

    [DllImport ("dbus-1", EntryPoint="dbus_connection_send_with_reply_and_block")]
    private extern static IntPtr dbus_connection_send_with_reply_and_block (IntPtr rawConnection, IntPtr  message, int timeout, ref Error error);

    [DllImport("dbus-1")]
    private extern static int dbus_message_get_type(IntPtr rawMessage);

    [DllImport("dbus-1")]
    private extern static bool dbus_message_set_path(IntPtr rawMessage, string pathName);

    [DllImport("dbus-1")]
    private extern static IntPtr dbus_message_get_path(IntPtr rawMessage);
    
    [DllImport("dbus-1")]
    private extern static bool dbus_message_set_interface (IntPtr rawMessage, string interfaceName);

    [DllImport("dbus-1")]
    private extern static IntPtr dbus_message_get_interface(IntPtr rawMessage);
    
    [DllImport("dbus-1")]
    private extern static bool dbus_message_set_member(IntPtr rawMessage, string name);

    [DllImport("dbus-1")]
    private extern static IntPtr dbus_message_get_member(IntPtr rawMessage);

    [DllImport("dbus-1")]
    private extern static bool dbus_message_set_destination(IntPtr rawMessage, string serviceName);

    [DllImport("dbus-1")]
    private extern static IntPtr dbus_message_get_destination(IntPtr rawMessage);
  }
}