46 string $protocol =
'tcp',
47 ?
string $domain =
null,
48 ?callable $resolver =
null
50 $this->service = $service;
51 $this->protocol = $protocol;
52 $this->domain = $domain;
54 $this->resolver = $resolver ??
static function ( $srv ) {
55 return dns_get_record( $srv, DNS_SRV );
69 $records = ( $this->resolver )( $this->
getSrvName() );
75 || ( count( $records ) === 1 && $records[0][
'target'] ===
'.' )
80 foreach ( $records as $record ) {
82 'target' => $record[
'target'],
83 'port' => (int)$record[
'port'],
84 'pri' => (
int)$record[
'pri'],
85 'weight' => (int)$record[
'weight'],
102 usort( $records,
static function ( $a, $b ) {
103 if ( $a[
'pri'] === $b[
'pri'] ) {
104 return mt_rand( 0, 1 ) ? 1 : -1;
107 return $a[
'pri'] - $b[
'pri'];
110 $serversAndPorts = [];
112 foreach ( $records as $record ) {
113 $serversAndPorts[] = [ $record[
'target'], $record[
'port'] ];
116 return $serversAndPorts;
123 $srv =
"_{$this->service}._{$this->protocol}";
125 if ( $this->domain ===
null || $this->domain ===
'' ) {
129 return "$srv.{$this->domain}";
__construct(string $service, string $protocol='tcp', ?string $domain=null, ?callable $resolver=null)
Construct a new discoverer for the given domain, service, and protocol.
getRecords()
Queries the resolver for an SRV resource record matching the service, protocol, and domain and return...
getServers()
Performs discovery for the domain, service, and protocol, and returns a list of resolved server name/...
getSrvName()
Returns the SRV resource record name.