Skip to content

Commit

Permalink
Hide normal operation diagnostic messages
Browse files Browse the repository at this point in the history
  • Loading branch information
vrza committed Feb 20, 2024
1 parent 829273d commit 2cb578f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/SocketStreamClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function sendMessage(string $msg)
socket_strerror(socket_last_error($this->socket)) . PHP_EOL
);
} else {
if ($this->verbosity > 1) fwrite(STDOUT, ">>>> $msg" . PHP_EOL);
if ($this->verbosity > 1) fwrite(STDERR, ">>>> $msg" . PHP_EOL);
if ($this->verbosity > 1) fwrite(STDERR, "$bytes bytes sent" . PHP_EOL);
}
return $bytes;
Expand Down
20 changes: 10 additions & 10 deletions src/SocketStreamsServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function closeAll(): void
{
foreach ($this->sockets as $socket) {
if (is_resource($socket) && get_resource_type($socket) === 'Socket') {
if ($this->verbosity > 1) fwrite(STDOUT, "Closing socket (SocketStreamsServer destructor)" . PHP_EOL);
if ($this->verbosity > 1) fwrite(STDERR, "Closing socket (SocketStreamsServer destructor)" . PHP_EOL);
socket_close($socket);
}
}
Expand Down Expand Up @@ -111,7 +111,7 @@ public function checkMessages(int $timeoutSeconds = 0, int $timeoutMicroseconds
$sec = $usec = 0;
}
if ($cnt > 0) {
fwrite(STDERR, "server handled $cnt messages" . PHP_EOL);
if ($this->verbosity) fwrite(STDERR, "server handled $cnt messages" . PHP_EOL);
}
return $cnt;
}
Expand Down Expand Up @@ -146,7 +146,7 @@ private function handleConnections(int $timeoutSeconds = 0, int $timeoutMicrosec
return -$error;
}
}
fwrite(STDOUT, "Accepted new client connection" . PHP_EOL);
if ($this->verbosity) fwrite(STDERR, "Accepted new client connection" . PHP_EOL);
$this->addSocket($newSocket, $i);
$this->handleClientData($newSocket, $i);
} else { // handle data from a client
Expand Down Expand Up @@ -181,16 +181,16 @@ private function handleClientData($socket, int $i): void
$response = $msgHandler->handleMessage($msg);
$this->sendResponse($response, $socket);
} else {
fwrite(STDOUT, "Client closed connection, removing and closing socket" . PHP_EOL);
if ($this->verbosity) fwrite(STDERR, "Client closed connection, removing and closing socket" . PHP_EOL);
$this->removeSocket($socket);
socket_close($socket);
fwrite(STDOUT, "Sockets/handlers set size: " . count($this->socketsData) . "/" . count($this->handlers) . PHP_EOL);
if ($this->verbosity) fwrite(STDERR, "Sockets/handlers set size: " . count($this->socketsData) . "/" . count($this->handlers) . PHP_EOL);
}
}

private function receiveMessage($connectionSocket)
{
fwrite(STDERR, '!' . PHP_EOL);
if ($this->verbosity) fwrite(STDERR, '!' . PHP_EOL);
if (($bytes = socket_recv($connectionSocket, $buf, $this->recvBufSize, 0)) === false) {
$error = socket_last_error($connectionSocket);
fwrite(STDERR, "socket_recv() failed with error $error: " . socket_strerror($error) . PHP_EOL);
Expand All @@ -207,15 +207,15 @@ private function receiveMessage($connectionSocket)
($bytes = socket_recv($connectionSocket, $buf, $this->recvBufSize, 0)) > 0
) {
$message->append($buf);
fwrite(STDERR, strlen($message->payload()) . " of " . $message->length() . " bytes received" . PHP_EOL);
if ($this->verbosity) fwrite(STDERR, strlen($message->payload()) . " of " . $message->length() . " bytes received" . PHP_EOL);
}
if ($bytes === false) {
$error = socket_last_error($connectionSocket);
fwrite(STDERR, "socket_recv() failed with error $error: " . socket_strerror($error) . PHP_EOL);
return null;
}

fwrite(STDOUT, "<<<< " . $message->payload() . PHP_EOL);
if ($this->verbosity) fwrite(STDERR, "<<<< " . $message->payload() . PHP_EOL);

return $message->payload();
}
Expand All @@ -224,8 +224,8 @@ private function sendResponse(string $response, $connectionSocket): void
{
$packed = Message::pack($response);
$bytes = socket_send($connectionSocket, $packed, strlen($packed), 0);
fwrite(STDOUT, ">>>> $response" . PHP_EOL);
fwrite(STDERR, "$bytes bytes sent" . PHP_EOL);
if ($this->verbosity) fwrite(STDERR, ">>>> $response" . PHP_EOL);
if ($this->verbosity) fwrite(STDERR, "$bytes bytes sent" . PHP_EOL);
}

}

0 comments on commit 2cb578f

Please sign in to comment.