/** * @return Connection */ protected function getConnection() { if ($this->connection) { $this->connection->close(); unset($this->connection); } $client = stream_socket_client($this->getControllerSocket()); $this->connection = new Connection($client, $this->loop); return $this->connection; }
/** * @return \React\Socket\Connection */ protected function getConnection() { if ($this->connection) { $this->connection->close(); unset($this->connection); } $client = stream_socket_client('tcp://127.0.0.1:5500'); $this->connection = new \React\Socket\Connection($client, $this->loop); return $this->connection; }
/** * Disconnect from server */ public function disconnect() { $this->connected = false; if ($this->socket instanceof Connection) { $this->socket->close(); } }
public function bye() { if ($this->connection->isWritable()) { $this->connection->write(json_encode(array('cmd' => 'unregister', 'pid' => getmypid()))); $this->connection->close(); } $this->loop->stop(); }
public function accept(Socket $socket) { if (count($this->connections) >= $this->maxConnections) { $socket->write(['alias' => 'capacity_reached']); $socket->close(); throw new \RuntimeException('Server capacity reached'); } $player = $this->playerFactory->newSocketPlayer(sprintf('PlayerBot%d', ++$this->connectionIndex), null, 5000, $socket); $connection = $player->getConnection(); $this->connections->pushBack($connection); $this->handleSocketEvents($connection); $this->handlePlayerNameChange($connection); $this->handlePlayerConnection($connection); }
/** * Shuts down the event loop. This basically exits the process. */ public function shutdown() { if ($this->inShutdown) { return; } $this->inShutdown = true; $this->sendCurrentFiles(); $this->loop->tick(); if ($this->connection && $this->connection->isWritable()) { $this->connection->close(); } if ($this->server) { @$this->server->shutdown(); } if ($this->loop) { $this->loop->tick(); @$this->loop->stop(); } exit; }
/** * Shuts down the event loop. This basically exits the process. */ public function shutdown() { if ($this->inShutdown) { return; } if ($this->errorLogger && ($logs = $this->errorLogger->cleanLogs())) { $messages = array_map(function ($item) { //array($level, $message, $context); $message = $item[1]; $context = $item[2]; if (isset($context['file'])) { $message .= ' in ' . $context['file'] . ':' . $context['line']; } if (isset($context['stack'])) { foreach ($context['stack'] as $idx => $stack) { $message .= PHP_EOL . sprintf("#%d: %s%s %s%s", $idx, isset($stack['class']) ? $stack['class'] . '->' : '', $stack['function'], isset($stack['file']) ? 'in' . $stack['file'] : '', isset($stack['line']) ? ':' . $stack['line'] : ''); } } return $message; }, $logs); error_log(implode(PHP_EOL, $messages)); } $this->inShutdown = true; if ($this->loop) { $this->sendCurrentFiles(); $this->loop->tick(); } if ($this->controller && $this->controller->isWritable()) { $this->controller->close(); } if ($this->server) { @$this->server->shutdown(); } if ($this->loop) { $this->sendCurrentFiles(); $this->loop->tick(); @$this->loop->stop(); } exit; }
/** * A slave sent a `register` command. * * @param array $data * @param Connection $conn */ protected function commandRegister(array $data, Connection $conn) { $pid = (int) $data['pid']; $port = (int) $data['port']; if (!isset($this->slaves[$port]) || !$this->slaves[$port]['waitForRegister']) { if ($this->output->isVeryVerbose()) { $this->output->writeln(sprintf('<error>Worker #%d wanted to register on master which was not expected.</error>', $port)); } $conn->close(); return; } $this->ports[spl_object_hash($conn)] = $port; if ($this->output->isVeryVerbose()) { $this->output->writeln(sprintf('Worker #%d registered. Waiting for application bootstrap ... ', $port)); } $this->slaves[$port]['pid'] = $pid; $this->slaves[$port]['connection'] = $conn; $this->slaves[$port]['ready'] = false; $this->slaves[$port]['waitForRegister'] = false; $this->slaves[$port]['duringBootstrap'] = true; $this->sendMessage($conn, 'bootstrap'); }
/** * Disconnect from server */ public function disconnect() { $this->connected = false; $this->socket->close(); }
/** * Disconnect client */ public function disconnect() { $this->clientConnection->close(); }