read = $read; $this->write = $write; $this->received = new \SplQueue; $this->parser = new ChannelParser([$this->received, 'push']); } /** * {@inheritdoc} */ public function send($data): Promise { return call(function () use ($data) { try { return yield $this->write->write($this->parser->encode($data)); } catch (StreamException $exception) { throw new ChannelException("Sending on the channel failed. Did the context die?", 0, $exception); } }); } /** * {@inheritdoc} */ public function receive(): Promise { return call(function () { while ($this->received->isEmpty()) { try { $chunk = yield $this->read->read(); } catch (StreamException $exception) { throw new ChannelException("Reading from the channel failed. Did the context die?", 0, $exception); } if ($chunk === null) { throw new ChannelException("The channel closed unexpectedly. Did the context die?"); } $this->parser->push($chunk); } return $this->received->shift(); }); } }