Skip to content

Commit 2316f5f

Browse files
committed
Fix UDP send on macOS for connected sockets (EISCONN) #1152
1 parent c426262 commit 2316f5f

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

src/Connection/UdpConnection.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,23 @@ class UdpConnection extends ConnectionInterface implements JsonSerializable
4646
public string $transport = 'udp';
4747

4848
/**
49-
* Construct.
50-
*
51-
* @param resource $socket
52-
* @param string $remoteAddress
49+
* Whether the socket is connected (created via stream_socket_client).
50+
* On BSD/macOS, sendto() on a connected UDP socket with a destination address
51+
* returns EISCONN(-1). We must omit the address for connected sockets.
5352
*/
53+
protected bool $connected = false;
54+
5455
/**
5556
* @param resource|null $socket
5657
*/
5758
public function __construct(
5859
/** @var resource|null */ protected $socket,
59-
protected string $remoteAddress) {}
60+
protected string $remoteAddress)
61+
{
62+
if (is_resource($socket) && stream_socket_get_name($socket, true) !== false) {
63+
$this->connected = true;
64+
}
65+
}
6066

6167
/**
6268
* Sends data on the connection.
@@ -73,6 +79,9 @@ public function send(mixed $sendBuffer, bool $raw = false): bool|null
7379
return null;
7480
}
7581
}
82+
if ($this->connected) {
83+
return strlen($sendBuffer) === stream_socket_sendto($this->socket, $sendBuffer);
84+
}
7685
return strlen($sendBuffer) === stream_socket_sendto($this->socket, $sendBuffer, 0, $this->isIpV6() ? '[' . $this->getRemoteIp() . ']:' . $this->getRemotePort() : $this->remoteAddress);
7786
}
7887

0 commit comments

Comments
 (0)