Пример #1
0
 public function send(string $payload)
 {
     return pipe($this->connect(), function () use($payload) {
         $this->outputBuffer .= $payload;
         $this->outputBufferLength += strlen($payload);
         if ($this->writeWatcher !== null) {
             enable($this->writeWatcher);
         }
     });
 }
Пример #2
0
 /**
  * Receive notifications from the server when it starts/stops
  *
  * @param \SplSubject $subject
  * @return \Amp\Promise
  */
 public function update(\SplSubject $subject) : amp\Promise
 {
     switch ($subject->state()) {
         case Server::STARTED:
             $this->debug = $subject->getOption("debug");
             amp\enable($this->cacheWatcher);
             break;
         case Server::STOPPED:
             amp\disable($this->cacheWatcher);
             $this->cache = [];
             $this->cacheTimeouts = [];
             $this->cacheEntryCount = 0;
             $this->bufferedFileCount = 0;
             break;
     }
     return new amp\Success();
 }
Пример #3
0
 /**
  * Write data to the socket
  *
  * Upon write completion the returned promise will resolve to an integer indicating
  * the number of bytes written. If no bytes were written prior to disconnection the
  * returned promise resolves to NULL.
  *
  * @param string $data
  * @return \Amp\Promise<int|null>
  */
 public function write($data)
 {
     $len = \strlen($data);
     if (!($len && \is_string($data))) {
         return new amp\Failure(new \LogicException("String of minimum length 1 required"));
     }
     $state = $this->state;
     if (!$this->alive()) {
         return new amp\Success(null);
     }
     if (empty($state->isWriteEnabled)) {
         amp\enable($this->state->writeWatcherId);
         $state->isWriteEnabled = true;
     }
     $op = new \StdClass();
     $op->buffer = $data;
     $op->size = $len;
     $op->bytesWritten = null;
     $op->promisor = $promisor = new amp\Deferred();
     $state->writeOperations[] = $op;
     return $promisor->promise();
 }
Пример #4
0
 /**
  * Receive notifications from the server when it starts/stops
  *
  * @param Server $server
  * @return \Amp\Promise
  */
 public function update(Server $server) : amp\Promise
 {
     switch ($server->state()) {
         case Server::STARTING:
             $this->loadMimeFileTypes(__DIR__ . "/../etc/mime");
             break;
         case Server::STARTED:
             $this->debug = $server->getOption("debug");
             amp\enable($this->cacheWatcher);
             break;
         case Server::STOPPED:
             amp\disable($this->cacheWatcher);
             $this->cache = [];
             $this->cacheTimeouts = [];
             $this->cacheEntryCount = 0;
             $this->bufferedFileCount = 0;
             break;
     }
     return new amp\Success();
 }
Пример #5
0
 /**
  * @param array $strings
  * @return Promise
  */
 public function send(array $strings)
 {
     foreach ($strings as $string) {
         if (!is_scalar($string)) {
             throw new \InvalidArgumentException("All elements must be of type string or scalar and convertible to a string.");
         }
     }
     return pipe($this->connect(), function () use($strings) {
         $payload = "";
         foreach ($strings as $string) {
             $payload .= "\$" . strlen($string) . "\r\n{$string}\r\n";
         }
         $payload = "*" . count($strings) . "\r\n{$payload}";
         $this->outputBuffer .= $payload;
         $this->outputBufferLength += strlen($payload);
         if ($this->writeWatcher !== null) {
             enable($this->writeWatcher);
         }
     });
 }