61 if ( preg_match(
'!^udp:(?://)?\[([0-9a-fA-F:]+)\]:(\d+)(?:/(.*))?$!', $info, $m ) ) {
64 $port = intval( $m[2] );
65 $prefix = $m[3] ??
false;
67 } elseif ( preg_match(
'!^udp:(?://)?([a-zA-Z0-9.-]+):(\d+)(?:/(.*))?$!', $info, $m ) ) {
69 if ( !IPUtils::isIPv4( $host ) ) {
70 $host = gethostbyname( $host );
72 $port = intval( $m[2] );
73 $prefix = $m[3] ??
false;
76 throw new InvalidArgumentException( __METHOD__ .
': Invalid UDP specification' );
79 return new self( $host, $port, $domain, $prefix );
85 public function emit( $text ): void {
87 if ( $this->prefix !== false ) {
88 $text = preg_replace(
'/^/m', $this->prefix .
' ', $text );
90 if ( strlen( $text ) > self::MAX_PAYLOAD_SIZE - 1 ) {
91 $text = substr( $text, 0, self::MAX_PAYLOAD_SIZE - 1 );
94 if ( substr( $text, -1 ) !=
"\n" ) {
97 } elseif ( strlen( $text ) > self::MAX_PAYLOAD_SIZE ) {
98 $text = substr( $text, 0, self::MAX_PAYLOAD_SIZE );
101 $sock = socket_create( $this->domain, SOCK_DGRAM, SOL_UDP );
106 socket_sendto( $sock, $text, strlen( $text ), 0, $this->host, $this->port );
107 socket_close( $sock );