Inheritance: BaseOverlappedAsyncResult
Exemplo n.º 1
0
        private void DoBeginSendFile(
            string fileName,
            byte[] preBuffer,
            byte[] postBuffer,
            TransmitFileOptions flags,
            TransmitFileOverlappedAsyncResult asyncResult)
        {
            if(s_LoggingEnabled)Logging.Enter(Logging.Sockets, this, "BeginSendFile", "");

            if (CleanedUp) {
                throw new ObjectDisposedException(this.GetType().FullName);
            }

            if (CleanedUp) {
                throw new ObjectDisposedException(this.GetType().FullName);
            }

#if FEATURE_PAL
            throw new PlatformNotSupportedException(SR.GetString(SR.WinNTRequired));
#endif // FEATURE_PAL


            if (!Connected) {
                throw new NotSupportedException(SR.GetString(SR.net_notconnected));
            }

            GlobalLog.Print("Socket#" + ValidationHelper.HashString(this) + "::DoBeginSendFile() SRC:" + ValidationHelper.ToString(LocalEndPoint) + " DST:" + ValidationHelper.ToString(RemoteEndPoint) + " fileName:" + fileName);

            FileStream fileStream = null;
            if (fileName != null && fileName.Length>0) {
                fileStream = new FileStream(fileName,FileMode.Open,FileAccess.Read,FileShare.Read);
            }

            SafeHandle fileHandle = null;

            if (fileStream != null) {
                ExceptionHelper.UnmanagedPermission.Assert();
                try {
                    fileHandle = fileStream.SafeFileHandle;
                }
                finally {
                    SecurityPermission.RevertAssert();
                }
            }

            // Guarantee to call CheckAsyncCallOverlappedResult if we call SetUnamangedStructures with a cache in order to
            // avoid a Socket leak in case of error.
            SocketError errorCode = SocketError.SocketError;
            try
            {
                asyncResult.SetUnmanagedStructures(preBuffer, postBuffer, fileStream, flags, ref Caches.SendOverlappedCache);
                bool result = false;

                // This can throw ObjectDisposedException.
                if (fileHandle != null){
                    result = UnsafeNclNativeMethods.OSSOCK.TransmitFile(m_Handle,fileHandle,0,0,asyncResult.OverlappedHandle,asyncResult.TransmitFileBuffers,flags);
                }
                else{
                    result = UnsafeNclNativeMethods.OSSOCK.TransmitFile2(m_Handle,IntPtr.Zero,0,0,asyncResult.OverlappedHandle,asyncResult.TransmitFileBuffers,flags);
                }
                if(!result)
                {
                    errorCode =  (SocketError)Marshal.GetLastWin32Error();
                }
                else
                {
                    errorCode = SocketError.Success;
                }
            }
            finally
            {
                errorCode = asyncResult.CheckAsyncCallOverlappedResult(errorCode);
            }

            //
            // if the native call fails we'll throw a SocketException
            //
            if (errorCode!=SocketError.Success) {
                //
                // update our internal state after this socket error and throw
                //
                asyncResult.ExtractCache(ref Caches.SendOverlappedCache);
                SocketException socketException = new SocketException(errorCode);
                UpdateStatusAfterSocketError(socketException);

                if(s_LoggingEnabled)Logging.Exception(Logging.Sockets, this, "BeginSendFile", socketException);
                throw socketException;
            }

            GlobalLog.Print("Socket#" + ValidationHelper.HashString(this) + "::DoBeginSendFile() UnsafeNclNativeMethods.OSSOCK.send returns:" + errorCode.ToString());

            if(s_LoggingEnabled)Logging.Exit(Logging.Sockets, this, "BeginSendFile", errorCode);
        }
Exemplo n.º 2
0
        public void SendFile(string fileName, byte[] preBuffer, byte[] postBuffer, TransmitFileOptions flags) {
            if(s_LoggingEnabled)Logging.Enter(Logging.Sockets, this, "SendFile", "");

            if (CleanedUp) {
                throw new ObjectDisposedException(this.GetType().FullName);
            }

            if (!Connected) {
                throw new NotSupportedException(SR.GetString(SR.net_notconnected));
            }

            ValidateBlockingMode();
            GlobalLog.Print("Socket#" + ValidationHelper.HashString(this) + "::SendFile() SRC:" + ValidationHelper.ToString(LocalEndPoint) + " DST:" + ValidationHelper.ToString(RemoteEndPoint) + " fileName:" + fileName);

            TransmitFileOverlappedAsyncResult asyncResult = new TransmitFileOverlappedAsyncResult(this);

            FileStream fileStream = null;
            if (fileName != null && fileName.Length>0) {
                fileStream = new FileStream(fileName,FileMode.Open,FileAccess.Read,FileShare.Read);
            }

            SafeHandle fileHandle = null;

            if (fileStream != null) {
                ExceptionHelper.UnmanagedPermission.Assert();
                try {
                    fileHandle = fileStream.SafeFileHandle;
                }
                finally {
                    SecurityPermission.RevertAssert();
                }
            }

            SocketError errorCode = SocketError.Success;
            try {
                asyncResult.SetUnmanagedStructures(preBuffer, postBuffer, fileStream, 0, true);

                // This can throw ObjectDisposedException.
                if (fileHandle != null ?
                    !UnsafeNclNativeMethods.OSSOCK.TransmitFile_Blocking(m_Handle.DangerousGetHandle(), fileHandle, 0, 0, SafeNativeOverlapped.Zero, asyncResult.TransmitFileBuffers, flags) :
                    !UnsafeNclNativeMethods.OSSOCK.TransmitFile_Blocking2(m_Handle.DangerousGetHandle(), IntPtr.Zero, 0, 0, SafeNativeOverlapped.Zero, asyncResult.TransmitFileBuffers, flags))
                {
                    errorCode = (SocketError) Marshal.GetLastWin32Error();
                }
            }
            finally {
                asyncResult.SyncReleaseUnmanagedStructures();
            }

#if TRAVE
            try
            {
                GlobalLog.Print("Socket#" + ValidationHelper.HashString(this) + "::SendFile() SRC:" + ValidationHelper.ToString(LocalEndPoint) + " DST:" + ValidationHelper.ToString(RemoteEndPoint) + " UnsafeNclNativeMethods.OSSOCK.TransmitFile returns errorCode:" + errorCode);
            }
            catch (ObjectDisposedException) { }
#endif

            //
            // if the native call fails we'll throw a SocketException
            //
            if (errorCode!=SocketError.Success) {
                //
                // update our internal state after this socket error and throw
                //
                SocketException socketException = new SocketException(errorCode);
                UpdateStatusAfterSocketError(socketException);
                if(s_LoggingEnabled)Logging.Exception(Logging.Sockets, this, "SendFile", socketException);
                throw socketException;
            }

            if ((asyncResult.Flags & (TransmitFileOptions.Disconnect | TransmitFileOptions.ReuseSocket) )!=0) {
                SetToDisconnected();
                m_RemoteEndPoint = null;
            }

            if(s_LoggingEnabled)Logging.Exit(Logging.Sockets, this, "SendFile", errorCode);
            return;
        }
Exemplo n.º 3
0
        public IAsyncResult BeginSendFile(
            string fileName,
            byte[] preBuffer,
            byte[] postBuffer,
            TransmitFileOptions flags,
            AsyncCallback callback,
            object state)
        {

            // Start the context flowing.  No lock necessary.
            TransmitFileOverlappedAsyncResult asyncResult = new TransmitFileOverlappedAsyncResult(this,state,callback);
            asyncResult.StartPostingAsyncOp(false);

            // Start the operation.
            DoBeginSendFile(fileName, preBuffer, postBuffer, flags, asyncResult);

            // Finish the op, collect the context or maybe call the callback.
            asyncResult.FinishPostingAsyncOp(ref Caches.SendClosureCache);
            return asyncResult;
        }
Exemplo n.º 4
0
 private void DoBeginSendFile(string fileName, byte[] preBuffer, byte[] postBuffer, TransmitFileOptions flags, TransmitFileOverlappedAsyncResult asyncResult)
 {
     if (Socket.s_LoggingEnabled)
     Logging.Enter(Logging.Sockets, (object) this, "BeginSendFile", "");
       if (this.CleanedUp)
     throw new ObjectDisposedException(this.GetType().FullName);
       if (this.CleanedUp)
     throw new ObjectDisposedException(this.GetType().FullName);
       if (!this.Connected)
     throw new NotSupportedException(SR.GetString("net_notconnected"));
       FileStream fileStream = (FileStream) null;
       if (fileName != null && fileName.Length > 0)
     fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
       SafeHandle fileHandle = (SafeHandle) null;
       if (fileStream != null)
       {
     ExceptionHelper.UnmanagedPermission.Assert();
     try
     {
       fileHandle = (SafeHandle) fileStream.SafeFileHandle;
     }
     finally
     {
       CodeAccessPermission.RevertAssert();
     }
       }
       SocketError socketError = SocketError.SocketError;
       try
       {
     asyncResult.SetUnmanagedStructures(preBuffer, postBuffer, fileStream, flags, ref this.Caches.SendOverlappedCache);
     socketError = (fileHandle == null ? UnsafeNclNativeMethods.OSSOCK.TransmitFile2(this.m_Handle, IntPtr.Zero, 0, 0, asyncResult.OverlappedHandle, asyncResult.TransmitFileBuffers, flags) : UnsafeNclNativeMethods.OSSOCK.TransmitFile(this.m_Handle, fileHandle, 0, 0, asyncResult.OverlappedHandle, asyncResult.TransmitFileBuffers, flags)) ? SocketError.Success : (SocketError) Marshal.GetLastWin32Error();
       }
       finally
       {
     socketError = asyncResult.CheckAsyncCallOverlappedResult(socketError);
       }
       if (socketError != SocketError.Success)
       {
     asyncResult.ExtractCache(ref this.Caches.SendOverlappedCache);
     SocketException socketException = new SocketException(socketError);
     this.UpdateStatusAfterSocketError(socketException);
     if (Socket.s_LoggingEnabled)
       Logging.Exception(Logging.Sockets, (object) this, "BeginSendFile", (Exception) socketException);
     throw socketException;
       }
       else
       {
     if (!Socket.s_LoggingEnabled)
       return;
     Logging.Exit(Logging.Sockets, (object) this, "BeginSendFile", (object) socketError);
       }
 }
Exemplo n.º 5
0
 /// <summary>
 /// Sends the file <paramref name="fileName"/> and buffers of data to a connected <see cref="T:System.Net.Sockets.Socket"/> object using the specified <see cref="T:System.Net.Sockets.TransmitFileOptions"/> value.
 /// </summary>
 /// <param name="fileName">A <see cref="T:System.String"/> that contains the path and name of the file to be sent. This parameter can be null. </param><param name="preBuffer">A <see cref="T:System.Byte"/> array that contains data to be sent before the file is sent. This parameter can be null. </param><param name="postBuffer">A <see cref="T:System.Byte"/> array that contains data to be sent after the file is sent. This parameter can be null. </param><param name="flags">One or more of <see cref="T:System.Net.Sockets.TransmitFileOptions"/> values. </param><exception cref="T:System.NotSupportedException">The operating system is not Windows NT or later.- or - The socket is not connected to a remote host. </exception><exception cref="T:System.ObjectDisposedException">The <see cref="T:System.Net.Sockets.Socket"/> object has been closed. </exception><exception cref="T:System.InvalidOperationException">The <see cref="T:System.Net.Sockets.Socket"/> object is not in blocking mode and cannot accept this synchronous call. </exception><exception cref="T:System.IO.FileNotFoundException">The file <paramref name="fileName"/> was not found. </exception><exception cref="T:System.Net.Sockets.SocketException">An error occurred when attempting to access the socket. See the Remarks section for more information. </exception>
 public void SendFile(string fileName, byte[] preBuffer, byte[] postBuffer, TransmitFileOptions flags)
 {
     if (Socket.s_LoggingEnabled)
     Logging.Enter(Logging.Sockets, (object) this, "SendFile", "");
       if (this.CleanedUp)
     throw new ObjectDisposedException(this.GetType().FullName);
       if (!this.Connected)
     throw new NotSupportedException(SR.GetString("net_notconnected"));
       this.ValidateBlockingMode();
       TransmitFileOverlappedAsyncResult overlappedAsyncResult = new TransmitFileOverlappedAsyncResult(this);
       FileStream fileStream = (FileStream) null;
       if (fileName != null && fileName.Length > 0)
     fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
       SafeHandle fileHandle = (SafeHandle) null;
       if (fileStream != null)
       {
     ExceptionHelper.UnmanagedPermission.Assert();
     try
     {
       fileHandle = (SafeHandle) fileStream.SafeFileHandle;
     }
     finally
     {
       CodeAccessPermission.RevertAssert();
     }
       }
       SocketError socketError = SocketError.Success;
       try
       {
     overlappedAsyncResult.SetUnmanagedStructures(preBuffer, postBuffer, fileStream, TransmitFileOptions.UseDefaultWorkerThread, true);
     if ((fileHandle != null ? (!UnsafeNclNativeMethods.OSSOCK.TransmitFile_Blocking(this.m_Handle.DangerousGetHandle(), fileHandle, 0, 0, (SafeHandle) SafeNativeOverlapped.Zero, overlappedAsyncResult.TransmitFileBuffers, flags) ? 1 : 0) : (!UnsafeNclNativeMethods.OSSOCK.TransmitFile_Blocking2(this.m_Handle.DangerousGetHandle(), IntPtr.Zero, 0, 0, (SafeHandle) SafeNativeOverlapped.Zero, overlappedAsyncResult.TransmitFileBuffers, flags) ? 1 : 0)) != 0)
       socketError = (SocketError) Marshal.GetLastWin32Error();
       }
       finally
       {
     overlappedAsyncResult.SyncReleaseUnmanagedStructures();
       }
       if (socketError != SocketError.Success)
       {
     SocketException socketException = new SocketException(socketError);
     this.UpdateStatusAfterSocketError(socketException);
     if (Socket.s_LoggingEnabled)
       Logging.Exception(Logging.Sockets, (object) this, "SendFile", (Exception) socketException);
     throw socketException;
       }
       else
       {
     if ((overlappedAsyncResult.Flags & (TransmitFileOptions.Disconnect | TransmitFileOptions.ReuseSocket)) != TransmitFileOptions.UseDefaultWorkerThread)
     {
       this.SetToDisconnected();
       this.m_RemoteEndPoint = (EndPoint) null;
     }
     if (!Socket.s_LoggingEnabled)
       return;
     Logging.Exit(Logging.Sockets, (object) this, "SendFile", (object) socketError);
       }
 }
Exemplo n.º 6
0
 public IAsyncResult BeginSendFile(string fileName, byte[] preBuffer, byte[] postBuffer, TransmitFileOptions flags, AsyncCallback callback, object state)
 {
     TransmitFileOverlappedAsyncResult asyncResult = new TransmitFileOverlappedAsyncResult(this, state, callback);
       asyncResult.StartPostingAsyncOp(false);
       this.DoBeginSendFile(fileName, preBuffer, postBuffer, flags, asyncResult);
       asyncResult.FinishPostingAsyncOp(ref this.Caches.SendClosureCache);
       return (IAsyncResult) asyncResult;
 }
 public void SendFile(string fileName, byte[] preBuffer, byte[] postBuffer, TransmitFileOptions flags)
 {
     if (s_LoggingEnabled)
     {
         Logging.Enter(Logging.Sockets, this, "SendFile", "");
     }
     if (this.CleanedUp)
     {
         throw new ObjectDisposedException(base.GetType().FullName);
     }
     if (!ComNetOS.IsWinNt)
     {
         throw new PlatformNotSupportedException(SR.GetString("WinNTRequired"));
     }
     if (!this.Connected)
     {
         throw new NotSupportedException(SR.GetString("net_notconnected"));
     }
     this.ValidateBlockingMode();
     TransmitFileOverlappedAsyncResult result = new TransmitFileOverlappedAsyncResult(this);
     FileStream fileStream = null;
     if ((fileName != null) && (fileName.Length > 0))
     {
         fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
     }
     System.Runtime.InteropServices.SafeHandle fileHandle = null;
     if (fileStream != null)
     {
         ExceptionHelper.UnmanagedPermission.Assert();
         try
         {
             fileHandle = fileStream.SafeFileHandle;
         }
         finally
         {
             CodeAccessPermission.RevertAssert();
         }
     }
     SocketError success = SocketError.Success;
     try
     {
         result.SetUnmanagedStructures(preBuffer, postBuffer, fileStream, TransmitFileOptions.UseDefaultWorkerThread, true);
         if ((fileHandle != null) ? !UnsafeNclNativeMethods.OSSOCK.TransmitFile_Blocking(this.m_Handle.DangerousGetHandle(), fileHandle, 0, 0, SafeNativeOverlapped.Zero, result.TransmitFileBuffers, flags) : !UnsafeNclNativeMethods.OSSOCK.TransmitFile_Blocking2(this.m_Handle.DangerousGetHandle(), IntPtr.Zero, 0, 0, SafeNativeOverlapped.Zero, result.TransmitFileBuffers, flags))
         {
             success = (SocketError) Marshal.GetLastWin32Error();
         }
     }
     finally
     {
         result.SyncReleaseUnmanagedStructures();
     }
     if (success != SocketError.Success)
     {
         SocketException socketException = new SocketException(success);
         this.UpdateStatusAfterSocketError(socketException);
         if (s_LoggingEnabled)
         {
             Logging.Exception(Logging.Sockets, this, "SendFile", socketException);
         }
         throw socketException;
     }
     if ((result.Flags & (TransmitFileOptions.ReuseSocket | TransmitFileOptions.Disconnect)) != TransmitFileOptions.UseDefaultWorkerThread)
     {
         this.SetToDisconnected();
         this.m_RemoteEndPoint = null;
     }
     if (s_LoggingEnabled)
     {
         Logging.Exit(Logging.Sockets, this, "SendFile", success);
     }
 }
 private void DoBeginSendFile(string fileName, byte[] preBuffer, byte[] postBuffer, TransmitFileOptions flags, TransmitFileOverlappedAsyncResult asyncResult)
 {
     if (s_LoggingEnabled)
     {
         Logging.Enter(Logging.Sockets, this, "BeginSendFile", "");
     }
     if (this.CleanedUp)
     {
         throw new ObjectDisposedException(base.GetType().FullName);
     }
     if (this.CleanedUp)
     {
         throw new ObjectDisposedException(base.GetType().FullName);
     }
     if (!ComNetOS.IsWinNt)
     {
         throw new PlatformNotSupportedException(SR.GetString("WinNTRequired"));
     }
     if (!this.Connected)
     {
         throw new NotSupportedException(SR.GetString("net_notconnected"));
     }
     FileStream fileStream = null;
     if ((fileName != null) && (fileName.Length > 0))
     {
         fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
     }
     System.Runtime.InteropServices.SafeHandle fileHandle = null;
     if (fileStream != null)
     {
         ExceptionHelper.UnmanagedPermission.Assert();
         try
         {
             fileHandle = fileStream.SafeFileHandle;
         }
         finally
         {
             CodeAccessPermission.RevertAssert();
         }
     }
     SocketError socketError = SocketError.SocketError;
     try
     {
         asyncResult.SetUnmanagedStructures(preBuffer, postBuffer, fileStream, flags, ref this.Caches.SendOverlappedCache);
         bool flag = false;
         if (fileHandle != null)
         {
             flag = UnsafeNclNativeMethods.OSSOCK.TransmitFile(this.m_Handle, fileHandle, 0, 0, asyncResult.OverlappedHandle, asyncResult.TransmitFileBuffers, flags);
         }
         else
         {
             flag = UnsafeNclNativeMethods.OSSOCK.TransmitFile2(this.m_Handle, IntPtr.Zero, 0, 0, asyncResult.OverlappedHandle, asyncResult.TransmitFileBuffers, flags);
         }
         if (!flag)
         {
             socketError = (SocketError) Marshal.GetLastWin32Error();
         }
         else
         {
             socketError = SocketError.Success;
         }
     }
     finally
     {
         socketError = asyncResult.CheckAsyncCallOverlappedResult(socketError);
     }
     if (socketError != SocketError.Success)
     {
         asyncResult.ExtractCache(ref this.Caches.SendOverlappedCache);
         SocketException socketException = new SocketException(socketError);
         this.UpdateStatusAfterSocketError(socketException);
         if (s_LoggingEnabled)
         {
             Logging.Exception(Logging.Sockets, this, "BeginSendFile", socketException);
         }
         throw socketException;
     }
     if (s_LoggingEnabled)
     {
         Logging.Exit(Logging.Sockets, this, "BeginSendFile", socketError);
     }
 }