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 -----------------------------------------
require_once(__DIR__."/functions.php");
require_once ("class.TronGame.php");
require_once ("class.TronPlayer.php");
switch ($_POST['act']){
case "initGame":
@ -41,21 +42,19 @@ switch ($_POST['act']){
break;
case "play":
$logs = "";
//check for correct game ID
if(!isset($_SESSION['game'])){
echo '{"status":"error"}';
die;
}
$game= unserialize($_SESSION['game']);
$game = unserialize($_SESSION['game']);
if($game->getGameId() <> $_POST['gameId']){
//sometimes if an ajax callback is applied after init an other game
echo '{"status":"error"}';
die;
}
$game()->newLap();
$game->new_lap();
//make the board array
for ($botCount = 0; $botCount < count($bots); $botCount ++){

View File

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

View File

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

View File

@ -381,4 +381,60 @@ function get_IA_Response($iaUrl,$postParams){
'response' => $output,
'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;
}