[#7872] Nonblocking socket-connect — "Francis Cianfrocca" <garbagecat10@...>

All, I needed a nonblocking socket connect for my asynchronous-event

18 messages 2006/05/14
[#7873] Re: Nonblocking socket-connect — Tanaka Akira <akr@...17n.org> 2006/05/14

In article <3a94cf510605140559l7baa0205le341dac4f47d424b@mail.gmail.com>,

[#7874] Re: Nonblocking socket-connect — "Francis Cianfrocca" <garbagecat10@...> 2006/05/15

How about introducing the method Socket#set_nonblocking, or alternatively

[#7875] Re: Nonblocking socket-connect — Yukihiro Matsumoto <matz@...> 2006/05/15

Hi,

[#7876] Re: Nonblocking socket-connect — "Francis Cianfrocca" <garbagecat10@...> 2006/05/15

Well, it's ok then. I'm comfortable adding in the nonblocking

[#7877] Re: Nonblocking socket-connect — Yukihiro Matsumoto <matz@...> 2006/05/15

Hi,

Nonblocking socket-connect

From: "Francis Cianfrocca" <garbagecat10@...>
Date: 2006-05-14 12:59:49 UTC
List: ruby-core #7872
All, I needed a nonblocking socket connect for my asynchronous-event
framework, and I notice that ruby_connect blocks the calling thread with
wait_connectable. I solved my problem with this (eliding some error-checking
and platform-specific branches):

static VALUE
sock_connect_nonblocking (self, addr)
        VALUE self, addr;
{
        struct sockaddr_in *pin;
        int sd, val, outcome;

        pin = (struct sockaddr_in*)StringValuePtr (addr);
        sd = FIX2INT (rb_funcall (self, rb_intern ("fileno"), 0));
        val = fcntl (sd, F_GETFL, 0);
        fcntl (sd, F_SETFL, val | O_NONBLOCK);

        outcome = connect (sd, (struct sockaddr*)pin, sizeof (*pin));

        if (outcome < 0)
                rb_sys_fail ("connect_nonblocking");
        return INT2FIX (outcome);
}



With this code, the caller rescues Errno::EINPROGRESS (or the platform
equivalent) and waits for the socket to select writable. Did I miss
something in socket.c? Are there any good reasons not to do this?

In This Thread

Prev Next