2 // System.Net.Sockets.NetworkStream.cs
5 // Miguel de Icaza (miguel@ximian.com)
7 // (C) 2002 Ximian, Inc. http://www.ximian.com
11 using System.Runtime.InteropServices;
13 namespace System.Net.Sockets
15 public class NetworkStream : Stream, IDisposable {
19 bool readable, writeable;
20 bool disposed = false;
22 public NetworkStream (Socket socket)
23 : this (socket, FileAccess.ReadWrite, false)
27 public NetworkStream (Socket socket, bool owns_socket)
28 : this (socket, FileAccess.ReadWrite, owns_socket)
32 public NetworkStream (Socket socket, FileAccess access)
33 : this (socket, access, false)
37 public NetworkStream (Socket socket, FileAccess access, bool owns_socket)
40 throw new ArgumentNullException ("socket is null");
41 if (!socket.Connected)
42 throw new ArgumentException ("Not connected", "socket");
43 if (socket.SocketType != SocketType.Stream)
44 throw new ArgumentException ("Socket is not of type Stream", "socket");
46 throw new IOException ();
49 this.owns_socket = owns_socket;
56 public override bool CanRead {
58 return access == FileAccess.ReadWrite || access == FileAccess.Read;
62 public override bool CanSeek {
64 // network sockets cant seek.
69 public override bool CanWrite {
71 return access == FileAccess.ReadWrite || access == FileAccess.Write;
75 public virtual bool DataAvailable {
78 return socket.Available > 0;
82 public override long Length {
84 // Network sockets always throw an exception
85 throw new NotSupportedException ();
89 public override long Position {
91 // Network sockets always throw an exception
92 throw new NotSupportedException ();
96 // Network sockets always throw an exception
97 throw new NotSupportedException ();
101 protected bool Readable {
111 protected Socket Socket {
117 protected bool Writeable {
127 public override IAsyncResult BeginRead (byte [] buffer, int offset, int size,
128 AsyncCallback callback, object state)
134 throw new ArgumentNullException ("buffer is null");
135 int len = buffer.Length;
136 if(offset<0 || offset>len) {
137 throw new ArgumentOutOfRangeException("offset exceeds the size of buffer");
139 if(size<0 || offset+size>len) {
140 throw new ArgumentOutOfRangeException("offset+size exceeds the size of buffer");
144 retval = socket.BeginReceive (buffer, offset, size, 0, callback, state);
146 throw new IOException ("BeginReceive failure");
152 public override IAsyncResult BeginWrite (byte [] buffer, int offset, int size,
153 AsyncCallback callback, object state)
159 throw new ArgumentNullException ("buffer is null");
161 int len = buffer.Length;
162 if(offset<0 || offset>len) {
163 throw new ArgumentOutOfRangeException("offset exceeds the size of buffer");
165 if(size<0 || offset+size>len) {
166 throw new ArgumentOutOfRangeException("offset+size exceeds the size of buffer");
170 retval = socket.BeginSend (buffer, offset, size, 0, callback, state);
172 throw new IOException ("BeginWrite failure");
183 public override void Close ()
185 ((IDisposable) this).Dispose ();
188 protected virtual void Dispose (bool disposing)
202 public override int EndRead (IAsyncResult ar)
208 throw new ArgumentNullException ("async result is null");
211 res = socket.EndReceive (ar);
212 } catch (Exception e) {
213 throw new IOException ("EndRead failure", e);
218 public override void EndWrite (IAsyncResult ar)
222 throw new ArgumentNullException ("async result is null");
226 } catch (Exception e) {
227 throw new IOException ("EndWrite failure", e);
231 public override void Flush ()
233 // network streams are non-buffered, this is a no-op
236 void IDisposable.Dispose ()
239 GC.SuppressFinalize (this);
242 public override int Read ([In,Out] byte [] buffer, int offset, int size)
248 throw new ArgumentNullException ("buffer is null");
249 if(offset<0 || offset>buffer.Length) {
250 throw new ArgumentOutOfRangeException("offset exceeds the size of buffer");
252 if(size < 0 || offset+size>buffer.Length) {
253 throw new ArgumentOutOfRangeException("offset+size exceeds the size of buffer");
257 res = socket.Receive (buffer, offset, size, 0);
258 } catch (Exception e) {
259 throw new IOException ("Read failure", e);
265 public override long Seek (long offset, SeekOrigin origin)
267 // NetworkStream objects do not support seeking.
269 throw new NotSupportedException ();
272 public override void SetLength (long value)
274 // NetworkStream objects do not support SetLength
276 throw new NotSupportedException ();
279 public override void Write (byte [] buffer, int offset, int size)
283 throw new ArgumentNullException ("buffer");
285 if (offset < 0 || offset > buffer.Length)
286 throw new ArgumentOutOfRangeException("offset exceeds the size of buffer");
288 if (size < 0 || size > buffer.Length - offset)
289 throw new ArgumentOutOfRangeException("offset+size exceeds the size of buffer");
292 socket.Send (buffer, offset, size, 0);
293 } catch (Exception e) {
294 throw new IOException ("Write failure", e);
298 private void CheckDisposed ()
301 throw new ObjectDisposedException (GetType().FullName);