SocketEx.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // SocketEx.cs implementation by J. Arturo <webmaster at komodosoft dot net>
  2. //
  3. // This library is free software; you can redistribute it and/or
  4. // modify it under the terms of the GNU Lesser General Public
  5. // License as published by the Free Software Foundation; either
  6. // version 2.1 of the License, or (at your option) any later version.
  7. //
  8. // This library is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. // Lesser General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU Lesser General Public
  14. // License along with this library; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. using System;
  17. using System.Net;
  18. using System.Net.Sockets;
  19. using System.Threading;
  20. namespace SharpCifs.Util.Sharpen
  21. {
  22. public class SocketEx : Socket
  23. {
  24. private int _soTimeOut = -1;
  25. public int SoTimeOut
  26. {
  27. get
  28. {
  29. return _soTimeOut;
  30. }
  31. set
  32. {
  33. if (value > 0)
  34. {
  35. _soTimeOut = value;
  36. }
  37. else
  38. {
  39. _soTimeOut = -1;
  40. }
  41. }
  42. }
  43. public SocketEx(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType)
  44. : base(addressFamily, socketType, protocolType)
  45. {
  46. }
  47. public void Connect(IPEndPoint endPoint, int timeOut)
  48. {
  49. using (var evt = new ManualResetEventSlim(false))
  50. {
  51. using (var args = new SocketAsyncEventArgs
  52. {
  53. RemoteEndPoint = endPoint
  54. })
  55. {
  56. args.Completed += delegate
  57. {
  58. evt.Set();
  59. };
  60. ConnectAsync(args);
  61. if (!evt.Wait(timeOut))
  62. {
  63. CancelConnectAsync(args);
  64. throw new ConnectException("Can't connect to end point.");
  65. }
  66. if (args.SocketError != SocketError.Success)
  67. {
  68. throw new ConnectException("Can't connect to end point.");
  69. }
  70. }
  71. }
  72. }
  73. public void Bind2(EndPoint ep)
  74. {
  75. if (ep == null)
  76. Bind(new IPEndPoint(IPAddress.Any, 0));
  77. else
  78. Bind(ep);
  79. }
  80. public int Receive(byte[] buffer, int offset, int count)
  81. {
  82. using (var evt = new ManualResetEventSlim(false))
  83. {
  84. using (var args = new SocketAsyncEventArgs
  85. {
  86. UserToken = this
  87. })
  88. {
  89. args.SetBuffer(buffer, offset, count);
  90. args.Completed += delegate
  91. {
  92. evt.Set();
  93. };
  94. if (ReceiveAsync(args))
  95. {
  96. if (!evt.Wait(_soTimeOut))
  97. {
  98. throw new TimeoutException("No data received.");
  99. }
  100. }
  101. return args.BytesTransferred;
  102. }
  103. }
  104. }
  105. public void Send(byte[] buffer, int offset, int length, EndPoint destination = null)
  106. {
  107. using (var evt = new ManualResetEventSlim(false))
  108. {
  109. using (SocketAsyncEventArgs args = new SocketAsyncEventArgs
  110. {
  111. UserToken = this
  112. })
  113. {
  114. args.SetBuffer(buffer, offset, length);
  115. args.Completed += delegate
  116. {
  117. evt.Set();
  118. };
  119. args.RemoteEndPoint = destination ?? RemoteEndPoint;
  120. SendToAsync(args);
  121. if (!evt.Wait(_soTimeOut))
  122. {
  123. throw new TimeoutException("No data sent.");
  124. }
  125. }
  126. }
  127. }
  128. public InputStream GetInputStream()
  129. {
  130. return new NetworkStream(this);
  131. }
  132. public OutputStream GetOutputStream()
  133. {
  134. return new NetworkStream(this);
  135. }
  136. }
  137. }