Skip to content

Commit

Permalink
Fixed high CPU usage #150, enhanced error message
Browse files Browse the repository at this point in the history
  • Loading branch information
Forceu committed Aug 16, 2021
1 parent 2be948a commit f1477a6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 31 deletions.
12 changes: 9 additions & 3 deletions incl/sse/sse_data.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function connectToSocket(): bool {
function outputSocketError(): void {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
sendData('{"action":"error","data":"E' . $errorcode . ' ' . $errormsg . '"}', 100000000);
sendData('{"action":"error","data":"EError ' . $errorcode . ': ' . $errormsg . '"}', 100000000);
}

function sendStillAlive(): void {
Expand All @@ -45,10 +45,16 @@ function readData(): void {
$timeStart = microtime(true);
while (microtime(true) - $timeStart < MAX_EXECUTION_TIME_S) {
$data = $client->readData();
if ($data !== false)
if ($data !== false && $data != "")
sendData($data);
else
else {
if (socket_last_error() != 0) {
outputSocketError();
die();
}
sendStillAlive();
}

}
$client->close();
}
Expand Down
54 changes: 26 additions & 28 deletions wsserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,48 +73,46 @@
//manage multiple connections
$changed = $clients;
//returns the socket resources in $changed array
socket_select($changed, $null, $null, 0, 10);
socket_select($changed, $null, $null, $null, 0);

//check for new clients
if (in_array($socket, $changed)) {
$socket_new = socket_accept($socket); //accept new socket
$clients[] = $socket_new; //add socket to client array
$socket_new = socket_accept($socket); //accept new socket
$clients[] = $socket_new; //add socket to client array
$found_socket = array_search($socket, $changed);
unset($changed[$found_socket]);
}

//loop through all connected clients
foreach ($changed as $changed_socket) {
$buf = @socket_read($changed_socket, 1024);
if ($buf === false) { // check disconnected client
if ($buf === false || $buf === "") { // check disconnected client
// remove client for $clients array
$found_socket = array_search($changed_socket, $clients);
unset($clients[$found_socket]);
} else {
// A message was received
if (strlen($buf) > 1) {
$command = $buf[0];
$data = substr($buf, 1);
switch ($command) {
// Get mode
case '0':
sendMode();
break;
// Set mode
case '1':
if (in_array($data, $allowedModes)) {
$currentBBMode = $data;
}
sendMode();
break;
// Echo
case '2':
sendMessage('{"action":"echo","data":"' . $data . '"}');
break;
// Invalid command
default:
echo "Unknown command " . $buf;
}
$command = $buf[0];
$data = substr($buf, 1);
switch ($command) {
// Get mode
case '0':
sendMode();
break;
// Set mode
case '1':
if (in_array($data, $allowedModes)) {
$currentBBMode = $data;
}
sendMode();
break;
// Echo
case '2':
sendMessage('{"action":"echo","data":"' . $data . '"}');
break;
// Invalid command
default:
echo "Unknown command " . $buf;
}
}
}
Expand All @@ -135,7 +133,7 @@ function sendMessage(string $msg): void {
/**
* @return never
*/
function showErrorAndDie(string $functionName):void {
function showErrorAndDie(string $functionName): void {
echo $functionName . " failed. Reason: " . socket_strerror(socket_last_error()) . "\n";
sleep(5);
die();
Expand Down

0 comments on commit f1477a6

Please sign in to comment.