From f047b1f0dcaab60f22619046d419b36f2cc3f6b0 Mon Sep 17 00:00:00 2001 From: usa Date: Wed, 6 Jun 2012 04:29:38 +0000 Subject: [PATCH] * win32/win32.c, include/ruby/win32.h (rb_w32_wrap_io_handle): new API. this API wraps an I/O handle (HANDLE or SOCKET) and returns fd. the second parameter should be combination of O_*, for example, O_RDWR | O_BINARY | O_NOINHERT. * win32/win32.c, include/ruby/win32.h (rb_w32_unwrap_io_handle): new API. this API unwraps an I/O handle and close the fd (not closes the handle itself). [Feature #4960] [ruby-core:37227] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@35937 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 13 +++++++++++++ include/ruby/win32.h | 2 ++ win32/win32.c | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+) diff --git a/ChangeLog b/ChangeLog index 7a1e0f2f6c..ed3c5fd87d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,16 @@ +Wed Jun 6 13:25:04 2012 NAKAMURA Usaku + + * win32/win32.c, include/ruby/win32.h (rb_w32_wrap_io_handle): new API. + this API wraps an I/O handle (HANDLE or SOCKET) and returns fd. + the second parameter should be combination of O_*, for example, + O_RDWR | O_BINARY | O_NOINHERT. + + * win32/win32.c, include/ruby/win32.h (rb_w32_unwrap_io_handle): new + API. this API unwraps an I/O handle and close the fd (not closes + the handle itself). + + [Feature #4960] [ruby-core:37227] + Wed Jun 6 13:18:26 2012 NAKAMURA Usaku * win32/win32.c (rb_w32_close): of course, console handle is not socket. diff --git a/include/ruby/win32.h b/include/ruby/win32.h index 757d2f19e1..9ad2f86641 100644 --- a/include/ruby/win32.h +++ b/include/ruby/win32.h @@ -726,6 +726,8 @@ long rb_w32_write_console(uintptr_t, int); /* use uintptr_t instead of VALUE bec int WINAPI rb_w32_Sleep(unsigned long msec); int rb_w32_wait_events_blocking(HANDLE *events, int num, DWORD timeout); int rb_w32_time_subtract(struct timeval *rest, const struct timeval *wait); +int rb_w32_wrap_io_handle(HANDLE, int); +int rb_w32_unwrap_io_handle(int); /* == ***CAUTION*** diff --git a/win32/win32.c b/win32/win32.c index 8633c8600e..414f7e458f 100644 --- a/win32/win32.c +++ b/win32/win32.c @@ -6747,3 +6747,41 @@ localtime_r(const time_t *tp, struct tm *rp) #endif return rp; } + +/* License: Ruby's */ +int +rb_w32_wrap_io_handle(HANDLE h, int flags) +{ + BOOL tmp; + int len = sizeof(tmp); + int r = getsockopt((SOCKET)h, SOL_SOCKET, SO_DEBUG, (char *)&tmp, &len); + if (r != SOCKET_ERROR || WSAGetLastError() != WSAENOTSOCK) { + int f = 0; + if (flags & O_NONBLOCK) { + flags &= ~O_NONBLOCK; + f = O_NONBLOCK; + } + socklist_insert((SOCKET)h, f); + } + else if (flags & O_NONBLOCK) { + errno = EINVAL; + return -1; + } + return rb_w32_open_osfhandle((intptr_t)h, flags); +} + +/* License: Ruby's */ +int +rb_w32_unwrap_io_handle(int fd) +{ + SOCKET sock = TO_SOCKET(fd); + _set_osfhnd(fd, (SOCKET)INVALID_HANDLE_VALUE); + if (!is_socket(sock)) { + UnlockFile((HANDLE)sock, 0, 0, LK_LEN, LK_LEN); + constat_delete((HANDLE)sock); + } + else { + socklist_delete(&sock, NULL); + } + return _close(fd); +}