Francis, et al.
I have a C++-based eventmachine module that works without Ruby
in the picture at all, but I''m also writing a wrapper for it as
a Ruby extension.
Thing is, it expects to hook into the EM callback mechanism at
the C level. Which makes it an either/or situation right now
with rubymain.cpp also wanting to initialize EM with its own
event callback.
The simplest solution that occurred to me was to add an
evma_set_event_callback(...) which installs a new callback,
while returning a pointer to the old callback; thus allowing my
code to initialize after rubymain.cpp, but chain its callback in
ahead of the default EM ruby callback.
(If my handler gets an event signature it doesn''t recognize,
it''ll pass the event along to the original ruby handler.)
Does this seem reasonable? (If so, I''ve attached the diffs for
the patch to add the new functions. :)
Side note #1: I notice in em.cpp that each EventableDescriptor
is constructed with its own copy of the pointer to the callback
handler. This means any ed''s that existed prior to a subsequent
evma_set_event_callback() invocation would escape being chained.
But that seems fair to me.
Side note #2: I notice there''s an eventmachine_cpp.h/cplusplus.cpp
now. I wrote my EM C++ interface before then, so I haven''t
modified those files with this patch.
Side note #3: C declarations get a little baroque/obscure with
functions that both take and return pointers to functions. If
you''d like, I''d be happy to refactor EM to use a typedef for
the callback, instead.
For example, this:
void (*evma_set_event_callback(void(*cb)(const char*, int, const char*, int)))
(const char*, int, const char*, int);
would become something like:
EventCallbackFunc evma_set_event_callback (EventCallbackFunc);
or maybe:
event_callback_t *evma_set_event_callback (event_callback_t *);
...something like that...
Regards,
Bill
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: set_event_callback_diffs.txt
Url:
http://rubyforge.org/pipermail/eventmachine-talk/attachments/20071124/01328a99/attachment.txt
On Nov 25, 2007 12:16 AM, Bill Kelly <billk at cts.com> wrote:> > Francis, et al. > > I have a C++-based eventmachine module that works without Ruby > in the picture at all, but I''m also writing a wrapper for it as > a Ruby extension. > > Thing is, it expects to hook into the EM callback mechanism at > the C level. Which makes it an either/or situation right now > with rubymain.cpp also wanting to initialize EM with its own > event callback.Acknowledged- interesting problem. I''m still thinking about it and will write more later. So I understand, you''re looking to integrate a non-Ruby EM module into Ruby through the extension mechanism? Since EM blocks, I assume the utility of that is so you can pass callbacks written in Ruby into the EM reactor. Right?
From: "Francis Cianfrocca" <garbagecat10 at gmail.com>> On Nov 25, 2007 12:16 AM, Bill Kelly <billk at cts.com> wrote: >> >> Francis, et al. >> >> I have a C++-based eventmachine module that works without Ruby >> in the picture at all, but I''m also writing a wrapper for it as >> a Ruby extension. >> >> Thing is, it expects to hook into the EM callback mechanism at >> the C level. Which makes it an either/or situation right now >> with rubymain.cpp also wanting to initialize EM with its own >> event callback. > > Acknowledged- interesting problem. I''m still thinking about it and > will write more later. > > So I understand, you''re looking to integrate a non-Ruby EM module into > Ruby through the extension mechanism? Since EM blocks, I assume the > utility of that is so you can pass callbacks written in Ruby into the > EM reactor. Right?Right. It''s a pure-C++ RPC library, which talks directly to EM''s ''C'' API. So from ruby, I would indeed be receiving callbacks directly from my library, via its own ruby extension wrapper code. But unless I''ve missed something, I think my library can play nicely with the ruby EM module, as long as the ruby EM is allowed to initialize first, and my library hooks in afterward. So when my ruby app does "require ''cila/rpc''" to load my extension, it will in turn do: require ''eventmachine'' # EM must initialize first require ''cila/rpc.so'' # hook into EM event callback chain . . . I''ll admit I haven''t actually TRIED any of this yet. <grin> But it seems like it should work. Regards, Bill