TrackingServices.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //
  2. // System.Runtime.Remoting.Services.TrackingServices.cs
  3. //
  4. // Author:
  5. // Jaime Anguiano Olarra ([email protected])
  6. // Patrik Torstensson
  7. //
  8. // (C) 2002, Jaime Anguiano Olarra
  9. //
  10. //
  11. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  12. //
  13. // Permission is hereby granted, free of charge, to any person obtaining
  14. // a copy of this software and associated documentation files (the
  15. // "Software"), to deal in the Software without restriction, including
  16. // without limitation the rights to use, copy, modify, merge, publish,
  17. // distribute, sublicense, and/or sell copies of the Software, and to
  18. // permit persons to whom the Software is furnished to do so, subject to
  19. // the following conditions:
  20. //
  21. // The above copyright notice and this permission notice shall be
  22. // included in all copies or substantial portions of the Software.
  23. //
  24. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  28. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  29. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  30. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. //
  32. using System;
  33. using System.Collections;
  34. using System.Runtime.Remoting;
  35. namespace System.Runtime.Remoting.Services {
  36. public class TrackingServices {
  37. static ArrayList _handlers = new ArrayList();
  38. public TrackingServices () {
  39. }
  40. public static void RegisterTrackingHandler (ITrackingHandler handler) {
  41. if (null == handler)
  42. throw new ArgumentNullException("handler");
  43. lock (_handlers.SyncRoot) {
  44. if (-1 != _handlers.IndexOf(handler))
  45. throw new RemotingException("handler already registered");
  46. _handlers.Add(handler);
  47. }
  48. }
  49. public static void UnregisterTrackingHandler (ITrackingHandler handler) {
  50. if (null == handler)
  51. throw new ArgumentNullException("handler");
  52. lock (_handlers.SyncRoot) {
  53. int idx = _handlers.IndexOf(handler);
  54. if (idx == -1)
  55. throw new RemotingException("handler is not registered");
  56. _handlers.RemoveAt(idx);
  57. }
  58. }
  59. public static ITrackingHandler[] RegisteredHandlers {
  60. get {
  61. lock (_handlers.SyncRoot) {
  62. if (_handlers.Count == 0)
  63. return new ITrackingHandler[0];
  64. return (ITrackingHandler[]) _handlers.ToArray (typeof(ITrackingHandler));
  65. }
  66. }
  67. }
  68. internal static void NotifyMarshaledObject(Object obj, ObjRef or)
  69. {
  70. ITrackingHandler[] handlers;
  71. lock (_handlers.SyncRoot) {
  72. if (_handlers.Count == 0) return;
  73. handlers = (ITrackingHandler[]) _handlers.ToArray (typeof(ITrackingHandler));
  74. }
  75. for(int i = 0; i < handlers.Length; i++) {
  76. handlers[i].MarshaledObject (obj, or);
  77. }
  78. }
  79. internal static void NotifyUnmarshaledObject(Object obj, ObjRef or)
  80. {
  81. ITrackingHandler[] handlers;
  82. lock (_handlers.SyncRoot) {
  83. if (_handlers.Count == 0) return;
  84. handlers = (ITrackingHandler[]) _handlers.ToArray (typeof(ITrackingHandler));
  85. }
  86. for(int i = 0; i < handlers.Length; i++) {
  87. handlers[i].UnmarshaledObject (obj, or);
  88. }
  89. }
  90. internal static void NotifyDisconnectedObject(Object obj)
  91. {
  92. ITrackingHandler[] handlers;
  93. lock (_handlers.SyncRoot) {
  94. if (_handlers.Count == 0) return;
  95. handlers = (ITrackingHandler[]) _handlers.ToArray (typeof(ITrackingHandler));
  96. }
  97. for(int i = 0; i < handlers.Length; i++) {
  98. handlers[i].DisconnectedObject (obj);
  99. }
  100. }
  101. }
  102. }