5 DXUDP - A Mojo compatible UDP thingy
14 my $handle = DXUDP->new;
16 $handle->on(read => sub {
17 my ($handle, $data) = @_;
21 $handle->on(error => sub {
22 warn "DXUDP: $_[1]\n";
25 $handle->on(finish => sub {
26 my($handle, $c, $error) = @_;
27 warn "Connection: $error\n" if $error;
31 $handle->ioloop->start unless $handle->ioloop->is_running;
35 A simple Mojo compatible UDP thingy
39 use Mojo::Base 'Mojo::EventEmitter';
41 use Scalar::Util qw(weaken);
43 our $VERSION = '0.04';
49 $self->on(error => sub {
53 This event is emitted when something goes wrong: Fail to L</listen> to socket,
54 read from socket or other internal errors.
58 $self->on(finish => sub {
59 my($self, $c, $error) = @_;
62 This event is emitted when the client finish, either successfully or due to an
63 error. C<$error> will be an empty string on success.
67 $self->on(read => sub {
68 my($self, $data) = @_;
71 This event is emitted when a new read request arrives from a client.
77 Holds an instance of L<Mojo::IOLoop>.
81 has ioloop => sub { Mojo::IOLoop->singleton };
83 =head2 inactive_timeout
85 How long a L<connection|Mojo::TFTPd::Connection> can stay idle before
86 being dropped. Default is 0 (no timeout).
90 has inactive_timeout => 0;
97 Starts listening to the address and port set in L</Listen>. The L</error>
98 event will be emitted if the server fail to start.
103 my ($self, $args) = (shift, ref $_[0] ? $_[0] : {@_});
104 my $reactor = $self->ioloop->reactor;
107 my $host = $args->{LocalAddr} || $args->{host} || '0.0.0.0';
108 my $port = $args->{LocalPort} || $args->{port} || 1234;
110 $socket = IO::Socket::IP->new(
117 return $self->emit(error => "Can't create listen socket: $!");
120 Scalar::Util::weaken($self);
122 $socket->blocking(0);
123 $reactor->io($socket, sub { $self->_incoming });
124 $reactor->watch($socket, 1, 0); # watch read events
125 $self->{socket} = $socket;
132 my $socket = $self->{socket};
133 my $read = $socket->recv(my $datagram, 65534);
136 $self->emit(error => "Read: $!");
139 $self->emit(read => $datagram);
145 my $reactor = eval { $self->ioloop->reactor } or return; # may be undef during global destruction
147 $reactor->remove($self->{socket}) if $self->{socket};
152 Svetoslav Naydenov - C<harryl@cpan.org>
154 Jan Henning Thorsen - C<jhthorsen@cpan.org>