Skip to content

HTTPS clone URL

Subversion checkout URL

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