summaryrefslogtreecommitdiffstats
path: root/mono/Connection.cs
blob: 56dcb7a2b71a43ffd12754e8ef9be5c051fb01bf (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
namespace DBus {
  
  using System;
  using System.Runtime.InteropServices;
  using System.Diagnostics;
  
  public class Connection {

    public Connection (string address) {
      // the assignment bumps the refcount
      Error error = new Error ();
      error.Init ();
      raw = dbus_connection_open (address, ref error);
      if (raw != IntPtr.Zero) {
        dbus_connection_unref (raw);
      } else {
        Exception e = new Exception (ref error);
        error.Free ();
        throw e;
      }
      dbus_connection_setup_with_g_main (raw, IntPtr.Zero);
    }

    // Keep in sync with C
    public enum BusType {
      Session = 0,
      System = 1,
      Activation = 2
    }

    public static Connection GetBus (BusType bus) {
      Error error = new Error ();

      error.Init ();
      
      IntPtr ptr = dbus_bus_get ((int) bus, ref error);
      if (ptr != IntPtr.Zero) {
        Connection c = Wrap (ptr);
        dbus_connection_unref (ptr);
        return c;
      } else {
        Exception e = new Exception (ref error);
        error.Free ();
        throw e;   
      }
    }
    
    public void Send (Message m,
                      ref int serial) {
      if (!dbus_connection_send (raw, m.raw, ref serial))
        throw new OutOfMemoryException ();
    }

    public void Send (Message m) {
      int ignored = 0;
      Send (m, ref ignored);
    }

    public void Flush () {
      dbus_connection_flush (raw);
    }

    public void Disconnect () {
      dbus_connection_disconnect (raw);
    }
    
    public static Connection Wrap (IntPtr ptr) {
      IntPtr gch_ptr;
      
      gch_ptr = dbus_connection_get_data (ptr, wrapper_slot);
      if (gch_ptr != IntPtr.Zero) {
        return (DBus.Connection) ((GCHandle)gch_ptr).Target;
      } else {
        return new Connection (ptr);
      }
    }

    // surely there's a convention for this pattern with the property
    // and the real member
    IntPtr raw_;
    internal IntPtr raw {
      get {
        return raw_; 
      }
      set {
        if (value == raw_)
          return;
        
        if (raw_ != IntPtr.Zero) {
          IntPtr gch_ptr;
          
          gch_ptr = dbus_connection_get_data (raw_,
                                              wrapper_slot);
          Debug.Assert (gch_ptr != IntPtr.Zero);

          dbus_connection_set_data (raw_, wrapper_slot,
                                    IntPtr.Zero, IntPtr.Zero);
          
          ((GCHandle) gch_ptr).Free ();
          
          dbus_connection_unref (raw_);
        }
        
        raw_ = value;

        if (raw_ != IntPtr.Zero) {
          GCHandle gch;

          dbus_connection_ref (raw_);

          // We store a weak reference to the C# object on the C object
          gch = GCHandle.Alloc (this, GCHandleType.WeakTrackResurrection);
          
          dbus_connection_set_data (raw_, wrapper_slot,
                                    (IntPtr) gch, IntPtr.Zero);
        }
      }
    }

    ~Connection () {
      if (raw != IntPtr.Zero) {
        Disconnect ();
      }
      raw = IntPtr.Zero; // free the native object
    }
    
    Connection (IntPtr r) {
      raw = r;
    }
    
    // static constructor runs before any methods 
    static Connection () {
      DBus.Internals.Init ();
      
      Debug.Assert (wrapper_slot == -1);
      
      if (!dbus_connection_allocate_data_slot (ref wrapper_slot))
        throw new OutOfMemoryException ();

      Debug.Assert (wrapper_slot >= 0);
    }

    // slot used to store the C# object on the C object
    static int wrapper_slot = -1;
    
    [DllImport (DBus.Internals.DBusLibname, EntryPoint="dbus_connection_open")]
      private extern static IntPtr dbus_connection_open (string address,
                                                         ref Error error);

    [DllImport (DBus.Internals.DBusLibname, EntryPoint="dbus_connection_unref")]
      private extern static void dbus_connection_unref (IntPtr ptr);

    [DllImport (DBus.Internals.DBusLibname, EntryPoint="dbus_connection_ref")]
      private extern static void dbus_connection_ref (IntPtr ptr);

    [DllImport (DBus.Internals.DBusLibname, EntryPoint="dbus_connection_allocate_data_slot")]
      private extern static bool dbus_connection_allocate_data_slot (ref int slot);

    [DllImport (DBus.Internals.DBusLibname, EntryPoint="dbus_connection_free_data_slot")]
      private extern static void dbus_connection_free_data_slot (ref int slot);

    [DllImport (DBus.Internals.DBusLibname, EntryPoint="dbus_connection_set_data")]
      private extern static bool dbus_connection_set_data (IntPtr ptr,
                                                           int    slot,
                                                           IntPtr data,
                                                           IntPtr free_data_func);

    [DllImport (DBus.Internals.DBusLibname, EntryPoint="dbus_connection_send")]
      private extern static bool dbus_connection_send (IntPtr  ptr,
                                                       IntPtr  message,
                                                       ref int client_serial);

    [DllImport (DBus.Internals.DBusLibname, EntryPoint="dbus_connection_flush")]
      private extern static void dbus_connection_flush (IntPtr  ptr);
    
    [DllImport (DBus.Internals.DBusLibname, EntryPoint="dbus_bus_get")]
      private extern static IntPtr dbus_bus_get (int        which,
                                                 ref Error  error);
    
    [DllImport (DBus.Internals.DBusLibname, EntryPoint="dbus_connection_get_data")]
      private extern static IntPtr dbus_connection_get_data (IntPtr ptr,
                                                             int    slot);

    [DllImport (DBus.Internals.DBusLibname, EntryPoint="dbus_connection_disconnect")]
      private extern static void dbus_connection_disconnect (IntPtr ptr);
    
    [DllImport (DBus.Internals.DBusGLibname, EntryPoint="dbus_connection_setup_with_g_main")]
      private extern static void dbus_connection_setup_with_g_main (IntPtr ptr,
                                                                    IntPtr context);
    
  }
}