rb_w32_add_socket / rb_w32_remove_socket
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
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();
ruby_fd = rb_w32_add_socket(fd, 0);
ruby_fd = fd;
... do stuff with the ruby_fd as you would on other platforms ...
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.