SyncReleaseUnmanagedStructures() private method

private SyncReleaseUnmanagedStructures ( ) : void
return void
Ejemplo n.º 1
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);
       }
 }
Ejemplo 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;
        }
 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);
     }
 }