public function handleConnection(ConnectionInterface $connection) { $parser = $this->protocol->createResponseParser(); $parser = new RequestParser(); $that = $this; $business = $this->business; if ($this->config->get('requirepass') !== '') { $business = new AuthInvoker($business); } $client = new Client($connection, $business, reset($this->databases)); $this->clients->attach($client); $connection->on('data', function ($data) use($parser, $that, $client) { try { $messages = $parser->pushIncoming($data); } catch (ParserException $e) { $that->emit('error', array($e, $client)); return; } foreach ($messages as $message) { $that->handleRequest($message, $client); } }); $connection->on('close', function () use($that, $client) { $that->handleDisconnection($client); }); $this->emit('connection', array($client, $this)); }
/** * Triggered when a new connection is received from React * @param \React\Socket\ConnectionInterface $conn */ public function handleConnect($conn) { $conn->decor = new IoConnection($conn); $conn->decor->resourceId = (int) $conn->stream; $conn->decor->remoteAddress = $conn->getRemoteAddress(); $this->app->onOpen($conn->decor); $conn->on('data', $this->handlers[0]); $conn->on('end', $this->handlers[1]); $conn->on('error', $this->handlers[2]); }
public function connect(ConnectionInterface $conn) { $waiting = $this->waiting; if (null === $waiting || !$waiting->isReadable()) { $this->waiting = $conn; $conn->emit('wait'); return; } $conn->pipe($waiting)->pipe($conn); $this->waiting = null; }
private function handleConnection(ConnectionInterface $connection) { $decoder = new Decoder(function (RequestInterface $request) use($connection) { $cb = function (Response $response) use($connection) { foreach ($response->toRecords() as $r) { $connection->write($r->encode()); } }; $this->requestHandler->handle($request, $cb); }); $connection->pipe($decoder); }
public function connect(ConnectionInterface $conn) { $this->logger->info(sprintf("New connection %s", $conn->id)); $conn->on('end', function () use($conn) { $this->logger->info(sprintf("Connection %s disconnected", $conn->id)); }); $conn->on('pipe', function ($source) use($conn) { if (!empty($conn->pipeLogged) || !empty($source->pipeLogged)) { return; } $this->logger->info(sprintf("Pairing up connection %s with waiting connection %s", $source->id, $conn->id)); $conn->pipeLogged = $conn->pipeLogged = true; }); $this->app->connect($conn); }
public function __construct(ConnectionInterface $src) { $this->src = $src; $src->on('data', function ($data) { $this->buffer .= $data; if ($this->dst) { $this->dst->write($this->buffer); $this->buffer = ''; } }); $src->on('end', function () { $this->end = true; if ($this->dst) { $this->dst->end(); } }); }
public function close() { if ($this->closed) { return; } $this->closed = true; $this->emit('close'); $this->removeAllListeners(); $this->connection->close(); }
/** * @param ConnectionInterface $conn */ public function onConnection(ConnectionInterface $conn) { $labels = ['remote' => $conn->getRemoteAddress(), 'request_id' => Uuid::uuid4()->toString()]; $request = new HttpdRequest($conn->getRemoteAddress(), $labels); $response = new HttpdResponse($conn, $labels); // Wire request and response event to global observer $request->subscribe($this->observable); $response->subscribe($this->observable); // Remote connection closed, notify everything's done $conn->on("end", [$request, 'notifyCompleted']); $conn->on("end", function () use($response, $labels) { $response->notifyNext(new Event("/httpd/connection/closed", $response, $labels)); $response->notifyCompleted(); }); // No observers we can't do anything if (!$this->observers) { $response->sendError("No route defined", 404); return; } $parser = new RequestParser($request); $conn->on('data', array($parser, 'parse')); // Head is received we can dispatch route $request->take(1)->subscribeCallback(function () use($request, $response, $labels) { $this->dispatch($request, $response, $labels); }); }
public function connect(ConnectionInterface $conn) { $conn->write(sprintf("Hello %s!\n", $conn->id)); $conn->on('wait', function () use($conn) { $conn->write("Please wait until a partner connects.\n"); }); $conn->on('pipe', function ($source) use($conn) { $message = "You are now talking to %s.\n"; $conn->write(sprintf($message, $source->id)); }); $this->app->connect($conn); }
/** * Handles the given data. * * @param string $data Consumed data * @param string $consumer Command to execute * @param ConnectionInterface $connection The connection to the producer * * @return void */ public function handleData($data, $consumer, ConnectionInterface $connection, $callback = null) { $command = sprintf('%s "%s"', $consumer, addslashes($data)); if ($callback && is_callable($callback)) { call_user_func($callback, $data); } $this->processFactory->newProcess($command)->run(); $connection->close(); }
/** * {@inheritdoc} */ public function close() { $this->conn->end(); }
/** * Detect and run command received from client. * @param array $arguments * @param ConnectionInterface $connection */ private function runCommand($arguments, ConnectionInterface $connection) { try { $commandClass = array_shift($arguments); if (null !== $this->getLogger()) { $this->log('Command from ' . $connection->getRemoteAddress() . ": [{$commandClass}] " . join(', ', array_map('json_encode', $arguments))); } if (isset($this->commands[$commandClass])) { $command = $this->commands[$commandClass]; } else { if (!class_exists($commandClass)) { throw new \RuntimeException("Command class `{$commandClass}` does not found."); } $command = new $commandClass($this); if (!$command instanceof Command\CommandInterface) { throw new \RuntimeException("Every command must implement Command\\CommandInterface."); } $this->commands[$commandClass] = $command; } $result = $command->run($arguments, $connection); $result = [self::RESULT, $result]; } catch (\Exception $e) { $result = [self::EXCEPTION, get_class($e), $e->getMessage()]; $this->log('Exception: ' . $e->getMessage()); } $connection->write(json_encode($result) . self::END_OF_RESULT); }
/** * Executes a Doctrine command. * * @param \React\Socket\ConnectionInterface $conn The connection resource * @param array $command The array with the command elements to execute * * @return void */ public function doctrine($conn, $command) { // switch to the new runlevel $result = $this->synchronized(function ($self, $cmd) { // wait till the previous commands has been finished while ($self->locked === true) { sleep(1); } // set connection and command name $self->command = DoctrineCommand::COMMAND; // lock process $self->locked = true; $self->params = $cmd; $self->result = null; // notify the AS to execute the command $self->notify(); // wait for the result of the command while ($self->result == null) { $self->wait(10000); } // return the result return $self->result; }, $this, $command); // write the result to the output $conn->write($result); }
public function connection(ConnectionInterface $connection) { $requestEmitter = new RequestProcessor($connection, $this->queue); $connection->on('data', [$requestEmitter, 'parseRequest']); $requestEmitter->on('request', [$requestEmitter, 'runRequest']); }
private function assertClosed(ConnectionInterface $conn) { $this->assertFalse($conn->isReadable()); $this->assertFalse($conn->isWritable()); }