/** * Execute a request to the FastCGI application asynchronously * This sends request to application and returns the assigned ID for that request. * You should keep this id for later use with wait_for_response(). Ids are chosen randomly * rather than sequentially to guard against false-positives when using persistent sockets. * In that case it is possible that a delayed response to a request made by a previous script * invocation comes back on this socket and is mistaken for response to request made with same ID * during this request. * * @param array $params Array of parameters * @param String $stdin Content * @return Response * @throws CommunicationException */ public function asyncRequest(array $params, $stdin) { $this->connect(); // Ensure new requestID is not already being tracked do { $this->_requestCounter++; if ($this->_requestCounter >= 65536) { $this->_requestCounter = 1; } $id = $this->_requestCounter; } while (isset($this->_requests[$id])); $request = $this->buildPacket(self::BEGIN_REQUEST, chr(0) . chr(self::RESPONDER) . chr((int) $this->keepAlive) . str_repeat(chr(0), 5), $id); $paramsRequest = ''; foreach ($params as $key => $value) { $paramsRequest .= $this->buildNvpair($key, $value, $id); } if ($paramsRequest) { $request .= $this->buildPacket(self::PARAMS, $paramsRequest, $id); } $request .= $this->buildPacket(self::PARAMS, '', $id); if ($stdin) { $request .= $this->buildPacket(self::STDIN, $stdin, $id); } $request .= $this->buildPacket(self::STDIN, '', $id); if (false === socket_write($this->sock, $request)) { // The developer may wish to close() and re-open the socket throw CommunicationException::socketWrite($this->sock); } $req = new Response($this, $id); $req->state = Response::REQ_STATE_WRITTEN; $this->_requests[$id] = $req; return $req; }
/** * Execute a request to the FastCGI application * * @param array $params Array of parameters * @param String $stdin Content * @throws CommunicationException */ protected function doRequest(array $params, $stdin) { $this->connect(); $request = $this->buildPacket(self::BEGIN_REQUEST, chr(0) . chr(self::RESPONDER) . chr((int) $this->keepAlive) . str_repeat(chr(0), 5)); $paramsRequest = ''; foreach ($params as $key => $value) { $paramsRequest .= $this->buildNvpair($key, $value); } if ($paramsRequest) { $request .= $this->buildPacket(self::PARAMS, $paramsRequest); } $request .= $this->buildPacket(self::PARAMS, ''); if ($stdin) { $request .= $this->buildPacket(self::STDIN, $stdin); } $request .= $this->buildPacket(self::STDIN, ''); // Write the request and break. if (false === @socket_write($this->sock, $request)) { throw CommunicationException::socketWrite($this->sock); } $this->awaitingResponse = true; }