public function toggle(Stream $stream, $toggle) { if ($stream instanceof SecureStream) { $stream = $stream->getDecorating(); } // pause actual stream instance to continue operation on raw stream socket $stream->pause(); // TODO: add write() event to make sure we're not sending any excessive data $deferred = new Deferred(); // get actual stream socket from stream instance $socket = $stream->getStream(); $toggleCrypto = function () use($socket, $deferred, $toggle) { $this->toggleCrypto($socket, $deferred, $toggle); }; $this->loop->addReadStream($socket, $toggleCrypto); $toggleCrypto(); return $deferred->promise()->then(function () use($stream, $toggle) { if ($toggle && $this->wrapSecure) { return new SecureStream($stream, $this->loop); } $stream->resume(); return $stream; }, function ($error) use($stream) { $stream->resume(); throw $error; }); }
/** * @inheritdoc */ public function resume() : self { if ($this->isReadable()) { $this->loop->addReadStream($this->stream, [$this, 'handleData']); } return $this; }
/** * {@inheritDoc} */ public function listen(int $port, string $host = '127.0.0.1') { if (strpos($host, ':') !== false) { // enclose IPv6 addresses in square brackets before appending port $host = '[' . $host . ']'; } $this->socket = @stream_socket_server("tcp://{$host}:{$port}", $errno, $errstr); if (false === $this->socket) { $message = sprintf('Could not bind to tcp://%s:%s: %s', $host, $port, $errstr); throw new ConnectionException($message, $errno); } stream_set_blocking($this->socket, 0); $this->loop->addReadStream($this->socket, function ($master) { $newSocket = @stream_socket_accept($master, 0); if (false === $newSocket) { $this->emit('error', [new \RuntimeException('Error accepting new connection')]); return; } $this->handleConnection($newSocket); }); }