public function parse(Context $context, ?Request $request = null): \Generator
    {
        $header = \unpack('nid/nflags/nqc/nac/nauth/nadd', yield $this->stream->readBuffer($context, 12));
        $this->offset += 12;
        
        $response = new Response($header['id']);
        $response->setFlags($header['flags']);
        
        if ($request && $response->getId() !== $request->getId()) {
            throw new \RuntimeException(\sprintf('Expected DNS message ID is %u, server returned %u', $request->getId(), $response->getId()));
        }
        
        $response->setAuthorityCount($header['auth']);
        $response->setAdditionalCount($header['add']);
        
        // Parse questions:
        for ($i = 0; $i < $header['qc']; $i++) {
            yield from $this->parseQuestion($context, $response);
        }
        
        if ($request && $response->getQuestions() !== $request->getQuestions()) {
            throw new \RuntimeException('DNS server did not return the question(s) in the response message');
        }
        
        // Parse answers:
        for ($i = 0; $i < $header['ac']; $i++) {
            yield from $this->parseAnswer($context, $response);
        }
        
        return $response;
    }
public function parse(Context $context, ?Request $request = null): \Generator
    {
        $header = \unpack('nid/nflags/nqc/nac/nauth/nadd', yield $this->stream->readBuffer($context, 12));
        $this->offset += 12;
        
        $response = new Response($header['id']);
        $response->setFlags($header['flags']);
        
        if ($request && $response->getId() !== $request->getId()) {
            throw new \RuntimeException(\sprintf('Expected DNS message ID is %u, server returned %u', $request->getId(), $response->getId()));
        }
        
        $response->setAuthorityCount($header['auth']);
        $response->setAdditionalCount($header['add']);
        
        // Parse questions:
        for ($i = 0; $i < $header['qc']; $i++) {
            yield from $this->parseQuestion($context, $response);
        }
        
        if ($request && $response->getQuestions() !== $request->getQuestions()) {
            throw new \RuntimeException('DNS server did not return the question(s) in the response message');
        }
        
        // Parse answers:
        for ($i = 0; $i < $header['ac']; $i++) {
            yield from $this->parseAnswer($context, $response);
        }
        
        return $response;
    }
public function parse ( Context $ context , ? Request $ request = null ) : \ Generator { $ header = \ unpack ( 'nid/nflags/nqc/nac/nauth/nadd' , yield $ this -> stream -> readBuffer ( $ context , 12 ) ) ; $ this -> offset += 12 ; $ response = new Response ( $ header [ 'id' ] ) ; $ response -> setFlags ( $ header [ 'flags' ] ) ; if ( $ request && $ response -> getId ( ) !== $ request -> getId ( ) ) { throw new \ RuntimeException ( \ sprintf ( 'Expected DNS message ID is %u, server returned %u' , $ request -> getId ( ) , $ response -> getId ( ) ) ) ; } $ response -> setAuthorityCount ( $ header [ 'auth' ] ) ; $ response -> setAdditionalCount ( $ header [ 'add' ] ) ; // Parse questions: for ( $ i = 0 ; $ i < $ header [ 'qc' ] ; $ i ++ ) { yield from $ this -> parseQuestion ( $ context , $ response ) ; } if ( $ request && $ response -> getQuestions ( ) !== $ request -> getQuestions ( ) ) { throw new \ RuntimeException ( 'DNS server did not return the question(s) in the response message' ) ; } // Parse answers: for ( $ i = 0 ; $ i < $ header [ 'ac' ] ; $ i ++ ) { yield from $ this -> parseAnswer ( $ context , $ response ) ; } return $ response ; }
Read and parse a DNS response message.

@param Context $context Async execution context.
@param Request $request DNS request that has been sent.
@return Response Validated DNS response received from the server.
Read and parse a DNS response message .