Update event_socket_create and event_socket_request.

This commit is contained in:
Mark Crane 2015-01-16 03:29:51 +00:00
parent bee6b9ae5e
commit 6a2723a787
1 changed files with 17 additions and 11 deletions

View File

@ -267,7 +267,8 @@ function build_menu() {
} }
function event_socket_create($host, $port, $password){
function event_socket_create($host, $port, $password) {
$fp = fsockopen($host, $port, $errno, $errdesc, 3); $fp = fsockopen($host, $port, $errno, $errdesc, 3);
socket_set_blocking($fp,false); socket_set_blocking($fp,false);
@ -293,23 +294,28 @@ function event_socket_create($host, $port, $password){
function event_socket_request($fp, $cmd) { function event_socket_request($fp, $cmd) {
if ($fp) { if ($fp) {
fputs($fp, $cmd."\n\n"); $cmd_array = explode("\n",$cmd);
foreach ($cmd_array as &$value) {
fputs($fp, $value."\n");
}
fputs($fp, "\n"); //second line feed to end the headers
usleep(100); //allow time for reponse usleep(100); //allow time for reponse
$response = ""; $response = "";
$i = 0; $i = 0;
$contentlength = 0; $content_length = 0;
while (!feof($fp)) { while (!feof($fp)) {
$buffer = fgets($fp, 4096); $buffer = fgets($fp, 4096);
if ($contentlength > 0) { if ($content_length > 0) {
$response .= $buffer; $response .= $buffer;
} }
if ($contentlength == 0) { //if the content has length don't process again if ($content_length == 0) { //if the content has length don't process again
if (strlen(trim($buffer)) > 0) { //run only if buffer has content if (strlen(trim($buffer)) > 0) { //run only if buffer has content
$temparray = explode(":", trim($buffer)); $array = explode(":", trim($buffer));
if ($temparray[0] == "Content-Length") { if ($array[0] == "Content-Length") {
$contentlength = trim($temparray[1]); $content_length = trim($array[1]);
} }
} }
} }
@ -317,11 +323,11 @@ function event_socket_request($fp, $cmd) {
usleep(20); //allow time for reponse usleep(20); //allow time for reponse
//prevent an endless loop //optional because of script timeout //prevent an endless loop //optional because of script timeout
if ($i > 1000000) { break; } if ($i > 1000) { break; }
if ($contentlength > 0) { //is contentlength set if ($content_length > 0) { //is content_length set
//stop reading if all content has been read. //stop reading if all content has been read.
if (strlen($response) >= $contentlength) { if (strlen($response) >= $content_length) {
break; break;
} }
} }