Ejemplo n.º 1
0
 private function onPutOpen($data, $result, $req)
 {
     list($contents, $promisor) = $data;
     if ($result === -1) {
         $promisor->fail(new \RuntimeException(\eio_get_last_error($req)));
     } else {
         $length = strlen($contents);
         $offset = 0;
         $priority = \EIO_PRI_DEFAULT;
         $callback = [$this, "onPutWrite"];
         $fhAndPromisor = [$result, $promisor];
         \eio_write($result, $contents, $length, $offset, $priority, $callback, $fhAndPromisor);
     }
 }
Ejemplo n.º 2
0
 /**
  * Writes data to file
  * @param  string $data Data
  * @param  callable $cb Callback
  * @param  integer $offset Offset
  * @param  integer $pri Priority
  * @return resource|false
  */
 public function write($data, $cb = null, $offset = null, $pri = EIO_PRI_DEFAULT)
 {
     $cb = CallbackWrapper::forceWrap($cb);
     if (!$this->fd) {
         if ($cb) {
             $cb($this, false);
         }
         return false;
     }
     if ($data === '') {
         if ($cb) {
             $cb($this, 0);
         }
         return false;
     }
     if (!FileSystem::$supported) {
         if ($offset !== null) {
             fseek($data, $offset);
         }
         $r = fwrite($this->fd, $data);
         if ($cb) {
             $cb($this, $r);
         }
         return false;
     }
     if ($cb !== null) {
         $this->onWriteOnce->push($cb);
     }
     $l = mb_orig_strlen($data);
     if ($offset === null) {
         $offset = $this->offset;
         $this->offset += $l;
     }
     $this->writing = true;
     $res = eio_write($this->fd, $data, $l, $offset, $pri, function ($file, $result) {
         $this->writing = false;
         $this->onWriteOnce->executeAll($file, $result);
     }, $this);
     return $res;
 }
Ejemplo n.º 3
0
 /**
  * {@inheritdoc}
  */
 public function write($data)
 {
     $promisor = new Deferred();
     $op = new \StdClass();
     $op->type = self::OP_WRITE;
     $op->position = $this->position;
     $op->promisor = $promisor;
     $op->writeData = $data;
     $this->pendingWriteOps++;
     if ($this->isActive) {
         $this->queue[] = $op;
     } else {
         \call_user_func($this->incrementor, 1);
         $this->isActive = true;
         \eio_write($this->fh, $data, strlen($data), $op->position, $priority = null, [$this, "onWrite"], $op);
     }
     return $promisor->promise();
 }
Ejemplo n.º 4
0
 /**
  * @param string $data
  *
  * @return \Icicle\Awaitable\Delayed
  *
  * @throws \Icicle\File\Exception\FileException
  */
 private function push(string $data) : Awaitable
 {
     $length = strlen($data);
     $delayed = new Delayed();
     \eio_write($this->handle, $data, $length, $this->append ? $this->size : $this->position, null, function (Delayed $delayed, $result, $req) use($length) {
         if (-1 === $result) {
             $delayed->reject(new FileException(sprintf('Writing to the file failed: %s.', \eio_get_last_error($req))));
         } elseif ($this->queue->isEmpty()) {
             $delayed->reject(new FileException('No pending write, the file may have been closed.'));
         } else {
             $this->queue->shift();
             if ($this->append) {
                 $this->size += $result;
             } else {
                 $this->position += $result;
                 if ($this->position > $this->size) {
                     $this->size = $this->position;
                 }
             }
             $delayed->resolve($result);
         }
     }, $delayed);
     return $delayed;
 }
Ejemplo n.º 5
0
 public function write($data, $cb = null, $offset = null, $pri = EIO_PRI_DEFAULT)
 {
     if (!FS::$supported) {
         if ($offset !== null) {
             fseek($data, $offset);
         }
         $r = fwrite($this->fd, $data);
         if ($cb) {
             call_user_func($cb, $this, $r);
         }
         return;
     }
     return eio_write($this->fd, $data, null, $offset, $pri, $cb, $this);
 }