NAME

Net::BitTorrent::Protocol::BEP09 - Metadata Exchange Implementation

SYNOPSIS

# Inherits from Net::BitTorrent::Protocol::BEP10 (Extension Protocol)
use Net::BitTorrent::Protocol::BEP09;

my $proto = Net::BitTorrent::Protocol::BEP09->new(...);

# Request segment 0 of the info dictionary
$proto->send_metadata_request(0);

# Handle incoming metadata pieces
$proto->on(metadata_data => sub ( $emitter, $piece, $total, $data ) {
    say "Received metadata piece $piece of $total";
});

DESCRIPTION

Net::BitTorrent::Protocol::BEP09 implements the Extension for Peers to Send Metadata Files (BEP 09). This is the protocol that makes Magnet Links work by allowing a client to download the torrent's info dictionary directly from other peers when the local .torrent file is missing.

It works as a sub-protocol of the Extension Protocol (BEP 10), using the ut_metadata message name.

METHODS

send_metadata_request( $piece )

Sends a metadata piece request.

$proto->send_metadata_request( 0 );

This method sends a ut_metadata request for a 16KiB segment of the torrent's metadata.

Expected parameters:

$piece

The zero-based metadata piece index.

send_metadata_data( $piece, $total_size, $data )

Sends a metadata piece.

$proto->send_metadata_data( 0, 32768, $piece_data );

This method provides a segment of the info dictionary to a peer in response to a request.

Expected parameters:

$piece

The metadata piece index.

$total_size

The full length of the bencoded info dictionary in bytes.

$data

The raw 16KiB piece data.

send_metadata_reject( $piece )

Rejects a metadata request.

$proto->send_metadata_reject( 0 );

This method informs the peer that the requested metadata segment cannot be provided.

Expected parameters:

$piece

The metadata piece index.

metadata_request event

Emitted when a metadata request is received.

$proto->on( metadata_request => sub ( $self, $piece ) { ... } );

Expected parameters:

$piece

The requested metadata piece index.

metadata_data event

Emitted when metadata data is received.

$proto->on( metadata_data => sub ( $self, $piece, $total_size, $data ) { ... } );

Expected parameters:

$piece

The metadata piece index.

$total_size

The total size of the metadata.

$data

The received piece data.

metadata_reject event

Emitted when a metadata request is rejected.

$proto->on( metadata_reject => sub ( $self, $piece ) { ... } );

Expected parameters:

$piece

The piece index that was rejected.

SPECIFICATIONS

  • BEP 09: Extension for Peers to Send Metadata Files

AUTHOR

Sanko Robinson <sanko@cpan.org>

COPYRIGHT

Copyright (C) 2008-2026 by Sanko Robinson.

This library is free software; you can redistribute it and/or modify it under the terms of the Artistic License 2.0.