Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 49 lines (39 sloc) 0.916 kB
5b0127b @rcaputo Remove emit() from roles.
authored
1 # vim: ts=2 sw=2 noexpandtab
de14455 @rcaputo Add the beginnings of a socket listener, a stream reader/writer, and …
authored
2 # A TCP echo server.
eb5cc0f @rcaputo Reflex::Collection manages a collection of objects so you don't have to.
authored
3 # Implements and demonstrates collections of related objects.
4 # Also composition to create a simple echo server.
de14455 @rcaputo Add the beginnings of a socket listener, a stream reader/writer, and …
authored
5
5c01112 @rcaputo Try to resolve on_my_foo not looking right. See eg 35 for newness.
authored
6 use lib qw(../lib);
de14455 @rcaputo Add the beginnings of a socket listener, a stream reader/writer, and …
authored
7
eb5cc0f @rcaputo Reflex::Collection manages a collection of objects so you don't have to.
authored
8 # Define the server.
de14455 @rcaputo Add the beginnings of a socket listener, a stream reader/writer, and …
authored
9
10 {
11 package TcpEchoServer;
12
13 use Moose;
ccc3a37 @rcaputo Replace Reflex::Listener with Reflex::Role::Accepting and Reflex::Acc…
authored
14 extends 'Reflex::Acceptor';
eb5cc0f @rcaputo Reflex::Collection manages a collection of objects so you don't have to.
authored
15 use Reflex::Collection;
16 use EchoStream;
de14455 @rcaputo Add the beginnings of a socket listener, a stream reader/writer, and …
authored
17
134c7e0 @rcaputo Add simpler has_many syntax for Reflex::Collection.
authored
18 has_many clients => ( handles => { remember_client => "remember" } );
de14455 @rcaputo Add the beginnings of a socket listener, a stream reader/writer, and …
authored
19
ccc3a37 @rcaputo Replace Reflex::Listener with Reflex::Role::Accepting and Reflex::Acc…
authored
20 sub on_accept {
5c57d49 @rcaputo !!! Experiment with Reflex events being objects.
authored
21 my ($self, $socket) = @_;
eb5cc0f @rcaputo Reflex::Collection manages a collection of objects so you don't have to.
authored
22 $self->remember_client(
23 EchoStream->new(
5c57d49 @rcaputo !!! Experiment with Reflex events being objects.
authored
24 handle => $socket->handle(),
eb5cc0f @rcaputo Reflex::Collection manages a collection of objects so you don't have to.
authored
25 rd => 1,
26 )
de14455 @rcaputo Add the beginnings of a socket listener, a stream reader/writer, and …
authored
27 );
28 }
29
ccc3a37 @rcaputo Replace Reflex::Listener with Reflex::Role::Accepting and Reflex::Acc…
authored
30 sub on_error {
5c57d49 @rcaputo !!! Experiment with Reflex events being objects.
authored
31 my ($self, $error) = @_;
32 warn $error->formatted(), "\n";
ccc3a37 @rcaputo Replace Reflex::Listener with Reflex::Role::Accepting and Reflex::Acc…
authored
33 $self->stop();
de14455 @rcaputo Add the beginnings of a socket listener, a stream reader/writer, and …
authored
34 }
35 }
36
eb5cc0f @rcaputo Reflex::Collection manages a collection of objects so you don't have to.
authored
37 # Now actually user the server.
38
5c01112 @rcaputo Try to resolve on_my_foo not looking right. See eg 35 for newness.
authored
39 my $port = 12345;
40 print "Setting up TCP echo server on localhost:$port...\n";
de14455 @rcaputo Add the beginnings of a socket listener, a stream reader/writer, and …
authored
41 TcpEchoServer->new(
ccc3a37 @rcaputo Replace Reflex::Listener with Reflex::Role::Accepting and Reflex::Acc…
authored
42 listener => IO::Socket::INET->new(
de14455 @rcaputo Add the beginnings of a socket listener, a stream reader/writer, and …
authored
43 LocalAddr => '127.0.0.1',
5c01112 @rcaputo Try to resolve on_my_foo not looking right. See eg 35 for newness.
authored
44 LocalPort => $port,
de14455 @rcaputo Add the beginnings of a socket listener, a stream reader/writer, and …
authored
45 Listen => 5,
46 Reuse => 1,
47 ),
48 )->run_all();
Something went wrong with that request. Please try again.