summaryrefslogtreecommitdiffstats
path: root/mono/Server.cs
blob: e39b7ca2f186c282f4ce9ec1cff580d5a395c777 (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
namespace DBus
{
  using System;
  using System.Runtime.InteropServices;
  using System.Diagnostics;
  
  public class Server
  {
    private IntPtr rawServer;
    
    /// <summary>
    /// The current slot number
    /// </summary>
    private static int slot = -1;

    private string address = null;
    
    private Server(IntPtr rawServer)
    {
      RawServer = rawServer;
    }
    
    public Server(string address)
    {
      Error error = new Error();
      error.Init();
      RawServer = dbus_server_listen(address, ref error);
      if (RawServer != IntPtr.Zero){
	dbus_server_unref(RawServer);
      } else {
	throw new DBusException(error);
      }
    }
    
    ~Server()
    {
      if (RawServer != IntPtr.Zero) {
	dbus_server_unref(rawServer);
      }
      
      RawServer = IntPtr.Zero;
    }
    
    public string Address 
    {
      get
	{
	  if (address == null) {
	    address = dbus_server_get_address(rawServer);
	  }
	  
	  return address;
	}
    }

    private int Slot
    {
      get 
	{
	  if (slot == -1) 
	    {
	      // We need to initialize the slot
	      if (!dbus_server_allocate_data_slot (ref slot))
		throw new OutOfMemoryException ();
	      
	      Debug.Assert (slot >= 0);
	    }
	  
	  return slot;
	}
    }

    internal IntPtr RawServer 
    {
      get 
	{
	  return rawServer;
	}
      set 
	{
	  if (value == rawServer)
	    return;
	  
	  if (rawServer != IntPtr.Zero) 
	    {
	      // Get the reference to this
	      IntPtr rawThis = dbus_server_get_data (rawServer, Slot);
	      Debug.Assert (rawThis != IntPtr.Zero);
	      
	      // Blank over the reference
	      dbus_server_set_data (rawServer, Slot, IntPtr.Zero, IntPtr.Zero);
	      
	      // Free the reference
	      ((GCHandle) rawThis).Free();
	      
	      // Unref the connection
	      dbus_server_unref(rawServer);
	    }
	  
	  this.rawServer = value;
	  
	  if (rawServer != IntPtr.Zero) 
	    {
	      GCHandle rawThis;
	      
	      dbus_server_ref (rawServer);
	      
	      // We store a weak reference to the C# object on the C object
	      rawThis = GCHandle.Alloc (this, GCHandleType.WeakTrackResurrection);
	      
	      dbus_server_set_data(rawServer, Slot, (IntPtr) rawThis, IntPtr.Zero);
	    }
	}
    }

    [DllImport ("dbus-1", EntryPoint="dbus_server_listen")]
    private extern static IntPtr dbus_server_listen(string address, ref Error error);

    [DllImport ("dbus-1", EntryPoint="dbus_server_unref")]
    private extern static IntPtr dbus_server_unref(IntPtr rawServer);

    [DllImport ("dbus-1", EntryPoint="dbus_server_ref")]
    private extern static void dbus_server_ref(IntPtr rawServer);

    [DllImport ("dbus-1", EntryPoint="dbus_server_disconnect")]
    private extern static void dbus_server_disconnect(IntPtr rawServer);

    [DllImport ("dbus-1", EntryPoint="dbus_server_get_address")]
    private extern static string dbus_server_get_address(IntPtr rawServer);

    [DllImport ("dbus-1", EntryPoint="dbus_server_set_data")]
    private extern static bool dbus_server_set_data(IntPtr rawServer,
						    int slot,
						    IntPtr data,
						    IntPtr freeDataFunc);

    [DllImport ("dbus-1", EntryPoint="dbus_server_get_data")]
    private extern static IntPtr dbus_server_get_data(IntPtr rawServer,
						      int slot);

    [DllImport ("dbus-1", EntryPoint="dbus_server_allocate_data_slot")]
    private extern static bool dbus_server_allocate_data_slot (ref int slot);
    
    [DllImport ("dbus-1", EntryPoint="dbus_server_free_data_slot")]
    private extern static void dbus_server_free_data_slot (ref int slot);

  }
}