On Tuesday, 6 March 2012 at 04:54:44 UTC, Mike Parker wrote:
On 3/6/2012 1:34 PM, Tyler Jameson Little wrote:
I've been playing with libev in D lately, and I've run into a
problem.
I've been able to hack around it, but it'd like to find a
better, more
general solution. Here's a link to the code:
https://github.com/beatgammit/fun-with-d/blob/master/libev/tcp_server.d
The code is a basic TCP server that responds to connections in
a
non-blocking fashion. It's not perfect, and the current
problem I'm
trying to solve is how to get my Socket instance (from accept)
to the
handler. Since everything is asynchronous, and the return
value of
accept() will get lost (garbage collected, I think). When I
try to get
the address of it, the compiler complains that it's not an
lvalue.
Socket instance returned by accept won't be garbage collected
(or lost) as long as you have a reference to it active
somewhere in your program. It doesn't matter which thread. Just
take the return value of accept and pass it to your handler as
is. As long as your handler holds on to the reference, you're
fine. No need to try and get the address, or hack around it.
Maybe I'm missing something then. I tried to do this:
Add the following after line 34 in my code:
watcher.data = &req;
Change line 14 to:
auto req = cast(Socket*)w.data;
That all compiled ok, but I got a seg fault, which I assume is
because it's been garbage collected. w.data should be the pointer
that I assigned to it, or a Socket*.
w.data is defined as a void*, and the struct comes from C (I
think), so it wouldn't be read by the garbage collector in its
sweep. Maybe I'm wrong, but that's what I think is going on.
Is there something else going on here that I'm not seeing?
Thanks so much for your help!