RemoteEndpointMessageProperty.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. //----------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //----------------------------------------------------------------------------
  4. namespace System.ServiceModel.Channels
  5. {
  6. using System;
  7. using System.Net;
  8. public sealed class RemoteEndpointMessageProperty
  9. {
  10. string address;
  11. int port;
  12. IPEndPoint remoteEndPoint;
  13. IRemoteEndpointProvider remoteEndpointProvider;
  14. InitializationState state;
  15. object thisLock = new object();
  16. public RemoteEndpointMessageProperty(string address, int port)
  17. {
  18. if (string.IsNullOrEmpty(address))
  19. {
  20. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("address");
  21. }
  22. if (port < IPEndPoint.MinPort || port > IPEndPoint.MaxPort)
  23. {
  24. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument("port",
  25. SR.GetString(SR.ValueMustBeInRange, IPEndPoint.MinPort, IPEndPoint.MaxPort));
  26. }
  27. this.port = port;
  28. this.address = address;
  29. this.state = InitializationState.All;
  30. }
  31. internal RemoteEndpointMessageProperty(IRemoteEndpointProvider remoteEndpointProvider)
  32. {
  33. this.remoteEndpointProvider = remoteEndpointProvider;
  34. }
  35. internal RemoteEndpointMessageProperty(IPEndPoint remoteEndPoint)
  36. {
  37. this.remoteEndPoint = remoteEndPoint;
  38. }
  39. public static string Name
  40. {
  41. get { return "System.ServiceModel.Channels.RemoteEndpointMessageProperty"; }
  42. }
  43. public string Address
  44. {
  45. get
  46. {
  47. if ((this.state & InitializationState.Address) != InitializationState.Address)
  48. {
  49. lock (ThisLock)
  50. {
  51. if ((this.state & InitializationState.Address) != InitializationState.Address)
  52. {
  53. Initialize(false);
  54. }
  55. }
  56. }
  57. return this.address;
  58. }
  59. }
  60. public int Port
  61. {
  62. get
  63. {
  64. if ((this.state & InitializationState.Port) != InitializationState.Port)
  65. {
  66. lock (ThisLock)
  67. {
  68. if ((this.state & InitializationState.Port) != InitializationState.Port)
  69. {
  70. Initialize(true);
  71. }
  72. }
  73. }
  74. return this.port;
  75. }
  76. }
  77. object ThisLock
  78. {
  79. get { return thisLock; }
  80. }
  81. void Initialize(bool getHostedPort)
  82. {
  83. if (remoteEndPoint != null)
  84. {
  85. this.address = remoteEndPoint.Address.ToString();
  86. this.port = remoteEndPoint.Port;
  87. this.state = InitializationState.All;
  88. this.remoteEndPoint = null;
  89. }
  90. else
  91. {
  92. if ((this.state & InitializationState.Address) != InitializationState.Address)
  93. {
  94. this.address = remoteEndpointProvider.GetAddress();
  95. this.state |= InitializationState.Address;
  96. }
  97. if (getHostedPort)
  98. {
  99. this.port = remoteEndpointProvider.GetPort();
  100. this.state |= InitializationState.Port;
  101. this.remoteEndpointProvider = null;
  102. }
  103. }
  104. }
  105. internal interface IRemoteEndpointProvider
  106. {
  107. string GetAddress();
  108. int GetPort();
  109. }
  110. [Flags]
  111. enum InitializationState
  112. {
  113. None = 0,
  114. Address = 1,
  115. Port = 2,
  116. All = 3
  117. }
  118. }
  119. }