36 private $host, $port, $prefix, $domain;
57 if ( preg_match(
'!^udp:(?://)?\[([0-9a-fA-F:]+)\]:(\d+)(?:/(.*))?$!', $info, $m ) ) {
60 $port = intval( $m[2] );
61 $prefix = $m[3] ??
false;
63 } elseif ( preg_match(
'!^udp:(?://)?([a-zA-Z0-9.-]+):(\d+)(?:/(.*))?$!', $info, $m ) ) {
65 if ( !IPUtils::isIPv4( $host ) ) {
66 $host = gethostbyname( $host );
68 $port = intval( $m[2] );
69 $prefix = $m[3] ??
false;
72 throw new InvalidArgumentException( __METHOD__ .
': Invalid UDP specification' );
75 return new self( $host, $port, $domain, $prefix );
81 public function emit( $text ): void {
83 if ( $this->prefix !== false ) {
84 $text = preg_replace(
'/^/m', $this->prefix .
' ', $text );
86 if ( strlen( $text ) > self::MAX_PAYLOAD_SIZE - 1 ) {
87 $text = substr( $text, 0, self::MAX_PAYLOAD_SIZE - 1 );
90 if ( substr( $text, -1 ) !=
"\n" ) {
93 } elseif ( strlen( $text ) > self::MAX_PAYLOAD_SIZE ) {
94 $text = substr( $text, 0, self::MAX_PAYLOAD_SIZE );
97 $sock = socket_create( $this->domain, SOCK_DGRAM, SOL_UDP );
102 socket_sendto( $sock, $text, strlen( $text ), 0, $this->host, $this->port );
103 socket_close( $sock );