NetworkStream.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. try {
  65. return socket.Available > 0;
  66. } finally {
  67. CheckDisposed ();
  68. }
  69. }
  70. }
  71. public override long Length {
  72. get {
  73. // Network sockets always throw an exception
  74. throw new NotSupportedException ();
  75. }
  76. }
  77. public override long Position {
  78. get {
  79. // Network sockets always throw an exception
  80. throw new NotSupportedException ();
  81. }
  82. set {
  83. // Network sockets always throw an exception
  84. throw new NotSupportedException ();
  85. }
  86. }
  87. protected bool Readable {
  88. get {
  89. return readable;
  90. }
  91. set {
  92. readable = value;
  93. }
  94. }
  95. protected Socket Socket {
  96. get {
  97. return socket;
  98. }
  99. }
  100. protected bool Writeable {
  101. get {
  102. return writeable;
  103. }
  104. set {
  105. writeable = value;
  106. }
  107. }
  108. public override IAsyncResult BeginRead (byte [] buffer, int offset, int size,
  109. AsyncCallback callback, object state)
  110. {
  111. try {
  112. IAsyncResult retval;
  113. if (buffer == null)
  114. throw new ArgumentNullException ("buffer is null");
  115. int len = buffer.Length;
  116. if(offset<0 || offset>=len) {
  117. throw new ArgumentOutOfRangeException("offset exceeds the size of buffer");
  118. }
  119. if(offset+size<0 || offset+size>len) {
  120. throw new ArgumentOutOfRangeException("offset+size exceeds the size of buffer");
  121. }
  122. try {
  123. retval = socket.BeginReceive (buffer, offset, size, 0, callback, state);
  124. } catch {
  125. throw new IOException ("BeginReceive failure");
  126. }
  127. return retval;
  128. } finally {
  129. CheckDisposed ();
  130. }
  131. }
  132. public override IAsyncResult BeginWrite (byte [] buffer, int offset, int size,
  133. AsyncCallback callback, object state)
  134. {
  135. try {
  136. IAsyncResult retval;
  137. if (buffer == null)
  138. throw new ArgumentNullException ("buffer is null");
  139. int len = buffer.Length;
  140. if(offset<0 || offset>=len) {
  141. throw new ArgumentOutOfRangeException("offset exceeds the size of buffer");
  142. }
  143. if(offset+size<0 || offset+size>len) {
  144. throw new ArgumentOutOfRangeException("offset+size exceeds the size of buffer");
  145. }
  146. try {
  147. retval = socket.BeginSend (buffer, offset, size, 0, callback, state);
  148. } catch {
  149. throw new IOException ("BeginWrite failure");
  150. }
  151. return retval;
  152. } finally {
  153. CheckDisposed ();
  154. }
  155. }
  156. ~NetworkStream ()
  157. {
  158. Dispose (false);
  159. }
  160. public override void Close ()
  161. {
  162. ((IDisposable) this).Dispose ();
  163. }
  164. protected virtual void Dispose (bool disposing)
  165. {
  166. if (disposed)
  167. return;
  168. disposed = true;
  169. if (owns_socket) {
  170. Socket s = socket;
  171. if (s != null)
  172. s.Close ();
  173. }
  174. socket = null;
  175. }
  176. public override int EndRead (IAsyncResult ar)
  177. {
  178. try {
  179. int res;
  180. if (ar == null)
  181. throw new ArgumentNullException ("async result is null");
  182. try {
  183. res = socket.EndReceive (ar);
  184. } catch {
  185. throw new IOException ("EndRead failure");
  186. }
  187. return res;
  188. } finally {
  189. CheckDisposed ();
  190. }
  191. }
  192. public override void EndWrite (IAsyncResult ar)
  193. {
  194. try {
  195. if (ar == null)
  196. throw new ArgumentNullException ("async result is null");
  197. try {
  198. socket.EndSend (ar);
  199. } catch {
  200. throw new IOException ("EndWrite failure");
  201. }
  202. } finally {
  203. CheckDisposed ();
  204. }
  205. }
  206. public override void Flush ()
  207. {
  208. // network streams are non-buffered, this is a no-op
  209. }
  210. void IDisposable.Dispose ()
  211. {
  212. Dispose (true);
  213. GC.SuppressFinalize (this);
  214. }
  215. public override int Read (byte [] buffer, int offset, int size)
  216. {
  217. try {
  218. int res;
  219. if (buffer == null)
  220. throw new ArgumentNullException ("buffer is null");
  221. if(offset<0 || offset>=buffer.Length) {
  222. throw new ArgumentOutOfRangeException("offset exceeds the size of buffer");
  223. }
  224. if(offset+size < 0 || offset+size>buffer.Length) {
  225. throw new ArgumentOutOfRangeException("offset+size exceeds the size of buffer");
  226. }
  227. try {
  228. res = socket.Receive (buffer, offset, size, 0);
  229. } catch {
  230. throw new IOException ("Read failure");
  231. }
  232. return res;
  233. } finally {
  234. CheckDisposed ();
  235. }
  236. }
  237. public override long Seek (long offset, SeekOrigin origin)
  238. {
  239. // NetworkStream objects do not support seeking.
  240. throw new NotSupportedException ();
  241. }
  242. public override void SetLength (long value)
  243. {
  244. // NetworkStream objects do not support SetLength
  245. throw new NotSupportedException ();
  246. }
  247. public override void Write (byte [] buffer, int offset, int size)
  248. {
  249. try {
  250. if (buffer == null)
  251. throw new ArgumentNullException ("buffer is null");
  252. if(offset<0 || offset>=buffer.Length) {
  253. throw new ArgumentOutOfRangeException("offset exceeds the size of buffer");
  254. }
  255. if(offset+size<0 || offset+size>buffer.Length) {
  256. throw new ArgumentOutOfRangeException("offset+size exceeds the size of buffer");
  257. }
  258. try {
  259. socket.Send (buffer, offset, size, 0);
  260. } catch {
  261. throw new IOException ("Write failure");
  262. }
  263. } finally {
  264. CheckDisposed ();
  265. }
  266. }
  267. private void CheckDisposed ()
  268. {
  269. if (disposed)
  270. throw new ObjectDisposedException (GetType().FullName);
  271. }
  272. }
  273. }