**** Merged from MCS ****
[mono-project.git] / mcs / class / System / System.Net.Sockets / NetworkStream.cs
blob2a965dcdf2f2a347736ba77c71661c32c55b920e
1 //
2 // System.Net.Sockets.NetworkStream.cs
3 //
4 // Author:
5 // Miguel de Icaza (miguel@ximian.com)
6 //
7 // (C) 2002 Ximian, Inc. http://www.ximian.com
8 //
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:
18 //
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 //
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.
31 using System.IO;
32 using System.Runtime.InteropServices;
34 namespace System.Net.Sockets
36 public class NetworkStream : Stream, IDisposable {
37 FileAccess access;
38 Socket socket;
39 bool owns_socket;
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)
60 if (socket == null)
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");
66 if (!socket.Blocking)
67 throw new IOException ();
69 this.socket = socket;
70 this.owns_socket = owns_socket;
71 this.access = access;
73 readable = CanRead;
74 writeable = CanWrite;
77 public override bool CanRead {
78 get {
79 return access == FileAccess.ReadWrite || access == FileAccess.Read;
83 public override bool CanSeek {
84 get {
85 // network sockets cant seek.
86 return false;
90 public override bool CanWrite {
91 get {
92 return access == FileAccess.ReadWrite || access == FileAccess.Write;
96 public virtual bool DataAvailable {
97 get {
98 CheckDisposed ();
99 return socket.Available > 0;
103 public override long Length {
104 get {
105 // Network sockets always throw an exception
106 throw new NotSupportedException ();
110 public override long Position {
111 get {
112 // Network sockets always throw an exception
113 throw new NotSupportedException ();
116 set {
117 // Network sockets always throw an exception
118 throw new NotSupportedException ();
122 protected bool Readable {
123 get {
124 return readable;
127 set {
128 readable = value;
132 protected Socket Socket {
133 get {
134 return socket;
138 protected bool Writeable {
139 get {
140 return writeable;
143 set {
144 writeable = value;
148 public override IAsyncResult BeginRead (byte [] buffer, int offset, int size,
149 AsyncCallback callback, object state)
151 CheckDisposed ();
152 IAsyncResult retval;
154 if (buffer == null)
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");
164 try {
165 retval = socket.BeginReceive (buffer, offset, size, 0, callback, state);
166 } catch {
167 throw new IOException ("BeginReceive failure");
170 return retval;
173 public override IAsyncResult BeginWrite (byte [] buffer, int offset, int size,
174 AsyncCallback callback, object state)
176 CheckDisposed ();
177 IAsyncResult retval;
179 if (buffer == null)
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");
190 try {
191 retval = socket.BeginSend (buffer, offset, size, 0, callback, state);
192 } catch {
193 throw new IOException ("BeginWrite failure");
196 return retval;
199 ~NetworkStream ()
201 Dispose (false);
204 public override void Close ()
206 ((IDisposable) this).Dispose ();
209 protected virtual void Dispose (bool disposing)
211 if (disposed)
212 return;
213 disposed = true;
215 if (owns_socket) {
216 Socket s = socket;
217 if (s != null)
218 s.Close ();
220 socket = null;
223 public override int EndRead (IAsyncResult ar)
225 CheckDisposed ();
226 int res;
228 if (ar == null)
229 throw new ArgumentNullException ("async result is null");
231 try {
232 res = socket.EndReceive (ar);
233 } catch (Exception e) {
234 throw new IOException ("EndRead failure", e);
236 return res;
239 public override void EndWrite (IAsyncResult ar)
241 CheckDisposed ();
242 if (ar == null)
243 throw new ArgumentNullException ("async result is null");
245 try {
246 socket.EndSend (ar);
247 } catch (Exception e) {
248 throw new IOException ("EndWrite failure", e);
252 public override void Flush ()
254 // network streams are non-buffered, this is a no-op
257 void IDisposable.Dispose ()
259 Dispose (true);
260 GC.SuppressFinalize (this);
263 public override int Read ([In,Out] byte [] buffer, int offset, int size)
265 CheckDisposed ();
266 int res;
268 if (buffer == null)
269 throw new ArgumentNullException ("buffer is null");
270 if(offset<0 || offset>buffer.Length) {
271 throw new ArgumentOutOfRangeException("offset exceeds the size of buffer");
273 if(size < 0 || offset+size>buffer.Length) {
274 throw new ArgumentOutOfRangeException("offset+size exceeds the size of buffer");
277 try {
278 res = socket.Receive (buffer, offset, size, 0);
279 } catch (Exception e) {
280 throw new IOException ("Read failure", e);
283 return res;
286 public override long Seek (long offset, SeekOrigin origin)
288 // NetworkStream objects do not support seeking.
290 throw new NotSupportedException ();
293 public override void SetLength (long value)
295 // NetworkStream objects do not support SetLength
297 throw new NotSupportedException ();
300 public override void Write (byte [] buffer, int offset, int size)
302 CheckDisposed ();
303 if (buffer == null)
304 throw new ArgumentNullException ("buffer");
306 if (offset < 0 || offset > buffer.Length)
307 throw new ArgumentOutOfRangeException("offset exceeds the size of buffer");
309 if (size < 0 || size > buffer.Length - offset)
310 throw new ArgumentOutOfRangeException("offset+size exceeds the size of buffer");
312 try {
313 socket.Send (buffer, offset, size, 0);
314 } catch (Exception e) {
315 throw new IOException ("Write failure", e);
319 private void CheckDisposed ()
321 if (disposed)
322 throw new ObjectDisposedException (GetType().FullName);