Skip to content

Instantly share code, notes, and snippets.

@Porges
Created October 31, 2013 10:05
Show Gist options
  • Save Porges/7247250 to your computer and use it in GitHub Desktop.
Save Porges/7247250 to your computer and use it in GitHub Desktop.
Where we're going we don't need FileStreams
public Task Write(ArraySegment<byte> buffer)
{
unsafe
{
fixed (byte* thing = buffer.Array) // this fixed expires but the real fixed is that done by Pack (lives until callback)
{
var tcs = new TaskCompletionSource<uint>();
var overlapped = new Overlapped((int)_pos, (int)(_pos>>32), IntPtr.Zero, null);
_pos += (ulong)buffer.Count;
NativeMethods.WriteFileChecked(_handle, &thing[buffer.Offset], buffer.Count, IntPtr.Zero, overlapped.Pack(
(errCode, numBytes, nativeOverlapped) =>
{
Overlapped.Free(nativeOverlapped);
if (errCode != 0)
{
tcs.SetException(new Win32Exception((int)errCode));
}
else
{
tcs.SetResult(numBytes);
}
}, buffer.Array));
return tcs.Task;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment