48 if ( preg_match(
'!^udp:(?://)?\[([0-9a-fA-F:]+)\]:(\d+)(?:/(.*))?$!', $info, $m ) ) {
51 $port = intval( $m[2] );
52 $prefix = $m[3] ??
false;
54 } elseif ( preg_match(
'!^udp:(?://)?([a-zA-Z0-9.-]+):(\d+)(?:/(.*))?$!', $info, $m ) ) {
56 if ( !IPUtils::isIPv4( $host ) ) {
57 $host = gethostbyname( $host );
59 $port = intval( $m[2] );
60 $prefix = $m[3] ??
false;
63 throw new InvalidArgumentException( __METHOD__ .
': Invalid UDP specification' );
66 return new self( $host, $port, $domain, $prefix );
72 public function emit( $text ): void {
74 if ( $this->prefix !== false ) {
75 $text = preg_replace(
'/^/m', $this->prefix .
' ', $text );
77 if ( strlen( $text ) > self::MAX_PAYLOAD_SIZE - 1 ) {
78 $text = substr( $text, 0, self::MAX_PAYLOAD_SIZE - 1 );
81 if ( !str_ends_with( $text,
"\n" ) ) {
84 } elseif ( strlen( $text ) > self::MAX_PAYLOAD_SIZE ) {
85 $text = substr( $text, 0, self::MAX_PAYLOAD_SIZE );
88 $sock = socket_create( $this->domain, SOCK_DGRAM, SOL_UDP );
93 socket_sendto( $sock, $text, strlen( $text ), 0, $this->host, $this->port );
94 socket_close( $sock );