From: ES Date: 2005-10-17T01:43:16+09:00 Subject: Re: convert fd to IO object in extension? Toby DiPasquale wrote: > Hi all, > > I'm writing a C extension for Ruby to bind to the epoll(4) facility in > later Linux kernels (one for *BSD's kqueue will be coming after that) and > I've hit a snag. You may want to peek at Zed A. Shaw's wrapping of Provos' libevent (which abstracts the architectures you mention) [1]. That said... > epoll_wait(2) returns a list of epoll_event records, each of which > contains (among other things) the fd that the particular event fired on. > However, unlike select(2), the whole point of epoll(4) is that you > don't need to maintain and pass in big arrays of fds each time you do a > call. Unfortunately for me, those input arrays are exactly the method by > which Kernel#select can map the results back into the output arrays. > > So, is there a way in the Ruby source to map a fd to its associated IO > object? I do have the option of maintaining a list/hash when the IO > objects are inserted, but this would negate the main advantage of epoll(4) > so I'd understandably not like to have to do that. Thanks in advance. You can use IO#for_fd to create an IO object for a given file descriptor; scour the source for the C function or just use rb_funcall. This, of course, should only be done once after which you refer to the socket using the IO object (you can get the FD again with IO#to_i). I am not sure what you mean by maintaining a Hash; while an explicit data structure is by no means necessary, you (presumably) have to have some means of referring back to the sockets. This is what the IO object would provide, however you store it. Please clarify if I am understanding incorrectly. If you find you need to do some special processing on the FD for each read/write/update, you might do well to just subclass IO and augment it with that functionality. > Toby DiPasquale E [1] http://www.zedshaw.com/projects/ruby_event/index.html