Esempio n. 1
0
 protected function createSocketChannel($socket, int $chunkSize, int $length = null) : Channel
 {
     $size = (int) \max(1, \ceil(65535 / $chunkSize));
     return Channel::fromGenerator($size, function (Channel $channel) use($socket, $length, $chunkSize) {
         $reader = new SocketReader($socket);
         $remaining = $length ?? \PHP_INT_MAX;
         try {
             while ($remaining && null !== ($chunk = (yield $reader->readBuffer(\min($remaining, $chunkSize))))) {
                 $remaining -= \strlen($chunk);
                 (yield $channel->send($chunk));
             }
         } finally {
             $reader->dispose();
         }
     });
 }
Esempio n. 2
0
 /**
  * {@inheritdoc}
  */
 public function read(int $length) : \Generator
 {
     $chunk = (yield $this->reader->readBuffer($length, true));
     $this->offset += \strlen($chunk);
     return $chunk;
 }