public function read_file($sd, $f = '', $size = 0) { global $start; $s = ''; $len = 0; if ($size <= 0) { return $s; } fputs($sd, "RETR {$f}\r\n"); while (!safe_feof($sd, $start) && microtime(true) - $start < PILERGETD_TIMEOUT) { $s .= fread($sd, PILERGETD_READ_LENGTH); $len += PILERGETD_READ_LENGTH; if ($len >= $size) { break; } } return $s; }
$event_started = false; $start = NULL; $timeout = ini_get('default_socket_timeout'); stream_set_timeout($amiSocket, 60); // sets timeout to 60 seconds. $consecutiveFailures = 0; $queueChannels = array(); // Keep a loop going to read the socket and parse the resulting commands. // Apparently there is no good way to detect if socket is still alive??? // This is my hack... if we fail 60 times in a row we reconnect to manager... // I suspect that fgets will return very quickly if socket error has occurs // So, it'll reach 60 very quickly and then force relogin. // Otherwise, every hour it'll just relogin. // Perhaps you can check socket error some other way then socket_read? // All I know is this reconnect method has made my asteriskLogger a lot more stable. while ($consecutiveFailures < 60 && !safe_feof($amiSocket, $start) && microtime(true) - $start < $timeout) { $buffer = fgets($amiSocket, 4096); // echo("# Read " . strlen($buffer) . " " . $buffer . "\n"); if ($buffer === FALSE) { logLine(getTimestamp() . " Patiently Waiting...! (mem_used: " . memory_get_usage() . ")\n"); purgeExpiredEventsFromDb(); $consecutiveFailures++; } else { $consecutiveFailures = 0; if ($buffer == "\r\n") { // handle partial packets $event_started = false; // parse the event and get the result hashtable $e = getEvent($event); dumpEvent($e); // prints to screen