Esempio n. 1
0
 /**
  * Remove event listener from event loop.
  *
  * @param mixed $fd
  * @param int   $flag
  * @return bool
  */
 public function del($fd, $flag)
 {
     switch ($flag) {
         case EventInterface::EV_READ:
             return $this->_loop->removeReadStream($fd);
         case EventInterface::EV_WRITE:
             return $this->_loop->removeWriteStream($fd);
         case EventInterface::EV_SIGNAL:
             return $this->_loop->removeSignal($fd);
         case EventInterface::EV_TIMER:
         case EventInterface::EV_TIMER_ONCE:
             return $this->_loop->cancelTimer($fd);
     }
     return false;
 }
Esempio n. 2
0
 public function handleWriteEvent()
 {
     foreach ($this->messages as $i => $message) {
         try {
             if (!is_array($message)) {
                 $message = array($message);
             }
             $sent = (bool) $this->socket->sendmulti($message, ZMQ::MODE_NOBLOCK);
             if ($sent) {
                 unset($this->messages[$i]);
                 if (0 === count($this->messages)) {
                     $this->loop->removeWriteStream($this->fileDescriptor);
                     $this->listening = false;
                     $this->emit('end');
                 }
             }
         } catch (ZMQSocketException $e) {
             $this->emit('error', array($e));
         }
     }
 }
Esempio n. 3
0
 public function testRecursiveFutureTick()
 {
     $stream = $this->createStream();
     $this->loop->addWriteStream($stream, function () use($stream) {
         echo 'stream' . PHP_EOL;
         $this->loop->removeWriteStream($stream);
     });
     $this->loop->futureTick(function () {
         echo 'future-tick-1' . PHP_EOL;
         $this->loop->futureTick(function () {
             echo 'future-tick-2' . PHP_EOL;
         });
     });
     $this->expectOutputString('future-tick-1' . PHP_EOL . 'stream' . PHP_EOL . 'future-tick-2' . PHP_EOL);
     $this->loop->run();
 }
Esempio n. 4
0
 /**
  * Cancel an existing timer/stream watcher
  *
  * @param int $watcherId
  */
 public function cancel($watcherId)
 {
     if (isset($this->watchers[$watcherId])) {
         list($type, $data) = $this->watchers[$watcherId];
         switch ($type) {
             case self::WATCHER_TYPE_READ:
                 $this->reactor->removeReadStream($data);
                 break;
             case self::WATCHER_TYPE_WRITE:
                 $this->reactor->removeWriteStream($data);
                 break;
             case self::WATCHER_TYPE_TIMER:
                 $this->reactor->cancelTimer($data);
                 break;
         }
     }
     unset($this->watchers[$watcherId], $this->disabledWatchers[$watcherId]);
 }
Esempio n. 5
0
 /**
  * Asynchronously sends buffered data over the wire.
  *
  * - Calls {@link eventLoops}'s addWriteStream() with client's stream.
  * - Consecutive calls will return the same instance of promise.
  *
  * @return Promise\PromiseInterface
  */
 protected function flushWriteBuffer()
 {
     if ($this->flushWriteBufferPromise) {
         return $this->flushWriteBufferPromise;
     } else {
         $deferred = new Promise\Deferred();
         $this->eventLoop->addWriteStream($this->getStream(), function ($stream) use($deferred) {
             try {
                 $this->write();
                 if ($this->writeBuffer->isEmpty()) {
                     $this->eventLoop->removeWriteStream($stream);
                     $this->flushWriteBufferPromise = null;
                     $deferred->resolve(true);
                 }
             } catch (\Exception $e) {
                 $this->eventLoop->removeWriteStream($stream);
                 $this->flushWriteBufferPromise = null;
                 $deferred->reject($e);
             }
         });
         return $this->flushWriteBufferPromise = $deferred->promise();
     }
 }
 /**
  * Remove the write event listener for the given stream.
  *
  * @param stream $stream The PHP stream resource.
  */
 public function removeWriteStream($stream)
 {
     $this->emit('removeWriteStream', [$stream]);
     $this->loop->removeWriteStream($stream);
 }