filereader improvements

This commit is contained in:
kremsy 2014-02-17 00:34:20 +01:00 committed by Steffen Schröder
parent 2696c184dd
commit 93a7a997f8
3 changed files with 59 additions and 52 deletions

View File

@ -40,71 +40,72 @@ class AsynchronousFileReader {
public function appendData() { public function appendData() {
foreach($this->sockets as $key => &$socket) { foreach($this->sockets as $key => &$socket) {
/** @var SocketStructure $socket */ /** @var SocketStructure $socket */
$socket->streamBuffer .= fread($socket->socket, 4096); do {
$line = fgets($socket->socket, 4096);
if (empty($socket->header) && $line == "\r\n") {
$socket->header = $this->parseHeader($socket->streamBuffer);
$socket->streamBuffer = "";
$line = "";
}
$socket->streamBuffer .= $line;
if (feof($socket->socket) || isset($socket->header["content-length"]) && strlen($socket->streamBuffer) >= $socket->header["content-length"]) {
//$socket->streamBuffer .= fgets($socket->socket, 4096); //TODO special handling for chunked...
//var_dump($socket->streamBuffer); fclose($socket->socket);
/* $meta = stream_get_meta_data($socket->socket); unset($this->sockets[$key]);
while($meta["unread_bytes"] > 0){ $this->handleContent($socket);
var_dump("test"); continue 2;
$socket->streamBuffer .= fgets($socket->socket, 4096); }else{
$meta = stream_get_meta_data($socket->socket); $meta = stream_get_meta_data($socket->socket);
var_dump($meta);
} }
} while($meta["unread_bytes"] > 0);
var_dump($meta);
exit();*/
if (feof($socket->socket) || time() > ($socket->creationTime + self::SOCKET_TIMEOUT)) {
fclose($socket->socket);
unset($this->sockets[$key]);
$result = "";
$error = 0;
if (time() > ($socket->creationTime + self::SOCKET_TIMEOUT)) {
$error = self::TIMEOUT_ERROR;
} else if (substr($socket->streamBuffer, 9, 3) != "200") {
$error = self::RESPONSE_ERROR;
$result = $this->parseResult($socket->streamBuffer);
} else if ($socket->streamBuffer == '') {
$error = self::NO_DATA_ERROR;
} else {
$result = $this->parseResult($socket->streamBuffer);
if ($result == self::INVALID_RESULT_ERROR) {
$error = self::INVALID_RESULT_ERROR;
}
}
call_user_func($socket->function, $result, $error);
}
} }
} }
/**
* Handles the Content
* @param $socket
*/
private function handleContent(SocketStructure $socket){ //TODO timeout handling
//if (feof($socket->socket) || time() > ($socket->creationTime + self::SOCKET_TIMEOUT)) {
$result = "";
$error = 0;
/*if (time() > ($socket->creationTime + self::SOCKET_TIMEOUT)) {
$error = self::TIMEOUT_ERROR;
} else*/
if ($socket->header["status"] != "200") {
$error = self::RESPONSE_ERROR;
$result = $this->parseResult2($socket);
} else if ($socket->streamBuffer == '') {
$error = self::NO_DATA_ERROR;
} else {
$result = $this->parseResult2($socket);
if ($result == self::INVALID_RESULT_ERROR) {
$error = self::INVALID_RESULT_ERROR;
}
}
//var_dump($result);
call_user_func($socket->function, $result, $error);
//}
}
/** /**
* Parse the Stream Result * Parse the Stream Result
* *
* @param $streamBuffer * @param SocketStructure $socket
* @internal param $streamBuffer
* @return string * @return string
*/ */
private function parseResult($streamBuffer) { private function parseResult2(SocketStructure $socket) {
$resultArray = explode("\r\n\r\n", $streamBuffer, 2);
switch(count($resultArray)) { if (isset($socket->header["transfer-encoding"])) {
case 0: $result = $this->decode_chunked($socket->streamBuffer);
return self::INVALID_RESULT_ERROR;
case 1:
return '';
}
$header = $this->parseHeader($resultArray[0]);
if (isset($header["transfer-encoding"])) {
$result = $this->decode_chunked($resultArray[1]);
} else { } else {
$result = $resultArray[1]; $result = $socket->streamBuffer;
} }
return $this->decompressData($header, $result); return $this->decompressData($socket->header, $result);
} }
/** /**
@ -160,6 +161,9 @@ class AsynchronousFileReader {
} }
foreach($headers as $v) { foreach($headers as $v) {
if ($v == "") {
break;
}
$h = preg_split('/:\s*/', $v); $h = preg_split('/:\s*/', $v);
$output[strtolower($h[0])] = $h[1]; $output[strtolower($h[0])] = $h[1];
} }

View File

@ -15,6 +15,7 @@ class SocketStructure {
public $function; public $function;
public $url; public $url;
public $creationTime; public $creationTime;
public $header;
public function __construct($url, $socket, $function) { public function __construct($url, $socket, $function) {
$this->url = $url; $this->url = $url;
@ -22,5 +23,6 @@ class SocketStructure {
$this->function = $function; $this->function = $function;
$this->creationTime = time(); $this->creationTime = time();
$this->streamBuffer = ''; $this->streamBuffer = '';
$this->header = array();
} }
} }

View File

@ -12,6 +12,7 @@ use ManiaControl\ManiaExchange\ManiaExchangeList;
use ManiaControl\ManiaExchange\ManiaExchangeManager; use ManiaControl\ManiaExchange\ManiaExchangeManager;
use ManiaControl\ManiaExchange\MXMapInfo; use ManiaControl\ManiaExchange\MXMapInfo;
use ManiaControl\Players\Player; use ManiaControl\Players\Player;
use Maniaplanet\DedicatedServer\InvalidArgumentException;
use Maniaplanet\DedicatedServer\Xmlrpc\Exception; use Maniaplanet\DedicatedServer\Xmlrpc\Exception;
/** /**
@ -561,8 +562,8 @@ class MapManager implements CallbackListener {
} else { } else {
try { try {
$this->maniaControl->client->writeFileFromString($mapFileName, $file); $this->maniaControl->client->writeFileFromString($mapFileName, $file);
} catch(Exception $e) { } catch(InvalidArgumentException $e) {
if ($e->getMessage() == 'transport error - request too large!') { if ($e->getMessage() == 'data are too big') {
$this->maniaControl->chat->sendError("Map is too big for a remote save.", $login); $this->maniaControl->chat->sendError("Map is too big for a remote save.", $login);
return; return;
} }