Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 142 lines (108 sloc) 2.749 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
package Reflex::UdpPeer;
use Moose;
extends 'Reflex::Base';

has socket => (
is => 'rw',
isa => 'Maybe[FileHandle]',
required => 1,
);

with 'Reflex::Role::Recving' => {
handle => 'socket',
method_send => 'send',
method_stop => 'stop',
cb_datagram => 'on_datagram',
cb_error => 'on_error',
ev_datagram => 'datagram',
ev_error => 'error',
};

1;

__END__

=head1 NAME

Reflex::UdpPeer - Base class for non-blocking UDP networking peers.

=head1 SYNOPSIS

TODO - Rewritten. Need to rewrite docs, too.

Inherit it.

package Reflex::Udp::Echo;
use Moose;
extends 'Reflex::UdpPeer';

sub on_socket_datagram {
my ($self, $args) = @_;
my $data = $args->{datagram};

if ($data =~ /^\s*shutdown\s*$/) {
$self->stop_socket_readable();
return;
}

$self->send(
datagram => $data,
remote_addr => $args->{remote_addr},
);
}

sub on_socket_error {
my ($self, $args) = @_;
warn "$args->{op} error $args->{errnum}: $args->{errstr}";
$self->destruct();
}

Use it as a helper.

package Reflex::Udp::Echo;
use Moose;
extends 'Reflex::Base';
use Reflex::UdpPeer;

has port => ( isa => 'Int', is => 'ro' );

observes peer => (
isa => 'Maybe[Reflex::UdpPeer]',
setup => sub {
my $self = shift;
Reflex::UdpPeer->new(
socket => IO::Socket::INET->new(
LocalPort => $self->port(),
Proto => 'udp',
)
)
},
);

sub on_peer_datagram {
my ($self, $args) = @_;
my $data = $args->{datagram};

if ($data =~ /^\s*shutdown\s*$/) {
$self->peer(undef);
return;
}

$self->peer()->send(
datagram => $data,
remote_addr => $args->{remote_addr},
);
}

sub on_peer_error {
my ($self, $args) = @_;
warn "$args->{op} error $args->{errnum}: $args->{errstr}";
$self->peer(undef);
}

Compose objects with its base role.

# See L<Reflex::Role::Recving>.

Use it as a promise (like a condvar), or set callbacks in its
constructor.

# TODO - Make an example.

=head1 DESCRIPTION

Reflex::UdpPeer is a base class for UDP network peers. It waits for
datagrams on a socket, automatically receives them when they arrive,
and emits "datagram" events containing the data and senders'
addresses. It also provides a send() method that handles errors.

However, all this is done by its implementation, which is over in
Reflex::Role::UdpPeer. The documentation won't be repeated here, so
further details will be found with the role. Code and docs together,
you know.

=head1 SEE ALSO

L<Moose::Manual::Concepts>

L<Reflex>
L<Reflex::Base>
L<Reflex::Role::UdpPeer>

L<Reflex/ACKNOWLEDGEMENTS>
L<Reflex/ASSISTANCE>
L<Reflex/AUTHORS>
L<Reflex/BUGS>
L<Reflex/BUGS>
L<Reflex/CONTRIBUTORS>
L<Reflex/COPYRIGHT>
L<Reflex/LICENSE>
L<Reflex/TODO>

=cut
Something went wrong with that request. Please try again.