Address.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. Copyright (c) 2012 Spotify AB
  3. Copyright (c) 2012-2015 Other contributors as noted in the AUTHORS file
  4. This file is part of 0MQ.
  5. 0MQ is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU Lesser General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. 0MQ is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. using System.Net;
  17. namespace NetMQ.Core
  18. {
  19. /// <summary>
  20. /// Class Address contains a specification of a protocol and an MqEndPoint.
  21. /// </summary>
  22. internal sealed class Address
  23. {
  24. /// <summary>
  25. /// The string-literal "inproc"
  26. /// - this denotes in-process, or inter-thread, communication.
  27. /// </summary>
  28. public const string InProcProtocol = "inproc";
  29. /// <summary>
  30. /// The string-literal "tcp"
  31. /// - this denotes TCP communication over the network.
  32. /// </summary>
  33. public const string TcpProtocol = "tcp";
  34. /// <summary>
  35. /// The string-literal "ipc"
  36. /// - this denotes inter-process communication, which on NetMQ is exactly the same as TCP.
  37. /// </summary>
  38. public const string IpcProtocol = "ipc";
  39. /// <summary>
  40. /// The string-literal "pgm"
  41. /// - this denotes the Pragmatic General Multicast (PGM) reliable multicast protocol.
  42. /// </summary>
  43. public const string PgmProtocol = "pgm";
  44. /// <summary>
  45. /// The string-literal "epgm"
  46. /// - this denotes the Encapsulated PGM protocol.
  47. /// </summary>
  48. public const string EpgmProtocol = "epgm";
  49. /// <summary>
  50. /// Interface IZAddress specifies that Resolve and property Address must be implemented.
  51. /// </summary>
  52. public interface IZAddress
  53. {
  54. void Resolve(string name, bool ip4Only);
  55. IPEndPoint? Address { get; }
  56. string Protocol { get; }
  57. }
  58. /// <summary>
  59. /// Create a new Address instance with the given protocol and text expression of an address.
  60. /// </summary>
  61. /// <param name="protocol">the protocol of this Address - as in tcp, ipc, pgm</param>
  62. /// <param name="address">a text representation of the address</param>
  63. public Address(string protocol, string address)
  64. {
  65. Protocol = protocol;
  66. AddressString = address;
  67. Resolved = null;
  68. }
  69. /// <summary>
  70. /// Create a new Address instance based upon the given endpoint, assuming a protocol of tcp.
  71. /// </summary>
  72. /// <param name="endpoint">the subclass of EndPoint to base this Address upon</param>
  73. public Address(EndPoint endpoint)
  74. {
  75. Protocol = TcpProtocol;
  76. var dnsEndPoint = endpoint as DnsEndPoint;
  77. if (dnsEndPoint != null)
  78. {
  79. AddressString = dnsEndPoint.Host + ":" + dnsEndPoint.Port;
  80. return;
  81. }
  82. var ipEndPoint = endpoint as IPEndPoint;
  83. if (ipEndPoint != null)
  84. {
  85. AddressString = ipEndPoint.Address + ":" + ipEndPoint.Port;
  86. return;
  87. }
  88. AddressString = endpoint.ToString();
  89. }
  90. public override string ToString()
  91. {
  92. if (Resolved != null)
  93. {
  94. switch (Protocol)
  95. {
  96. case TcpProtocol: return Resolved.ToString();
  97. case IpcProtocol: return Resolved.ToString();
  98. case PgmProtocol: return Resolved.ToString();
  99. }
  100. }
  101. if (!string.IsNullOrEmpty(Protocol) && !string.IsNullOrEmpty(AddressString))
  102. {
  103. return Protocol + "://" + AddressString;
  104. }
  105. return base.ToString();
  106. }
  107. public string Protocol { get; }
  108. public string AddressString { get; }
  109. public IZAddress? Resolved { get; set; }
  110. }
  111. }