Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 157 lines (119 sloc) 3.018 kb
b811c71 Rocco Caputo Give the fourth sortie a new name. It's 53% done now!
authored
1 package Reflex::UdpPeer;
5b0127b Rocco Caputo Remove emit() from roles.
authored
2 # vim: ts=2 sw=2 noexpandtab
3
b11c4bb Rocco Caputo Add inheritance and I/O support. Emitted events can be handled by subcl...
authored
4 use Moose;
1f5ed27 Rocco Caputo Rename Reflex::Role::Object to Reflex::Role::Reactive; Reflex::Object to...
authored
5 extends 'Reflex::Base';
5b0127b Rocco Caputo Remove emit() from roles.
authored
6 use Reflex::Callbacks qw(make_emitter make_terminal_emitter);
b11c4bb Rocco Caputo Add inheritance and I/O support. Emitted events can be handled by subcl...
authored
7
08bafbd Rocco Caputo Massive redoing of Reflex roles and some of the objects that use them.
authored
8 has socket => (
9 is => 'rw',
10 isa => 'Maybe[FileHandle]',
11 required => 1,
12 );
13
5b0127b Rocco Caputo Remove emit() from roles.
authored
14 has active => (
15 is => 'rw',
16 isa => 'Bool',
17 default => 1,
18 );
19
08bafbd Rocco Caputo Massive redoing of Reflex roles and some of the objects that use them.
authored
20 with 'Reflex::Role::Recving' => {
5b0127b Rocco Caputo Remove emit() from roles.
authored
21 att_handle => 'socket',
22 att_active => 'active',
39bad19 Rocco Caputo Fix some fundamental thinkos to make things actually work.
authored
23 method_send => 'send',
24 method_stop => 'stop',
5b0127b Rocco Caputo Remove emit() from roles.
authored
25 cb_datagram => make_emitter(on_datagram => "datagram"),
26 cb_error => make_terminal_emitter(on_error => "error"),
08bafbd Rocco Caputo Massive redoing of Reflex roles and some of the objects that use them.
authored
27 };
b11c4bb Rocco Caputo Add inheritance and I/O support. Emitted events can be handled by subcl...
authored
28
5bc7069 Rocco Caputo Make classes immutable. Mutable classes are a lot slower.
authored
29 __PACKAGE__->meta->make_immutable;
30
b11c4bb Rocco Caputo Add inheritance and I/O support. Emitted events can be handled by subcl...
authored
31 1;
d75f256 Rocco Caputo Begin documentation.
authored
32
33 __END__
34
35 =head1 NAME
36
e932bd7 Rocco Caputo Document UDP peers and a coupe of the POE adapter classes.
authored
37 Reflex::UdpPeer - Base class for non-blocking UDP networking peers.
30dfe8c Rocco Caputo Add a quick round of documentation, and edit some code and examples for ...
authored
38
39 =head1 SYNOPSIS
40
08bafbd Rocco Caputo Massive redoing of Reflex roles and some of the objects that use them.
authored
41 TODO - Rewritten. Need to rewrite docs, too.
42
30dfe8c Rocco Caputo Add a quick round of documentation, and edit some code and examples for ...
authored
43 Inherit it.
44
08bafbd Rocco Caputo Massive redoing of Reflex roles and some of the objects that use them.
authored
45 package Reflex::Udp::Echo;
e932bd7 Rocco Caputo Document UDP peers and a coupe of the POE adapter classes.
authored
46 use Moose;
47 extends 'Reflex::UdpPeer';
30dfe8c Rocco Caputo Add a quick round of documentation, and edit some code and examples for ...
authored
48
08bafbd Rocco Caputo Massive redoing of Reflex roles and some of the objects that use them.
authored
49 sub on_socket_datagram {
5c57d49 Rocco Caputo !!! Experiment with Reflex events being objects.
authored
50 my ($self, $datagram) = @_;
51 my $data = $datagram->octets();
30dfe8c Rocco Caputo Add a quick round of documentation, and edit some code and examples for ...
authored
52
e932bd7 Rocco Caputo Document UDP peers and a coupe of the POE adapter classes.
authored
53 if ($data =~ /^\s*shutdown\s*$/) {
08bafbd Rocco Caputo Massive redoing of Reflex roles and some of the objects that use them.
authored
54 $self->stop_socket_readable();
e932bd7 Rocco Caputo Document UDP peers and a coupe of the POE adapter classes.
authored
55 return;
30dfe8c Rocco Caputo Add a quick round of documentation, and edit some code and examples for ...
authored
56 }
e932bd7 Rocco Caputo Document UDP peers and a coupe of the POE adapter classes.
authored
57
58 $self->send(
5c57d49 Rocco Caputo !!! Experiment with Reflex events being objects.
authored
59 datagram => $data,
60 peer => $datagram->peer(),
e932bd7 Rocco Caputo Document UDP peers and a coupe of the POE adapter classes.
authored
61 );
30dfe8c Rocco Caputo Add a quick round of documentation, and edit some code and examples for ...
authored
62 }
63
08bafbd Rocco Caputo Massive redoing of Reflex roles and some of the objects that use them.
authored
64 sub on_socket_error {
5c57d49 Rocco Caputo !!! Experiment with Reflex events being objects.
authored
65 my ($self, $error) = @_;
66 warn(
67 $error->function(),
68 " error ", $error->number(),
69 ": ", $error->string(),
70 );
e932bd7 Rocco Caputo Document UDP peers and a coupe of the POE adapter classes.
authored
71 $self->destruct();
72 }
30dfe8c Rocco Caputo Add a quick round of documentation, and edit some code and examples for ...
authored
73
e932bd7 Rocco Caputo Document UDP peers and a coupe of the POE adapter classes.
authored
74 Use it as a helper.
30dfe8c Rocco Caputo Add a quick round of documentation, and edit some code and examples for ...
authored
75
08bafbd Rocco Caputo Massive redoing of Reflex roles and some of the objects that use them.
authored
76 package Reflex::Udp::Echo;
e932bd7 Rocco Caputo Document UDP peers and a coupe of the POE adapter classes.
authored
77 use Moose;
1f5ed27 Rocco Caputo Rename Reflex::Role::Object to Reflex::Role::Reactive; Reflex::Object to...
authored
78 extends 'Reflex::Base';
e932bd7 Rocco Caputo Document UDP peers and a coupe of the POE adapter classes.
authored
79 use Reflex::UdpPeer;
80
08bafbd Rocco Caputo Massive redoing of Reflex roles and some of the objects that use them.
authored
81 has port => ( isa => 'Int', is => 'ro' );
e932bd7 Rocco Caputo Document UDP peers and a coupe of the POE adapter classes.
authored
82
9513af3 Rocco Caputo Rename "observe" to "watch".
authored
83 watches peer => (
08bafbd Rocco Caputo Massive redoing of Reflex roles and some of the objects that use them.
authored
84 isa => 'Maybe[Reflex::UdpPeer]',
e932bd7 Rocco Caputo Document UDP peers and a coupe of the POE adapter classes.
authored
85 setup => sub {
86 my $self = shift;
08bafbd Rocco Caputo Massive redoing of Reflex roles and some of the objects that use them.
authored
87 Reflex::UdpPeer->new(
88 socket => IO::Socket::INET->new(
89 LocalPort => $self->port(),
90 Proto => 'udp',
91 )
92 )
e932bd7 Rocco Caputo Document UDP peers and a coupe of the POE adapter classes.
authored
93 },
94 );
95
96 sub on_peer_datagram {
97 my ($self, $args) = @_;
98 my $data = $args->{datagram};
99
100 if ($data =~ /^\s*shutdown\s*$/) {
101 $self->peer(undef);
102 return;
103 }
30dfe8c Rocco Caputo Add a quick round of documentation, and edit some code and examples for ...
authored
104
e932bd7 Rocco Caputo Document UDP peers and a coupe of the POE adapter classes.
authored
105 $self->peer()->send(
106 datagram => $data,
107 remote_addr => $args->{remote_addr},
30dfe8c Rocco Caputo Add a quick round of documentation, and edit some code and examples for ...
authored
108 );
e932bd7 Rocco Caputo Document UDP peers and a coupe of the POE adapter classes.
authored
109 }
30dfe8c Rocco Caputo Add a quick round of documentation, and edit some code and examples for ...
authored
110
e932bd7 Rocco Caputo Document UDP peers and a coupe of the POE adapter classes.
authored
111 sub on_peer_error {
112 my ($self, $args) = @_;
113 warn "$args->{op} error $args->{errnum}: $args->{errstr}";
114 $self->peer(undef);
115 }
30dfe8c Rocco Caputo Add a quick round of documentation, and edit some code and examples for ...
authored
116
e932bd7 Rocco Caputo Document UDP peers and a coupe of the POE adapter classes.
authored
117 Compose objects with its base role.
30dfe8c Rocco Caputo Add a quick round of documentation, and edit some code and examples for ...
authored
118
08bafbd Rocco Caputo Massive redoing of Reflex roles and some of the objects that use them.
authored
119 # See L<Reflex::Role::Recving>.
30dfe8c Rocco Caputo Add a quick round of documentation, and edit some code and examples for ...
authored
120
e932bd7 Rocco Caputo Document UDP peers and a coupe of the POE adapter classes.
authored
121 Use it as a promise (like a condvar), or set callbacks in its
122 constructor.
30dfe8c Rocco Caputo Add a quick round of documentation, and edit some code and examples for ...
authored
123
e932bd7 Rocco Caputo Document UDP peers and a coupe of the POE adapter classes.
authored
124 # TODO - Make an example.
d75f256 Rocco Caputo Begin documentation.
authored
125
126 =head1 DESCRIPTION
127
e932bd7 Rocco Caputo Document UDP peers and a coupe of the POE adapter classes.
authored
128 Reflex::UdpPeer is a base class for UDP network peers. It waits for
129 datagrams on a socket, automatically receives them when they arrive,
130 and emits "datagram" events containing the data and senders'
131 addresses. It also provides a send() method that handles errors.
30dfe8c Rocco Caputo Add a quick round of documentation, and edit some code and examples for ...
authored
132
e932bd7 Rocco Caputo Document UDP peers and a coupe of the POE adapter classes.
authored
133 However, all this is done by its implementation, which is over in
134 Reflex::Role::UdpPeer. The documentation won't be repeated here, so
135 further details will be found with the role. Code and docs together,
136 you know.
30dfe8c Rocco Caputo Add a quick round of documentation, and edit some code and examples for ...
authored
137
d75f256 Rocco Caputo Begin documentation.
authored
138 =head1 SEE ALSO
139
e932bd7 Rocco Caputo Document UDP peers and a coupe of the POE adapter classes.
authored
140 L<Moose::Manual::Concepts>
141
75cf6c4 Rocco Caputo Begin documenting in earnest.
authored
142 L<Reflex>
1f5ed27 Rocco Caputo Rename Reflex::Role::Object to Reflex::Role::Reactive; Reflex::Object to...
authored
143 L<Reflex::Base>
e932bd7 Rocco Caputo Document UDP peers and a coupe of the POE adapter classes.
authored
144 L<Reflex::Role::UdpPeer>
145
75cf6c4 Rocco Caputo Begin documenting in earnest.
authored
146 L<Reflex/ACKNOWLEDGEMENTS>
147 L<Reflex/ASSISTANCE>
148 L<Reflex/AUTHORS>
30dfe8c Rocco Caputo Add a quick round of documentation, and edit some code and examples for ...
authored
149 L<Reflex/BUGS>
75cf6c4 Rocco Caputo Begin documenting in earnest.
authored
150 L<Reflex/BUGS>
151 L<Reflex/CONTRIBUTORS>
152 L<Reflex/COPYRIGHT>
153 L<Reflex/LICENSE>
154 L<Reflex/TODO>
30dfe8c Rocco Caputo Add a quick round of documentation, and edit some code and examples for ...
authored
155
d75f256 Rocco Caputo Begin documentation.
authored
156 =cut
Something went wrong with that request. Please try again.