2008-10-31 Gonzalo Paniagua Javier <gonzalo@novell.com>
[mono.git] / mcs / class / System / System.Net / FtpDataStream.cs
index 9f448bf7538ef6bc871613cdb671f4e0aaf228c4..bc33e24c0d8c92b96f768c4372ce4080e27ef371 100644 (file)
@@ -12,6 +12,7 @@ using System.IO;
 using System.Net.Sockets;
 using System.Runtime.Remoting.Messaging;
 using System.Threading;
+using System.Net;
 
 #if NET_2_0
 
@@ -24,7 +25,6 @@ namespace System.Net
                bool disposed;
                bool isRead;
                int totalRead;
-               int contentLength;
 
                ManualResetEvent closewh;
 
@@ -38,10 +38,13 @@ namespace System.Net
                                throw new ArgumentException ("socket");
 
                        this.request = request;
-                       this.contentLength = socket.Available;
                        this.networkStream = new NetworkStream (socket, true);
                        this.isRead = isRead;
 
+                       if (request.EnableSsl) {
+                               FtpWebRequest.ChangeToSSLSocket (ref networkStream);
+                       }
+
                        closewh = new ManualResetEvent (false);
                }
 
@@ -87,7 +90,11 @@ namespace System.Net
 
                public override void Close ()
                {
-                       ((IDisposable) this).Dispose ();
+                       if (!disposed) {
+                               networkStream.Close ();
+                               request.SetTransferCompleted ();
+                               ((IDisposable) this).Dispose ();
+                       }
                }
 
                public override void Flush ()
@@ -108,19 +115,22 @@ namespace System.Net
                int ReadInternal (byte [] buffer, int offset, int size)
                {
                        int nbytes;
+
+                       request.CheckIfAborted ();
+
                        try {
                                // Probably it would be better to have the socket here
                                nbytes = networkStream.Read (buffer, offset, size);
-                       } catch (IOException exc) {
+                       } catch (IOException) {
                                throw new ProtocolViolationException ("Server commited a protocol violation");
                        }
 
                        totalRead += nbytes;
-                       if (nbytes == 0)
-                               contentLength = totalRead;
-                       if (totalRead >= contentLength)
+                       if (nbytes == 0) {
+                               networkStream.Close ();
                                request.SetTransferCompleted ();
-                       
+                       }
+
                        return nbytes;
                }
 
@@ -159,6 +169,7 @@ namespace System.Net
 
                public override int Read (byte [] buffer, int offset, int size)
                {
+                       request.CheckIfAborted ();
                        IAsyncResult ar = BeginRead (buffer, offset, size, null, null);
                        if (!ar.IsCompleted && !ar.AsyncWaitHandle.WaitOne (request.ReadWriteTimeout, false))
                                throw new WebException ("Read timed out.", WebExceptionStatus.Timeout);
@@ -171,9 +182,11 @@ namespace System.Net
                
                void WriteInternal (byte [] buffer, int offset, int size)
                {
+                       request.CheckIfAborted ();
+                       
                        try {
                                networkStream.Write (buffer, offset, size);
-                       } catch (IOException exc) {
+                       } catch (IOException) {
                                throw new ProtocolViolationException ();
                        }
                }
@@ -212,6 +225,7 @@ namespace System.Net
 
                public override void Write (byte [] buffer, int offset, int size)
                {
+                       request.CheckIfAborted ();
                        IAsyncResult ar = BeginWrite (buffer, offset, size, null, null);
                        if (!ar.IsCompleted && !ar.AsyncWaitHandle.WaitOne (request.ReadWriteTimeout, false))
                                throw new WebException ("Read timed out.", WebExceptionStatus.Timeout);