actually switch and delete old receive_class_call code
[scpubgit/Object-Remote.git] / lib / Object / Remote / ConnectionServer.pm
CommitLineData
5c608989 1package Object::Remote::ConnectionServer;
2
3use Scalar::Util qw(blessed weaken);
4use Module::Runtime qw(use_module);
5use Object::Remote;
6use IO::Socket::UNIX;
7use POSIX ();
8use Moo;
9
10has listen_on => (
11 is => 'ro',
12 coerce => sub {
13 return $_[0] if blessed($_[0]);
14 unlink($_[0]);
15 IO::Socket::UNIX->new(
16 Local => $_[0],
17 Listen => 1
18 ) or die "Couldn't liten to $_[0]: $!";
19 },
20 trigger => sub {
21 my ($self, $fh) = @_;
22 weaken($self);
23 Object::Remote->current_loop
24 ->watch_io(
25 handle => $fh,
26 on_read_ready => sub { $self->_listen_ready($fh) }
27 );
28 },
29);
30
31has connection_args => (
32 is => 'ro', default => sub { [] }
33);
34
35sub BUILD {
36 Object::Remote->current_loop->want_run;
37}
38
39sub run {
40 Object::Remote->current_loop->run_while_wanted;
41}
42
43sub _listen_ready {
44 my ($self, $fh) = @_;
45 my $new = $fh->accept or die "Couldn't accept: $!";
46 $new->blocking(0);
47 my $f = CPS::Future->new;
48 my $c = use_module('Object::Remote::Connection')->new(
49 receive_from_fh => $new,
50 send_to_fh => $new,
51 on_close => $f, # and so will die $c
52 @{$self->connection_args}
53 );
54 $f->on_ready(sub { undef($c) });
55 $c->ready_future->done;
56 print $new "Shere\n" or die "Couldn't send to new socket: $!";
57 return $c;
58}
59
60sub DEMOLISH {
61 my ($self, $gd) = @_;
62 return if $gd;
63 Object::Remote->current_loop
64 ->unwatch_io(
65 handle => $self->listen_on,
66 on_read_ready => 1
67 );
68 if ($self->listen_on->can('hostpath')) {
69 unlink($self->listen_on->hostpath);
70 }
71 Object::Remote->current_loop->want_stop;
72}
73
741;