From a1193adca1ddba0f6ac1c58154ae8377225063a3 Mon Sep 17 00:00:00 2001 From: LRN Date: Sat, 24 Jan 2015 23:01:22 +0000 Subject: [PATCH] Add support for selecting on W32 events --- src/include/gnunet_disk_lib.h | 5 +++++ src/util/disk.c | 33 +++++++++++++++++++++++++++------ src/util/network.c | 26 +++++++++++++++++++++----- 3 files changed, 53 insertions(+), 11 deletions(-) diff --git a/src/include/gnunet_disk_lib.h b/src/include/gnunet_disk_lib.h index f14285f89..56c6dca41 100644 --- a/src/include/gnunet_disk_lib.h +++ b/src/include/gnunet_disk_lib.h @@ -35,6 +35,11 @@ struct GNUNET_DISK_PipeHandle; */ enum GNUNET_FILE_Type { + /** + * Handle represents an event. + */ + GNUNET_DISK_HANLDE_TYPE_EVENT, + /** * Handle represents a file. */ diff --git a/src/util/disk.c b/src/util/disk.c index 6753d8141..25a01aba6 100644 --- a/src/util/disk.c +++ b/src/util/disk.c @@ -854,7 +854,7 @@ GNUNET_DISK_file_read (const struct GNUNET_DISK_FileHandle *h, #ifdef MINGW DWORD bytes_read; - if (h->type != GNUNET_DISK_HANLDE_TYPE_PIPE) + if (h->type == GNUNET_DISK_HANLDE_TYPE_FILE) { if (!ReadFile (h->h, result, len, &bytes_read, NULL)) { @@ -862,7 +862,7 @@ GNUNET_DISK_file_read (const struct GNUNET_DISK_FileHandle *h, return GNUNET_SYSERR; } } - else + else if (h->type == GNUNET_DISK_HANLDE_TYPE_PIPE) { if (!ReadFile (h->h, result, len, &bytes_read, h->oOverlapRead)) { @@ -877,6 +877,10 @@ GNUNET_DISK_file_read (const struct GNUNET_DISK_FileHandle *h, } LOG (GNUNET_ERROR_TYPE_DEBUG, "Read %u bytes from pipe\n", bytes_read); } + else + { + bytes_read = 0; + } return bytes_read; #else return read (h->fd, result, len); @@ -908,7 +912,7 @@ GNUNET_DISK_file_read_non_blocking (const struct GNUNET_DISK_FileHandle *h, #ifdef MINGW DWORD bytes_read; - if (h->type != GNUNET_DISK_HANLDE_TYPE_PIPE) + if (h->type == GNUNET_DISK_HANLDE_TYPE_FILE) { if (!ReadFile (h->h, result, len, &bytes_read, NULL)) { @@ -916,7 +920,7 @@ GNUNET_DISK_file_read_non_blocking (const struct GNUNET_DISK_FileHandle *h, return GNUNET_SYSERR; } } - else + else if (h->type == GNUNET_DISK_HANLDE_TYPE_PIPE) { if (!ReadFile (h->h, result, len, &bytes_read, h->oOverlapRead)) { @@ -937,6 +941,10 @@ GNUNET_DISK_file_read_non_blocking (const struct GNUNET_DISK_FileHandle *h, } LOG (GNUNET_ERROR_TYPE_DEBUG, "Read %u bytes\n", bytes_read); } + else + { + bytes_read = 0; + } return bytes_read; #else int flags; @@ -1005,7 +1013,7 @@ GNUNET_DISK_file_write (const struct GNUNET_DISK_FileHandle * h, #ifdef MINGW DWORD bytes_written; - if (h->type != GNUNET_DISK_HANLDE_TYPE_PIPE) + if (h->type == GNUNET_DISK_HANLDE_TYPE_FILE) { if (!WriteFile (h->h, buffer, n, &bytes_written, NULL)) { @@ -1013,7 +1021,7 @@ GNUNET_DISK_file_write (const struct GNUNET_DISK_FileHandle * h, return GNUNET_SYSERR; } } - else + else if (h->type == GNUNET_DISK_HANLDE_TYPE_PIPE) { LOG (GNUNET_ERROR_TYPE_DEBUG, "It is a pipe trying to write %u bytes\n", n); if (!WriteFile (h->h, buffer, n, &bytes_written, h->oOverlapWrite)) @@ -1062,6 +1070,10 @@ GNUNET_DISK_file_write (const struct GNUNET_DISK_FileHandle * h, } LOG (GNUNET_ERROR_TYPE_DEBUG, "Wrote %u bytes\n", bytes_written); } + else + { + bytes_written = 0; + } return bytes_written; #else return write (h->fd, buffer, n); @@ -1899,6 +1911,15 @@ GNUNET_DISK_get_handle_from_w32_handle (HANDLE osfh) case FILE_TYPE_PIPE: ftype = GNUNET_DISK_HANLDE_TYPE_PIPE; break; + case FILE_TYPE_UNKNOWN: + if (GetLastError () == NO_ERROR) + { + if (0 != ResetEvent (osfh)) + ftype = GNUNET_DISK_HANLDE_TYPE_EVENT; + } + else + return NULL; + break; default: return NULL; } diff --git a/src/util/network.c b/src/util/network.c index f2be1aab6..c2ba51d94 100644 --- a/src/util/network.c +++ b/src/util/network.c @@ -1725,15 +1725,26 @@ check_handles_status (struct GNUNET_NETWORK_FDSet *fds, int except) struct GNUNET_DISK_FileHandle *fh; unsigned int roff; unsigned int woff; - int is_pipe; for (woff = 0, roff = 0; roff < fds->handles_pos; roff++) { fh = fds->handles[roff]; - is_pipe = fh->type == GNUNET_DISK_HANLDE_TYPE_PIPE; - if ((except && is_pipe && pipe_except_ready (fh)) || - (!except && (!is_pipe || pipe_read_ready (fh)))) - fds->handles[woff++] = fh; + if (fh->type == GNUNET_DISK_HANLDE_TYPE_PIPE) + { + if ((except && pipe_except_ready (fh)) || + (!except && pipe_read_ready (fh))) + fds->handles[woff++] = fh; + } + else if (fh->type == GNUNET_DISK_HANLDE_TYPE_FILE) + { + if (!except) + fds->handles[woff++] = fh; + } + else + { + if (WAIT_OBJECT_0 == WaitForSingleObject (fh, 0)) + fds->handles[woff++] = fh; + } } fds->handles_pos = woff; return woff; @@ -1951,6 +1962,11 @@ GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds, for (i = 0; i handles_pos; i++) { fh = rfds->handles[i]; + if (fh->type == GNUNET_DISK_HANLDE_TYPE_EVENT) + { + handle_array[nhandles++] = fh->h; + continue; + } if (fh->type != GNUNET_DISK_HANLDE_TYPE_PIPE) continue; /* Read zero bytes to check the status of the pipe */ -- 2.25.1