Address.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #region License
  2. /*
  3. ENet for C#
  4. Copyright (c) 2011 James F. Bellinger <jfb@zer7.com>
  5. Permission to use, copy, modify, and/or distribute this software for any
  6. purpose with or without fee is hereby granted, provided that the above
  7. copyright notice and this permission notice appear in all copies.
  8. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #endregion
  17. using System;
  18. using System.Net;
  19. using System.Text;
  20. namespace ENet
  21. {
  22. public unsafe struct Address : IEquatable<Address>
  23. {
  24. public const uint IPv4HostAny = Native.ENET_HOST_ANY;
  25. public const uint IPv4HostBroadcast = Native.ENET_HOST_BROADCAST;
  26. private Native.ENetAddress address;
  27. public override bool Equals(object obj)
  28. {
  29. return obj is Address && this.Equals((Address) obj);
  30. }
  31. public bool Equals(Address addr)
  32. {
  33. return this.Port == addr.Port && Native.memcmp(this.GetHostBytes(), addr.GetHostBytes());
  34. }
  35. public override int GetHashCode()
  36. {
  37. return Type.GetHashCode() ^ this.Port.GetHashCode() ^ this.IPv4Host.GetHashCode();
  38. }
  39. public byte[] GetHostBytes()
  40. {
  41. return BitConverter.GetBytes(IPAddress.NetworkToHostOrder((int) this.IPv4Host));
  42. }
  43. public string GetHostName()
  44. {
  45. var name = new byte[256];
  46. fixed (byte* hostName = name)
  47. {
  48. if (Native.enet_address_get_host(ref this.address, hostName, (IntPtr)name.Length) < 0)
  49. {
  50. return null;
  51. }
  52. }
  53. return BytesToString(name);
  54. }
  55. public string GetHostIP()
  56. {
  57. var ip = new byte[256];
  58. fixed (byte* hostIP = ip)
  59. {
  60. if (Native.enet_address_get_host_ip(ref this.address, hostIP, (IntPtr) ip.Length) < 0)
  61. {
  62. return null;
  63. }
  64. }
  65. return BytesToString(ip);
  66. }
  67. public bool SetHost(string hostName)
  68. {
  69. if (hostName == null)
  70. {
  71. throw new ArgumentNullException("hostName");
  72. }
  73. return Native.enet_address_set_host(ref this.address, Encoding.ASCII.GetBytes(hostName)) == 0;
  74. }
  75. private static string BytesToString(byte[] bytes)
  76. {
  77. try
  78. {
  79. return Encoding.ASCII.GetString(bytes, 0, Native.strlen(bytes));
  80. }
  81. catch
  82. {
  83. return null;
  84. }
  85. }
  86. public Native.ENetAddress NativeData
  87. {
  88. get
  89. {
  90. return this.address;
  91. }
  92. set
  93. {
  94. this.address = value;
  95. }
  96. }
  97. public uint IPv4Host
  98. {
  99. get
  100. {
  101. return this.address.host;
  102. }
  103. set
  104. {
  105. this.address.host = value;
  106. }
  107. }
  108. public ushort Port
  109. {
  110. get
  111. {
  112. return this.address.port;
  113. }
  114. set
  115. {
  116. this.address.port = value;
  117. }
  118. }
  119. public static AddressType Type
  120. {
  121. get
  122. {
  123. return AddressType.IPv4;
  124. }
  125. }
  126. }
  127. }