NetworkStream.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. //
  2. // System.Net.Sockets.NetworkStream.cs
  3. //
  4. // Author:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. // (C) 2002 Ximian, Inc. http://www.ximian.com
  8. //
  9. using System.IO;
  10. using System.Runtime.InteropServices;
  11. namespace System.Net.Sockets
  12. {
  13. public class NetworkStream : Stream, IDisposable {
  14. FileAccess access;
  15. Socket socket;
  16. bool owns_socket;
  17. bool readable, writeable;
  18. bool disposed = false;
  19. public NetworkStream (Socket socket)
  20. : this (socket, FileAccess.ReadWrite, false)
  21. {
  22. }
  23. public NetworkStream (Socket socket, bool owns_socket)
  24. : this (socket, FileAccess.ReadWrite, owns_socket)
  25. {
  26. }
  27. public NetworkStream (Socket socket, FileAccess access)
  28. : this (socket, access, false)
  29. {
  30. }
  31. public NetworkStream (Socket socket, FileAccess access, bool owns_socket)
  32. {
  33. if (socket == null)
  34. throw new ArgumentNullException ("socket is null");
  35. if (!socket.Connected)
  36. throw new ArgumentException ("Not connected", "socket");
  37. if (socket.SocketType != SocketType.Stream)
  38. throw new ArgumentException ("Socket is not of type Stream", "socket");
  39. if (!socket.Blocking)
  40. throw new IOException ();
  41. this.socket = socket;
  42. this.owns_socket = owns_socket;
  43. this.access = access;
  44. readable = CanRead;
  45. writeable = CanWrite;
  46. }
  47. public override bool CanRead {
  48. get {
  49. return access == FileAccess.ReadWrite || access == FileAccess.Read;
  50. }
  51. }
  52. public override bool CanSeek {
  53. get {
  54. // network sockets cant seek.
  55. return false;
  56. }
  57. }
  58. public override bool CanWrite {
  59. get {
  60. return access == FileAccess.ReadWrite || access == FileAccess.Write;
  61. }
  62. }
  63. public virtual bool DataAvailable {
  64. get {
  65. CheckDisposed ();
  66. return socket.Available > 0;
  67. }
  68. }
  69. public override long Length {
  70. get {
  71. // Network sockets always throw an exception
  72. throw new NotSupportedException ();
  73. }
  74. }
  75. public override long Position {
  76. get {
  77. // Network sockets always throw an exception
  78. throw new NotSupportedException ();
  79. }
  80. set {
  81. // Network sockets always throw an exception
  82. throw new NotSupportedException ();
  83. }
  84. }
  85. protected bool Readable {
  86. get {
  87. return readable;
  88. }
  89. set {
  90. readable = value;
  91. }
  92. }
  93. protected Socket Socket {
  94. get {
  95. return socket;
  96. }
  97. }
  98. protected bool Writeable {
  99. get {
  100. return writeable;
  101. }
  102. set {
  103. writeable = value;
  104. }
  105. }
  106. public override IAsyncResult BeginRead (byte [] buffer, int offset, int size,
  107. AsyncCallback callback, object state)
  108. {
  109. CheckDisposed ();
  110. IAsyncResult retval;
  111. if (buffer == null)
  112. throw new ArgumentNullException ("buffer is null");
  113. int len = buffer.Length;
  114. if(offset<0 || offset>len) {
  115. throw new ArgumentOutOfRangeException("offset exceeds the size of buffer");
  116. }
  117. if(size<0 || offset+size>len) {
  118. throw new ArgumentOutOfRangeException("offset+size exceeds the size of buffer");
  119. }
  120. try {
  121. retval = socket.BeginReceive (buffer, offset, size, 0, callback, state);
  122. } catch {
  123. throw new IOException ("BeginReceive failure");
  124. }
  125. return retval;
  126. }
  127. public override IAsyncResult BeginWrite (byte [] buffer, int offset, int size,
  128. AsyncCallback callback, object state)
  129. {
  130. CheckDisposed ();
  131. IAsyncResult retval;
  132. if (buffer == null)
  133. throw new ArgumentNullException ("buffer is null");
  134. int len = buffer.Length;
  135. if(offset<0 || offset>len) {
  136. throw new ArgumentOutOfRangeException("offset exceeds the size of buffer");
  137. }
  138. if(size<0 || offset+size>len) {
  139. throw new ArgumentOutOfRangeException("offset+size exceeds the size of buffer");
  140. }
  141. try {
  142. retval = socket.BeginSend (buffer, offset, size, 0, callback, state);
  143. } catch {
  144. throw new IOException ("BeginWrite failure");
  145. }
  146. return retval;
  147. }
  148. ~NetworkStream ()
  149. {
  150. Dispose (false);
  151. }
  152. public override void Close ()
  153. {
  154. ((IDisposable) this).Dispose ();
  155. }
  156. protected virtual void Dispose (bool disposing)
  157. {
  158. if (disposed)
  159. return;
  160. disposed = true;
  161. if (owns_socket) {
  162. Socket s = socket;
  163. if (s != null)
  164. s.Close ();
  165. }
  166. socket = null;
  167. }
  168. public override int EndRead (IAsyncResult ar)
  169. {
  170. CheckDisposed ();
  171. int res;
  172. if (ar == null)
  173. throw new ArgumentNullException ("async result is null");
  174. try {
  175. res = socket.EndReceive (ar);
  176. } catch (Exception e) {
  177. throw new IOException ("EndRead failure", e);
  178. }
  179. return res;
  180. }
  181. public override void EndWrite (IAsyncResult ar)
  182. {
  183. CheckDisposed ();
  184. if (ar == null)
  185. throw new ArgumentNullException ("async result is null");
  186. try {
  187. socket.EndSend (ar);
  188. } catch (Exception e) {
  189. throw new IOException ("EndWrite failure", e);
  190. }
  191. }
  192. public override void Flush ()
  193. {
  194. // network streams are non-buffered, this is a no-op
  195. }
  196. void IDisposable.Dispose ()
  197. {
  198. Dispose (true);
  199. GC.SuppressFinalize (this);
  200. }
  201. public override int Read ([In,Out] byte [] buffer, int offset, int size)
  202. {
  203. CheckDisposed ();
  204. int res;
  205. if (buffer == null)
  206. throw new ArgumentNullException ("buffer is null");
  207. if(offset<0 || offset>buffer.Length) {
  208. throw new ArgumentOutOfRangeException("offset exceeds the size of buffer");
  209. }
  210. if(size < 0 || offset+size>buffer.Length) {
  211. throw new ArgumentOutOfRangeException("offset+size exceeds the size of buffer");
  212. }
  213. try {
  214. res = socket.Receive (buffer, offset, size, 0);
  215. } catch (Exception e) {
  216. throw new IOException ("Read failure", e);
  217. }
  218. return res;
  219. }
  220. public override long Seek (long offset, SeekOrigin origin)
  221. {
  222. // NetworkStream objects do not support seeking.
  223. throw new NotSupportedException ();
  224. }
  225. public override void SetLength (long value)
  226. {
  227. // NetworkStream objects do not support SetLength
  228. throw new NotSupportedException ();
  229. }
  230. public override void Write (byte [] buffer, int offset, int size)
  231. {
  232. CheckDisposed ();
  233. if (buffer == null)
  234. throw new ArgumentNullException ("buffer");
  235. if (offset < 0 || offset > buffer.Length)
  236. throw new ArgumentOutOfRangeException("offset exceeds the size of buffer");
  237. if (size < 0 || size > buffer.Length - offset)
  238. throw new ArgumentOutOfRangeException("offset+size exceeds the size of buffer");
  239. try {
  240. socket.Send (buffer, offset, size, 0);
  241. } catch (Exception e) {
  242. throw new IOException ("Write failure", e);
  243. }
  244. }
  245. private void CheckDisposed ()
  246. {
  247. if (disposed)
  248. throw new ObjectDisposedException (GetType().FullName);
  249. }
  250. }
  251. }