/// <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);
}
}