Skip to content

Commit 51b5dec

Browse files
committed
Do not use fwrite() to send packets and properly handle errors
1 parent 60c5c4e commit 51b5dec

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

Datagram/Buffer.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Datagram;
44

55
use React\EventLoop\LoopInterface;
6+
use \Exception;
67

78
class Buffer
89
{
@@ -36,9 +37,16 @@ public function handleWrite()
3637
list($data, $remoteAddress) = array_shift($this->outgoing);
3738

3839
if ($remoteAddress === null) {
39-
fwrite($this->socket, $data);
40+
// do not use fwrite() as it obeys the stream buffer size and
41+
// packets are not to be split at 8kb
42+
$ret = @stream_socket_sendto($this->socket, $data);
4043
} else {
41-
stream_socket_sendto($this->socket, $data, 0, $remoteAddress);
44+
$ret = @stream_socket_sendto($this->socket, $data, 0, $remoteAddress);
45+
}
46+
47+
if ($ret < 0) {
48+
$error = error_get_last();
49+
throw new Exception('Unable to send packet: ' . $error['message']);
4250
}
4351

4452
if (!$this->outgoing) {

0 commit comments

Comments
 (0)