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);
42 use IO::Socket::INET6;
44 our $VERSION = '0.04';
50 $self->on(error => sub {
54 This event is emitted when something goes wrong: Fail to L</listen> to socket,
55 read from socket or other internal errors.
59 $self->on(finish => sub {
60 my($self, $c, $error) = @_;
63 This event is emitted when the client finish, either successfully or due to an
64 error. C<$error> will be an empty string on success.
68 $self->on(read => sub {
69 my($self, $data) = @_;
72 This event is emitted when a new read request arrives from a client.
78 Holds an instance of L<Mojo::IOLoop>.
82 has ioloop => sub { Mojo::IOLoop->singleton };
84 =head2 inactive_timeout
86 How long a L<connection|Mojo::TFTPd::Connection> can stay idle before
87 being dropped. Default is 0 (no timeout).
91 has inactive_timeout => 0;
98 Starts listening to the address and port set in L</Listen>. The L</error>
99 event will be emitted if the server fail to start.
104 my ($self, $args) = (shift, ref $_[0] ? $_[0] : {@_});
105 my $reactor = $self->ioloop->reactor;
108 my $host = $args->{LocalAddr} || $args->{host} || '0.0.0.0';
109 my $port = $args->{LocalPort} || $args->{port} || 1234;
111 $socket = IO::Socket::IP->new(
118 return $self->emit(error => "Can't create listen socket: $!");
121 Scalar::Util::weaken($self);
123 $socket->blocking(0);
124 $reactor->io($socket, sub { $self->_incoming });
125 $reactor->watch($socket, 1, 0); # watch read events
126 $self->{socket} = $socket;
133 my $socket = $self->{socket};
134 my $read = $socket->recv(my $datagram, 65534);
137 $self->emit(error => "Read: $!");
140 $self->emit(read => $datagram);
143 has peerhost => sub { return $_[0]->{socket}->peerhost };
144 has peerport => sub { return $_[0]->{socket}->peerport };
148 my $reactor = eval { $self->ioloop->reactor } or return; # may be undef during global destruction
150 $reactor->remove($self->{socket}) if $self->{socket};
155 Svetoslav Naydenov - C<harryl@cpan.org>
157 Jan Henning Thorsen - C<jhthorsen@cpan.org>