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 ();
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
);
167 throw new IOException ("BeginReceive failure");
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 ();
209 protected virtual void Dispose (bool disposing
)
223 public override int EndRead (IAsyncResult ar
)
229 throw new ArgumentNullException ("async result is null");
232 res
= socket
.EndReceive (ar
);
233 } catch (Exception e
) {
234 throw new IOException ("EndRead failure", e
);
239 public override void EndWrite (IAsyncResult ar
)
243 throw new ArgumentNullException ("async result is null");
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 ()
260 GC
.SuppressFinalize (this);
263 public override int Read ([In
,Out
] byte [] buffer
, int offset
, int size
)
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");
278 res
= socket
.Receive (buffer
, offset
, size
, 0);
279 } catch (Exception e
) {
280 throw new IOException ("Read failure", e
);
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
)
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");
313 socket
.Send (buffer
, offset
, size
, 0);
314 } catch (Exception e
) {
315 throw new IOException ("Write failure", e
);
319 private void CheckDisposed ()
322 throw new ObjectDisposedException (GetType().FullName
);