Draft client/server socket without functionality

This commit is contained in:
Dennis Eichhorn 2019-10-28 21:28:38 +01:00
parent c92f784967
commit 6c5a7dbdcb
2 changed files with 21 additions and 7 deletions

View File

@ -72,6 +72,8 @@ class Client extends SocketAbstract
\socket_connect($this->sock, $this->ip, $this->port);
$i = 0;
$errorCounter = 0;
while ($this->run) {
try {
++$i;
@ -80,6 +82,12 @@ class Client extends SocketAbstract
$read = [$this->sock];
if (\socket_last_error() !== 0) {
++$errorCounter;
}
// todo: create reset condition for errorCounter. Probably if a successfull read happened
//if (socket_select($read, $write = null, $except = null, 0) < 1) {
// error
// socket_last_error();
@ -102,7 +110,11 @@ class Client extends SocketAbstract
$this->commands->trigger($data[0], 0, $data);
}
}
} catch (\Error $e) {
if ($errorCounter > 10) {
$this->run = false;
}
} catch (\Throwable $e) {
$this->run = false;
}
}

View File

@ -128,13 +128,13 @@ class Server extends SocketAbstract
public function handshake($client, $headers) : bool
{
// todo: different handshake for normal tcp connection
//return true;
return true;
if (\preg_match("/Sec-WebSocket-Version: (.*)\r\n/", $headers, $match) === false) {
return false;
}
$version = (int) $match[1];
$version = (int) ($match[1] ?? -1);
if ($version !== 13) {
return false;
@ -176,8 +176,8 @@ class Server extends SocketAbstract
public function run() : void
{
$this->app->logger->info('Start listening...');
\socket_listen($this->sock);
\socket_set_nonblock($this->sock);
@\socket_listen($this->sock);
@\socket_set_nonblock($this->sock);
$this->conn[] = $this->sock;
$this->app->logger->info('Start running...');
@ -196,11 +196,12 @@ class Server extends SocketAbstract
foreach ($read as $key => $socket) {
if ($this->sock === $socket) {
$newc = \socket_accept($this->sock);
$newc = @\socket_accept($this->sock);
$this->connectClient($newc);
} else {
$client = $this->clientManager->getBySocket($socket);
$data = \socket_read($socket, 1024);
$data = @\socket_read($socket, 1024, \PHP_NORMAL_READ);
var_dump($data);
if (!$client->getHandshake()) {
$this->app->logger->debug('Doing handshake...');
@ -211,6 +212,7 @@ class Server extends SocketAbstract
$this->disconnectClient($client);
}
} else {
// todo: maybe implement shutdown and help for testing?
$this->packetManager->handle($this->unmask($data), $client);
}
}