Ejemplo n.º 1
0
 protected function buildActionCallbacks()
 {
     # TODO: Check cache and get filters from there instead of re-building them.
     $callbacks = new Callbacks();
     $callbacks->defineCallbacks(['action' => ['terminator' => 'isPerformed']]);
     $filters = $this->extractFilters();
     $filters = $this->convertFilters($filters);
     $callbacks->setCallbacks('action', $filters);
     return $callbacks;
 }
Ejemplo n.º 2
0
 /**
  ** Init
  ** Register the callback for OnPlayerKeyStateChange
  **/
 public static function Init()
 {
     sampkeys_init(MAX_PLAYERS);
     Keybinds::$actions = array();
     Callbacks::Instance()->Register(cOnPlayerKeyStateChange, null, array('Keybinds', 'OnPlayerKeyStateChange'));
     Callbacks::Instance()->Register(cOnPlayerConnect, null, array('Keybinds', 'OnPlayerConnect'));
     /* Make the list of key translations */
     Keybinds::$keyt_onfoot[KEY_ACTION] = 'PED_ANSWER_PHONE';
     Keybinds::$keyt_onfoot[KEY_CROUCH] = 'PED_DUCK';
     Keybinds::$keyt_onfoot[KEY_FIRE] = 'PED_FIREWEAPON';
     Keybinds::$keyt_onfoot[KEY_SPRINT] = 'PED_SPRINT';
     Keybinds::$keyt_onfoot[KEY_SECONDARY_ATTACK] = 'VEHICLE_ENTER_EXIT';
     Keybinds::$keyt_onfoot[KEY_LOOK_BEHIND] = 'PED_LOOKBEHIND';
     Keybinds::$keyt_onfoot[KEY_WALK] = 'SNEAK_ABOUT';
     Keybinds::$keyt_onfoot[KEY_ANALOG_LEFT] = 'VEHICLE_LOOKLEFT';
     Keybinds::$keyt_onfoot[KEY_ANALOG_RIGHT] = 'VEHICLE_LOOKRIGHT';
     Keybinds::$keyt_vehicle[KEY_FIRE] = 'VEHICLE_FIREWEAPON';
     Keybinds::$keyt_vehicle[KEY_SECONDARY_ATTACK] = 'VEHICLE_FIREWEAPON_ALT';
     Keybinds::$keyt_vehicle[KEY_LOOK_RIGHT] = 'VEHICLE_LOOKRIGHT';
     Keybinds::$keyt_vehicle[KEY_LOOK_LEFT] = 'VEHICLE_LOOKLEFT';
     Keybinds::$keyt_vehicle[KEY_HANDBRAKE] = 'VEHICLE_HANDBRAKE';
     Keybinds::$keyt_vehicle[KEY_SUBMISSION] = 'TOGGLE_SUBMISSIONS';
     Keybinds::$keyt_vehicle[KEY_ANALOG_UP] = 'VEHICLE_TURRETUP';
     Keybinds::$keyt_vehicle[KEY_ANALOG_DOWN] = 'VEHICLE_TURRETDOWN';
     Keybinds::$keyt_vehicle[KEY_ANALOG_LEFT] = 'VEHICLE_TURRETLEFT';
     Keybinds::$keyt_vehicle[KEY_ANALOG_RIGHT] = 'VEHICLE_TURRETRIGHT';
 }
Ejemplo n.º 3
0
 public static function Init()
 {
     Callbacks::Instance()->Register(cOnPlayerText, null, array('Messages', 'OnPlayerText'));
     Callbacks::Instance()->Register(cOnPlayerPrivmsg, null, array('Messages', 'OnPlayerPrivmsg'));
     CommandHandler::Register('l', 1, null, array('Messages', 'cmdLocal'), '[message]', 1, -1000);
     CommandHandler::Register('s', 1, null, array('Messages', 'cmdShout'), '[message]', 1, -1000);
     CommandHandler::Register('w', 2, null, array('Messages', 'cmdWhisper'), '[id] [message]', 1, -1000);
     CommandHandler::Register('me', 1, null, array('Messages', 'cmdMe'), '[message]', 1, -1000);
     CommandHandler::Register('/', 1, null, array('Messages', 'cmdAdminchat'), '[message]', 1, -1000);
 }
Ejemplo n.º 4
0
 public static function Init()
 {
     Callbacks::Instance()->Register(cOnPlayerPickUpPickup, null, array('Locations', 'OnPickUpPickup'));
     Callbacks::Instance()->Register(cOnPlayerEnterCheckpoint, null, array('Locations', 'OnPlayerEnterCheckpoint'));
     CommandHandler::Register('enter', 0, null, array('Locations', 'cmdEnter'), '', 1, -1000);
     CommandHandler::Register('exit', 0, null, array('Locations', 'cmdExit'), '', 1, -1000);
     Keybinds::Register(array(KEY_ACTION, KEY_ACTION), null, array('Locations', 'PlayerEnterExitKeybind'));
     echo ">>>>> Creating Location0 ...\n";
     $location0 = new Location0();
 }
Ejemplo n.º 5
0
 public static function Init()
 {
     Callbacks::Instance()->Register(cOnPlayerEnterVehicle, null, array('VehicleShop', 'OnPlayerEnterVehicle'), -1000);
     echo ">>>>> Adding vehicle shops ...\n";
     /* Register vehicles */
     VehicleShop::Add('Bullet', 200000, array(6, 6), new Position(1116.5715, 2261.9004, 10.4453, 179.8618));
     VehicleShop::Add('Perenniel', 35000, array(0, 0), new Position(1106.2313, 2262.6445, 10.553, 358.8151));
     VehicleShop::Add('NRG 500', 50000, array(5, 6), new Position(1109.7905, 2262.1455, 10.3839, 176.2969));
     VehicleShop::Add('Maverick', 150000, array(0, 0), new Position(1076.9761, 2311.6589, 10.9968, 179.3844));
 }
Ejemplo n.º 6
0
 public static function Init()
 {
     /* Register the admin commands */
     CommandHandler::Register('admins', 0, null, array('Admin', 'cmdAdmins'), '');
     CommandHandler::Register('kick', 2, null, array('Admin', 'cmdKick'), '[ID] [reason]');
     CommandHandler::Register('suspend', 3, null, array('Admin', 'cmdSuspend'), '[ID] [time] [reason]');
     CommandHandler::Register('unsuspend', 1, null, array('Admin', 'cmdUnsuspend'), '[exact name]');
     CommandHandler::Register('goto', 1, null, array('Admin', 'cmdGoto'), '[ID]');
     CommandHandler::Register('bring', 1, null, array('Admin', 'cmdBring'), '[ID]');
     CommandHandler::Register('ajail', 3, null, array('Admin', 'cmdAjail'), '[ID] [time] [reason]');
     CommandHandler::Register('aunjail', 1, null, array('Admin', 'cmdAunjail'), '[ID]');
     CommandHandler::Register('givecash', 2, null, array('Admin', 'cmdGivecash'), '[ID] [amount]');
     CommandHandler::Register('showme', 1, null, array('Admin', 'cmdShowme'), '[on/off]');
     CommandHandler::Register('setfaction', 2, null, array('Admin', 'cmdSetfaction'), '[ID] [faction name]');
     CommandHandler::Register('setrank', 2, null, array('Admin', 'cmdSetrank'), '[ID] [rank id]');
     /* Register callbacks */
     Callbacks::Instance()->Register(cOnPlayerConnect, null, array('Admin', 'OnPlayerConnect'));
     Admin::$showadmins = array_fill(0, MAX_PLAYERS, true);
 }
Ejemplo n.º 7
0
 public static function Init()
 {
     /* Register callbacks */
     Callbacks::Instance()->Register(cOnPlayerKeyStateChange, null, array('Animations', 'OnPlayerKeyStateChange'));
     Callbacks::Instance()->Register(cOnPlayerText, null, array('Animations', 'OnPlayerText'), -10);
     /* Register commands */
     CommandHandler::Register('stopanim', 0, null, array('Animations', 'cmdStopanim'), '', 1);
     /*                         lib    anim              loop    movex   movey   continue  time  forced */
     $seat_up = new Animation('PED', 'seat_up', false, false, false, false, -1, false);
     $seat_idle = new Animation('PED', 'seat_idle', true, false, false, false, -1, false, -1, $seat_up);
     $seat_down = new Animation('PED', 'seat_down', false, false, false, false, 1350, false, 1200, $seat_up, $seat_idle);
     Animations::Register('seat', $seat_down);
     Animations::$seat_animation = $seat_idle;
     Animations::$stalk_animation = new Animation('MISC', 'seat_talk_01', false, false, false, false, 1800, false, 1600, $seat_up, $seat_idle);
     $drunkstop = new Animation('PED', 'walk_drunk', true, true, true, false, 100, false);
     $drunk = new Animation('PED', 'walk_drunk', true, true, true, true, 1, false, -1, $drunkstop);
     Animations::Register('drunk', $drunk);
     $fdrunkstop = new Animation('FOOD', 'eat_vomit_p', true, true, true, false, -1, true, 7200);
     $fdrunk = new Animation('PED', 'walk_drunk', true, true, true, true, 1, true, 5000, null, $fdrunkstop);
     Animations::Register('fdrunk', $fdrunk);
     $lay_up = new Animation('SUNBATHE', 'lay_bac_out', false, false, false, false, -1, false);
     $lay_idle = new Animation('BEACH', 'bather', true, false, false, false, -1, false, -1, $lay_up);
     $lay_down = new Animation('SUNBATHE', 'lay_bac_in', false, false, false, false, 2100, false, 2000, $lay_up, $lay_idle);
     Animations::Register('lay', $lay_down);
     /* Create the textdraw for stopping animations */
     Animations::$stopping_draw = TextDrawCreate(214.0, 421.0, 'Use ~r~~k~~PED_SPRINT~~w~ to stop the animation');
     TextDrawUseBox(Animations::$stopping_draw, 1);
     TextDrawBoxColor(Animations::$stopping_draw, 0x66);
     TextDrawTextSize(Animations::$stopping_draw, 460.0, 0.0);
     TextDrawAlignment(Animations::$stopping_draw, 0);
     TextDrawBackgroundColor(Animations::$stopping_draw, 0xff);
     TextDrawFont(Animations::$stopping_draw, 1);
     TextDrawLetterSize(Animations::$stopping_draw, 0.399999, 1.3);
     TextDrawColor(Animations::$stopping_draw, 0xffffffff);
     TextDrawSetOutline(Animations::$stopping_draw, 1);
     TextDrawSetProportional(Animations::$stopping_draw, 1);
     /* Create global animations */
     Animations::$death_animation = new Animation('WUZI', 'CS_Dead_Guy', true, false, false, false, -1, true);
 }
Ejemplo n.º 8
0
 public static function Init()
 {
     /* Register commands and callbacks */
     CommandHandler::Register('exercise', 0, null, array('Gym', 'cmdExercise'), '', 1);
     CommandHandler::Register('stopexercise', 0, null, array('Gym', 'cmdStopexercise'), '', 1);
     Callbacks::Instance()->Register(cOnPlayerDisconnect, null, array('Gym', 'OnPlayerDisconnect'));
     Callbacks::Instance()->Register(cOnPlayerKeyStateChange, null, array('Gym', 'OnPlayerKeyStateChange'));
     Callbacks::Instance()->Register(cOnPlayerSpawn, null, array('Gym', 'OnPlayerSpawn'));
     /* Create the animations */
     $benchpress_getoff = new Animation('benchpress', 'gym_bp_getoff', false, false, false, false, 8100, true);
     $benchpress_exercise = new Animation('benchpress', 'gym_bp_up_smooth', true, false, false, false, -1, true, -1, $benchpress_getoff);
     $benchpress_anim = new Animation('benchpress', 'gym_bp_geton', false, false, false, false, 5200, true, 5100, $benchpress_getoff, $benchpress_exercise, array('Gym', 'startExercise'));
     Gym::$anims['benchpress'] = $benchpress_anim;
     $bike_getoff = new Animation('gymnasium', 'gym_bike_getoff', false, false, false, false, 1350, true);
     $bike_exercise = new Animation('gymnasium', 'gym_bike_pedal', true, false, false, false, -1, true, -1, $bike_getoff);
     $bike_anim = new Animation('gymnasium', 'gym_bike_geton', false, false, false, false, 1500, true, 1400, $bike_getoff, $bike_exercise, array('Gym', 'startExercise'));
     Gym::$anims['bike'] = $bike_anim;
     $tread_getoff = new Animation('gymnasium', 'gym_tread_getoff', false, false, false, false, 3000, true);
     $tread_exercise = new Animation('gymnasium', 'gym_tread_jog', true, false, false, false, -1, true, -1, $tread_getoff);
     $tread_anim = new Animation('gymnasium', 'gym_tread_geton', false, false, false, false, 2250, true, 2200, $tread_getoff, $tread_exercise, array('Gym', 'startExercise'));
     Gym::$anims['tread'] = $tread_anim;
     /* Make the list of keys */
     Gym::RegisterKey(KEY_ACTION, 'TAB');
     Gym::RegisterKey(KEY_CROUCH, 'CROUCH');
     Gym::RegisterKey(KEY_SPRINT, 'SPRINT');
     Gym::RegisterKey(KEY_JUMP, 'JUMP');
     Gym::RegisterKey(KEY_WALK, 'WALK');
     Gym::RegisterKey(KEY_SECONDARY_ATTACK, 'ENTER V.');
     /* Initialize arrays */
     Gym::$players_key = array_fill(0, MAX_PLAYERS, -1);
     Gym::$players_gained = array_fill(0, MAX_PLAYERS, -1);
     Gym::$draws = array_fill(0, MAX_PLAYERS, null);
     /* Register all exercise machines */
     echo ">>>>> Registering all exercise machines locations ...\n";
     /* Madd doggs */
     Gym::Register('benchpress', new Position(1233.6235, -769.3942, 1084.0, 180), new Position(1233.6235, -767.3942, 1086));
     Gym::Register('bike', new Position(1242.4896, -767.7238, 1084.0121, 90), new Position(1239, -766.7238, 1086));
     Gym::Register('tread', new Position(1226.5122, -762.5506, 1084.0055, 180), new Position(1226.5352, -768.6758, 1087));
 }
Ejemplo n.º 9
0
 /**
  ** Static methods
  **/
 public static function Init()
 {
     Callbacks::Instance()->Register(cOnPlayerExitedMenu, null, array('Menu', 'OnPlayerExitedMenu'));
     Callbacks::Instance()->Register(cOnPlayerSelectedMenuRow, null, array('Menu', 'OnPlayerSelectedMenuRow'));
 }
Ejemplo n.º 10
0
	public function notifyIsFailed(MultiRequest_Exception $exception, Handler $handler) {
		$this->callbacks->onFailed($this, $exception, $handler);
	}
Ejemplo n.º 11
0
 public function addCallback($name, $callback)
 {
     $this->callbacks->add($name, $callback);
     return $this;
 }
Ejemplo n.º 12
0
 public function start()
 {
     if ($this->isActive || $this->isStopped) {
         return;
     }
     $this->isActive = true;
     $this->isStarted = true;
     $curlHandle = curl_multi_init();
     try {
         do {
             // send requests from queue to CURL
             if (count($this->activeRequests) < $this->connectionsLimit) {
                 for ($i = $this->connectionsLimit - count($this->activeRequests); $i > 0; $i--) {
                     $request = $this->queue->pop();
                     if ($request) {
                         $this->sendRequestToMultiCurl($curlHandle, $request);
                         $this->activeRequests[$request->getId()] = $request;
                     } else {
                         break;
                     }
                 }
             }
             while (CURLM_CALL_MULTI_PERFORM === curl_multi_exec($curlHandle, $activeThreads)) {
             }
             // check complete requests
             curl_multi_select($curlHandle, $this->requestingDelay);
             while ($completeCurlInfo = curl_multi_info_read($curlHandle)) {
                 $completeRequestId = Request::getRequestIdByCurlHandle($completeCurlInfo['handle']);
                 $completeRequest = $this->activeRequests[$completeRequestId];
                 unset($this->activeRequests[$completeRequestId]);
                 curl_multi_remove_handle($curlHandle, $completeRequest->getCurlHandle());
                 $completeRequest->handleCurlResult();
                 // check if response code is 301 or 302 and follow location
                 $ignoreNotification = false;
                 $completeRequestCode = $completeRequest->getCode();
                 if ($completeRequestCode == 301 || $completeRequestCode == 302) {
                     $completeRequestOptions = $completeRequest->getCurlOptions();
                     if (!empty($completeRequestOptions[CURLOPT_FOLLOWLOCATION])) {
                         $completeRequest->_permanentlyMoved = empty($completeRequest->_permanentlyMoved) ? 1 : $completeRequest->_permanentlyMoved + 1;
                         $responseHeaders = $completeRequest->getResponseHeaders(true);
                         if ($completeRequest->_permanentlyMoved < 5 && !empty($responseHeaders['Location'])) {
                             // figure out whether we're dealing with an absolute or relative redirect (thanks to kmontag https://github.com/kmontag for this bugfix)
                             $redirectedUrl = (parse_url($responseHeaders['Location'], PHP_URL_SCHEME) === null ? $completeRequest->getBaseUrl() : '') . $responseHeaders['Location'];
                             $completeRequest->setUrl($redirectedUrl);
                             $completeRequest->reInitCurlHandle();
                             $this->pushRequestToQueue($completeRequest);
                             $ignoreNotification = true;
                         }
                     }
                 }
                 if (!$ignoreNotification) {
                     $this->notifyRequestComplete($completeRequest);
                 }
             }
         } while (!$this->isStopped && ($this->activeRequests || $this->queue->count()));
     } catch (Exception $exception) {
     }
     $this->isActive = false;
     if ($curlHandle && is_resource($curlHandle)) {
         curl_multi_close($curlHandle);
     }
     if (!empty($exception)) {
         throw $exception;
     }
     /** @noinspection PhpUndefinedMethodInspection */
     $this->callbacks->onComplete($this);
 }
 /**
  * {@inheritdoc}
  */
 public function __invoke($event)
 {
     if ($this->getCallback()) {
         $this->update($event, Callbacks::callArgs($this->getCallback(), $this->getArgs($event)));
     }
 }
Ejemplo n.º 14
0
function OnPlayerKeyStateChange($playerid, $newkeys, $oldkeys)
{
    return Callbacks::Instance()->OnPlayerKeyStateChange($playerid, $newkeys, $oldkeys);
}
Ejemplo n.º 15
0
 public static function Init()
 {
     /* Initialize the skills */
     Players::RegisterSkill(SKILL_COOKING, 5, 'cooking', 'Cooking');
     Players::RegisterSkill(SKILL_LUCK, 10, 'luck', 'Luck');
     Players::RegisterSkill(SKILL_ALCOHOLIC, 5, 'alcoholic', 'Alcoholic');
     $skill = Players::RegisterSkill(SKILL_DRUGSADDICT, 4, 'drugsaddict', 'Drugs addict');
     $skill->SetReqLevel(2, 4);
     $skill->SetReqLevel(3, 7);
     $skill->SetReqLevel(4, 11);
     $skill = Players::RegisterSkill(SKILL_PROFKILLER, 6, 'profkiller', 'Professional killer');
     $skill->SetReqLevel(2, 6);
     $skill->SetReqLevel(3, 8);
     $skill->SetReqLevel(4, 11);
     $skill->SetReqLevel(5, 15);
     $skill->SetReqLevel(6, 20);
     $skill = Players::RegisterSkill(SKILL_GUNSDEALER, 6, 'gunsdealer', 'Guns dealer');
     $skill->SetReqLevel(2, 3);
     $skill->SetReqLevel(3, 5);
     $skill->SetReqLevel(4, 8);
     $skill->SetReqLevel(5, 12);
     $skill->SetReqLevel(6, 17);
     $skill = Players::RegisterSkill(SKILL_DRUGSDEALER, 4, 'drugsdealer', 'Drugs dealer');
     $skill->SetReqLevel(2, 3);
     $skill->SetReqLevel(3, 6);
     $skill->SetReqLevel(4, 10);
     Players::RegisterSkill(SKILL_NEGSKILLS, 5, 'negskills', 'Negotiation skills', SKILL_LUCK, 5);
     Players::RegisterSkill(SKILL_FARMING, 0, 'farming', 'Farming');
     Players::RegisterSkill(SKILL_FISHING, 0, 'fishing', 'Fishing');
     /* Create the upgrade menu */
     $menu = new Menu(null, 'Upgrades', array('Players', 'menuUpgradesEnter'), array('Players', 'menuUpgradesExit'), 1, new Position(50, 120), 150);
     foreach (Players::$skills as $skill) {
         if ($skill->flag < SKILL_MAXUPGRADE) {
             $menu->Add($skill->name, 0, ROW_ACTION, array('Players', 'menuUpgradesSelect'));
         }
     }
     Players::$upgrade_menu = $menu;
     /* Create the upgrade textdraws list */
     Players::$upgrade_draws = array_fill(0, MAX_PLAYERS, null);
     /* Register timers, callbacks and commands */
     AddTimer(array('Players', 'Update'), 500, 1);
     AddTimer(array('Players', 'Stream'), 800, 1);
     Callbacks::Instance()->Register(cOnPlayerConnect, null, array('Players', 'OnPlayerConnect'), 0);
     Callbacks::Instance()->Register(cOnPlayerDisconnect, null, array('Players', 'OnPlayerDisconnect'), 5);
     Callbacks::Instance()->Register(cOnPlayerDeath, null, array('Players', 'OnPlayerDeath'));
     Callbacks::Instance()->Register(cOnPlayerSpawn, null, array('Players', 'OnPlayerSpawn'));
     Callbacks::Instance()->Register(cOnPlayerRequestClass, null, array('Players', 'OnPlayerRequestClass'), -1000);
     Callbacks::Instance()->Register(cOnPlayerRequestSpawn, null, array('Players', 'OnPlayerRequestSpawn'));
     Callbacks::Instance()->Register(cOnPlayerEnterVehicle, null, array('Players', 'OnPlayerEnterVehicle'), -1000);
     Callbacks::Instance()->Register(cOnPlayerKeyStateChange, null, array('Players', 'OnPlayerKeyStatechange'));
     CommandHandler::Register('togjq', 0, null, array('Players', 'cmdTogjq'), '', 1, -1000);
     CommandHandler::Register('togooc', 0, null, array('Players', 'cmdTogooc'), '', 1, -1000);
     CommandHandler::Register('die', 0, null, array('Players', 'cmdDie'), '', 1, -1000);
     CommandHandler::Register('stats', 0, null, array('Players', 'cmdStats'), '', 1, -1000);
     CommandHandler::Register('pay', 2, null, array('Players', 'cmdPay'), '[ID] [amount]', 1, -1000);
     CommandHandler::Register('skills', 0, null, array('Players', 'cmdSkills'), '', 1, -1000);
     CommandHandler::Register('upgrade', 0, null, array('Players', 'cmdUpgrade'), '', 1, -1000);
     CommandHandler::Register('speedometer', 1, null, array('Players', 'cmdSpeedometer'), '[kmh/mph]', 1, -1000);
     CommandHandler::Register('autowalk', 0, null, array('Players', 'cmdAutoWalk'), '', 1, -1000);
     Players::$players_by_id = array_fill(0, MAX_PLAYERS, null);
     /* Create the incar textdraws */
     for ($i = 0; $i < MAX_PLAYERS; $i++) {
         Players::$incar_draws[$i] = TextDrawCreate(549.0, 393.0, '.');
         TextDrawUseBox(Players::$incar_draws[$i], 1);
         TextDrawBoxColor(Players::$incar_draws[$i], 0x66);
         TextDrawTextSize(Players::$incar_draws[$i], 627.0, -10.0);
         TextDrawAlignment(Players::$incar_draws[$i], 0);
         TextDrawBackgroundColor(Players::$incar_draws[$i], 0xff);
         TextDrawFont(Players::$incar_draws[$i], 1);
         TextDrawLetterSize(Players::$incar_draws[$i], 0.499999, 1.1);
         TextDrawColor(Players::$incar_draws[$i], 0xffffffff);
         TextDrawSetOutline(Players::$incar_draws[$i], 1);
         TextDrawSetProportional(Players::$incar_draws[$i], 1);
         TextDrawSetShadow(Players::$incar_draws[$i], 1);
         TextDrawHideForAll(Players::$incar_draws[$i]);
     }
 }
Ejemplo n.º 16
0
 public static function Init()
 {
     Callbacks::Instance()->Register(cOnPlayerConnect, null, array('Accounts', 'OnPlayerConnect'), 5);
     Callbacks::Instance()->Register(cOnPlayerDisconnect, null, array('Accounts', 'OnPlayerDisconnect'), 0);
     CommandHandler::Register('login', 1, null, array('Accounts', 'cmdLogin'), '[password]', 0, -1000);
 }
Ejemplo n.º 17
0
	public function start() {
		if($this->isActive || $this->isStopped) {
			return;
		}
		$this->isActive = true;
		$this->isStarted = true;

		try {

			$this->mcurlHandle = $mcurlHandle = curl_multi_init();

			do {

				// send requests from queue to CURL
				if(count($this->activeRequests) < $this->connectionsLimit) {
					for($i = $this->connectionsLimit - count($this->activeRequests); $i > 0; $i --) {
						$request = $this->queue->pop();
						if($request) {
							$this->sendRequestToMultiCurl($mcurlHandle, $request);
							$this->activeRequests[$request->getId()] = $request;
						}
						else {
							break;
						}
					}
				}

				while(CURLM_CALL_MULTI_PERFORM === curl_multi_exec($mcurlHandle, $activeThreads));

				// check complete requests
				curl_multi_select($mcurlHandle, $this->requestingDelay);
				while($completeCurlInfo = curl_multi_info_read($mcurlHandle)) {
					$completeRequestId = Request::getRequestIdByCurlHandle($completeCurlInfo['handle']);
					$completeRequest = $this->activeRequests[$completeRequestId];
					unset($this->activeRequests[$completeRequestId]);
					curl_multi_remove_handle($mcurlHandle, $completeRequest->getCurlHandle());
					$completeRequest->initResponseDataFromHandler($this);

					// check if response code is 301 or 302 and follow location
					$ignoreNotification = false;
					$completeRequestCode = $completeRequest->getCode();

					if($completeRequestCode == 301 || $completeRequestCode == 302) {
						$completeRequestOptions = $completeRequest->getCurlOptions();
						if(!empty($completeRequestOptions[CURLOPT_FOLLOWLOCATION])) {
							$completeRequest->_permanentlyMoved = empty($completeRequest->_permanentlyMoved) ? 1 : $completeRequest->_permanentlyMoved + 1;
							$responseHeaders = $completeRequest->getResponseHeaders(true);
							if($completeRequest->_permanentlyMoved < 5 && !empty($responseHeaders['Location'])) {
								$completeRequest->setUrl($completeRequest->getBaseUrl() . $responseHeaders['Location']);
								$completeRequest->reinitCurlHandle();
								$this->pushRequestToQueue($completeRequest);
								$ignoreNotification = true;
							}
						}
					}
					if(!$ignoreNotification) {
						$this->notifyRequestComplete($completeRequest);
					}
				}
			}
			while(!$this->isStopped && ($this->activeRequests || $this->queue->count()));
		}
		catch(\Exception $exception) {
		}

		$this->isActive = false;

		if($mcurlHandle && is_resource($mcurlHandle)) {
			curl_multi_close($mcurlHandle);
		}

		if(!empty($exception)) {
			throw $exception;
		}

		$this->callbacks->onComplete($this);
	}
Ejemplo n.º 18
0
 public function notifyRequestIsComplete(Request $request, Handler $mrHandler)
 {
     $this->lastRequest = $request;
     $this->callbacks->onRequestComplete($request, $this, $mrHandler);
 }