52 lines
1.5 KiB
PHP
52 lines
1.5 KiB
PHP
<?php
|
|
class SeriaAnnounceResponse implements BEncodeSerializable {
|
|
private int $interval;
|
|
private int $minInterval;
|
|
private ?SeriaTorrent $torrent;
|
|
private bool $includePeerId;
|
|
private bool $compactPeers;
|
|
private array $peers = [];
|
|
|
|
public function __construct(
|
|
int $interval = 0,
|
|
int $minInterval = 0,
|
|
SeriaTorrent $torrent = null,
|
|
bool $includePeerId = false,
|
|
bool $compactPeers = false
|
|
) {
|
|
$this->interval = $interval;
|
|
$this->minInterval = $minInterval;
|
|
$this->torrent = $torrent;
|
|
$this->includePeerId = $includePeerId;
|
|
$this->compactPeers = $compactPeers;
|
|
}
|
|
|
|
public function addPeer(SeriaTorrentPeer $peer): void {
|
|
if(!in_array($peer, $this->peers))
|
|
$this->peers[] = $peer;
|
|
}
|
|
|
|
public function bencodeSerialize(): mixed {
|
|
$data = [
|
|
'interval' => $this->interval,
|
|
'min interval' => $this->minInterval,
|
|
'complete' => $this->torrent?->getCompletePeers() ?? 0,
|
|
'incomplete' => $this->torrent?->getIncompletePeers() ?? 0,
|
|
];
|
|
|
|
if($this->compactPeers) {
|
|
$peers = '';
|
|
foreach($this->peers as $peer)
|
|
$peers .= $peer->getAddressRaw() . pack('n', $peer->getPort());
|
|
$data['peers'] = $peers;
|
|
} else {
|
|
$peers = [];
|
|
foreach($this->peers as $peer)
|
|
$peers[] = $peer->encodeInfo($this->includePeerId);
|
|
$data['peers'] = $peers;
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
}
|