Skip to content

Commit f4ea4cf

Browse files
committed
Buffer sends events
1 parent 51b5dec commit f4ea4cf

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

Datagram/Buffer.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
namespace Datagram;
44

5+
use Evenement\EventEmitter;
56
use React\EventLoop\LoopInterface;
67
use \Exception;
78

8-
class Buffer
9+
class Buffer extends EventEmitter
910
{
1011
private $loop;
1112
private $socket;
@@ -46,7 +47,7 @@ public function handleWrite()
4647

4748
if ($ret < 0) {
4849
$error = error_get_last();
49-
throw new Exception('Unable to send packet: ' . $error['message']);
50+
$this->emit('error', array(new Exception('Unable to send packet: ' . $error['message'])));
5051
}
5152

5253
if (!$this->outgoing) {
@@ -61,12 +62,15 @@ public function close()
6162
return false;
6263
}
6364

65+
$this->emit('close', array($this));
66+
6467
if ($this->listening) {
6568
$this->loop->removeWriteStream($this->socket);
6669
$this->listening = false;
6770
}
6871

6972
$this->socket = false;
7073
$this->outgoing = array();
74+
$this->removeAllListeners();
7175
}
7276
}

Datagram/Socket.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ public function __construct(LoopInterface $loop, $socket)
2020
$this->socket = $socket;
2121

2222
$this->buffer = new Buffer($loop, $socket);
23+
$that = $this;
24+
$this->buffer->on('error', function ($error) use ($that) {
25+
$that->emit('error', array($error, $that));
26+
});
27+
$this->buffer->on('close', array($this, 'close'));
2328

2429
$this->resume();
2530
}

0 commit comments

Comments
 (0)