Handles processing the server details from goldsource response

@param \GameQ\Buffer $buffer

@return array
@throws \GameQ\Exception\Protocol
545398
protected function processDetailsGoldSource(Buffer $buffer)
    {

        // Set the result to a new result instance
        $result = new Result();

        $result->add('address', $buffer->readString());
        $result->add('hostname', $buffer->readString());
        $result->add('map', $buffer->readString());
        $result->add('game_dir', $buffer->readString());
        $result->add('game_descr', $buffer->readString());
        $result->add('num_players', $buffer->readInt8());
        $result->add('max_players', $buffer->readInt8());
        $result->add('version', $buffer->readInt8());
        $result->add('dedicated', $buffer->read());
        $result->add('os', $buffer->read());
        $result->add('password', $buffer->readInt8());

        // Mod section
        $result->add('ismod', $buffer->readInt8());

        // We only run these if ismod is 1 (true)
        if ($result->get('ismod') == 1) {
            $result->add('mod_urlinfo', $buffer->readString());
            $result->add('mod_urldl', $buffer->readString());
            $buffer->skip();
            $result->add('mod_version', $buffer->readInt32Signed());
            $result->add('mod_size', $buffer->readInt32Signed());
            $result->add('mod_type', $buffer->readInt8());
            $result->add('mod_cldll', $buffer->readInt8());
        }

        $result->add('secure', $buffer->readInt8());
        $result->add('num_bots', $buffer->readInt8());

        unset($buffer);

        return $result->fetch();
    }
1322166
public static function parseFrame(string &$buffer): Record
    {
        $bufferLength = strlen($buffer);
        if ($bufferLength < FCGI::HEADER_LEN) {
            throw new \RuntimeException("Not enough data in the buffer to parse");
        }
        $recordHeader = unpack(FCGI::HEADER_FORMAT, $buffer);
        $recordType   = $recordHeader['type'];
        if (!isset(self::$classMapping[$recordType])) {
            throw new \DomainException("Invalid FCGI record type {$recordType} received");
        }

        /** @var Record $className */
        $className = self::$classMapping[$recordType];
        $record    = $className::unpack($buffer);


        $offset = FCGI::HEADER_LEN + $record->getContentLength() + $record->getPaddingLength();
        $buffer = substr($buffer, $offset);

        return $record;
    }
1353812
protected function processRules(Buffer $buffer)
    {

        // Set the result to a new result instance
        $result = new Result();

        // Named values
        $inc = -1;
        while ($buffer->getLength()) {
            // Grab the key
            $key = $buffer->readPascalString(1);

            // Make sure mutators don't overwrite each other
            if ($key === 'Mutator') {
                $key .= ++$inc;
            }

            $result->add(strtolower($key), utf8_encode($buffer->readPascalString(1)));
        }

        unset($buffer);

        return $result->fetch();
    }
942399
public function write($buffer)
    {
        $ret = @socket_write($this->resource, $buffer);
        if ($ret === false) {
            throw Exception::createFromSocketResource($this->resource);
        }
        return $ret;
    }
514239
public function processResponse()
    {
        // Init results
        $results = [];

        foreach ($this->packets_response as $response) {
            $buffer = new Buffer($response);

            // Figure out what packet response this is for
            $response_type = $buffer->read(6);

            // Figure out which packet response this is
            if (!array_key_exists($response_type, $this->responses)) {
                throw new Exception(__METHOD__ . " response type '" . bin2hex($response_type) . "' is not valid");
            }

            // Now we need to call the proper method
            $results = array_merge(
                $results,
                call_user_func_array([$this, $this->responses[$response_type]], [$buffer])
            );

            unset($buffer);
        }

        return $results;
    }
243360
public function handle($payload)
    {
        $data = json_decode($payload, true) ?: [];
        $events = (array)@$data['events'];
        $results = [];

        try {
            foreach ($events as $event) {
                $input = $this->captureInput($event);
                $command = $this->registry->findCommand($input);
                $command->input = $input;

                $results[] = $this->commandBus->handle($command);
            }
        } catch (DerailException $e) {
            $results[] = $this->handler->handle($e->command);
        } catch (\Exception $e) {
            $results[] = sprintf('ERROR: %s', $e->getMessage());
        }

        return $results;
    }
1167980
public function compileBody(Buffer $buffer)
    {
        $buffer->writeln('$ret = null;');

        foreach ($this->taskDef['flags'] as $flag => $value) {
            $buffer
                ->write('if (null === $z->resolve(')->asPhp($flag)->raw(', false)) {')->eol()
                ->indent(1)
                ->write('$z->set(')->asPhp($flag)->raw(', ')->asPhp($value)->raw(');')->eol()
                ->indent(-1)
                ->writeln('}')
            ;
        }
        foreach ($this->taskDef['args'] as $node) {
            $node->compile($buffer);
        }
        foreach ($this->taskDef['opts'] as $node) {
            $node->compile($buffer);
        }
        foreach ($this->taskDef['set'] as $node) {
            $node->compile($buffer);
        }
        $buffer->writeln('$skip = false;');
        foreach (array('pre', 'do', 'post') as $scope) {
            if ($scope === 'do') {
                if (!empty($this->taskDef['unless'])) {
                    $buffer->write('if (!$z->resolve(\'FORCE\') &&');
                    $this->taskDef['unless']->compile($buffer);
                    $buffer->raw(') {')->eol()->indent(1);
                    $echoStr = sprintf('echo "%s skipped", because \'unless\' evaluated to true.', join('.', $this->path));
                    $buffer->writeln(sprintf('$z->cmd(%s);', Util::toPhp($echoStr)));
                    $buffer->writeln('$skip = true;');
                    $buffer->indent(-1);
                    $buffer->writeln('}');
                }
                if (!empty($this->taskDef['if'])) {
                    $buffer->write('if (!$z->resolve(\'FORCE\') && !(');
                    $this->taskDef['if']->compile($buffer);
                    $buffer->raw(') ) {')->eol()->indent(1);
                    $echoStr = sprintf('echo "%s skipped", because \'if\' evaluated to false.', join('.', $this->path));
                    $buffer->writeln(sprintf('$z->cmd(%s);', Util::toPhp($echoStr)));
                    $buffer->writeln('$skip = true;');
                    $buffer->indent(-1);
                    $buffer->writeln('}');
                }

                if (!empty($this->taskDef['assert'])) {
                    $buffer->write('if (!(');
                    $this->taskDef['assert']->compile($buffer);
                    $buffer->raw(')) {')->eol()->indent(1);
                    $buffer->writeln('throw new \RuntimeException("Assertion failed");');
                    $buffer->indent(-1)->writeln('}');
                }

                $buffer->writeln('if (!$skip) {');
                $buffer->indent(1);
            }
            foreach ($this->taskDef[$scope] as $i => $cmd) {
                $buffer->write('Debug::enterScope(')->asPhp($scope . '[' . $i . ']')->raw(');')->eol();
                if ($cmd) {
                    $cmd->compile($buffer);
                }
                $buffer->write('Debug::exitScope(')->asPhp($scope . '[' . $i . ']')->raw(');')->eol();
            }

            if ($scope === 'post') {
                $buffer->indent(-1);
                $buffer->writeln('}');
            }
        }
        if (!empty($this->taskDef['yield'])) {
            $buffer->writeln('$ret = ');
            $this->taskDef['yield']->compile($buffer);
            $buffer->write(';');
        }
        $buffer->writeln('return $ret;');
    }
875345
protected function processRules(Buffer $buffer)
    {

        // Set the result to a new result instance
        $result = new Result();

        // Count the number of rules
        $num_rules = $buffer->readInt16Signed();

        // Add the count of the number of rules this server has
        $result->add('num_rules', $num_rules);

        // Rules
        while ($buffer->getLength()) {
            $result->add($buffer->readString(), $buffer->readString());
        }

        unset($buffer);

        return $result->fetch();
    }
1252396
public function consumeArray(Buffer $originalBuffer)
    {
        $buffer = $originalBuffer->consumeSlice($originalBuffer->consumeUint32());
        $data = [];
        while (!$buffer->isEmpty()) {
            $data[] = $this->consumeFieldValue($buffer);
        }
        return $data;
    }
588640
protected function processPlayers(Buffer $buffer)
    {

        // Set the result to a new result instance
        $result = new Result();

        // We need to read the number of players because this response has other data at the end usually
        $num_players = $buffer->readInt8();

        // Player count
        $result->add('num_players', $num_players);

        // No players, no work
        if ($num_players == 0) {
            return $result->fetch();
        }

        // Players list
        for ($player = 0; $player < $num_players; $player++) {
            $result->addPlayer('id', $buffer->readInt8());
            $result->addPlayer('name', $buffer->readString());
            $result->addPlayer('score', $buffer->readInt32Signed());
            $result->addPlayer('time', $buffer->readFloat32());
        }

        // Extra data
        if ($buffer->getLength() > 0) {
            for ($player = 0; $player < $num_players; $player++) {
                $result->addPlayer('deaths', $buffer->readInt32Signed());
                $result->addPlayer('money', $buffer->readInt32Signed());
            }
        }

        unset($buffer);

        return $result->fetch();
    }
648332
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface
    {
        $this->startTransaction();
        $response = $handler->handle($request);
        $this->endTransaction();

        return $response;
    }
343127
protected function handleTag_a_converter($tag, $buffer)
    {
        $output = parent::handleTag_a_converter($tag, $buffer);
        if (!empty($tag['cssSelector'])) {
            // [This link][id]{#id.class}
            $output .= '{' . $tag['cssSelector'] . '}';
        }

        return $output;
    }
1321961
protected function processPlayers(Buffer $buffer)
    {
        // Some games do not have a number of current players
        $playerCount = 0;

        // Set the result to a new result instance
        $result = new Result();

        // Loop until we are out of data
        while ($buffer->getLength()) {
            // Make a new buffer with this block
            $playerInfo = new Buffer($buffer->readString("\x0A"));

            // Add player info
            $result->addPlayer('frags', $playerInfo->readString("\x20"));
            $result->addPlayer('ping', $playerInfo->readString("\x20"));

            // Skip first "
            $playerInfo->skip(1);

            // Add player name, encoded
            $result->addPlayer('name', utf8_encode(trim(($playerInfo->readString('"')))));

            // Skip first "
            $playerInfo->skip(2);

            // Add address
            $result->addPlayer('address', trim($playerInfo->readString('"')));

            // Increment
            $playerCount++;

            // Clear
            unset($playerInfo);
        }

        $result->add('clients', $playerCount);

        // Clear
        unset($buffer, $playerCount);

        return $result->fetch();
    }
1114826
public static function decode($buffer)
    {
        $data = unpack("Npack_len/Ccmd/Nlocal_ip/nlocal_port/Nclient_ip/nclient_port/Nconnection_id/Cflag/ngateway_port/Next_len",
            $buffer);
        if ($data['ext_len'] > 0) {
            $data['ext_data'] = substr($buffer, self::HEAD_LEN, $data['ext_len']);
            if ($data['flag'] & self::FLAG_BODY_IS_SCALAR) {
                $data['body'] = substr($buffer, self::HEAD_LEN + $data['ext_len']);
            } else {
                $data['body'] = unserialize(substr($buffer, self::HEAD_LEN + $data['ext_len']));
            }
        } else {
            $data['ext_data'] = '';
            if ($data['flag'] & self::FLAG_BODY_IS_SCALAR) {
                $data['body'] = substr($buffer, self::HEAD_LEN);
            } else {
                $data['body'] = unserialize(substr($buffer, self::HEAD_LEN));
            }
        }
        return $data;
    }
917149
protected function processPlayers(Buffer $buffer)
    {

        // Set the result to a new result instance
        $result = new Result();

        // Parse players
        while ($buffer->getLength()) {
            // Player id
            if (($id = $buffer->readInt32()) !== 0) {
                // Add the results
                $result->addPlayer('id', $id);
                $result->addPlayer('name', utf8_encode($buffer->readPascalString(1)));
                $result->addPlayer('ping', $buffer->readInt32());
                $result->addPlayer('score', $buffer->readInt32());

                // Skip the next 4, unsure what they are for
                $buffer->skip(4);
            }
        }

        unset($buffer, $id);

        return $result->fetch();
    }
964560
protected function parseEventAndPayload($event, $payload)
    {
        if (is_object($event)) {
            list($payload, $event) = [[$event], get_class($event)];
        }

        return [$event, Arr::wrap($payload)];
    }
1110367
private function clearTokenBuffer(array $buffer)
    {
        foreach ($buffer as $bufferItem) {
            // Clear out associated access token
            if ($bufferItem instanceof AccessToken) {
                // We have to use this method of retrieving the entity because the refresh token
                // is not accurately being set on the Access token entity for some reason.
                $refreshToken = $this->entityManager->getRepository(RefreshToken::class)
                    ->findOneByAccessToken($bufferItem);
                if ($refreshToken) {
                    $this->entityManager->remove($refreshToken);
                }
            }

            // Clear out associated refresh token
            if ($bufferItem instanceof RefreshToken) {
                $accessToken = $bufferItem->getAccessToken();
                if ($accessToken) {
                    $this->entityManager->remove($accessToken);
                }
            }

            $this->entityManager->remove($bufferItem);
        }

        $this->entityManager->flush();
    }