Skip to content
Newer
Older
100644 84 lines (67 sloc) 1.72 KB
de14455 @rcaputo Add the beginnings of a socket listener, a stream reader/writer, and …
authored
1 # A TCP echo server.
36c08bd @rcaputo Add a strawman tcp client implementation.
authored
2 # Strawman use cases for Reflex::Stream and Reflex::Listener.
de14455 @rcaputo Add the beginnings of a socket listener, a stream reader/writer, and …
authored
3
5c01112 @rcaputo Try to resolve on_my_foo not looking right. See eg 35 for newness.
authored
4 use lib qw(../lib);
de14455 @rcaputo Add the beginnings of a socket listener, a stream reader/writer, and …
authored
5
6 {
7 package TcpEchoSession;
8 use Moose;
9 extends 'Reflex::Stream';
10
5c01112 @rcaputo Try to resolve on_my_foo not looking right. See eg 35 for newness.
authored
11 sub on_stream_data {
de14455 @rcaputo Add the beginnings of a socket listener, a stream reader/writer, and …
authored
12 my ($self, $args) = @_;
13 $self->put($args->{data});
14 }
15
5c01112 @rcaputo Try to resolve on_my_foo not looking right. See eg 35 for newness.
authored
16 sub on_stream_fail {
de14455 @rcaputo Add the beginnings of a socket listener, a stream reader/writer, and …
authored
17 my ($self, $args) = @_;
18 warn "$args->{errfun} error $args->{errnum}: $args->{errstr}\n";
19 $self->emit( event => "shutdown", args => {} );
20 }
21
5c01112 @rcaputo Try to resolve on_my_foo not looking right. See eg 35 for newness.
authored
22 sub on_stream_close {
de14455 @rcaputo Add the beginnings of a socket listener, a stream reader/writer, and …
authored
23 my ($self, $args) = @_;
24 $self->emit( event => "shutdown", args => {} );
25 }
26 }
27
28 {
29 package TcpEchoServer;
30
31 use Moose;
32 extends 'Reflex::Listener';
33 use Reflex::Callbacks qw(cb_method);
34
35 has clients => (
36 is => 'rw',
37 isa => 'HashRef[Reflex::Stream]',
38 default => sub { {} },
39 );
40
5c01112 @rcaputo Try to resolve on_my_foo not looking right. See eg 35 for newness.
authored
41 sub on_listener_accepted {
de14455 @rcaputo Add the beginnings of a socket listener, a stream reader/writer, and …
authored
42 my ($self, $args) = @_;
43
44 # TODO - We're developing a pattern here:
45 # 1. Create a managed object,
46 # 2. The new managed object will contain a weak manager reference.
47 # 3. The manager enters the object into a hash, keyed on object.
48 # 4. Later, the object will tell the manager when it shuts down.
49
50 my $client = TcpEchoSession->new(
51 handle => $args->{socket},
52 rd => 1,
53 );
54
55 $self->observe(
56 $client,
57 shutdown => cb_method($self, "on_client_shutdown")
58 );
59
60 $self->clients()->{$client} = $client;
61 }
62
5c01112 @rcaputo Try to resolve on_my_foo not looking right. See eg 35 for newness.
authored
63 sub on_listener_fail {
de14455 @rcaputo Add the beginnings of a socket listener, a stream reader/writer, and …
authored
64 my ($self, $args) = @_;
65 warn "$args->{errfun} error $args->{errnum}: $args->{errstr}\n";
66 }
67
68 sub on_client_shutdown {
69 my ($self, $args) = @_;
70 delete $self->clients()->{$args->{_sender}};
71 }
72 }
73
5c01112 @rcaputo Try to resolve on_my_foo not looking right. See eg 35 for newness.
authored
74 my $port = 12345;
75 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
76 TcpEchoServer->new(
77 handle => IO::Socket::INET->new(
78 LocalAddr => '127.0.0.1',
5c01112 @rcaputo Try to resolve on_my_foo not looking right. See eg 35 for newness.
authored
79 LocalPort => $port,
de14455 @rcaputo Add the beginnings of a socket listener, a stream reader/writer, and …
authored
80 Listen => 5,
81 Reuse => 1,
82 ),
83 )->run_all();
Something went wrong with that request. Please try again.