|
@@ -12,6 +12,7 @@
|
|
|
namespace Monolog\Handler\SyslogUdp;
|
|
namespace Monolog\Handler\SyslogUdp;
|
|
|
|
|
|
|
|
use Monolog\Utils;
|
|
use Monolog\Utils;
|
|
|
|
|
+use Socket;
|
|
|
|
|
|
|
|
class UdpSocket
|
|
class UdpSocket
|
|
|
{
|
|
{
|
|
@@ -21,7 +22,7 @@ class UdpSocket
|
|
|
protected $ip;
|
|
protected $ip;
|
|
|
/** @var int */
|
|
/** @var int */
|
|
|
protected $port;
|
|
protected $port;
|
|
|
- /** @var resource|null */
|
|
|
|
|
|
|
+ /** @var resource|Socket|null */
|
|
|
protected $socket;
|
|
protected $socket;
|
|
|
|
|
|
|
|
public function __construct(string $ip, int $port = 514)
|
|
public function __construct(string $ip, int $port = 514)
|
|
@@ -35,7 +36,7 @@ class UdpSocket
|
|
|
$domain = AF_UNIX;
|
|
$domain = AF_UNIX;
|
|
|
$protocol = IPPROTO_IP;
|
|
$protocol = IPPROTO_IP;
|
|
|
}
|
|
}
|
|
|
- $this->socket = socket_create($domain, SOCK_DGRAM, $protocol);
|
|
|
|
|
|
|
+ $this->socket = socket_create($domain, SOCK_DGRAM, $protocol) ?: null;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public function write($line, $header = "")
|
|
public function write($line, $header = "")
|
|
@@ -45,7 +46,7 @@ class UdpSocket
|
|
|
|
|
|
|
|
public function close(): void
|
|
public function close(): void
|
|
|
{
|
|
{
|
|
|
- if (is_resource($this->socket)) {
|
|
|
|
|
|
|
+ if (is_resource($this->socket) || $this->socket instanceof Socket) {
|
|
|
socket_close($this->socket);
|
|
socket_close($this->socket);
|
|
|
$this->socket = null;
|
|
$this->socket = null;
|
|
|
}
|
|
}
|
|
@@ -53,7 +54,7 @@ class UdpSocket
|
|
|
|
|
|
|
|
protected function send(string $chunk): void
|
|
protected function send(string $chunk): void
|
|
|
{
|
|
{
|
|
|
- if (!is_resource($this->socket)) {
|
|
|
|
|
|
|
+ if (!is_resource($this->socket) && !$this->socket instanceof Socket) {
|
|
|
throw new \RuntimeException('The UdpSocket to '.$this->ip.':'.$this->port.' has been closed and can not be written to anymore');
|
|
throw new \RuntimeException('The UdpSocket to '.$this->ip.':'.$this->port.' has been closed and can not be written to anymore');
|
|
|
}
|
|
}
|
|
|
socket_sendto($this->socket, $chunk, strlen($chunk), $flags = 0, $this->ip, $this->port);
|
|
socket_sendto($this->socket, $chunk, strlen($chunk), $flags = 0, $this->ip, $this->port);
|