| 1 | /* $Id: overlappedio.cpp,v 1.15 2002-06-10 17:46:53 sandervl Exp $ */
|
|---|
| 2 |
|
|---|
| 3 | /*
|
|---|
| 4 | * Win32 overlapped IO class
|
|---|
| 5 | *
|
|---|
| 6 | * Copyright 2001 Sander van Leeuwen <sandervl@xs4all.nl>
|
|---|
| 7 | *
|
|---|
| 8 | * Project Odin Software License can be found in LICENSE.TXT
|
|---|
| 9 | *
|
|---|
| 10 | */
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 | #include <os2win.h>
|
|---|
| 15 | #include <string.h>
|
|---|
| 16 | #include <handlemanager.h>
|
|---|
| 17 | #include <heapstring.h>
|
|---|
| 18 | #include <overlappedio.h>
|
|---|
| 19 | #include "oslibdos.h"
|
|---|
| 20 |
|
|---|
| 21 | #define DBG_LOCALLOG DBG_overlappedio
|
|---|
| 22 | #include "dbglocal.h"
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 | //******************************************************************************
|
|---|
| 26 | //******************************************************************************
|
|---|
| 27 | OverlappedIOHandler::OverlappedIOHandler(LPOVERLAPPED_HANDLER lpReadHandler,
|
|---|
| 28 | LPOVERLAPPED_HANDLER lpWriteHandler,
|
|---|
| 29 | LPOVERLAPPED_HANDLER lpPollHandler,
|
|---|
| 30 | BOOL fFullDuplex) :
|
|---|
| 31 | hThreadRead(0), hThreadWrite(0), hThreadPoll(0)
|
|---|
| 32 | {
|
|---|
| 33 | OverlappedIOError errcode = OutOfMemory;
|
|---|
| 34 |
|
|---|
| 35 | this->fFullDuplex = fFullDuplex;
|
|---|
| 36 |
|
|---|
| 37 | if(lpReadHandler == NULL) {
|
|---|
| 38 | throw(InvalidParameter);
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | pending[ASYNC_INDEX_READ] = pending[ASYNC_INDEX_WRITE] = NULL;
|
|---|
| 42 | pending [ASYNC_INDEX_POLL] = pending [ASYNC_INDEX_BUSY] = NULL;
|
|---|
| 43 |
|
|---|
| 44 | this->lpReadHandler = lpReadHandler;
|
|---|
| 45 | this->lpWriteHandler = lpWriteHandler;
|
|---|
| 46 | this->lpPollHandler = lpPollHandler;
|
|---|
| 47 |
|
|---|
| 48 | ::InitializeCriticalSection(&critsect);
|
|---|
| 49 | //poll, read & write event semaphores are auto-reset (one thread wakes up
|
|---|
| 50 | //after a SetEvent call)
|
|---|
| 51 | hEventPoll = ::CreateEventA(NULL, FALSE, FALSE, NULL);
|
|---|
| 52 | hEventRead = ::CreateEventA(NULL, FALSE, FALSE, NULL);
|
|---|
| 53 | hEventWrite = ::CreateEventA(NULL, FALSE, FALSE, NULL);
|
|---|
| 54 |
|
|---|
| 55 | dprintf(("OverlappedIOThread: hEventRead %x hEventWrite %x hEventPoll %x", hEventRead, hEventWrite, hEventPoll));
|
|---|
| 56 |
|
|---|
| 57 | //the exit & cancel event semaphores are manual reset, because these events
|
|---|
| 58 | //must be able to wake up multiple threads
|
|---|
| 59 | hEventExit = ::CreateEventA(NULL, TRUE, FALSE, NULL);
|
|---|
| 60 | hEventCancel = ::CreateEventA(NULL, TRUE, FALSE, NULL);
|
|---|
| 61 | if(!hEventPoll || !hEventRead || !hEventWrite || !hEventExit || !hEventCancel)
|
|---|
| 62 | {
|
|---|
| 63 | DebugInt3();
|
|---|
| 64 | errcode = EventCreationFailed;
|
|---|
| 65 | goto failed;
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | DWORD dwThreadId;
|
|---|
| 69 | LPOVERLAPPED_THREAD_PARAM threadparam;
|
|---|
| 70 |
|
|---|
| 71 | dwAsyncType = (lpWriteHandler && fFullDuplex) ? ASYNCIO_READ : ASYNCIO_READWRITE;
|
|---|
| 72 | threadparam = (LPOVERLAPPED_THREAD_PARAM)malloc(sizeof(OVERLAPPED_THREAD_PARAM));
|
|---|
| 73 | if(!threadparam) goto outofmem;
|
|---|
| 74 | threadparam->dwOperation = dwAsyncType;
|
|---|
| 75 | threadparam->lpOverlappedObj = this;
|
|---|
| 76 | hThreadRead = ::CreateThread(NULL, 32*1024, OverlappedIOThread, (LPVOID)threadparam, 0, &dwThreadId);
|
|---|
| 77 |
|
|---|
| 78 | if(lpWriteHandler && fFullDuplex) {
|
|---|
| 79 | dwAsyncType |= ASYNCIO_WRITE;
|
|---|
| 80 |
|
|---|
| 81 | threadparam = (LPOVERLAPPED_THREAD_PARAM)malloc(sizeof(OVERLAPPED_THREAD_PARAM));
|
|---|
| 82 | if(!threadparam) goto outofmem;
|
|---|
| 83 | threadparam->dwOperation = ASYNCIO_WRITE;
|
|---|
| 84 | threadparam->lpOverlappedObj = this;
|
|---|
| 85 | hThreadWrite = ::CreateThread(NULL, 32*1024, OverlappedIOThread, (LPVOID)threadparam, 0, &dwThreadId);
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | if(lpPollHandler) {
|
|---|
| 89 | dwAsyncType |= ASYNCIO_POLL;
|
|---|
| 90 |
|
|---|
| 91 | threadparam = (LPOVERLAPPED_THREAD_PARAM)malloc(sizeof(OVERLAPPED_THREAD_PARAM));
|
|---|
| 92 | if(!threadparam) goto outofmem;
|
|---|
| 93 | threadparam->dwOperation = ASYNCIO_POLL;
|
|---|
| 94 | threadparam->lpOverlappedObj = this;
|
|---|
| 95 | hThreadPoll = ::CreateThread(NULL, 32*1024, OverlappedIOThread, (LPVOID)threadparam, 0, &dwThreadId);
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | if((lpPollHandler && !hThreadPoll) || !hThreadRead || (lpWriteHandler && fFullDuplex && !hThreadWrite))
|
|---|
| 99 | {
|
|---|
| 100 | DebugInt3();
|
|---|
| 101 | errcode = ThreadCreationFailed;
|
|---|
| 102 | goto failed;
|
|---|
| 103 | }
|
|---|
| 104 | return;
|
|---|
| 105 |
|
|---|
| 106 | outofmem:
|
|---|
| 107 | errcode = OutOfMemory;
|
|---|
| 108 | //fall through
|
|---|
| 109 | failed:
|
|---|
| 110 | //SvL: NOTE: We might not fail gracefully when threads have already been
|
|---|
| 111 | // created. (thread accessing memory that has been freed)
|
|---|
| 112 | // Don't feel like wasting time to fix this as this should never
|
|---|
| 113 | // happen anyway.
|
|---|
| 114 | if(hEventExit) {
|
|---|
| 115 | ::SetEvent(hEventExit);
|
|---|
| 116 | ::CloseHandle(hEventExit);
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | if(hEventRead) ::CloseHandle(hEventRead);
|
|---|
| 120 | if(hEventWrite) ::CloseHandle(hEventWrite);
|
|---|
| 121 | if(hEventPoll) ::CloseHandle(hEventPoll);
|
|---|
| 122 |
|
|---|
| 123 | if(hThreadRead) ::CloseHandle(hThreadRead);
|
|---|
| 124 | if(hThreadPoll) ::CloseHandle(hThreadPoll);
|
|---|
| 125 | if(hThreadWrite) ::CloseHandle(hThreadWrite);
|
|---|
| 126 | ::DeleteCriticalSection(&critsect);
|
|---|
| 127 |
|
|---|
| 128 | throw(errcode);
|
|---|
| 129 | }
|
|---|
| 130 | //******************************************************************************
|
|---|
| 131 | //******************************************************************************
|
|---|
| 132 | OverlappedIOHandler::~OverlappedIOHandler()
|
|---|
| 133 | {
|
|---|
| 134 | dprintf(("~OverlappedIOHandler: signalling overlapped threads"));
|
|---|
| 135 | ::SetEvent(hEventExit);
|
|---|
| 136 |
|
|---|
| 137 | ::CloseHandle(hEventExit);
|
|---|
| 138 | ::CloseHandle(hEventRead);
|
|---|
| 139 | ::CloseHandle(hEventWrite);
|
|---|
| 140 | ::CloseHandle(hEventPoll);
|
|---|
| 141 |
|
|---|
| 142 | ::CloseHandle(hThreadRead);
|
|---|
| 143 | if(hThreadPoll) ::CloseHandle(hThreadPoll);
|
|---|
| 144 | if(hThreadWrite) ::CloseHandle(hThreadWrite);
|
|---|
| 145 |
|
|---|
| 146 | DeleteCriticalSection(&critsect);
|
|---|
| 147 | }
|
|---|
| 148 | //******************************************************************************
|
|---|
| 149 | //******************************************************************************
|
|---|
| 150 | DWORD CALLBACK OverlappedIOThread(LPVOID lpThreadParam)
|
|---|
| 151 | {
|
|---|
| 152 | LPOVERLAPPED_THREAD_PARAM threadparam = (LPOVERLAPPED_THREAD_PARAM)lpThreadParam;
|
|---|
| 153 | DWORD dwOperation;
|
|---|
| 154 | OverlappedIOHandler *lpOverlappedObj;
|
|---|
| 155 |
|
|---|
| 156 | if(threadparam == NULL) {
|
|---|
| 157 | DebugInt3();
|
|---|
| 158 | return 0;
|
|---|
| 159 | }
|
|---|
| 160 | lpOverlappedObj = threadparam->lpOverlappedObj;
|
|---|
| 161 | dwOperation = threadparam->dwOperation;
|
|---|
| 162 | //free thread parameter first
|
|---|
| 163 | free(threadparam);
|
|---|
| 164 |
|
|---|
| 165 | return lpOverlappedObj->threadHandler(dwOperation);
|
|---|
| 166 | }
|
|---|
| 167 | //******************************************************************************
|
|---|
| 168 | //******************************************************************************
|
|---|
| 169 | DWORD OverlappedIOHandler::threadHandler(DWORD dwOperation)
|
|---|
| 170 | {
|
|---|
| 171 | LPASYNCIOREQUEST lpRequest;
|
|---|
| 172 | LPOVERLAPPED lpOverlapped;
|
|---|
| 173 | HANDLE hEvents[2];
|
|---|
| 174 | HANDLE hEventsWait[2];
|
|---|
| 175 | HANDLE hHandle;
|
|---|
| 176 | DWORD ret, dwTimeOut, dwResult;
|
|---|
| 177 | int index;
|
|---|
| 178 |
|
|---|
| 179 | dprintf(("OverlappedIOThread: started for event %d", dwOperation));
|
|---|
| 180 | switch(dwOperation) {
|
|---|
| 181 | case ASYNCIO_READ:
|
|---|
| 182 | case ASYNCIO_READWRITE:
|
|---|
| 183 | hEvents[0] = hEventRead;
|
|---|
| 184 | index = ASYNC_INDEX_READ;
|
|---|
| 185 | break;
|
|---|
| 186 |
|
|---|
| 187 | case ASYNCIO_WRITE:
|
|---|
| 188 | hEvents[0] = hEventWrite;
|
|---|
| 189 | index = ASYNC_INDEX_WRITE;
|
|---|
| 190 | break;
|
|---|
| 191 |
|
|---|
| 192 | case ASYNCIO_POLL:
|
|---|
| 193 | hEvents[0] = hEventPoll;
|
|---|
| 194 | index = ASYNC_INDEX_POLL;
|
|---|
| 195 | break;
|
|---|
| 196 | default:
|
|---|
| 197 | DebugInt3();
|
|---|
| 198 | }
|
|---|
| 199 | hEvents[1] = hEventExit;
|
|---|
| 200 |
|
|---|
| 201 | while(TRUE)
|
|---|
| 202 | {
|
|---|
| 203 | ret = WaitForMultipleObjects(2, hEvents, FALSE, INFINITE);
|
|---|
| 204 | if(ret == WAIT_FAILED) {
|
|---|
| 205 | dprintf(("!WARNING!: WaitForMultipleObjects -> WAIT_FAILED!"));
|
|---|
| 206 | break;
|
|---|
| 207 | }
|
|---|
| 208 | //if hEventExit has been signalled, then we are told to exit
|
|---|
| 209 | if(ret == (WAIT_OBJECT_0+1)) {
|
|---|
| 210 | dprintf(("end of threadHandler signalled"));
|
|---|
| 211 | break;
|
|---|
| 212 | }
|
|---|
| 213 | //process all pending jobs
|
|---|
| 214 | while(TRUE)
|
|---|
| 215 | {
|
|---|
| 216 | ::EnterCriticalSection(&critsect);
|
|---|
| 217 | if(pending[index] == NULL) {
|
|---|
| 218 | ::LeaveCriticalSection(&critsect);
|
|---|
| 219 | break;
|
|---|
| 220 | }
|
|---|
| 221 | lpRequest = pending[index];
|
|---|
| 222 | pending[index] = lpRequest->next;
|
|---|
| 223 |
|
|---|
| 224 | //add to in process list
|
|---|
| 225 | lpRequest->next = pending[ASYNC_INDEX_BUSY];
|
|---|
| 226 | pending[ASYNC_INDEX_BUSY] = lpRequest;
|
|---|
| 227 | ::LeaveCriticalSection(&critsect);
|
|---|
| 228 |
|
|---|
| 229 | lpOverlapped = lpRequest->lpOverlapped;;
|
|---|
| 230 | hHandle = lpRequest->hHandle;
|
|---|
| 231 |
|
|---|
| 232 | switch(dwOperation) {
|
|---|
| 233 | case ASYNCIO_READ:
|
|---|
| 234 | case ASYNCIO_READWRITE:
|
|---|
| 235 | case ASYNCIO_WRITE:
|
|---|
| 236 | if(lpRequest->dwAsyncType == ASYNCIO_READ || lpWriteHandler == NULL) {
|
|---|
| 237 | lpRequest->dwLastError = lpReadHandler(lpRequest, &dwResult, NULL);
|
|---|
| 238 | }
|
|---|
| 239 | else lpRequest->dwLastError = lpWriteHandler(lpRequest, &dwResult, NULL);
|
|---|
| 240 |
|
|---|
| 241 | if(!lpRequest->fCancelled)
|
|---|
| 242 | {
|
|---|
| 243 | lpOverlapped->Internal = lpRequest->dwLastError;
|
|---|
| 244 | lpOverlapped->InternalHigh = dwResult;
|
|---|
| 245 |
|
|---|
| 246 | //must NOT store result in specified location!!! (stack corruption)
|
|---|
| 247 | //if(lpRequest->lpdwResult) {
|
|---|
| 248 | // *lpRequest->lpdwResult = dwResult;
|
|---|
| 249 | //}
|
|---|
| 250 | #ifdef DEBUG
|
|---|
| 251 | if(lpRequest->dwAsyncType == ASYNCIO_READ) {
|
|---|
| 252 | dprintf(("ASYNCIO_READ %x finished; result %x, last error %d", lpOverlapped, dwResult, lpRequest->dwLastError));
|
|---|
| 253 | }
|
|---|
| 254 | else dprintf(("ASYNCIO_WRITE %x finished; result %x, last error %d", lpOverlapped, dwResult, lpRequest->dwLastError));
|
|---|
| 255 | #endif
|
|---|
| 256 | //wake up user thread
|
|---|
| 257 | ::SetEvent(lpOverlapped->hEvent);
|
|---|
| 258 | }
|
|---|
| 259 | break;
|
|---|
| 260 |
|
|---|
| 261 | case ASYNCIO_POLL:
|
|---|
| 262 | hEventsWait[0] = hEventCancel;
|
|---|
| 263 | hEventsWait[1] = hEventExit;
|
|---|
| 264 | ret = WAIT_TIMEOUT;
|
|---|
| 265 | while(TRUE)
|
|---|
| 266 | {
|
|---|
| 267 | dwTimeOut = 0;
|
|---|
| 268 | lpRequest->dwLastError = lpPollHandler(lpRequest, &dwResult, &dwTimeOut);
|
|---|
| 269 | if(lpRequest->dwLastError != ERROR_IO_PENDING) {
|
|---|
| 270 | break;
|
|---|
| 271 | }
|
|---|
| 272 | if(dwTimeOut == 0) {
|
|---|
| 273 | dprintf(("!ERROR!: lpPollHandler returned timeout 0!!"));
|
|---|
| 274 | DebugInt3();
|
|---|
| 275 | break;
|
|---|
| 276 | }
|
|---|
| 277 | //sleep a while to avoid wasting too many cpu cycles; we are woken up when a timeout occurs,
|
|---|
| 278 | //when the operation is cancelled or when the process exits
|
|---|
| 279 | ret = WaitForMultipleObjects(2, hEventsWait, FALSE, dwTimeOut);
|
|---|
| 280 | if(ret != WAIT_TIMEOUT) {
|
|---|
| 281 | dprintf(("ASYNCIO_POLL: WaitForSingleObject didn't time out, abort (ret = %x)", ret));
|
|---|
| 282 | break;
|
|---|
| 283 | }
|
|---|
| 284 | }
|
|---|
| 285 | //Don't access the overlapped & result memory when CancelIo was used to cancel the operation
|
|---|
| 286 | if(ret == WAIT_TIMEOUT && !lpRequest->fCancelled)
|
|---|
| 287 | {
|
|---|
| 288 | dprintf(("ASYNCIO_POLL %x: result %x, last error %d", lpOverlapped, dwResult, lpRequest->dwLastError));
|
|---|
| 289 | lpOverlapped->Internal = lpRequest->dwLastError;
|
|---|
| 290 | lpOverlapped->InternalHigh = dwResult;
|
|---|
| 291 | if(lpRequest->lpdwResult) {
|
|---|
| 292 | *lpRequest->lpdwResult = dwResult;
|
|---|
| 293 | }
|
|---|
| 294 | //wake up user thread
|
|---|
| 295 | ::SetEvent(lpOverlapped->hEvent);
|
|---|
| 296 | }
|
|---|
| 297 | break;
|
|---|
| 298 | }
|
|---|
| 299 | //remove from in-process list and delete async request object
|
|---|
| 300 | findAndRemoveRequest(ASYNC_INDEX_BUSY, hHandle);
|
|---|
| 301 | delete lpRequest;
|
|---|
| 302 | } //while(TRUE)
|
|---|
| 303 | }
|
|---|
| 304 | return 0;
|
|---|
| 305 | }
|
|---|
| 306 | //******************************************************************************
|
|---|
| 307 | //******************************************************************************
|
|---|
| 308 | BOOL OverlappedIOHandler::WriteFile(HANDLE hHandle,
|
|---|
| 309 | LPCVOID lpBuffer,
|
|---|
| 310 | DWORD nNumberOfBytesToWrite,
|
|---|
| 311 | LPDWORD lpNumberOfBytesWritten,
|
|---|
| 312 | LPOVERLAPPED lpOverlapped,
|
|---|
| 313 | LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine,
|
|---|
| 314 | DWORD dwUserData,
|
|---|
| 315 | DWORD dwTimeOut)
|
|---|
| 316 | {
|
|---|
| 317 | LPASYNCIOREQUEST lpRequest, current;
|
|---|
| 318 | int index;
|
|---|
| 319 |
|
|---|
| 320 | if(!lpOverlapped || lpOverlapped->hEvent == 0) {
|
|---|
| 321 | ::SetLastError(ERROR_INVALID_PARAMETER);
|
|---|
| 322 | return FALSE;
|
|---|
| 323 | }
|
|---|
| 324 |
|
|---|
| 325 | lpRequest = new ASYNCIOREQUEST;
|
|---|
| 326 | if(lpRequest == NULL) {
|
|---|
| 327 | ::SetLastError(ERROR_NOT_ENOUGH_MEMORY);
|
|---|
| 328 | return FALSE;
|
|---|
| 329 | }
|
|---|
| 330 | lpRequest->dwAsyncType = ASYNCIO_WRITE;
|
|---|
| 331 | lpRequest->hHandle = hHandle;
|
|---|
| 332 | lpRequest->lpBuffer = lpBuffer;
|
|---|
| 333 | lpRequest->nNumberOfBytes = nNumberOfBytesToWrite;
|
|---|
| 334 | //must NOT store result in specified location!!! (stack corruption)
|
|---|
| 335 | //// lpRequest->lpdwResult = lpNumberOfBytesWritten;
|
|---|
| 336 | lpRequest->lpdwResult = NULL;
|
|---|
| 337 | lpRequest->lpOverlapped = lpOverlapped;
|
|---|
| 338 | lpRequest->lpCompletionRoutine = lpCompletionRoutine;
|
|---|
| 339 | lpRequest->dwUserData = dwUserData;
|
|---|
| 340 | lpRequest->dwTimeOut = dwTimeOut;
|
|---|
| 341 | lpRequest->next = NULL;
|
|---|
| 342 |
|
|---|
| 343 | if(dwAsyncType & ASYNCIO_READWRITE) {
|
|---|
| 344 | index = ASYNC_INDEX_READ;
|
|---|
| 345 | }
|
|---|
| 346 | else index = ASYNC_INDEX_WRITE;
|
|---|
| 347 |
|
|---|
| 348 | ::EnterCriticalSection(&critsect);
|
|---|
| 349 | if(pending[index]) {
|
|---|
| 350 | current = pending[index];
|
|---|
| 351 | while(current->next) {
|
|---|
| 352 | current = current->next;
|
|---|
| 353 | }
|
|---|
| 354 | current->next = lpRequest;
|
|---|
| 355 | }
|
|---|
| 356 | else pending[index] = lpRequest;
|
|---|
| 357 | ::LeaveCriticalSection(&critsect);
|
|---|
| 358 |
|
|---|
| 359 | lpOverlapped->Internal = STATUS_PENDING;
|
|---|
| 360 | lpOverlapped->InternalHigh = 0;
|
|---|
| 361 | //reset overlapped semaphore to non-signalled
|
|---|
| 362 | ::ResetEvent(lpOverlapped->hEvent);
|
|---|
| 363 |
|
|---|
| 364 | //wake up async thread
|
|---|
| 365 | ::SetEvent((dwAsyncType & ASYNCIO_READWRITE) ? hEventRead : hEventWrite);
|
|---|
| 366 |
|
|---|
| 367 | ::SetLastError(ERROR_IO_PENDING);
|
|---|
| 368 | return FALSE;
|
|---|
| 369 | }
|
|---|
| 370 | //******************************************************************************
|
|---|
| 371 | //******************************************************************************
|
|---|
| 372 | BOOL OverlappedIOHandler::ReadFile(HANDLE hHandle,
|
|---|
| 373 | LPCVOID lpBuffer,
|
|---|
| 374 | DWORD nNumberOfBytesToRead,
|
|---|
| 375 | LPDWORD lpNumberOfBytesRead,
|
|---|
| 376 | LPOVERLAPPED lpOverlapped,
|
|---|
| 377 | LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine,
|
|---|
| 378 | DWORD dwUserData,
|
|---|
| 379 | DWORD dwTimeOut)
|
|---|
| 380 | {
|
|---|
| 381 | LPASYNCIOREQUEST lpRequest, current;
|
|---|
| 382 |
|
|---|
| 383 | if(!lpOverlapped || lpOverlapped->hEvent == 0) {
|
|---|
| 384 | ::SetLastError(ERROR_INVALID_PARAMETER);
|
|---|
| 385 | return FALSE;
|
|---|
| 386 | }
|
|---|
| 387 |
|
|---|
| 388 | lpRequest = new ASYNCIOREQUEST;
|
|---|
| 389 | if(lpRequest == NULL) {
|
|---|
| 390 | ::SetLastError(ERROR_NOT_ENOUGH_MEMORY);
|
|---|
| 391 | return FALSE;
|
|---|
| 392 | }
|
|---|
| 393 | lpRequest->dwAsyncType = ASYNCIO_READ;
|
|---|
| 394 | lpRequest->hHandle = hHandle;
|
|---|
| 395 | lpRequest->lpBuffer = lpBuffer;
|
|---|
| 396 | lpRequest->nNumberOfBytes = nNumberOfBytesToRead;
|
|---|
| 397 | //must NOT store result in specified location!!! (stack corruption)
|
|---|
| 398 | //// lpRequest->lpdwResult = lpNumberOfBytesRead;
|
|---|
| 399 | lpRequest->lpdwResult = NULL;
|
|---|
| 400 | lpRequest->lpOverlapped = lpOverlapped;
|
|---|
| 401 | lpRequest->lpCompletionRoutine = lpCompletionRoutine;
|
|---|
| 402 | lpRequest->dwUserData = dwUserData;
|
|---|
| 403 | lpRequest->dwTimeOut = dwTimeOut;
|
|---|
| 404 | lpRequest->next = NULL;
|
|---|
| 405 |
|
|---|
| 406 | ::EnterCriticalSection(&critsect);
|
|---|
| 407 | if(pending[ASYNC_INDEX_READ]) {
|
|---|
| 408 | current = pending[ASYNC_INDEX_READ];
|
|---|
| 409 | while(current->next) {
|
|---|
| 410 | current = current->next;
|
|---|
| 411 | }
|
|---|
| 412 | current->next = lpRequest;
|
|---|
| 413 | }
|
|---|
| 414 | else pending[ASYNC_INDEX_READ] = lpRequest;
|
|---|
| 415 | ::LeaveCriticalSection(&critsect);
|
|---|
| 416 |
|
|---|
| 417 | lpOverlapped->Internal = STATUS_PENDING;
|
|---|
| 418 | lpOverlapped->InternalHigh = 0;
|
|---|
| 419 | //reset overlapped semaphore to non-signalled
|
|---|
| 420 | ::ResetEvent(lpOverlapped->hEvent);
|
|---|
| 421 |
|
|---|
| 422 | //wake up async thread
|
|---|
| 423 | ::SetEvent(hEventRead);
|
|---|
| 424 | ::SetLastError(ERROR_IO_PENDING);
|
|---|
| 425 | return FALSE;
|
|---|
| 426 | }
|
|---|
| 427 | //******************************************************************************
|
|---|
| 428 | //******************************************************************************
|
|---|
| 429 | BOOL OverlappedIOHandler::WaitForEvent(HANDLE hHandle,
|
|---|
| 430 | DWORD dwEventMask,
|
|---|
| 431 | LPDWORD lpfdwEvtMask,
|
|---|
| 432 | LPOVERLAPPED lpOverlapped,
|
|---|
| 433 | LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine,
|
|---|
| 434 | DWORD dwUserData,
|
|---|
| 435 | DWORD dwTimeOut)
|
|---|
| 436 | {
|
|---|
| 437 | LPASYNCIOREQUEST lpRequest, current;
|
|---|
| 438 | DWORD dwLastError, dwResult;
|
|---|
| 439 |
|
|---|
| 440 | if(!lpOverlapped || lpOverlapped->hEvent == 0) {
|
|---|
| 441 | ::SetLastError(ERROR_INVALID_PARAMETER);
|
|---|
| 442 | return FALSE;
|
|---|
| 443 | }
|
|---|
| 444 | if(!lpPollHandler) {
|
|---|
| 445 | DebugInt3();
|
|---|
| 446 | ::SetLastError(ERROR_INVALID_PARAMETER);
|
|---|
| 447 | return FALSE;
|
|---|
| 448 | }
|
|---|
| 449 |
|
|---|
| 450 | lpRequest = new ASYNCIOREQUEST;
|
|---|
| 451 | if(lpRequest == NULL) {
|
|---|
| 452 | ::SetLastError(ERROR_NOT_ENOUGH_MEMORY);
|
|---|
| 453 | return FALSE;
|
|---|
| 454 | }
|
|---|
| 455 | lpRequest->dwAsyncType = ASYNCIO_POLL;
|
|---|
| 456 | lpRequest->hHandle = hHandle;
|
|---|
| 457 | lpRequest->lpBuffer = NULL;
|
|---|
| 458 | lpRequest->nNumberOfBytes = 0;
|
|---|
| 459 | //must store result also in specified location
|
|---|
| 460 | lpRequest->lpdwResult = lpfdwEvtMask;
|
|---|
| 461 | lpRequest->lpOverlapped = lpOverlapped;
|
|---|
| 462 | lpRequest->lpCompletionRoutine = lpCompletionRoutine;
|
|---|
| 463 | lpRequest->dwUserData = dwUserData;
|
|---|
| 464 | lpRequest->dwTimeOut = dwTimeOut;
|
|---|
| 465 | lpRequest->dwEventMask = dwEventMask;
|
|---|
| 466 | lpRequest->next = NULL;
|
|---|
| 467 |
|
|---|
| 468 | lpOverlapped->Internal = STATUS_PENDING;
|
|---|
| 469 | lpOverlapped->InternalHigh = 0;
|
|---|
| 470 | //reset overlapped semaphore to non-signalled
|
|---|
| 471 | ::ResetEvent(lpOverlapped->hEvent);
|
|---|
| 472 |
|
|---|
| 473 | //first check if the event has already occured; if so, return result
|
|---|
| 474 | //immediately
|
|---|
| 475 | dwLastError = lpPollHandler(lpRequest, &dwResult, &dwTimeOut);
|
|---|
| 476 | if(dwLastError != ERROR_IO_PENDING)
|
|---|
| 477 | {
|
|---|
| 478 | dprintf(("OverlappedIOHandler::WaitForEvent %x: result %x, last error %d", lpOverlapped, dwResult, dwLastError));
|
|---|
| 479 | lpOverlapped->Internal = dwLastError;
|
|---|
| 480 | lpOverlapped->InternalHigh = dwResult;
|
|---|
| 481 | if(lpfdwEvtMask) {
|
|---|
| 482 | *lpfdwEvtMask = dwResult;
|
|---|
| 483 | }
|
|---|
| 484 | //wake up user thread
|
|---|
| 485 | ::SetEvent(lpOverlapped->hEvent);
|
|---|
| 486 |
|
|---|
| 487 | delete lpRequest;
|
|---|
| 488 | ::SetLastError(dwLastError);
|
|---|
| 489 | return (dwLastError == ERROR_SUCCESS);
|
|---|
| 490 | }
|
|---|
| 491 |
|
|---|
| 492 | ::EnterCriticalSection(&critsect);
|
|---|
| 493 | if(pending[ASYNC_INDEX_POLL]) {
|
|---|
| 494 | current = pending[ASYNC_INDEX_READ];
|
|---|
| 495 | while(current->next) {
|
|---|
| 496 | current = current->next;
|
|---|
| 497 | }
|
|---|
| 498 | current->next = lpRequest;
|
|---|
| 499 | }
|
|---|
| 500 | else pending[ASYNC_INDEX_POLL] = lpRequest;
|
|---|
| 501 | ::LeaveCriticalSection(&critsect);
|
|---|
| 502 |
|
|---|
| 503 | //wake up async thread
|
|---|
| 504 | ::SetEvent(hEventPoll);
|
|---|
| 505 | ::SetLastError(ERROR_IO_PENDING);
|
|---|
| 506 | return FALSE;
|
|---|
| 507 | }
|
|---|
| 508 | //******************************************************************************
|
|---|
| 509 | //******************************************************************************
|
|---|
| 510 | BOOL OverlappedIOHandler::CancelIo(HANDLE hHandle)
|
|---|
| 511 | {
|
|---|
| 512 | LPASYNCIOREQUEST lpRequest;
|
|---|
| 513 |
|
|---|
| 514 | for(int i=ASYNC_INDEX_READ;i<NR_ASYNC_OPERATIONS;i++)
|
|---|
| 515 | {
|
|---|
| 516 | while(TRUE)
|
|---|
| 517 | {
|
|---|
| 518 | lpRequest = findAndRemoveRequest(i, hHandle);
|
|---|
| 519 |
|
|---|
| 520 | if(lpRequest) {
|
|---|
| 521 | //TODO: This doesn't work if multiple handles share the
|
|---|
| 522 | // same OverlappedIOHandler
|
|---|
| 523 | lpRequest->fCancelled = TRUE;
|
|---|
| 524 | ::SetEvent(hEventCancel); //cancel pending operation
|
|---|
| 525 | if(i != ASYNC_INDEX_BUSY) {//thread that handles the request will delete it
|
|---|
| 526 | delete lpRequest;
|
|---|
| 527 | }
|
|---|
| 528 | }
|
|---|
| 529 | else break;
|
|---|
| 530 | }
|
|---|
| 531 | }
|
|---|
| 532 | //TODO: return error if there were no pending requests???
|
|---|
| 533 | ::SetLastError(ERROR_SUCCESS);
|
|---|
| 534 | return TRUE;
|
|---|
| 535 | }
|
|---|
| 536 | //******************************************************************************
|
|---|
| 537 | //******************************************************************************
|
|---|
| 538 | BOOL OverlappedIOHandler::GetOverlappedResult(HANDLE hHandle,
|
|---|
| 539 | LPOVERLAPPED lpOverlapped,
|
|---|
| 540 | LPDWORD lpcbTransfer,
|
|---|
| 541 | BOOL fWait)
|
|---|
| 542 | {
|
|---|
| 543 | DWORD ret;
|
|---|
| 544 |
|
|---|
| 545 | ret = ::WaitForSingleObject(lpOverlapped->hEvent, (fWait) ? INFINITE : 0);
|
|---|
| 546 |
|
|---|
| 547 | if(lpcbTransfer)
|
|---|
| 548 | *lpcbTransfer = lpOverlapped->InternalHigh;
|
|---|
| 549 |
|
|---|
| 550 | ::SetLastError(lpOverlapped->Internal);
|
|---|
| 551 |
|
|---|
| 552 | dprintf(("GetOverlappedResult %x -> result %d last error %d", hHandle, lpOverlapped->InternalHigh, lpOverlapped->Internal));
|
|---|
| 553 | return (ret == WAIT_OBJECT_0);
|
|---|
| 554 | }
|
|---|
| 555 | //******************************************************************************
|
|---|
| 556 | //******************************************************************************
|
|---|
| 557 | LPASYNCIOREQUEST OverlappedIOHandler::findAndRemoveRequest(int index, HANDLE hHandle)
|
|---|
| 558 | {
|
|---|
| 559 | LPASYNCIOREQUEST lpRequest, lpFound = NULL;
|
|---|
| 560 |
|
|---|
| 561 | ::EnterCriticalSection(&critsect);
|
|---|
| 562 | if(pending[index])
|
|---|
| 563 | {
|
|---|
| 564 | if(pending[index]->hHandle != hHandle)
|
|---|
| 565 | {
|
|---|
| 566 | lpRequest = pending[index];
|
|---|
| 567 | while(lpRequest->next) {
|
|---|
| 568 | if(lpRequest->next->hHandle == hHandle) {
|
|---|
| 569 | lpFound = lpRequest->next;
|
|---|
| 570 | lpRequest->next = lpFound->next;
|
|---|
| 571 | break;
|
|---|
| 572 | }
|
|---|
| 573 | lpRequest = lpRequest->next;
|
|---|
| 574 | }
|
|---|
| 575 | }
|
|---|
| 576 | else {
|
|---|
| 577 | lpFound = pending[index];
|
|---|
| 578 | pending[index] = lpFound->next;
|
|---|
| 579 | }
|
|---|
| 580 | }
|
|---|
| 581 | ::LeaveCriticalSection(&critsect);
|
|---|
| 582 | return lpFound;
|
|---|
| 583 | }
|
|---|
| 584 | //******************************************************************************
|
|---|
| 585 | //******************************************************************************
|
|---|
| 586 |
|
|---|