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 47 lines (38 sloc) 0.915 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
# A TCP echo server.
# Implements and demonstrates collections of related objects.
# Also composition to create a simple echo server.

use lib qw(../lib);

# Define the server.

{
package TcpEchoServer;

use Moose;
extends 'Reflex::Acceptor';
use Reflex::Collection;
use EchoStream;

has_many clients => ( handles => { remember_client => "remember" } );

sub on_accept {
my ($self, $args) = @_;
$self->remember_client(
EchoStream->new(
handle => $args->{socket},
rd => 1,
)
);
}

sub on_error {
my ($self, $args) = @_;
warn "$args->{errfun} error $args->{errnum}: $args->{errstr}\n";
$self->stop();
}
}

# Now actually user the server.

my $port = 12345;
print "Setting up TCP echo server on localhost:$port...\n";
TcpEchoServer->new(
listener => IO::Socket::INET->new(
LocalAddr => '127.0.0.1',
LocalPort => $port,
Listen => 5,
Reuse => 1,
),
)->run_all();
Something went wrong with that request. Please try again.