* TabControl.cs: Fix typo, emilinates an unneeded expose event.
[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 ();
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 virtual void Dispose (bool disposing)
210                 {
211                         if (disposed) 
212                                 return;
213                         disposed = true;
214                         
215                         if (owns_socket) {
216                                 Socket s = socket;
217                                 if (s != null)
218                                         s.Close ();
219                         }
220                         socket = null;
221                 }
222
223                 public override int EndRead (IAsyncResult ar)
224                 {
225                         CheckDisposed ();
226                         int res;
227
228                         if (ar == null)
229                                 throw new ArgumentNullException ("async result is null");
230
231                         try {
232                                 res = socket.EndReceive (ar);
233                         } catch (Exception e) {
234                                 throw new IOException ("EndRead failure", e);
235                         }
236                         return res;
237                 }
238
239                 public override void EndWrite (IAsyncResult ar)
240                 {
241                         CheckDisposed ();
242                         if (ar == null)
243                                 throw new ArgumentNullException ("async result is null");
244
245                         try {
246                                 socket.EndSend (ar);
247                         } catch (Exception e) {
248                                 throw new IOException ("EndWrite failure", e);
249                         }
250                 }
251
252                 public override void Flush ()
253                 {
254                         // network streams are non-buffered, this is a no-op
255                 }
256
257                 void IDisposable.Dispose ()
258                 {
259                         Dispose (true);
260                         GC.SuppressFinalize (this);
261                 }
262
263                 public override int Read ([In,Out] byte [] buffer, int offset, int size)
264                 {
265                         CheckDisposed ();
266                         int res;
267
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");
272                         }
273                         if(size < 0 || offset+size>buffer.Length) {
274                                 throw new ArgumentOutOfRangeException("offset+size exceeds the size of buffer");
275                         }
276
277                         try {
278                                 res = socket.Receive (buffer, offset, size, 0);
279                         } catch (Exception e) {
280                                 throw new IOException ("Read failure", e);
281                         }
282                         
283                         return res;
284                 }
285
286                 public override long Seek (long offset, SeekOrigin origin)
287                 {
288                         // NetworkStream objects do not support seeking.
289                         
290                         throw new NotSupportedException ();
291                 }
292
293                 public override void SetLength (long value)
294                 {
295                         // NetworkStream objects do not support SetLength
296                         
297                         throw new NotSupportedException ();
298                 }
299
300                 public override void Write (byte [] buffer, int offset, int size)
301                 {
302                         CheckDisposed ();
303                         if (buffer == null)
304                                 throw new ArgumentNullException ("buffer");
305
306                         if (offset < 0 || offset > buffer.Length)
307                                 throw new ArgumentOutOfRangeException("offset exceeds the size of buffer");
308
309                         if (size < 0 || size > buffer.Length - offset)
310                                 throw new ArgumentOutOfRangeException("offset+size exceeds the size of buffer");
311
312                         try {
313                                 int count = 0;
314                                 while (size - count > 0) {
315                                         count += socket.Send (buffer, offset + count, size - count, 0);
316                                 }
317                         } catch (Exception e) {
318                                 throw new IOException ("Write failure", e); 
319                         }
320                 }
321                 
322                 private void CheckDisposed ()
323                 {
324                         if (disposed)
325                                 throw new ObjectDisposedException (GetType().FullName);
326                 }               
327              
328         }
329 }