-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Incoming.pm
154 lines (132 loc) · 5.06 KB
/
Incoming.pm
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
{
package Net::BitTorrent::Protocol::BEP03::Peer::Incoming;
use Moose;
use Moose::Util::TypeConstraints;
use lib '../../../../../../lib';
use Net::BitTorrent::Types qw[:addr];
use Net::BitTorrent::Network::Utility qw[:sockaddr :paddr];
extends 'Net::BitTorrent::Protocol::BEP03::Peer';
our $MAJOR = 0; our $MINOR = 74; our $DEV = 13; our $VERSION = sprintf('%0d.%03d' . ($DEV ? (($DEV < 0 ? '' : '_') . '%03d') : ('')), $MAJOR, $MINOR, abs $DEV);
has 'handle' => (
is => 'ro',
isa => 'AnyEvent::Handle::Throttle',
lazy_build => 1,
initializer => '_initializer_handle',
init_arg => 'handle',
required => 1,
handles => {
rbuf => 'rbuf',
push_read => 'push_read',
push_write => 'push_write',
total_download => 'download_total',
fh => sub { shift->handle->{'fh'} },
host => sub {
my $s = shift;
return $_[0] = undef #$s->disconnect('Failed to open socket')
if !defined $s->fh; # XXX - error creating socket?
my (undef, $addr) = unpack_sockaddr(getpeername($s->fh));
return paddr2ip($addr);
},
port => sub {
my $s = shift;
return $_[0] = undef #$s->disconnect('Failed to open socket')
if !defined $s->fh; # XXX - error creating socket?
my ($port, undef) = unpack_sockaddr(getpeername($s->fh));
return $port;
}
}
);
sub _initializer_handle {
my ($s, $c, $set, $attr) = @_;
$set->($c);
require Scalar::Util;
Scalar::Util::weaken $s;
$c->on_read(
sub {
return if !defined $s;
require Net::BitTorrent::Protocol::BEP03::Packets;
PACKET:
while (
my $p =
Net::BitTorrent::Protocol::BEP03::Packets::parse_packet(
\$s->handle->rbuf
)
)
{ $s->_handle_packet($p);
last PACKET if !defined $s || !$s->rbuf || !$p;
}
}
);
#on_connect => sub {
# return if !defined $s;
# $s->_send_handshake;
# },
# on_read => sub {
# return if !defined $s;
# require Net::BitTorrent::Protocol::BEP03::Packets;
# PACKET:
# while (
# my $p =
# Net::BitTorrent::Protocol::BEP03::Packets::parse_packet(
# \$s->handle->rbuf
# )
# )
# { $s->_handle_packet($p);
# last PACKET if !defined $s || !$s->rbuf || !$p;
# }
# }
}
my $infohash_constraint;
sub _handle_packet_handshake {
my ($s, $p) = @_;
my ($reserved, $info_hash, $peer_id) = @$p;
$infohash_constraint //=
Moose::Util::TypeConstraints::find_type_constraint(
'NBTypes::Torrent::Infohash');
$info_hash = $infohash_constraint->coerce($info_hash);
$s->_set_support_extensions if (ord(substr($reserved, 5, 1)) & 0x10);
$s->_set_peer_id($peer_id);
my $t = $s->client->torrent($info_hash);
return
$s->disconnect(sprintf 'Bad info_hash (We are not serving %s)',
$info_hash->to_Hex)
if !$t;
$s->_set_torrent($t);
#$s->_check_unique_connection;
return if !defined $s;
# send handshake
return if !defined $t;
$s->_send_handshake;
$s->_send_bitfield;
}
#
no Moose;
no Moose::Util::TypeConstraints;
__PACKAGE__->meta->make_immutable
}
1;
=pod
=head1 NAME
Net::BitTorrent::Protocol::BEP03::Peer::Incoming - Incoming TCP-based connection
=head1 Description
Go away.
=head1 Author
Sanko Robinson <sanko@cpan.org> - http://sankorobinson.com/
CPAN ID: SANKO
=head1 License and Legal
Copyright (C) 2008-2010 by Sanko Robinson <sanko@cpan.org>
This program is free software; you can redistribute it and/or modify it under
the terms of
L<The Artistic License 2.0|http://www.perlfoundation.org/artistic_license_2_0>.
See the F<LICENSE> file included with this distribution or
L<notes on the Artistic License 2.0|http://www.perlfoundation.org/artistic_2_0_notes>
for clarification.
When separated from the distribution, all original POD documentation is
covered by the
L<Creative Commons Attribution-Share Alike 3.0 License|http://creativecommons.org/licenses/by-sa/3.0/us/legalcode>.
See the
L<clarification of the CCA-SA3.0|http://creativecommons.org/licenses/by-sa/3.0/us/>.
Neither this module nor the L<Author|/Author> is affiliated with BitTorrent,
Inc.
=for rcs $Id$
=cut