Commit 64e5c245 authored by Hannes Peterseim's avatar Hannes Peterseim
Browse files

small bugfixes and more fancy colors

parent 3e628d44
......@@ -42,33 +42,33 @@ class server
*/
public function __construct($address, $port)
{
echo "YAMS Server v 1.0\n";
echo "2021 by Hannes Peterseim\n";
echo "https://www.petit-souris.de\n\n";
echo "creating socket...";
echo "\e[36mYAMS Server v 1.0\n";
echo "\e[36m2021 by Hannes Peterseim\n";
echo "\e[36mhttps://www.petit-souris.de\n\n";
echo "\e[39mGcreating socket...";
if (($this->socket = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
echo "\nCouldn't create socket" . socket_strerror(socket_last_error()) . "\n";
echo "\n\e[31mCouldn't create socket" . socket_strerror(socket_last_error()) . "\n";
exit;
}
echo "ok\n";
echo "binding socket on port " . $port . " for IP " . $address . "...";
echo "\e[32mok\n";
echo "\e[39mbinding socket on port " . $port . " for IP " . $address . "...";
if (@socket_bind($this->socket, $address, $port) === false) {
echo "\nBind Error " . socket_strerror(socket_last_error($this->socket)) . "\n";
echo "\n\e[31mBind Error " . socket_strerror(socket_last_error($this->socket)) . "\n";
exit;
}
echo "ok\n";
echo "listening...";
echo "\e[32mok\n";
echo "\e[39mlistening...";
if (@socket_listen($this->socket, 5) === false) {
echo "\nListen Failed " . socket_strerror(socket_last_error($this->socket)) . "\n";
echo "\n\e[31mListen Failed " . socket_strerror(socket_last_error($this->socket)) . "\n";
exit;
}
echo "ok\n";
echo "initializing standard memory store...";
echo "\e[32mok\n";
echo "\e[39minitializing standard memory store...";
$this->memMap[md5('standard')] = ['name' => 'standard', 'elements' => 0, 'values' => []];
echo "ok\n";
echo "\e[32mok\n";
echo memory_get_usage(true) . " Bytes of memory allocated\n";
echo memory_get_usage() . " Bytes of memory used\n";
echo "ready to be used...\n";
echo "\e[32mready to be used...\n";
$this->listen();
}
......@@ -91,7 +91,7 @@ class server
}
if (in_array($this->socket, $read)) {
if (($msgsock = socket_accept($this->socket)) === false) {
echo "socket_accept() error: " . socket_strerror(socket_last_error($this->socket)) . "\n";
echo "\e[31msocket_accept() error: " . socket_strerror(socket_last_error($this->socket)) . "\n";
break;
}
$this->clients[] = $msgsock;
......@@ -99,7 +99,7 @@ class server
foreach ($this->clients as $key => $client) {
if (in_array($client, $read)) {
if (false === ($buffer = socket_read($client, 1048576 , PHP_BINARY_READ))) {
echo "socket_read() error: " . socket_strerror(socket_last_error($client)) . "\n";
echo "s\e[31mocket_read() error: " . socket_strerror(socket_last_error($client)) . "\n";
socket_clear_error($client);
break 2;
}
......@@ -456,30 +456,32 @@ class server
}
if (isset($_SERVER[1])) {
if ($_SERVER[1] == 'help') {
if (isset($_SERVER['argv'][1])) {
if ($_SERVER['argv'][1] == 'help') {
echo "php server.php mode port\n";
echo "mode : help|listen -> standard listen\n";
echo "port : int between 450 and 65000 -> standard 5000\n";
exit;
}
if ($_SERVER[1] == 'version') {
echo "YAMS Server v 1.0\n";
echo "2021 by Hannes Peterseim\n";
echo "https://www.petit-souris.de\n";
if ($_SERVER['argv'][1] == 'version') {
echo "\e[36mYAMS Server v 1.0\n";
echo "\e[36m2021 by Hannes Peterseim\n";
echo "\e[36mhttps://www.petit-souris.de\n";
exit;
}
}
if (isset($_SERVER[2])) {
if (!is_int($_SERVER[2]) || $_SERVER[2] < 450 || $_SERVER[2] > 65000) {
echo "port " . $_SERVER[2] . " not possible\n";
echo "port should be int and between 450 and 65000\n";
if (isset($_SERVER['argv'][2])) {
if (!is_int($_SERVER['argv'][2]) || $_SERVER['argv'][2] < 450 || $_SERVER['argv'][2] > 65000) {
echo "\e[31mport " . $_SERVER[2] . " not possible\n";
echo "\e[31mport should be int and between 450 and 65000\n";
}
}
set_time_limit(0);
ob_implicit_flush();
$address = '127.0.0.1';
$port = 5000;
if (isset($_SERVER[2]) && is_int($_SERVER[2])) {
$port = $_SERVER[2];
if (isset($_SERVER['argv'][2]) && is_int($_SERVER['argv'][2])) {
$port = $_SERVER['argv'][2];
}
new server($address, $port);
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment