mscorlib(4.0.0.0) API with additions
TrackingServices.cs
2 using System.Security;
3 using System.Threading;
4 
6 {
8  [SecurityCritical]
9  [ComVisible(true)]
10  public class TrackingServices
11  {
12  private static volatile ITrackingHandler[] _Handlers = new ITrackingHandler[0];
13 
14  private static volatile int _Size = 0;
15 
16  private static object s_TrackingServicesSyncObject = null;
17 
18  private static object TrackingServicesSyncObject
19  {
20  get
21  {
22  if (s_TrackingServicesSyncObject == null)
23  {
24  object value = new object();
25  Interlocked.CompareExchange(ref s_TrackingServicesSyncObject, value, null);
26  }
27  return s_TrackingServicesSyncObject;
28  }
29  }
30 
33  public static ITrackingHandler[] RegisteredHandlers
34  {
35  [SecurityCritical]
36  get
37  {
38  lock (TrackingServicesSyncObject)
39  {
40  if (_Size == 0)
41  {
42  return new ITrackingHandler[0];
43  }
44  ITrackingHandler[] array = new ITrackingHandler[_Size];
45  for (int i = 0; i < _Size; i++)
46  {
47  array[i] = _Handlers[i];
48  }
49  return array;
50  }
51  }
52  }
53 
59  [SecurityCritical]
60  public static void RegisterTrackingHandler(ITrackingHandler handler)
61  {
62  if (handler == null)
63  {
64  throw new ArgumentNullException("handler");
65  }
66  lock (TrackingServicesSyncObject)
67  {
68  if (-1 != Match(handler))
69  {
70  throw new RemotingException(Environment.GetResourceString("Remoting_TrackingHandlerAlreadyRegistered", "handler"));
71  }
72  if (_Handlers == null || _Size == _Handlers.Length)
73  {
74  ITrackingHandler[] array = new ITrackingHandler[_Size * 2 + 4];
75  if (_Handlers != null)
76  {
77  Array.Copy(_Handlers, array, _Size);
78  }
79  _Handlers = array;
80  }
81  Volatile.Write(ref _Handlers[_Size++], handler);
82  }
83  }
84 
90  [SecurityCritical]
91  public static void UnregisterTrackingHandler(ITrackingHandler handler)
92  {
93  if (handler == null)
94  {
95  throw new ArgumentNullException("handler");
96  }
97  lock (TrackingServicesSyncObject)
98  {
99  int num = Match(handler);
100  if (-1 == num)
101  {
102  throw new RemotingException(Environment.GetResourceString("Remoting_HandlerNotRegistered", handler));
103  }
104  Array.Copy(_Handlers, num + 1, _Handlers, num, _Size - num - 1);
105  _Size--;
106  }
107  }
108 
109  [SecurityCritical]
110  internal static void MarshaledObject(object obj, ObjRef or)
111  {
112  try
113  {
114  ITrackingHandler[] handlers = _Handlers;
115  for (int i = 0; i < _Size; i++)
116  {
117  Volatile.Read(ref handlers[i]).MarshaledObject(obj, or);
118  }
119  }
120  catch
121  {
122  }
123  }
124 
125  [SecurityCritical]
126  internal static void UnmarshaledObject(object obj, ObjRef or)
127  {
128  try
129  {
130  ITrackingHandler[] handlers = _Handlers;
131  for (int i = 0; i < _Size; i++)
132  {
133  Volatile.Read(ref handlers[i]).UnmarshaledObject(obj, or);
134  }
135  }
136  catch
137  {
138  }
139  }
140 
141  [SecurityCritical]
142  internal static void DisconnectedObject(object obj)
143  {
144  try
145  {
146  ITrackingHandler[] handlers = _Handlers;
147  for (int i = 0; i < _Size; i++)
148  {
149  Volatile.Read(ref handlers[i]).DisconnectedObject(obj);
150  }
151  }
152  catch
153  {
154  }
155  }
156 
157  private static int Match(ITrackingHandler handler)
158  {
159  int result = -1;
160  for (int i = 0; i < _Size; i++)
161  {
162  if (_Handlers[i] == handler)
163  {
164  result = i;
165  break;
166  }
167  }
168  return result;
169  }
170  }
171 }
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
static void Write(ref bool location, bool value)
Writes the specified value to the specified field. On systems that require it, inserts a memory barri...
Definition: Volatile.cs:186
Definition: __Canon.cs:3
Stores all relevant information required to generate a proxy in order to communicate with a remote ob...
Definition: ObjRef.cs:19
static void RegisterTrackingHandler(ITrackingHandler handler)
Registers a new tracking handler with the T:System.Runtime.Remoting.Services.TrackingServices.
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
Indicates that the implementing object must be notified of marshaling, unmarshaling,...
static ITrackingHandler [] RegisteredHandlers
Gets an array of the tracking handlers that are currently registered with T:System....
static int CompareExchange(ref int location1, int value, int comparand)
Compares two 32-bit signed integers for equality and, if they are equal, replaces the first value.
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition: Array.cs:17
Contains methods for performing volatile memory operations.
Definition: Volatile.cs:8
static bool Read(ref bool location)
Reads the value of the specified field. On systems that require it, inserts a memory barrier that pre...
Definition: Volatile.cs:15
static void UnregisterTrackingHandler(ITrackingHandler handler)
Unregisters the specified tracking handler from T:System.Runtime.Remoting.Services....
static void Copy(Array sourceArray, Array destinationArray, int length)
Copies a range of elements from an T:System.Array starting at the first element and pastes them into ...
Definition: Array.cs:1275
Provides atomic operations for variables that are shared by multiple threads.
Definition: Interlocked.cs:10
The exception that is thrown when something has gone wrong during remoting.
Provides a way to register, unregister, and obtain a list of tracking handlers.