PeerNode.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel
  5. {
  6. using System;
  7. using System.Runtime;
  8. using System.ServiceModel.Channels;
  9. using System.ServiceModel.Dispatcher;
  10. using System.Threading;
  11. public sealed class PeerNode : IOnlineStatus
  12. {
  13. PeerNodeImplementation innerNode = null;
  14. SynchronizationContext synchronizationContext = null;
  15. MessageEncodingBindingElement encoderElement;
  16. internal PeerNode(PeerNodeImplementation peerNode)
  17. {
  18. this.innerNode = peerNode;
  19. }
  20. public event EventHandler Offline;
  21. public event EventHandler Online;
  22. internal void FireOffline(object source, EventArgs args)
  23. {
  24. FireEvent(Offline, source, args);
  25. }
  26. internal void FireOnline(object source, EventArgs args)
  27. {
  28. FireEvent(Online, source, args);
  29. }
  30. void FireEvent(EventHandler handler, object source, EventArgs args)
  31. {
  32. if (handler != null)
  33. {
  34. try
  35. {
  36. SynchronizationContext context = synchronizationContext;
  37. if (context != null)
  38. {
  39. context.Send(delegate(object state) { handler(source, args); }, null);
  40. }
  41. else
  42. {
  43. handler(source, args);
  44. }
  45. }
  46. catch (Exception e)
  47. {
  48. if (Fx.IsFatal(e)) throw;
  49. throw DiagnosticUtility.ExceptionUtility.ThrowHelperCallback(SR.GetString(SR.NotificationException), e);
  50. }
  51. }
  52. }
  53. public bool IsOnline { get { return InnerNode.IsOnline; } }
  54. internal bool IsOpen { get { return InnerNode.IsOpen; } }
  55. public int Port { get { return InnerNode.ListenerPort; } }
  56. public PeerMessagePropagationFilter MessagePropagationFilter
  57. {
  58. get { return InnerNode.MessagePropagationFilter; }
  59. set { InnerNode.MessagePropagationFilter = value; }
  60. }
  61. internal void OnOpen()
  62. {
  63. synchronizationContext = ThreadBehavior.GetCurrentSynchronizationContext();
  64. this.innerNode.Offline += FireOffline;
  65. this.innerNode.Online += FireOnline;
  66. this.innerNode.EncodingElement = this.encoderElement;
  67. }
  68. internal void OnClose()
  69. {
  70. this.innerNode.Offline -= FireOffline;
  71. this.innerNode.Online -= FireOnline;
  72. synchronizationContext = null;
  73. }
  74. internal PeerNodeImplementation InnerNode
  75. {
  76. get { return innerNode; }
  77. }
  78. public void RefreshConnection()
  79. {
  80. PeerNodeImplementation node = InnerNode;
  81. if (node != null)
  82. {
  83. node.RefreshConnection();
  84. }
  85. }
  86. public override string ToString()
  87. {
  88. if (this.IsOpen)
  89. {
  90. return SR.GetString(SR.PeerNodeToStringFormat, this.InnerNode.MeshId, this.InnerNode.NodeId, this.IsOnline, this.IsOpen, this.Port);
  91. }
  92. else
  93. {
  94. return SR.GetString(SR.PeerNodeToStringFormat, "", -1, this.IsOnline, this.IsOpen, -1);
  95. }
  96. }
  97. private MessageEncodingBindingElement EncodingElement
  98. {
  99. get { return this.encoderElement; }
  100. set { this.encoderElement = value; }
  101. }
  102. }
  103. }