SocketAddress.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //
  2. // System.Net.SocketAddress.cs
  3. //
  4. // Author:
  5. // Miguel de Icaza ([email protected])
  6. // Dick Porter ([email protected])
  7. //
  8. // (C) Ximian, Inc. http://www.ximian.com
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System.Net.Sockets;
  31. namespace System.Net {
  32. public class SocketAddress {
  33. private byte[] data;
  34. public SocketAddress (AddressFamily family, int size)
  35. {
  36. if(size<2) {
  37. throw new ArgumentOutOfRangeException("size is too small");
  38. }
  39. data=new byte[size];
  40. data[0]=(byte)family;
  41. data[1]=(byte) ((int) family >> 8);
  42. }
  43. public SocketAddress (AddressFamily family)
  44. : this (family, 32)
  45. {
  46. }
  47. //LAMESPEC: the MS doc about this class is wrong. The size is not stored in byte 1. Instead
  48. // byte [0] and byte [1] hold the family (little endian).
  49. public AddressFamily Family {
  50. get {
  51. return (AddressFamily) (data [0] + (data [1] << 8));
  52. }
  53. }
  54. public int Size {
  55. get {
  56. return data.Length;
  57. }
  58. }
  59. public byte this [ int offset ] {
  60. get {
  61. return(data[offset]);
  62. }
  63. set {
  64. data[offset]=value;
  65. }
  66. }
  67. public override string ToString() {
  68. string af=((AddressFamily)data[0]).ToString();
  69. int size = data.Length;
  70. string ret=af+":"+size+":{";
  71. for(int i=2; i<size; i++) {
  72. int val=(int)data[i];
  73. ret=ret+val;
  74. if(i<size-1) {
  75. ret=ret+",";
  76. }
  77. }
  78. ret=ret+"}";
  79. return(ret);
  80. }
  81. public override bool Equals (object obj)
  82. {
  83. if (obj is System.Net.SocketAddress &&
  84. ((System.Net.SocketAddress) obj).data.Length == data.Length){
  85. byte [] otherData = ((System.Net.SocketAddress) obj).data;
  86. for (int i = 0; i < data.Length; i++)
  87. if (otherData [i] != data [i])
  88. return false;
  89. return true;
  90. }
  91. return false;
  92. }
  93. public override int GetHashCode ()
  94. {
  95. int code = 0;
  96. for (int i = 0; i < data.Length; i++)
  97. code += data [i] + i;
  98. return code;
  99. }
  100. }
  101. }