NetworkStream.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System.IO;
  30. using System.Runtime.InteropServices;
  31. namespace System.Net.Sockets
  32. {
  33. public class NetworkStream : Stream, IDisposable {
  34. FileAccess access;
  35. Socket socket;
  36. bool owns_socket;
  37. bool readable, writeable;
  38. bool disposed = false;
  39. public NetworkStream (Socket socket)
  40. : this (socket, FileAccess.ReadWrite, false)
  41. {
  42. }
  43. public NetworkStream (Socket socket, bool owns_socket)
  44. : this (socket, FileAccess.ReadWrite, owns_socket)
  45. {
  46. }
  47. public NetworkStream (Socket socket, FileAccess access)
  48. : this (socket, access, false)
  49. {
  50. }
  51. public NetworkStream (Socket socket, FileAccess access, bool owns_socket)
  52. {
  53. if (socket == null)
  54. throw new ArgumentNullException ("socket is null");
  55. if (!socket.Connected)
  56. throw new ArgumentException ("Not connected", "socket");
  57. if (socket.SocketType != SocketType.Stream)
  58. throw new ArgumentException ("Socket is not of type Stream", "socket");
  59. if (!socket.Blocking)
  60. throw new IOException ("Operation not allowed on a non-blocking socket.");
  61. this.socket = socket;
  62. this.owns_socket = owns_socket;
  63. this.access = access;
  64. readable = CanRead;
  65. writeable = CanWrite;
  66. }
  67. public override bool CanRead {
  68. get {
  69. return access == FileAccess.ReadWrite || access == FileAccess.Read;
  70. }
  71. }
  72. public override bool CanSeek {
  73. get {
  74. // network sockets cant seek.
  75. return false;
  76. }
  77. }
  78. public override bool CanWrite {
  79. get {
  80. return access == FileAccess.ReadWrite || access == FileAccess.Write;
  81. }
  82. }
  83. public virtual bool DataAvailable {
  84. get {
  85. CheckDisposed ();
  86. return socket.Available > 0;
  87. }
  88. }
  89. public override long Length {
  90. get {
  91. // Network sockets always throw an exception
  92. throw new NotSupportedException ();
  93. }
  94. }
  95. public override long Position {
  96. get {
  97. // Network sockets always throw an exception
  98. throw new NotSupportedException ();
  99. }
  100. set {
  101. // Network sockets always throw an exception
  102. throw new NotSupportedException ();
  103. }
  104. }
  105. protected bool Readable {
  106. get {
  107. return readable;
  108. }
  109. set {
  110. readable = value;
  111. }
  112. }
  113. protected Socket Socket {
  114. get {
  115. return socket;
  116. }
  117. }
  118. protected bool Writeable {
  119. get {
  120. return writeable;
  121. }
  122. set {
  123. writeable = value;
  124. }
  125. }
  126. public override IAsyncResult BeginRead (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.BeginReceive (buffer, offset, size, 0, callback, state);
  142. } catch (Exception e) {
  143. throw new IOException ("BeginReceive failure", e);
  144. }
  145. return retval;
  146. }
  147. public override IAsyncResult BeginWrite (byte [] buffer, int offset, int size,
  148. AsyncCallback callback, object state)
  149. {
  150. CheckDisposed ();
  151. IAsyncResult retval;
  152. if (buffer == null)
  153. throw new ArgumentNullException ("buffer is null");
  154. int len = buffer.Length;
  155. if(offset<0 || offset>len) {
  156. throw new ArgumentOutOfRangeException("offset exceeds the size of buffer");
  157. }
  158. if(size<0 || offset+size>len) {
  159. throw new ArgumentOutOfRangeException("offset+size exceeds the size of buffer");
  160. }
  161. try {
  162. retval = socket.BeginSend (buffer, offset, size, 0, callback, state);
  163. } catch {
  164. throw new IOException ("BeginWrite failure");
  165. }
  166. return retval;
  167. }
  168. ~NetworkStream ()
  169. {
  170. Dispose (false);
  171. }
  172. public override void Close ()
  173. {
  174. ((IDisposable) this).Dispose ();
  175. }
  176. protected
  177. #if NET_2_0
  178. override
  179. #endif
  180. void Dispose (bool disposing)
  181. {
  182. if (disposed)
  183. return;
  184. disposed = true;
  185. if (owns_socket) {
  186. Socket s = socket;
  187. if (s != null)
  188. s.Close ();
  189. }
  190. socket = null;
  191. }
  192. public override int EndRead (IAsyncResult ar)
  193. {
  194. CheckDisposed ();
  195. int res;
  196. if (ar == null)
  197. throw new ArgumentNullException ("async result is null");
  198. try {
  199. res = socket.EndReceive (ar);
  200. } catch (Exception e) {
  201. throw new IOException ("EndRead failure", e);
  202. }
  203. return res;
  204. }
  205. public override void EndWrite (IAsyncResult ar)
  206. {
  207. CheckDisposed ();
  208. if (ar == null)
  209. throw new ArgumentNullException ("async result is null");
  210. try {
  211. socket.EndSend (ar);
  212. } catch (Exception e) {
  213. throw new IOException ("EndWrite failure", e);
  214. }
  215. }
  216. public override void Flush ()
  217. {
  218. // network streams are non-buffered, this is a no-op
  219. }
  220. void IDisposable.Dispose ()
  221. {
  222. Dispose (true);
  223. GC.SuppressFinalize (this);
  224. }
  225. public override int Read ([In,Out] byte [] buffer, int offset, int size)
  226. {
  227. CheckDisposed ();
  228. int res;
  229. if (buffer == null)
  230. throw new ArgumentNullException ("buffer is null");
  231. if(offset<0 || offset>buffer.Length) {
  232. throw new ArgumentOutOfRangeException("offset exceeds the size of buffer");
  233. }
  234. if(size < 0 || offset+size>buffer.Length) {
  235. throw new ArgumentOutOfRangeException("offset+size exceeds the size of buffer");
  236. }
  237. try {
  238. res = socket.Receive (buffer, offset, size, 0);
  239. } catch (Exception e) {
  240. throw new IOException ("Read failure", e);
  241. }
  242. return res;
  243. }
  244. public override long Seek (long offset, SeekOrigin origin)
  245. {
  246. // NetworkStream objects do not support seeking.
  247. throw new NotSupportedException ();
  248. }
  249. public override void SetLength (long value)
  250. {
  251. // NetworkStream objects do not support SetLength
  252. throw new NotSupportedException ();
  253. }
  254. public override void Write (byte [] buffer, int offset, int size)
  255. {
  256. CheckDisposed ();
  257. if (buffer == null)
  258. throw new ArgumentNullException ("buffer");
  259. if (offset < 0 || offset > buffer.Length)
  260. throw new ArgumentOutOfRangeException("offset exceeds the size of buffer");
  261. if (size < 0 || size > buffer.Length - offset)
  262. throw new ArgumentOutOfRangeException("offset+size exceeds the size of buffer");
  263. try {
  264. int count = 0;
  265. while (size - count > 0) {
  266. count += socket.Send (buffer, offset + count, size - count, 0);
  267. }
  268. } catch (Exception e) {
  269. throw new IOException ("Write failure", e);
  270. }
  271. }
  272. private void CheckDisposed ()
  273. {
  274. if (disposed)
  275. throw new ObjectDisposedException (GetType().FullName);
  276. }
  277. }
  278. }