Szymon Kulec
public override IAsyncResult BeginRead(
byte[] array,
int offset,
int numBytes,
AsyncCallback userCallback,
Object stateObject
)
public IAsyncResult BeginAccept(
Socket acceptSocket,
int receiveSize,
AsyncCallback callback,
Object state
)
Asynchronous .NET API for operations like
uses I/O Completion Ports
HANDLE WINAPI CreateIoCompletionPort(
_In_ HANDLE FileHandle,
_In_opt_ HANDLE ExistingCompletionPort,
_In_ ULONG_PTR CompletionKey,
_In_ DWORD NumberOfConcurrentThreads
);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr CreateIoCompletionPort(
IntPtr fileHandle,
IntPtr existingCompletionPort,
IntPtr completionKey,
uint numberOfConcurrentThreads
);
BOOL WINAPI GetQueuedCompletionStatus(
_In_ HANDLE CompletionPort,
_Out_ LPDWORD lpNumberOfBytes,
_Out_ PULONG_PTR lpCompletionKey,
_Out_ LPOVERLAPPED *lpOverlapped,
_In_ DWORD dwMilliseconds
);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool GetQueuedCompletionStatus(
IntPtr CompletionPort,
out uint numberOfTransferredBytes,
out IntPtr completionKey,
out NativeOverlapped* overlapped,
uint milisecondsTimeout
);
BOOL WINAPI GetQueuedCompletionStatusEx(
_In_ HANDLE CompletionPort,
_Out_ LPOVERLAPPED_ENTRY lpCompletionPortEntries,
_In_ ULONG ulCount,
_Out_ PULONG ulNumEntriesRemoved,
_In_ DWORD dwMilliseconds,
_In_ BOOL fAlertable
);
typedef struct _OVERLAPPED_ENTRY {
ULONG_PTR lpCompletionKey;
LPOVERLAPPED lpOverlapped;
ULONG_PTR Internal;
DWORD dwNumberOfBytesTransferred;
} OVERLAPPED_ENTRY, *LPOVERLAPPED_ENTRY;
[DllImport("kernel32.dll")]
public static extern bool GetQueuedCompletionStatusEx(
IntPtr completionPort,
OverlappedEntry* entries,
uint count,
out uint entriesRemoved,
uint milisecondsTimeout,
bool alertable
);
public struct OverlappedEntry
{
public IntPtr CompletionKey;
public NativeOverlapped* Overlapped;
public UIntPtr Internal;
public uint NumberOfTransferredBytes;
}
BOOL WINAPI PostQueuedCompletionStatus(
_In_ HANDLE CompletionPort,
_In_ DWORD dwNumberOfBytesTransferred,
_In_ ULONG_PTR dwCompletionKey,
_In_opt_ LPOVERLAPPED lpOverlapped
);
[DllImport("kernel32.dll")]
public static extern bool PostQueuedCompletionStatus(
IntPtr completionPort,
uint numberOfBytes,
IntPtr completionKey,
NativeOverlapped* overlapped
);
// assumes no errors
var sender = new IntPtr(1);
var overlapped = stackalloc NativeOverlapped[1];
var port = NativeMethods.CreateIoCompletionPort(
InvalidHandle, IntPtr.Zero, IntPtr.Zero, 1);
NativeMethods.PostQueuedCompletionStatus(
port, (uint) 1, sender, overlapped);
uint byteCount;
IntPtr completionKey;
Overlapped* resultOverlapped;
uint timeoutInMiliseconds = 1;
NativeMethods.GetQueuedCompletionStatus(
port, out byteCount, out completionKey,
out resultOverlapped, timeoutInMiliseconds);
Szymon Kulec