Iniitial RCP support (from theorbtwo)
[scpubgit/Object-Remote.git] / lib / Object / Remote / Connector / TCP.pm
1 package Object::Remote::Connector::TCP;
2
3 use IO::Socket::INET;
4 use Moo;
5
6 with 'Object::Remote::Role::Connector';
7
8 has host => (is => 'ro', required => 1);
9 has port => (is => 'ro', required => 1);
10 has sock => (is => 'rw');
11
12 {
13   no warnings 'once';
14   
15   push @Object::Remote::Connection::Guess, sub {
16     for ($_[0]) {
17       # FIXME: Use Regexp::Common regex for a hostname, or check this against spec.
18       print STDERR "tcp guess for $_\n";
19       if (defined and !ref and m!^tcp://([a-z0-9.]+):(\d+)/!i) {
20         return __PACKAGE__->new(host => $1, port => $2);
21       }
22     }
23     return;
24   };
25 }
26
27 sub _open2_for {
28   my ($self) = @_;
29   my $sock = IO::Socket::INET->new(PeerHost => $self->host, PeerPort => $self->port)
30     or die "Couldn't open connection: $!";
31   
32   ($sock, $sock, undef);
33 }