public function readData(ReadableStream $stream) : Generator { // Read the entire data block. $raw = (yield from Stream\readUntil($stream, "\r\n.\r\n")); // Undo dot-stuffing. $data = str_replace("\r\n..\r\n", "\r\n.\r\n", $raw); // Return the data portion. return substr($data, 0, -5); }
/** * @coroutine * * Reads a single line from the stream. * * Reads from the stream until a newline is reached or the stream is closed. * The newline characters are included in the returned string. If reading ends * in the middle of a character, the trailing bytes are not consumed. * * @param float|int $timeout Number of seconds until the returned promise is rejected with a TimeoutException * if no data is received. Use 0 for no timeout. * * @return \Generator * * @resolve string A line of text read from the stream. * * @throws \Icicle\Awaitable\Exception\TimeoutException If the operation times out. * @throws \Icicle\Stream\Exception\UnreadableException If the stream is no longer readable. * @throws \Icicle\Stream\Exception\ClosedException If the stream is unexpectedly closed. */ public function readLine(float $timeout = 0) : \Generator { $newLineSize = strlen($this->newLine); // Check if a new line is already in the buffer. if (($pos = $this->buffer->search($this->newLine)) !== false) { return $this->buffer->shift($pos + $newLineSize); } $this->buffer->push(yield from Stream\readUntil($this->stream, $this->newLine, 0, $timeout)); if (($pos = $this->buffer->search($this->newLine)) !== false) { return $this->buffer->shift($pos + $newLineSize); } return $this->buffer->shift(strlen(mb_strcut((string) $this->buffer, 0, null, $this->encoding))); }