Backport #4905
closedrb_w32_add_socket / rb_w32_remove_socket
Description
It is currently very difficult to use an externally created
SOCKET on Windows with ruby 1.8. Authors of all the gems I
found (pg, mysql2, curb) got it wrong. This bug is described
in detail here ( http://www.ruby-forum.com/topic/869239 ). In
the meantime, a small pair of functions would allow gem authors
to use SOCKETs, without a significant rewrite of ruby's win32
layer.
Attached is a very tiny patch for new functions which will
allow gem authors to use externally created sockets on Windows.
Their usage is as follows:
int fd = libwhatever_get_socket();
#ifdef _WIN32
ruby_fd = rb_w32_add_socket(fd, 0);
#else
ruby_fd = fd;
#endif
... do stuff with the ruby_fd as you would on other platforms ...
#ifdef _WIN32
rb_w32_remove_socket(ruby_fd);
#endif
libwhatever_close_socket(fd);
In a nutshell; you pass the SOCKET given to you by the external
library to ruby and it gives you a CRT fd you can use with ruby
functions. When you're done, you pass that CRT fd to ruby to remove
it, and then close the SOCKET however the library normally does.
If the ifdefs are not preferred, rb_add_socket / rb_remove_socket
could be added as no-ops on non-win32 platforms, however I should
stress that making this a permanent fixture of the API is probably a
mistake, and the ruby win32 code should be rewritten to use
HANDLEs/SOCKETs instead of CRT fds.
Files