class LWES::Listener

This class is only supported on Ruby 1.9

listener = :address => "", :port => 12345
listener.each do |event|
  p event

Public Class Methods

listener = "", port: 12345) click to toggle source

Binds an LWES listener socket to receive events on. It takes the following options:

  • :address is a dotted quad string of an IPv4 address to listen on.

  • :port is the port to listen on

  • :iface is a dotted quad string of the interface to listen on (optional)

static VALUE listener_init(VALUE self, VALUE options)
        struct lwes_listener *listener;
        LWES_SHORT_STRING address;
        LWES_SHORT_STRING iface;
        LWES_U_INT_32 port;
        VALUE tmp;

        if (DATA_PTR(self))
                         "initializing already initialized Listener");
        if (TYPE(options) != T_HASH)
                rb_raise(rb_eTypeError, "options must be a hash");

        tmp = rb_hash_aref(options, ID2SYM(rb_intern("address")));
        address = StringValueCStr(tmp);

        tmp = rb_hash_aref(options, ID2SYM(rb_intern("iface")));
        iface = NIL_P(tmp) ? NULL : StringValueCStr(tmp);

        tmp = rb_hash_aref(options, ID2SYM(rb_intern("port")));
        port = (LWES_U_INT_32)lwesrb_uint16(tmp);

        listener = lwes_listener_create(address, iface, port);
        if (listener == NULL) {
                listener = lwes_listener_create(address, iface, port);
                if (listener == NULL)
                                 "failed to create LWES Listener");

        DATA_PTR(self) = listener;

        return self;

Public Instance Methods

clone() click to toggle source
Alias for: dup
close → nil click to toggle source

Closes the socket used by the LWES::Listener object. Raises IOError if already closed.

static VALUE listener_close(VALUE self)
        struct lwes_listener *listener = listener_ptr(self);
        int err;

        DATA_PTR(self) = NULL;
        errno = 0;
        err = lwes_listener_destroy(listener);
        if (err)
                fail(err, "lwes_listener_destroy()");
        return Qnil;
dup() click to toggle source

we disallow dup-ing objects since GC could double-free otherwise

# File lib/lwes/listener.rb, line 12
def dup
Also aliased as: clone
each() { |recv| ... } click to toggle source

processes each LWES::Event object as it is received, yielding the LWES::Event to a given block

# File lib/lwes/listener.rb, line 20
def each
    yield recv
  rescue Errno::EINTR
  end while true
recv → LWES::Event click to toggle source
recv(timeout_ms) → LWES::Event or nil

Receives and returns one LWES::Event from the network. An optional timeout (in milliseconds) may be specified and cause this to return nil on timeout. This method is only available under Ruby 1.9.

static VALUE listener_recv(int argc, VALUE *argv, VALUE self)
        struct recv_args args;
        VALUE timeout;
        int r, saved_errno;

        rb_scan_args(argc, argv, "01", &timeout);

        args.listener = listener_ptr(self);
        args.event = lwes_event_create_no_name(NULL);
        args.timeout_ms = NIL_P(timeout) ? UINT_MAX : NUM2UINT(timeout);

        saved_errno = errno = 0;
        r = (int)rb_thread_blocking_region(recv_event, &args, RUBY_UBF_IO, 0);
        if (r >= 0)
                return lwesrb_wrap_event(cLWES_Event, args.event);
        saved_errno = errno;
        if (r == -2 && ! NIL_P(timeout))
                return Qnil;
        errno = saved_errno;
        fail(r, "lwes_listener_recv(_by)");
        return Qnil;