Address.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 Address(string hostName, ushort port) : this()
  28. {
  29. this.HostName = hostName;
  30. this.Port = port;
  31. }
  32. public override bool Equals(object obj)
  33. {
  34. return obj is Address && this.Equals((Address) obj);
  35. }
  36. public bool Equals(Address addr)
  37. {
  38. return this.Port == addr.Port && this.HostIP == addr.HostIP;
  39. }
  40. public override int GetHashCode()
  41. {
  42. return this.Port.GetHashCode() ^ this.HostIP.GetHashCode();
  43. }
  44. public string HostIP
  45. {
  46. get
  47. {
  48. var ip = new byte[256];
  49. fixed (byte* hostIP = ip)
  50. {
  51. if (Native.enet_address_get_host_ip(ref this.address, hostIP, (IntPtr) ip.Length) < 0)
  52. {
  53. return null;
  54. }
  55. }
  56. return Encoding.ASCII.GetString(ip, 0, Native.strlen(ip));
  57. }
  58. }
  59. public string HostName
  60. {
  61. get
  62. {
  63. var name = new byte[256];
  64. fixed (byte* hostName = name)
  65. {
  66. if (Native.enet_address_get_host(ref this.address, hostName, (IntPtr)name.Length) < 0)
  67. {
  68. return null;
  69. }
  70. }
  71. return Encoding.ASCII.GetString(name, 0, Native.strlen(name));
  72. }
  73. set
  74. {
  75. Native.enet_address_set_host(ref this.address, Encoding.ASCII.GetBytes(value));
  76. }
  77. }
  78. public Native.ENetAddress NativeData
  79. {
  80. get
  81. {
  82. return this.address;
  83. }
  84. }
  85. public ushort Port
  86. {
  87. get
  88. {
  89. return this.address.port;
  90. }
  91. set
  92. {
  93. this.address.port = value;
  94. }
  95. }
  96. }
  97. }