2 // System.Net.Sockets.NetworkStream.cs
5 // Miguel de Icaza (miguel@ximian.com)
7 // (C) 2002 Ximian, Inc. http://www.ximian.com
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Runtime.InteropServices;
34 namespace System.Net.Sockets
36 public class NetworkStream : Stream, IDisposable {
40 bool readable, writeable;
41 bool disposed = false;
43 public NetworkStream (Socket socket)
44 : this (socket, FileAccess.ReadWrite, false)
48 public NetworkStream (Socket socket, bool owns_socket)
49 : this (socket, FileAccess.ReadWrite, owns_socket)
53 public NetworkStream (Socket socket, FileAccess access)
54 : this (socket, access, false)
58 public NetworkStream (Socket socket, FileAccess access, bool owns_socket)
61 throw new ArgumentNullException ("socket is null");
62 if (!socket.Connected)
63 throw new ArgumentException ("Not connected", "socket");
64 if (socket.SocketType != SocketType.Stream)
65 throw new ArgumentException ("Socket is not of type Stream", "socket");
67 throw new IOException ("Operation not allowed on a non-blocking socket.");
70 this.owns_socket = owns_socket;
77 public override bool CanRead {
79 return access == FileAccess.ReadWrite || access == FileAccess.Read;
83 public override bool CanSeek {
85 // network sockets cant seek.
90 public override bool CanWrite {
92 return access == FileAccess.ReadWrite || access == FileAccess.Write;
96 public virtual bool DataAvailable {
99 return socket.Available > 0;
103 public override long Length {
105 // Network sockets always throw an exception
106 throw new NotSupportedException ();
110 public override long Position {
112 // Network sockets always throw an exception
113 throw new NotSupportedException ();
117 // Network sockets always throw an exception
118 throw new NotSupportedException ();
122 protected bool Readable {
132 protected Socket Socket {
138 protected bool Writeable {
148 public override IAsyncResult BeginRead (byte [] buffer, int offset, int size,
149 AsyncCallback callback, object state)
155 throw new ArgumentNullException ("buffer is null");
156 int len = buffer.Length;
157 if(offset<0 || offset>len) {
158 throw new ArgumentOutOfRangeException("offset exceeds the size of buffer");
160 if(size<0 || offset+size>len) {
161 throw new ArgumentOutOfRangeException("offset+size exceeds the size of buffer");
165 retval = socket.BeginReceive (buffer, offset, size, 0, callback, state);
166 } catch (Exception e) {
167 throw new IOException ("BeginReceive failure", e);
173 public override IAsyncResult BeginWrite (byte [] buffer, int offset, int size,
174 AsyncCallback callback, object state)
180 throw new ArgumentNullException ("buffer is null");
182 int len = buffer.Length;
183 if(offset<0 || offset>len) {
184 throw new ArgumentOutOfRangeException("offset exceeds the size of buffer");
186 if(size<0 || offset+size>len) {
187 throw new ArgumentOutOfRangeException("offset+size exceeds the size of buffer");
191 retval = socket.BeginSend (buffer, offset, size, 0, callback, state);
193 throw new IOException ("BeginWrite failure");
204 public override void Close ()
206 ((IDisposable) this).Dispose ();
215 void Dispose (bool disposing)
229 public override int EndRead (IAsyncResult ar)
235 throw new ArgumentNullException ("async result is null");
238 res = socket.EndReceive (ar);
239 } catch (Exception e) {
240 throw new IOException ("EndRead failure", e);
245 public override void EndWrite (IAsyncResult ar)
249 throw new ArgumentNullException ("async result is null");
253 } catch (Exception e) {
254 throw new IOException ("EndWrite failure", e);
258 public override void Flush ()
260 // network streams are non-buffered, this is a no-op
263 void IDisposable.Dispose ()
266 GC.SuppressFinalize (this);
269 public override int Read ([In,Out] byte [] buffer, int offset, int size)
275 throw new ArgumentNullException ("buffer is null");
276 if(offset<0 || offset>buffer.Length) {
277 throw new ArgumentOutOfRangeException("offset exceeds the size of buffer");
279 if(size < 0 || offset+size>buffer.Length) {
280 throw new ArgumentOutOfRangeException("offset+size exceeds the size of buffer");
284 res = socket.Receive (buffer, offset, size, 0);
285 } catch (Exception e) {
286 throw new IOException ("Read failure", e);
292 public override long Seek (long offset, SeekOrigin origin)
294 // NetworkStream objects do not support seeking.
296 throw new NotSupportedException ();
299 public override void SetLength (long value)
301 // NetworkStream objects do not support SetLength
303 throw new NotSupportedException ();
306 public override void Write (byte [] buffer, int offset, int size)
310 throw new ArgumentNullException ("buffer");
312 if (offset < 0 || offset > buffer.Length)
313 throw new ArgumentOutOfRangeException("offset exceeds the size of buffer");
315 if (size < 0 || size > buffer.Length - offset)
316 throw new ArgumentOutOfRangeException("offset+size exceeds the size of buffer");
320 while (size - count > 0) {
321 count += socket.Send (buffer, offset + count, size - count, 0);
323 } catch (Exception e) {
324 throw new IOException ("Write failure", e);
328 private void CheckDisposed ()
331 throw new ObjectDisposedException (GetType().FullName);
335 public void ChangeToSSLSocket()
337 socket.ChangeToSSL();