Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.IO;
using System.Threading.Tasks;
using System.Runtime.Versioning;
using Microsoft.Win32.SafeHandles;

namespace System.Net.Sockets
{
Expand Down Expand Up @@ -190,12 +191,11 @@ private void SendFileInternal(string? fileName, ReadOnlySpan<byte> preBuffer, Re
{
CheckTransmitFileOptions(flags);

SocketError errorCode = SocketError.Success;

// Open the file, if any
// Open it before we send the preBuffer so that any exception happens first
FileStream? fileStream = OpenFile(fileName);

SocketError errorCode = SocketError.Success;
using (fileStream)
using (SafeFileHandle? fileHandle = OpenFileHandle(fileName))
{
// Send the preBuffer, if any
// This will throw on error
Expand All @@ -205,10 +205,10 @@ private void SendFileInternal(string? fileName, ReadOnlySpan<byte> preBuffer, Re
}

// Send the file, if any
if (fileStream != null)
if (fileHandle != null)
{
// This can throw ObjectDisposedException.
errorCode = SocketPal.SendFile(_handle, fileStream);
errorCode = SocketPal.SendFile(_handle, fileHandle);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -397,14 +397,11 @@ private Socket GetOrCreateAcceptSocket(Socket? acceptSocket, bool checkDisconnec

private void SendFileInternal(string? fileName, ReadOnlySpan<byte> preBuffer, ReadOnlySpan<byte> postBuffer, TransmitFileOptions flags)
{
// Open the file, if any
FileStream? fileStream = OpenFile(fileName);

SocketError errorCode;
using (fileStream)
{
SafeFileHandle? fileHandle = fileStream?.SafeFileHandle;

// Open the file, if any
using (SafeFileHandle? fileHandle = OpenFileHandle(fileName))
{
// This can throw ObjectDisposedException.
errorCode = SocketPal.SendFile(_handle, fileHandle, preBuffer, postBuffer, flags);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using System.Runtime.Versioning;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Win32.SafeHandles;

namespace System.Net.Sockets
{
Expand Down Expand Up @@ -3783,7 +3784,7 @@ private void ValidateBlockingMode()
partial void ValidateForMultiConnect(bool isMultiEndpoint);

// Helper for SendFile implementations
private static FileStream? OpenFile(string? name) => string.IsNullOrEmpty(name) ? null : File.OpenRead(name);
private static SafeFileHandle? OpenFileHandle(string? name) => string.IsNullOrEmpty(name) ? null : File.OpenHandle(name, FileMode.Open, FileAccess.Read);

private void UpdateReceiveSocketErrorForDisposed(ref SocketError socketError, int bytesTransferred)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1172,13 +1172,10 @@ public static SocketError Send(SafeSocketHandle handle, ReadOnlySpan<byte> buffe
return errorCode;
}

public static SocketError SendFile(SafeSocketHandle handle, FileStream fileStream)
public static SocketError SendFile(SafeSocketHandle handle, SafeFileHandle fileHandle)
{
long offset = 0;
long length = fileStream.Length;

SafeFileHandle fileHandle = fileStream.SafeFileHandle;

long length = RandomAccess.GetLength(fileHandle);
long bytesTransferred = 0;

if (!handle.IsNonBlocking)
Expand Down