From 9dc8b796e59494af7d400a8f88e2e45927b098a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Ludwig?= Date: Sun, 11 Mar 2018 00:27:14 +0100 Subject: [PATCH] Get rid of OVERLAPPED_FILE. The hEvent member is currently unused for files. Using it for the file handle makes the OVERLAPPED_CORE struct sufficient. --- source/eventcore/drivers/winapi/core.d | 10 ++-------- source/eventcore/drivers/winapi/files.d | 21 ++++++++++----------- 2 files changed, 12 insertions(+), 19 deletions(-) diff --git a/source/eventcore/drivers/winapi/core.d b/source/eventcore/drivers/winapi/core.d index ec5e2c5..f485e9f 100644 --- a/source/eventcore/drivers/winapi/core.d +++ b/source/eventcore/drivers/winapi/core.d @@ -225,7 +225,7 @@ private struct HandleSlot { package struct FileSlot { static struct Direction(bool RO) { - OVERLAPPED_FILE overlapped; + OVERLAPPED_CORE overlapped; FileIOCallback callback; ulong offset; size_t bytesTransferred; @@ -238,7 +238,7 @@ package struct FileSlot { auto cb = this.callback; this.callback = null; assert(cb !is null); - cb(overlapped.handle, status, bytes_transferred); + cb(cast(FileFD)cast(size_t)overlapped.hEvent, status, bytes_transferred); } } Direction!false read; @@ -259,12 +259,6 @@ package struct OVERLAPPED_CORE { WinAPIEventDriverCore driver; } -package struct OVERLAPPED_FILE { - OVERLAPPED_CORE core; - alias core this; - FileFD handle; -} - package struct IOEvent { void function(DWORD err, DWORD bts, OVERLAPPED_CORE*) @safe nothrow process; DWORD error; diff --git a/source/eventcore/drivers/winapi/files.d b/source/eventcore/drivers/winapi/files.d index 720aa7b..871e7ec 100644 --- a/source/eventcore/drivers/winapi/files.d +++ b/source/eventcore/drivers/winapi/files.d @@ -64,9 +64,9 @@ final class WinAPIEventDriverFiles : EventDriverFiles { auto s = m_core.setupSlot!FileSlot(handle); s.read.overlapped.driver = m_core; - s.read.overlapped.handle = FileFD(cast(size_t)handle); + s.read.overlapped.hEvent = handle; s.write.overlapped.driver = m_core; - s.write.overlapped.handle = FileFD(cast(size_t)handle); + s.write.overlapped.hEvent = handle; return FileFD(cast(size_t)handle); } @@ -75,9 +75,9 @@ final class WinAPIEventDriverFiles : EventDriverFiles { { auto h = idToHandle(file); auto slot = () @trusted { return &m_core.m_handles[h].file(); } (); - if (slot.read.overlapped.handle != FileFD.invalid) { + if (slot.read.overlapped.hEvent != INVALID_HANDLE_VALUE) { CloseHandle(h); - slot.read.overlapped.handle = slot.write.overlapped.handle = FileFD.invalid; + slot.read.overlapped.hEvent = slot.write.overlapped.hEvent = INVALID_HANDLE_VALUE; } } @@ -183,15 +183,14 @@ final class WinAPIEventDriverFiles : EventDriverFiles { } private static nothrow - void onIOFinished(alias fun, bool RO)(DWORD error, DWORD bytes_transferred, OVERLAPPED_CORE* _overlapped) + void onIOFinished(alias fun, bool RO)(DWORD error, DWORD bytes_transferred, OVERLAPPED_CORE* overlapped) { - auto ctx = () @trusted { return cast(OVERLAPPED_FILE*)_overlapped; } (); - FileFD id = ctx.handle; + FileFD id = cast(FileFD)cast(size_t)overlapped.hEvent; auto handle = idToHandle(id); static if (RO) - auto slot = () @trusted { return &ctx.driver.m_handles[handle].file.write; } (); + auto slot = () @trusted { return &overlapped.driver.m_handles[handle].file.write; } (); else - auto slot = () @trusted { return &ctx.driver.m_handles[handle].file.read; } (); + auto slot = () @trusted { return &overlapped.driver.m_handles[handle].file.read; } (); assert(slot !is null); if (!slot.callback) { @@ -200,7 +199,7 @@ final class WinAPIEventDriverFiles : EventDriverFiles { } if (error != 0) { - ctx.driver.removeWaiter(); + overlapped.driver.removeWaiter(); slot.invokeCallback(IOStatus.error, slot.bytesTransferred + bytes_transferred); return; } @@ -209,7 +208,7 @@ final class WinAPIEventDriverFiles : EventDriverFiles { slot.offset += bytes_transferred; if (slot.bytesTransferred >= slot.buffer.length || slot.mode != IOMode.all) { - ctx.driver.removeWaiter(); + overlapped.driver.removeWaiter(); slot.invokeCallback(IOStatus.ok, slot.bytesTransferred); } else { startIO!(fun, RO)(handle, slot);