New test.
[mono.git] / mcs / class / System / System.Net.Sockets / NetworkStream.cs
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 //
9
10 //
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.
29 //
30
31 using System.IO;
32 using System.Runtime.InteropServices;
33
34 namespace System.Net.Sockets
35 {
36         public class NetworkStream : Stream, IDisposable {
37                 FileAccess access;
38                 Socket socket;
39                 bool owns_socket;
40                 bool readable, writeable;
41                 bool disposed = false;
42                 
43                 public NetworkStream (Socket socket)
44                         : this (socket, FileAccess.ReadWrite, false)
45                 {
46                 }
47
48                 public NetworkStream (Socket socket, bool owns_socket)
49                         : this (socket, FileAccess.ReadWrite, owns_socket)
50                 {
51                 }
52
53                 public NetworkStream (Socket socket, FileAccess access)
54                         : this (socket, access, false)
55                 {
56                 }
57                 
58                 public NetworkStream (Socket socket, FileAccess access, bool owns_socket)
59                 {
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 ("Operation not allowed on a non-blocking socket.");
68                         
69                         this.socket = socket;
70                         this.owns_socket = owns_socket;
71                         this.access = access;
72
73                         readable = CanRead;
74                         writeable = CanWrite;
75                 }
76
77                 public override bool CanRead {
78                         get {
79                                 return access == FileAccess.ReadWrite || access == FileAccess.Read;
80                         }
81                 }
82
83                 public override bool CanSeek {
84                         get {
85                                 // network sockets cant seek.
86                                 return false;
87                         }
88                 }
89
90                 public override bool CanWrite {
91                         get {
92                                 return access == FileAccess.ReadWrite || access == FileAccess.Write;
93                         }
94                 }
95
96                 public virtual bool DataAvailable {
97                         get {
98                                 CheckDisposed ();
99                                 return socket.Available > 0;
100                         }
101                 }
102
103                 public override long Length {
104                         get {
105                                 // Network sockets always throw an exception
106                                 throw new NotSupportedException ();
107                         }
108                 }
109
110                 public override long Position {
111                         get {
112                                 // Network sockets always throw an exception
113                                 throw new NotSupportedException ();
114                         }
115                         
116                         set {
117                                 // Network sockets always throw an exception
118                                 throw new NotSupportedException ();
119                         }
120                 }
121
122                 protected bool Readable {
123                         get {
124                                 return readable;
125                         }
126
127                         set {
128                                 readable = value;
129                         }
130                 }
131
132                 protected Socket Socket {
133                         get {
134                                 return socket;
135                         }
136                 }
137
138                 protected bool Writeable {
139                         get {
140                                 return writeable;
141                         }
142
143                         set {
144                                 writeable = value;
145                         }
146                 }
147
148                 public override IAsyncResult BeginRead (byte [] buffer, int offset, int size,
149                                                         AsyncCallback callback, object state)
150                 {
151                         CheckDisposed ();                               
152                         IAsyncResult retval;
153
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");
159                         }
160                         if(size<0 || offset+size>len) {
161                                 throw new ArgumentOutOfRangeException("offset+size exceeds the size of buffer");
162                         }
163
164                         try {
165                                 retval = socket.BeginReceive (buffer, offset, size, 0, callback, state);
166                         } catch (Exception e) {
167                                 throw new IOException ("BeginReceive failure", e);
168                         }
169
170                         return retval;
171                 }
172
173                 public override IAsyncResult BeginWrite (byte [] buffer, int offset, int size,
174                                                         AsyncCallback callback, object state)
175                 {
176                         CheckDisposed ();
177                         IAsyncResult retval;
178
179                         if (buffer == null)
180                                 throw new ArgumentNullException ("buffer is null");
181
182                         int len = buffer.Length;
183                         if(offset<0 || offset>len) {
184                                 throw new ArgumentOutOfRangeException("offset exceeds the size of buffer");
185                         }
186                         if(size<0 || offset+size>len) {
187                                 throw new ArgumentOutOfRangeException("offset+size exceeds the size of buffer");
188                         }
189
190                         try {
191                                 retval = socket.BeginSend (buffer, offset, size, 0, callback, state);
192                         } catch {
193                                 throw new IOException ("BeginWrite failure");
194                         }
195
196                         return retval;
197                 }
198
199                 ~NetworkStream ()
200                 {
201                         Dispose (false);
202                 }
203                 
204                 public override void Close ()
205                 {
206                         ((IDisposable) this).Dispose ();
207                 }
208
209                 protected
210 #if NET_2_0
211                 override
212 #else
213                 virtual
214 #endif
215                 void Dispose (bool disposing)
216                 {
217                         if (disposed) 
218                                 return;
219                         disposed = true;
220                         
221                         if (owns_socket) {
222                                 Socket s = socket;
223                                 if (s != null)
224                                         s.Close ();
225                         }
226                         socket = null;
227                 }
228
229                 public override int EndRead (IAsyncResult ar)
230                 {
231                         CheckDisposed ();
232                         int res;
233
234                         if (ar == null)
235                                 throw new ArgumentNullException ("async result is null");
236
237                         try {
238                                 res = socket.EndReceive (ar);
239                         } catch (Exception e) {
240                                 throw new IOException ("EndRead failure", e);
241                         }
242                         return res;
243                 }
244
245                 public override void EndWrite (IAsyncResult ar)
246                 {
247                         CheckDisposed ();
248                         if (ar == null)
249                                 throw new ArgumentNullException ("async result is null");
250
251                         try {
252                                 socket.EndSend (ar);
253                         } catch (Exception e) {
254                                 throw new IOException ("EndWrite failure", e);
255                         }
256                 }
257
258                 public override void Flush ()
259                 {
260                         // network streams are non-buffered, this is a no-op
261                 }
262
263                 void IDisposable.Dispose ()
264                 {
265                         Dispose (true);
266                         GC.SuppressFinalize (this);
267                 }
268
269                 public override int Read ([In,Out] byte [] buffer, int offset, int size)
270                 {
271                         CheckDisposed ();
272                         int res;
273
274                         if (buffer == null)
275                                 throw new ArgumentNullException ("buffer is null");
276                         if(offset<0 || offset>buffer.Length) {
277                                 throw new ArgumentOutOfRangeException("offset exceeds the size of buffer");
278                         }
279                         if(size < 0 || offset+size>buffer.Length) {
280                                 throw new ArgumentOutOfRangeException("offset+size exceeds the size of buffer");
281                         }
282
283                         try {
284                                 res = socket.Receive (buffer, offset, size, 0);
285                         } catch (Exception e) {
286                                 throw new IOException ("Read failure", e);
287                         }
288                         
289                         return res;
290                 }
291
292                 public override long Seek (long offset, SeekOrigin origin)
293                 {
294                         // NetworkStream objects do not support seeking.
295                         
296                         throw new NotSupportedException ();
297                 }
298
299                 public override void SetLength (long value)
300                 {
301                         // NetworkStream objects do not support SetLength
302                         
303                         throw new NotSupportedException ();
304                 }
305
306                 public override void Write (byte [] buffer, int offset, int size)
307                 {
308                         CheckDisposed ();
309                         if (buffer == null)
310                                 throw new ArgumentNullException ("buffer");
311
312                         if (offset < 0 || offset > buffer.Length)
313                                 throw new ArgumentOutOfRangeException("offset exceeds the size of buffer");
314
315                         if (size < 0 || size > buffer.Length - offset)
316                                 throw new ArgumentOutOfRangeException("offset+size exceeds the size of buffer");
317
318                         try {
319                                 int count = 0;
320                                 while (size - count > 0) {
321                                         count += socket.Send (buffer, offset + count, size - count, 0);
322                                 }
323                         } catch (Exception e) {
324                                 throw new IOException ("Write failure", e); 
325                         }
326                 }
327                 
328                 private void CheckDisposed ()
329                 {
330                         if (disposed)
331                                 throw new ObjectDisposedException (GetType().FullName);
332                 }
333
334 #if TARGET_JVM
335                 public void ChangeToSSLSocket()
336                 {
337                         socket.ChangeToSSL();
338                 }
339 #endif
340
341         }
342 }