diff --git a/core/Files/AsynchronousFileReader.php b/core/Files/AsynchronousFileReader.php index 1e7ba86e..237f721d 100644 --- a/core/Files/AsynchronousFileReader.php +++ b/core/Files/AsynchronousFileReader.php @@ -3,6 +3,7 @@ namespace ManiaControl\Files; use cURL\Request; +use cURL\RequestsQueue; use ManiaControl\General\UsageInformationAble; use ManiaControl\General\UsageInformationTrait; use ManiaControl\ManiaControl; @@ -27,8 +28,9 @@ class AsynchronousFileReader implements UsageInformationAble { */ /** @var ManiaControl $maniaControl */ private $maniaControl = null; - /** @var Request[] $requests */ - private $requests = array(); + + /** @var \cURL\RequestsQueue|null $requestQueue */ + private $requestQueue = null; /** * Construct a new Asynchronous File Reader Instance @@ -37,19 +39,22 @@ class AsynchronousFileReader implements UsageInformationAble { */ public function __construct(ManiaControl $maniaControl) { $this->maniaControl = $maniaControl; + $this->requestQueue = new RequestsQueue(); } /** * Append available Data of active Requests */ public function appendData() { - foreach ($this->requests as $key => $request) { - if ($request->socketPerform()) { - $request->socketSelect(); - } else { - unset($this->requests[$key]); + do { + if (($count = $this->requestQueue->count()) == 0) { + break; } - } + + if ($this->requestQueue->socketPerform()) { + $this->requestQueue->socketSelect(); + } + } while ($count != $this->requestQueue->count()); } /** @@ -91,6 +96,6 @@ class AsynchronousFileReader implements UsageInformationAble { * @param Request $request */ public function addRequest(Request $request) { - array_push($this->requests, $request); + $request->attachTo($this->requestQueue); } } diff --git a/libs/curl-easy/cURL/Request.php b/libs/curl-easy/cURL/Request.php index 199ebfac..6fb263b0 100644 --- a/libs/curl-easy/cURL/Request.php +++ b/libs/curl-easy/cURL/Request.php @@ -33,18 +33,6 @@ class Request extends EventDispatcher implements RequestInterface $this->ch = curl_init(); } - /** - * Closes cURL resource and frees the memory. - * It is neccessary when you make a lot of requests - * and you want to avoid fill up the memory. - */ - public function __destruct() - { - if (isset($this->ch)) { - curl_close($this->ch); - } - } - /** * Get the cURL\Options instance * Creates empty one if does not exist @@ -61,7 +49,7 @@ class Request extends EventDispatcher implements RequestInterface /** * Sets Options - * + * * @param Options $options Options * @return void */ @@ -69,6 +57,17 @@ class Request extends EventDispatcher implements RequestInterface { $this->options = $options; } + + /** + * Closes cURL resource and frees the memory. + * It is neccessary when you make a lot of requests + * and you want to avoid fill up the memory. + */ + public function __destruct() { + if (isset($this->ch)) { + curl_close($this->ch); + } + } /** * Returns cURL raw resource @@ -116,6 +115,20 @@ class Request extends EventDispatcher implements RequestInterface return $response; } + /** + * Binds the request to a given RequestQueue. + * + * @param \cURL\RequestsQueue $requestsQueue + * @throws \cURL\Exception + */ + public function attachTo(RequestsQueue $requestsQueue) { + if (isset($this->queue)) { + throw new Exception('Already bound to a RequestQueue.'); + } + $this->queue = $requestsQueue; + $this->queue->attach($this); + } + /** * Creates new RequestsQueue with single Request attached to it * and calls RequestsQueue::socketPerform() method.