예제 #1
0
 private function waitForStreamOnce($stream) : PromiseInterface
 {
     $deferred = new Deferred();
     $this->loop->addWriteStream($stream, function ($stream) use($deferred) {
         $this->loop->removeWriteStream($stream);
         $deferred->resolve($stream);
     });
     return $deferred->promise();
 }
예제 #2
0
 public function handleWrite()
 {
     if (!is_resource($this->stream)) {
         $this->emit('error', [new \RuntimeException('Tried to write to invalid stream.'), $this]);
         return;
     }
     set_error_handler([$this, 'errorHandler']);
     $sent = fwrite($this->stream, $this->data);
     restore_error_handler();
     if (false === $sent) {
         $this->emit('error', [new \ErrorException($this->lastError['message'], 0, $this->lastError['number'], $this->lastError['file'], $this->lastError['line']), $this]);
         return;
     }
     if (0 === $sent && feof($this->stream)) {
         $this->emit('error', [new \RuntimeException('Tried to write to closed stream.'), $this]);
         return;
     }
     $len = strlen($this->data);
     $this->data = (string) substr($this->data, $sent);
     if ($len >= $this->softLimit && $len - $sent < $this->getSoftLimit()) {
         $this->emit('drain', [$this]);
     }
     if (0 === strlen($this->data)) {
         $this->loop->removeWriteStream($this->stream);
         $this->setListening(false);
         $this->emit('full-drain', [$this]);
     }
 }