private function acquireCommand(React\Socket\Connection $connection, $name) { if (isset($this->locks[$name])) { if (isset($this->connections[$connection][$name])) { if ($this->connections[$connection][$name]) { $connection->write(self::SUCCESS); return; } else { $connection->write(self::WAIT); return; } } else { $this->locks[$name][] = $connection; $connectionLocks = $this->connections[$connection]; $connectionLocks[$name] = false; $this->connections[$connection] = $connectionLocks; $connection->write(self::WAIT); return; } } else { $this->locks[$name] = new \SplDoublyLinkedList(); $this->locks[$name][] = $connection; $connectionLocks = $this->connections[$connection]; $connectionLocks[$name] = true; $this->connections[$connection] = $connectionLocks; $connection->write(self::SUCCESS); return; } }
/** * @covers React\Socket\Connection::write */ public function testWriteError() { $socket = "Silly developer, you can't write to to a string!"; $loop = $this->createWriteableLoopMock(); $conn = new Connection($socket, $loop); $conn->on('error', $this->expectCallableOnce()); $conn->write('Attempting to write to a string'); }
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); }
public function onWeb(\React\Socket\Connection $incoming) { $slaveId = $this->getNextSlave(); $port = $this->slaves[$slaveId]['port']; $client = stream_socket_client('tcp://127.0.0.1:' . $port); $redirect = new \React\Stream\Stream($client, $this->loop); $redirect->on('close', function () use($incoming) { $incoming->end(); }); $incoming->on('data', function ($data) use($redirect) { $redirect->write($data); }); $redirect->on('data', function ($data) use($incoming) { $incoming->write($data); }); }
/** * Handles incoming connections from $this->port. Basically redirects to a slave. * * @param Connection $incoming incoming connection from react */ public function onWeb(Connection $incoming) { // preload sent data from $incoming to $buffer, otherwise it would be lost, // since getNextSlave is async. $redirect = null; $buffer = ''; $incoming->on('data', function ($data) use(&$redirect, &$buffer) { if (!$redirect) { $buffer .= $data; } }); $this->getNextSlave(function ($id) use($incoming, &$buffer, &$redirect) { $slave =& $this->slaves[$id]; $slave['busy'] = true; $slave['connections']++; $this->tcpConnector->create('127.0.0.1', $slave['port'])->then(function (\React\Stream\Stream $stream) use(&$buffer, $redirect, $incoming, &$slave) { $stream->write($buffer); $stream->on('close', function () use($incoming, &$slave) { $slave['busy'] = false; $slave['connections']--; $slave['requests']++; $incoming->end(); if ($slave['requests'] > $this->maxRequests) { $info['ready'] = false; $slave['connection']->close(); } if ($slave['closeWhenFree']) { $slave['connection']->close(); } }); $stream->on('data', function ($data) use($incoming) { $incoming->write($data); }); $incoming->on('data', function ($data) use($stream) { $stream->write($data); }); $incoming->on('close', function () use($stream) { $stream->close(); }); }); }); }
/** * Handles incoming connections from $this->port. Basically redirects to a slave. * * @param Connection $incoming incoming connection from react */ public function onWeb(Connection $incoming) { //preload sent data from $incoming to $buffer, otherwise it would be lost, //since getNextSlave is async. $redirectionActive = false; $connectionOpen = true; $incomingBuffer = ''; $incoming->on('data', function ($data) use(&$redirectionActive, &$incomingBuffer) { if (!$redirectionActive) { $incomingBuffer .= $data; } }); $redirectionTries = 0; $incoming->on('close', function () use(&$redirectionActive, &$redirectionTries, &$connectionOpen) { $connectionOpen = false; }); $start = microtime(true); $redirectionTries++; $redirectRequest = function ($id) use(&$redirectRequest, &$incoming, &$incomingBuffer, &$redirectionActive, $start, &$redirectionTries, &$connectionOpen) { if (!$connectionOpen) { //since the initial connection of a client and getting a free worker the client meanwhile closed the connection, //so stop anything here. return; } if (!is_resource($incoming->stream)) { //Firefox closes somehow a connection directly after opening, at this state we need to check //whether the connection is still alive, to keep going. This check prevents the server from crashing return; } $took = microtime(true) - $start; if ($this->output->isVeryVerbose() && $took > 1) { $this->output->writeln(sprintf('<info>took abnormal %f seconds for choosing next free worker</info>', $took)); } $slave =& $this->slaves[$id]; $slave['busy'] = true; $slave['connections']++; $start = microtime(true); $stream = stream_socket_client($slave['host'], $errno, $errstr, $this->timeout); if (!$stream || !is_resource($stream)) { //we failed to connect to the worker. Maybe because of timeouts or it is in a crashed state //and is currently dieing. //since we don't know whether the worker is only very busy or dieing we just //set it back to available worker list. If it is really dying it will be //removed from the available worker list by itself during connection:close event. $slave['busy'] = false; $slave['connections']--; if ($this->output->isVeryVerbose()) { $this->output->writeln(sprintf('<error>Connection to worker %d failed. Try #%d, took %fs. ' . 'Try increasing your timeout of %d. Error message: [%d] %s</error>', $id, $redirectionTries, microtime(true) - $start, $this->timeout, $errno, $errstr)); } //Try next free client. It's important to do this here due to $incomingBuffer. $redirectionTries++; $this->getNextSlave($redirectRequest); return; } $connection = new \React\Socket\Connection($stream, $this->loop); $took = microtime(true) - $start; if ($this->output->isVeryVerbose() && $took > 1) { $this->output->writeln(sprintf('<info>took abnormal %f seconds for connecting to :%d</info>', $took, $slave['port'])); } $start = microtime(true); $headersToReplace = ['X-PHP-PM-Remote-IP' => $incoming->getRemoteAddress()]; $headerRedirected = false; if ($this->isHeaderEnd($incomingBuffer)) { $incomingBuffer = $this->replaceHeader($incomingBuffer, $headersToReplace); $headerRedirected = true; $connection->write($incomingBuffer); } $redirectionActive = true; $connection->on('close', function () use($incoming, &$slave, $start) { $took = microtime(true) - $start; if ($this->output->isVeryVerbose() && $took > 1) { $this->output->writeln(sprintf('<info>took abnormal %f seconds for handling a connection</info>', $took)); } $slave['busy'] = false; $slave['connections']--; $slave['requests']++; $incoming->end(); /** @var Connection $connection */ $connection = $slave['connection']; if ($slave['requests'] >= $this->maxRequests) { $slave['ready'] = false; $this->output->writeln(sprintf('Restart worker #%d because it reached maxRequests of %d', $slave['port'], $this->maxRequests)); $connection->close(); } else { if ($slave['closeWhenFree']) { $connection->close(); } } }); $connection->on('data', function ($buffer) use($incoming) { $incoming->write($buffer); }); $incoming->on('data', function ($buffer) use($connection, &$incomingBuffer, $headersToReplace, &$headerRedirected) { if (!$headerRedirected) { $incomingBuffer .= $buffer; if ($this->isHeaderEnd($incomingBuffer)) { $incomingBuffer = $this->replaceHeader($incomingBuffer, $headersToReplace); $headerRedirected = true; $connection->write($incomingBuffer); } else { //head has not completely received yet, wait } } else { //incomingBuffer has already been redirected, so redirect now buffer per buffer $connection->write($buffer); } }); $incoming->on('close', function () use($connection) { $connection->close(); }); }; $this->getNextSlave($redirectRequest); }
/** * Override the base write to add encryption if we're in an encypted stage * * @param $data String the data to write * @return bool * @Override */ public function write($data) { //encrypt the data if needed if ($this->secret !== null) { mcrypt_generic_init($this->encryptor, $this->secret, $this->secret); $data = mcrypt_generic($this->encryptor, $data); mcrypt_generic_deinit($this->encryptor); } return parent::write($data); }
protected function serve404Error(SocketConnection $conn) { $response = new Response('file not found.', Response::HTTP_NOT_FOUND); $conn->write($response); $conn->end(); }
/** * Sends a message through $conn. * * @param Connection $conn * @param string $command * @param array $message */ protected function sendMessage(Connection $conn, $command, array $message = []) { $message['cmd'] = $command; $conn->write(json_encode($message) . PHP_EOL); }
protected function writeBoard() { $ui = Ui::printStatus($this->game); $this->connection->write($ui); echo $ui; }
/** * Send some command to client via specified protocol * * @param $command * * @return bool|void */ public function send($command) { $toSend = $this->protocol->prepareCommand($command); return $this->clientConnection->write($toSend); }