Created
October 31, 2013 10:05
-
-
Save Porges/7247250 to your computer and use it in GitHub Desktop.
Where we're going we don't need FileStreams
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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