NetworkStream.cs 6.4 KB

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