Danga::Socket - Event loop and event-driven async socket base class


  package My::Socket
  use Danga::Socket;
  use base ('Danga::Socket');
  use fields ('my_attribute');

  sub new {
     my My::Socket $self = shift;
     $self = fields::new($self) unless ref $self;
     $self->SUPER::new( @_ );

     $self->{my_attribute} = 1234;
     return $self;

  sub event_err { ... }
  sub event_hup { ... }
  sub event_write { ... }
  sub event_read { ... }
  sub close { ... }


  # write returns 1 if all writes have gone through, or 0 if there
  # are writes in queue
  $my_sock->write(sub { ... });  # run when previous data written
  $my_sock->write(undef);        # kick-starts

  # read max $bytecount bytes, or undef on connection closed
  $scalar_ref = $my_sock->read($bytecount);

  # watch for writability.  not needed with ->write().  write()
  # will automatically turn on watch_write when you wrote too much
  # and turn it off when done

  # watch for readability

  # if you read too much and want to push some back on
  # readable queue.  (not incredibly well-tested)
  $my_sock->push_back_read($buf); # scalar or scalar ref

  Danga::Socket->WatchedSockets();  # count of DescriptorMap keys


This is an abstract base class for objects backed by a socket which provides the basic framework for event-driven asynchronous IO, designed to be fast. Danga::Socket is both a base class for objects, and an event loop.

Callers subclass Danga::Socket. Danga::Socket's constructor registers itself with the Danga::Socket event loop, and invokes callbacks on the object for readability, writability, errors, and other conditions.

Because Danga::Socket uses the "fields" module, your subclasses must too.


For now, see servers using Danga::Socket for guidance. For example: perlbal, mogilefsd, or ddlockd.


Brad Fitzpatrick <> - author

Michael Granger <> - docs, testing

Mark Smith <> - contributor, heavy user, testing

Matt Sergeant <> - kqueue support


Not documented enough.

tcp_cork only works on Linux for now. No BSD push/nopush support.


License is granted to use and distribute this module under the same terms as Perl itself.