46 if ( preg_match(
'!^udp:(?://)?\[([0-9a-fA-F:]+)\]:(\d+)(?:/(.*))?$!', $info, $m ) ) {
49 $port = intval( $m[2] );
50 $prefix = $m[3] ??
false;
52 } elseif ( preg_match(
'!^udp:(?://)?([a-zA-Z0-9.-]+):(\d+)(?:/(.*))?$!', $info, $m ) ) {
54 if ( !IPUtils::isIPv4( $host ) ) {
55 $host = gethostbyname( $host );
57 $port = intval( $m[2] );
58 $prefix = $m[3] ??
false;
61 throw new InvalidArgumentException( __METHOD__ .
': Invalid UDP specification' );
64 return new self( $host, $port, $domain, $prefix );
70 public function emit( $text ): void {
72 if ( $this->prefix !== false ) {
73 $text = preg_replace(
'/^/m', $this->prefix .
' ', $text );
75 if ( strlen( $text ) > self::MAX_PAYLOAD_SIZE - 1 ) {
76 $text = substr( $text, 0, self::MAX_PAYLOAD_SIZE - 1 );
79 if ( !str_ends_with( $text,
"\n" ) ) {
82 } elseif ( strlen( $text ) > self::MAX_PAYLOAD_SIZE ) {
83 $text = substr( $text, 0, self::MAX_PAYLOAD_SIZE );
86 $sock = socket_create( $this->domain, SOCK_DGRAM, SOL_UDP );
91 socket_sendto( $sock, $text, strlen( $text ), 0, $this->host, $this->port );
92 socket_close( $sock );