curl multi

This commit is contained in:
Gnieark 2016-07-07 08:02:29 +02:00
parent 59990fd97e
commit a31cf5e3ff
5 changed files with 67 additions and 9 deletions

View File

@ -1 +1 @@
1483 1486

View File

@ -11,7 +11,8 @@
# -- END LICENSE BLOCK ----------------------------------------- # -- END LICENSE BLOCK -----------------------------------------
require_once(__DIR__."/functions.php"); require_once(__DIR__."/functions.php");
require_once ("class.TronGame.php");
require_once ("class.TronPlayer.php");
switch ($_POST['act']){ switch ($_POST['act']){
case "initGame": case "initGame":
@ -41,8 +42,6 @@ switch ($_POST['act']){
break; break;
case "play": case "play":
$logs = ""; $logs = "";
//check for correct game ID
if(!isset($_SESSION['game'])){ if(!isset($_SESSION['game'])){
echo '{"status":"error"}'; echo '{"status":"error"}';
die; die;
@ -55,7 +54,7 @@ switch ($_POST['act']){
echo '{"status":"error"}'; echo '{"status":"error"}';
die; die;
} }
$game()->newLap(); $game->new_lap();
//make the board array //make the board array
for ($botCount = 0; $botCount < count($bots); $botCount ++){ for ($botCount = 0; $botCount < count($bots); $botCount ++){

View File

@ -30,6 +30,10 @@ class TronGame{
}else{ }else{
return false; return false;
} }
}
public function new_lap(){
} }
public function init_game(){ public function init_game(){
//send init messages to bots //send init messages to bots

View File

@ -1,6 +1,5 @@
<?php <?php
include ("class.TronGame.php");
include ("class.TronPlayer.php");
function save_draw_bots($arr){ function save_draw_bots($arr){
/* /*
* Recursive function who save all combionaisons of draw matches * Recursive function who save all combionaisons of draw matches

View File

@ -382,3 +382,59 @@ function get_IA_Response($iaUrl,$postParams){
'responseArr' => $arr 'responseArr' => $arr
); );
} }
function get_multi_IAS_Responses($iasUrls, $postParams){
//same as the previous function
// but more than one bot requested parallely
$cmh = curl_multi_init();
for ($i = 0; $i < count($iasUrls); $i++){
$data_string = json_encode($postParams[$i]);
$ch[$i] = curl_init($iaUrl);
curl_setopt($ch[$i], CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch[$i], CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch[$i], CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch[$i], CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch[$i], CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch[$i], CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
curl_multi_add_handle($cmh,$ch[$i]);
}
//send the requests
do {
$returnVal = curl_multi_exec($cmh, $runningHandles);
} while ($returnVal == CURLM_CALL_MULTI_PERFORM);
// Loop and continue processing the request
while ($runningHandles && $returnVal== CURLM_OK) {
// Wait forever for network
$numberReady = curl_multi_select($cmh);
if ($numberReady != -1) {
// Pull in any new data, or at least handle timeouts
do {
$returnVal = curl_multi_exec($cmh, $runningHandles);
} while ($returnVal == CURLM_CALL_MULTI_PERFORM);
}
}
//Get results
for ($i = 0; $i < count($iasUrls); $i++)
{
// Check for errors
$curlError = curl_error($ch[$i]);
if($curlError == "") {
$res[$i] = curl_multi_getcontent($ch[$i]);
} else {
$res[$i] = false
}
//close
curl_multi_remove_handle($cmh, $ch[$i]);
curl_close($ch[$i]);
}
// Clean up the curl_multi handle
curl_multi_close($mh);
return $res;
}