public function wait_for_response($requestId, $timeoutMs = 0) {

        if (!isset($this->_requests[$requestId])) {
            throw new \Exception('Invalid request id given');
        }

        // If we already read the response during an earlier call for different id, just return it
        if ($this->_requests[$requestId]['state'] == self::REQ_STATE_OK
            || $this->_requests[$requestId]['state'] == self::REQ_STATE_ERR
            ) {
            return $this->_requests[$requestId]['response'];
        }

        if ($timeoutMs > 0) {
            // Reset timeout on socket for now
            $this->set_ms_timeout($timeoutMs);
        } else {
            $timeoutMs = $this->_readWriteTimeout;
        }

        // Need to manually check since we might do several reads none of which timeout themselves
        // but still not get the response requested
        $startTime = microtime(true);

        do {
            $resp = $this->readPacket();

            if ($resp['type'] == self::STDOUT || $resp['type'] == self::STDERR) {
                if ($resp['type'] == self::STDERR) {
                    $this->_requests[$resp['requestId']]['state'] = self::REQ_STATE_ERR;
                }
                $this->_requests[$resp['requestId']]['response'] .= $resp['content'];
            }
            if ($resp['type'] == self::END_REQUEST) {
                $this->_requests[$resp['requestId']]['state'] = self::REQ_STATE_OK;
                if ($resp['requestId'] == $requestId) { 
                    break;
                }
            }
            if (microtime(true) - $startTime >= ($timeoutMs * 1000)) {
                // Reset
                $this->set_ms_timeout($this->_readWriteTimeout);
                throw new \Exception('Timed out');
            }
        } while ($resp);

        if (!is_array($resp)) {
            $info = stream_get_meta_data($this->_sock);

            // We must reset timeout but it must be AFTER we get info
            $this->set_ms_timeout($this->_readWriteTimeout);

            if ($info['timed_out']) {
                throw new TimedOutException('Read timed out');
            }

            if ($info['unread_bytes'] == 0
                    && $info['blocked']
                    && $info['eof']) {
                throw new ForbiddenException('Not in white list. Check listen.allowed_clients.');
            }

            throw new \Exception('Read failed');
        }

        // Reset timeout
        $this->set_ms_timeout($this->_readWriteTimeout);

        switch (ord($resp['content']{4})) {
            case self::CANT_MPX_CONN:
                throw new \Exception('This app can\'t multiplex [CANT_MPX_CONN]');
                break;
            case self::OVERLOADED:
                throw new \Exception('New request rejected; too busy [OVERLOADED]');
                break;
            case self::UNKNOWN_ROLE:
                throw new \Exception('Role value not known [UNKNOWN_ROLE]');
                break;
            case self::REQUEST_COMPLETE:
                return $this->_requests[$requestId]['response'];
        }
    }
public function wait_for_response($requestId, $timeoutMs = 0) {

        if (!isset($this->_requests[$requestId])) {
            throw new \Exception('Invalid request id given');
        }

        // If we already read the response during an earlier call for different id, just return it
        if ($this->_requests[$requestId]['state'] == self::REQ_STATE_OK
            || $this->_requests[$requestId]['state'] == self::REQ_STATE_ERR
            ) {
            return $this->_requests[$requestId]['response'];
        }

        if ($timeoutMs > 0) {
            // Reset timeout on socket for now
            $this->set_ms_timeout($timeoutMs);
        } else {
            $timeoutMs = $this->_readWriteTimeout;
        }

        // Need to manually check since we might do several reads none of which timeout themselves
        // but still not get the response requested
        $startTime = microtime(true);

        do {
            $resp = $this->readPacket();

            if ($resp['type'] == self::STDOUT || $resp['type'] == self::STDERR) {
                if ($resp['type'] == self::STDERR) {
                    $this->_requests[$resp['requestId']]['state'] = self::REQ_STATE_ERR;
                }
                $this->_requests[$resp['requestId']]['response'] .= $resp['content'];
            }
            if ($resp['type'] == self::END_REQUEST) {
                $this->_requests[$resp['requestId']]['state'] = self::REQ_STATE_OK;
                if ($resp['requestId'] == $requestId) { 
                    break;
                }
            }
            if (microtime(true) - $startTime >= ($timeoutMs * 1000)) {
                // Reset
                $this->set_ms_timeout($this->_readWriteTimeout);
                throw new \Exception('Timed out');
            }
        } while ($resp);

        if (!is_array($resp)) {
            $info = stream_get_meta_data($this->_sock);

            // We must reset timeout but it must be AFTER we get info
            $this->set_ms_timeout($this->_readWriteTimeout);

            if ($info['timed_out']) {
                throw new TimedOutException('Read timed out');
            }

            if ($info['unread_bytes'] == 0
                    && $info['blocked']
                    && $info['eof']) {
                throw new ForbiddenException('Not in white list. Check listen.allowed_clients.');
            }

            throw new \Exception('Read failed');
        }

        // Reset timeout
        $this->set_ms_timeout($this->_readWriteTimeout);

        switch (ord($resp['content']{4})) {
            case self::CANT_MPX_CONN:
                throw new \Exception('This app can\'t multiplex [CANT_MPX_CONN]');
                break;
            case self::OVERLOADED:
                throw new \Exception('New request rejected; too busy [OVERLOADED]');
                break;
            case self::UNKNOWN_ROLE:
                throw new \Exception('Role value not known [UNKNOWN_ROLE]');
                break;
            case self::REQUEST_COMPLETE:
                return $this->_requests[$requestId]['response'];
        }
    }
public function wait_for_response ( $ requestId , $ timeoutMs = 0 ) { if ( ! isset ( $ this -> _requests [ $ requestId ] ) ) { throw new \ Exception ( 'Invalid request id given' ) ; } // If we already read the response during an earlier call for different id, just return it if ( $ this -> _requests [ $ requestId ] [ 'state' ] == self :: REQ_STATE_OK || $ this -> _requests [ $ requestId ] [ 'state' ] == self :: REQ_STATE_ERR ) { return $ this -> _requests [ $ requestId ] [ 'response' ] ; } if ( $ timeoutMs > 0 ) { // Reset timeout on socket for now $ this -> set_ms_timeout ( $ timeoutMs ) ; } else { $ timeoutMs = $ this -> _readWriteTimeout ; } // Need to manually check since we might do several reads none of which timeout themselves // but still not get the response requested $ startTime = microtime ( true ) ; do { $ resp = $ this -> readPacket ( ) ; if ( $ resp [ 'type' ] == self :: STDOUT || $ resp [ 'type' ] == self :: STDERR ) { if ( $ resp [ 'type' ] == self :: STDERR ) { $ this -> _requests [ $ resp [ 'requestId' ] ] [ 'state' ] = self :: REQ_STATE_ERR ; } $ this -> _requests [ $ resp [ 'requestId' ] ] [ 'response' ] .= $ resp [ 'content' ] ; } if ( $ resp [ 'type' ] == self :: END_REQUEST ) { $ this -> _requests [ $ resp [ 'requestId' ] ] [ 'state' ] = self :: REQ_STATE_OK ; if ( $ resp [ 'requestId' ] == $ requestId ) { break ; } } if ( microtime ( true ) - $ startTime >= ( $ timeoutMs * 1000 ) ) { // Reset $ this -> set_ms_timeout ( $ this -> _readWriteTimeout ) ; throw new \ Exception ( 'Timed out' ) ; } } while ( $ resp ) ; if ( ! is_array ( $ resp ) ) { $ info = stream_get_meta_data ( $ this -> _sock ) ; // We must reset timeout but it must be AFTER we get info $ this -> set_ms_timeout ( $ this -> _readWriteTimeout ) ; if ( $ info [ 'timed_out' ] ) { throw new TimedOutException ( 'Read timed out' ) ; } if ( $ info [ 'unread_bytes' ] == 0 && $ info [ 'blocked' ] && $ info [ 'eof' ] ) { throw new ForbiddenException ( 'Not in white list. Check listen.allowed_clients.' ) ; } throw new \ Exception ( 'Read failed' ) ; } // Reset timeout $ this -> set_ms_timeout ( $ this -> _readWriteTimeout ) ; switch ( ord ( $ resp [ 'content' ] { 4 } ) ) { case self :: CANT_MPX_CONN : throw new \ Exception ( 'This app can\'t multiplex [CANT_MPX_CONN]' ) ; break ; case self :: OVERLOADED : throw new \ Exception ( 'New request rejected; too busy [OVERLOADED]' ) ; break ; case self :: UNKNOWN_ROLE : throw new \ Exception ( 'Role value not known [UNKNOWN_ROLE]' ) ; break ; case self :: REQUEST_COMPLETE : return $ this -> _requests [ $ requestId ] [ 'response' ] ; } }
Blocking call that waits for response to specific request

@param Integer $requestId
@param Integer $timeoutMs [optional] the number of milliseconds to wait. Defaults to the ReadWriteTimeout value set.
@return string  response body
Blocking call that waits for response to specific request