2
0

SocketEx.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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,
  44. SocketType socketType,
  45. ProtocolType protocolType)
  46. : base(addressFamily, socketType, protocolType)
  47. {
  48. }
  49. public void Connect(IPEndPoint endPoint, int timeOut)
  50. {
  51. using (var evt = new ManualResetEventSlim(false))
  52. {
  53. using (var args = new SocketAsyncEventArgs
  54. {
  55. RemoteEndPoint = endPoint
  56. })
  57. {
  58. args.Completed += delegate
  59. {
  60. evt.Set();
  61. };
  62. ConnectAsync(args);
  63. if (!evt.Wait(timeOut))
  64. {
  65. CancelConnectAsync(args);
  66. throw new ConnectException("Can't connect to end point.");
  67. }
  68. if (args.SocketError != SocketError.Success)
  69. {
  70. throw new ConnectException("Can't connect to end point.");
  71. }
  72. }
  73. }
  74. }
  75. public void Bind2(EndPoint ep)
  76. {
  77. if (ep == null)
  78. Bind(new IPEndPoint(IPAddress.Any, 0));
  79. else
  80. Bind(ep);
  81. }
  82. public int Receive(byte[] buffer, int offset, int count)
  83. {
  84. using (var evt = new ManualResetEventSlim(false))
  85. {
  86. using (var args = new SocketAsyncEventArgs
  87. {
  88. UserToken = this
  89. })
  90. {
  91. args.SetBuffer(buffer, offset, count);
  92. args.Completed += delegate
  93. {
  94. evt.Set();
  95. };
  96. if (ReceiveAsync(args))
  97. {
  98. if (!evt.Wait(_soTimeOut))
  99. {
  100. throw new TimeoutException("No data received.");
  101. }
  102. }
  103. return args.BytesTransferred;
  104. }
  105. }
  106. }
  107. public void Send(byte[] buffer, int offset, int length, EndPoint destination = null)
  108. {
  109. using (var evt = new ManualResetEventSlim(false))
  110. {
  111. using (SocketAsyncEventArgs args = new SocketAsyncEventArgs
  112. {
  113. UserToken = this
  114. })
  115. {
  116. args.SetBuffer(buffer, offset, length);
  117. args.Completed += delegate
  118. {
  119. evt.Set();
  120. };
  121. args.RemoteEndPoint = destination ?? RemoteEndPoint;
  122. SendToAsync(args);
  123. if (!evt.Wait(_soTimeOut))
  124. {
  125. throw new TimeoutException("No data sent.");
  126. }
  127. }
  128. }
  129. }
  130. public InputStream GetInputStream()
  131. {
  132. return new NetworkStream(this);
  133. }
  134. public OutputStream GetOutputStream()
  135. {
  136. return new NetworkStream(this);
  137. }
  138. }
  139. }