diff --git a/Socket/Packets/Header.php b/Message/Socket/Header.php similarity index 72% rename from Socket/Packets/Header.php rename to Message/Socket/Header.php index e443d5aa2..4611952ce 100644 --- a/Socket/Packets/Header.php +++ b/Message/Socket/Header.php @@ -4,7 +4,7 @@ * * PHP Version 7.4 * - * @package phpOMS\Socket\Packets + * @package phpOMS\Message\Socket * @copyright Dennis Eichhorn * @license OMS License 1.0 * @version 1.0.0 @@ -12,19 +12,21 @@ */ declare(strict_types=1); -namespace phpOMS\Socket\Packets; +namespace phpOMS\Message\Socket; + +use phpOMS\Message\HeaderAbstract; /** * Server class. * * Parsing/serializing arrays to and from php file * - * @package phpOMS\Socket\Packets + * @package phpOMS\Message\Socket * @license OMS License 1.0 * @link https://orange-management.org * @since 1.0.0 */ -class Header implements \Serializable +class Header extends HeaderAbstract implements \Serializable { private $sendFrom = null; @@ -54,6 +56,14 @@ class Header implements \Serializable */ private $subtype = 0; + /** + * Header. + * + * @var string[][] + * @since 1.0.0 + */ + private array $header = []; + public function getSendFrom() { return $this->sendFrom; @@ -176,4 +186,43 @@ class Header implements \Serializable public function unserialize($string) : void { } + + /** + * {@inheritdoc} + */ + public function getProtocolVersion() : string + { + return 'Socket/1.1'; + } + + /** + * {@inheritdoc} + */ + public function set(string $key, string $header, bool $overwrite = false) : bool + { + return true; + } + + /** + * {@inheritdoc} + */ + public function get(string $key = null) : array + { + return $key === null ? $this->header : ($this->header[\strtolower($key)] ?? []); + } + + /** + * {@inheritdoc} + */ + public function has(string $key) : bool + { + return isset($this->header[$key]); + } + + /** + * {@inheritdoc} + */ + public function generate(int $code) : void + { + } } diff --git a/Message/Socket/PacketManager.php b/Message/Socket/PacketManager.php new file mode 100644 index 000000000..2929a970a --- /dev/null +++ b/Message/Socket/PacketManager.php @@ -0,0 +1,88 @@ +router = $router; + $this->dispatcher = $dispatcher; + } + + /** + * Handle package. + * + * @param string $data Package data + * + * @return void + * + * @since 1.0.0 + */ + public function handle(string $data, $client) : void + { + $request = new Request($data); + $request->getHeader()->setAccount($client->getAccount()->getId()); + + $response = new Response(); + + $this->dispatcher->dispatch( + $this->router->route($data, 'Socket', 1, $client->getAccount()), + $request, + $response + ); + } +} diff --git a/Socket/Packets/PacketType.php b/Message/Socket/PacketType.php similarity index 86% rename from Socket/Packets/PacketType.php rename to Message/Socket/PacketType.php index 4b8555f0c..ada6c6af3 100644 --- a/Socket/Packets/PacketType.php +++ b/Message/Socket/PacketType.php @@ -4,7 +4,7 @@ * * PHP Version 7.4 * - * @package phpOMS\Socket\Packets + * @package phpOMS\Message\Socket * @copyright Dennis Eichhorn * @license OMS License 1.0 * @version 1.0.0 @@ -12,14 +12,14 @@ */ declare(strict_types=1); -namespace phpOMS\Socket\Packets; +namespace phpOMS\Message\Socket; use phpOMS\Stdlib\Base\Enum; /** * Packet type enum. * - * @package phpOMS\Socket\Packets + * @package phpOMS\Message\Socket * @license OMS License 1.0 * @link https://orange-management.org * @since 1.0.0 @@ -36,5 +36,5 @@ abstract class PacketType extends Enum public const LOGIN = 7; /* Login (server/sender) */ public const LOGOUT = 8; /* Logout (server/sender) */ public const CMD = 9; /* Other command */ - public const MODULE = 999999999; /* Module packet ??? */ + public const DOWNLOAD = 10; /* Download */ } diff --git a/Message/Socket/Request.php b/Message/Socket/Request.php index 1c99ccf6d..76e785abb 100644 --- a/Message/Socket/Request.php +++ b/Message/Socket/Request.php @@ -4,16 +4,62 @@ * * PHP Version 7.4 * - * @package TBD + * @package phpOMS\Message\Socket * @copyright Dennis Eichhorn * @license OMS License 1.0 * @version 1.0.0 * @link https://orange-management.org */ declare(strict_types=1); + namespace phpOMS\Message\Socket; -class Request -{ +use phpOMS\Message\RequestAbstract; +final class Request extends RequestAbstract +{ + public function __construct() + { + $this->header = new Header(); + } + + /** + * {@inheritdoc} + */ + public function getOrigin() : string + { + return '127.0.0.1'; + } + + /** + * Get request language + * + * @return string + * + * @since 1.0.0 + */ + public function getRequestLanguage() : string + { + return 'en'; + } + + /** + * Get request locale + * + * @return string + * + * @since 1.0.0 + */ + public function getLocale() : string + { + return 'en_US'; + } + + /** + * {@inheritdoc} + */ + public function getBody() : string + { + return ''; + } } diff --git a/Message/Socket/Response.php b/Message/Socket/Response.php index ed21075a8..4e5c84f59 100644 --- a/Message/Socket/Response.php +++ b/Message/Socket/Response.php @@ -4,16 +4,177 @@ * * PHP Version 7.4 * - * @package TBD + * @package phpOMS\Message\Socket * @copyright Dennis Eichhorn * @license OMS License 1.0 * @version 1.0.0 * @link https://orange-management.org */ declare(strict_types=1); + namespace phpOMS\Message\Socket; -class Response -{ +use phpOMS\Message\ResponseAbstract; +use phpOMS\Contract\RenderableInterface; +final class Response extends ResponseAbstract implements RenderableInterface +{ + /** + * Set response. + * + * @param array $response Response to set + * + * @return void + * + * @since 1.0.0 + */ + public function setResponse(array $response) : void + { + $this->response = $response; + } + + /** + * Remove response by ID. + * + * @param mixed $id Response ID + * + * @return bool + * + * @since 1.0.0 + */ + public function remove($id) : bool + { + if (isset($this->response[$id])) { + unset($this->response[$id]); + + return true; + } + + return false; + } + + /** + * {@inheritdoc} + */ + public function getJsonData() : array + { + return \json_decode($this->getRaw(), true); + } + + /** + * {@inheritdoc} + */ + public function getBody(bool $optimize = false) : string + { + return $this->render($optimize); + } + + /** + * Generate response based on header. + * + * @param bool $optimize Optimize response / minify + * + * @return string + * + * @since 1.0.0 + */ + public function render(bool $optimize = false) : string + { + $types = $this->header->get('Content-Type'); + + foreach ($types as $type) { + if (\stripos($type, MimeType::M_JSON) !== false) { + return (string) \json_encode($this->jsonSerialize()); + } + } + + return $this->getRaw($optimize); + } + + /** + * Generate raw response. + * + * @param bool $optimize Optimize response / minify + * + * @return string + * + * @throws \Exception + * + * @since 1.0.0 + */ + private function getRaw(bool $optimize = false) : string + { + $render = ''; + + foreach ($this->response as $key => $response) { + $render .= StringUtils::stringify($response); + } + + if ($optimize) { + return $this->removeWhitespaceAndLineBreak($render); + } + + return $render; + } + + /** + * Remove whitespace and line break from render + * + * @param string $render Rendered string + * + * @return string + * + * @since 1.0.0 + */ + private function removeWhitespaceAndLineBreak(string $render) : string + { + $types = $this->header->get('Content-Type'); + if (\stripos($types[0], MimeType::M_HTML) !== false) { + $clean = \preg_replace('/(?s)
.*?<\/pre>(*SKIP)(*F)|(\s{2,}|\n|\t)/', ' ', $render);
+
+ return \trim($clean ?? '');
+ }
+
+ return $render;
+ }
+
+ /**
+ * {@inheritdoc}
+ * @todo: this whole workflow with json got improved a little bit but this part looks bad. do i really need so much code or could i simplify it
+ */
+ public function toArray() : array
+ {
+ $result = [];
+
+ try {
+ foreach ($this->response as $key => $response) {
+ if ($response instanceof View) {
+ $result[] = $response->toArray();
+ } elseif (\is_array($response)) {
+ $result[] = $response;
+ } elseif (\is_scalar($response)) {
+ $result[] = $response;
+ } elseif ($response instanceof \JsonSerializable) {
+ $result[] = $response->jsonSerialize();
+ } elseif ($response === null) {
+ continue;
+ } else {
+ throw new \Exception('Wrong response type');
+ }
+ }
+ } catch (\Exception $e) {
+ FileLogger::getInstance('', false)
+ ->error(
+ FileLogger::MSG_FULL, [
+ 'message' => $e->getMessage(),
+ 'line' => __LINE__,
+ 'file' => self::class,
+ ]
+ );
+
+ $result = [];
+ } finally {
+ return $result;
+ }
+ }
}
diff --git a/Socket/Client/Client.php b/Socket/Client/Client.php
index 76f58040b..baed0e888 100644
--- a/Socket/Client/Client.php
+++ b/Socket/Client/Client.php
@@ -14,8 +14,10 @@ declare(strict_types=1);
namespace phpOMS\Socket\Client;
-use phpOMS\Socket\CommandManager;
use phpOMS\Socket\SocketAbstract;
+use phpOMS\ApplicationAbstract;
+use phpOMS\Socket\Server\ClientManager;
+use phpOMS\Message\Socket\PacketManager;
/**
* Client socket class.
@@ -27,21 +29,36 @@ use phpOMS\Socket\SocketAbstract;
*/
class Client extends SocketAbstract
{
- private $commands;
+ /**
+ * Packet manager.
+ *
+ * @var PacketManager
+ * @since 1.0.0
+ */
+ private $packetManager = null;
+
+ /**
+ * Socket application.
+ *
+ * @var SocketApplication
+ * @since 1.0.0
+ */
+ private $app = null;
+
+ private $clientManager = null;
+
+ private array $packets = [];
/**
* Constructor.
*
* @since 1.0.0
*/
- public function __construct()
+ public function __construct(ApplicationAbstract $app)
{
- $this->commands = new CommandManager();
-
- /** @noinspection PhpUnusedParameterInspection */
- $this->commands->attach('disconnect', function ($conn, $para) : void {
- $this->disconnect();
- }, $this);
+ $this->app = $app;
+ $this->clientManager = new ClientManager();
+ $this->packetManager = new PacketManager($this->app->router, $this->app->dispatcher);
}
/**
@@ -70,15 +87,16 @@ class Client extends SocketAbstract
public function run() : void
{
\socket_connect($this->sock, $this->ip, $this->port);
- $i = 0;
$errorCounter = 0;
while ($this->run) {
try {
- ++$i;
- $msg = 'disconnect';
- \socket_write($this->sock, $msg, \strlen($msg));
+ if (!empty($this->packets)) {
+ $msg = \array_shift($this->packets);
+
+ \socket_write($this->sock, $msg, \strlen($msg));
+ }
$read = [$this->sock];
@@ -97,6 +115,8 @@ class Client extends SocketAbstract
if (\count($read) > 0) {
$data = \socket_read($this->sock, 1024);
+ var_dump($data);
+
/* Server no data */
if ($data === false) {
continue;
@@ -122,6 +142,16 @@ class Client extends SocketAbstract
$this->close();
}
+ public function shutdown() : void
+ {
+ $this->run = false;
+ }
+
+ public function addPacket($packet) : void
+ {
+ $this->packets[] = $packet;
+ }
+
/**
* {@inheritdoc}
*/
diff --git a/Socket/Client/ClientConnection.php b/Socket/Client/ClientConnection.php
index 265e9dac3..43b44e851 100644
--- a/Socket/Client/ClientConnection.php
+++ b/Socket/Client/ClientConnection.php
@@ -14,6 +14,8 @@ declare(strict_types=1);
namespace phpOMS\Socket\Client;
+use phpOMS\Account\Account;
+
/**
* Client socket class.
*
@@ -29,10 +31,12 @@ class ClientConnection
private $handshake = false;
private $pid = null;
private $connected = true;
+ private Account $account;
- public function __construct($id, $socket)
+ public function __construct(Account $account, $socket)
{
- $this->id = $id;
+ $this->id = $account->getId();
+ $this->account = $account;
$this->socket = $socket;
}
@@ -41,6 +45,11 @@ class ClientConnection
return $this->id;
}
+ public function getAccount() : Account
+ {
+ return $this->account;
+ }
+
public function getSocket()
{
return $this->socket;
diff --git a/Socket/CommandManager.php b/Socket/CommandManager.php
deleted file mode 100644
index 9413c9e6f..000000000
--- a/Socket/CommandManager.php
+++ /dev/null
@@ -1,119 +0,0 @@
-commands[$cmd] = [$callback, $source];
- ++$this->count;
- }
-
- /**
- * Detach existing command.
- *
- * @param string $cmd Command ID
- * @param mixed $source Provider
- *
- * @return void
- *
- * @since 1.0.0
- */
- public function detach(string $cmd, $source) : void
- {
- if (\array_key_exists($cmd, $this->commands)) {
- unset($this->commands[$cmd]);
- --$this->count;
- }
- }
-
- /**
- * Trigger command.
- *
- * @param string $cmd Command ID
- * @param mixed $conn Client ID
- * @param mixed $para Parameters to pass
- *
- * @return bool|mixed
- *
- * @since 1.0.0
- */
- public function trigger(string $cmd, $conn, $para)
- {
- if (\array_key_exists($cmd, $this->commands)) {
- return $this->commands[$cmd][0]($conn, $para);
- }
-
- return false;
- }
-
- /**
- * Count commands.
- *
- * @return int
- *
- * @since 1.0.0
- */
- public function count() : int
- {
- return $this->count;
- }
-}
diff --git a/Socket/Packets/PacketAbstract.php b/Socket/Packets/PacketAbstract.php
deleted file mode 100644
index a1b304778..000000000
--- a/Socket/Packets/PacketAbstract.php
+++ /dev/null
@@ -1,98 +0,0 @@
-header;
- }
-
- /**
- * Set packet header.
- *
- * @param Header $header Header
- *
- * @return void
- *
- * @since 1.0.0
- */
- public function setHeader(Header $header) : void
- {
- $this->header = $header;
- }
-}
diff --git a/Socket/Packets/PacketManager.php b/Socket/Packets/PacketManager.php
deleted file mode 100644
index 1ad361481..000000000
--- a/Socket/Packets/PacketManager.php
+++ /dev/null
@@ -1,76 +0,0 @@
-commandManager = $cmd;
- $this->clientManager = $user;
- }
-
- /**
- * Handle package.
- *
- * @param string $data Package data
- *
- * @return void
- *
- * @since 1.0.0
- */
- public function handle(string $data, $client) : void
- {
- echo $data;
- }
-}
diff --git a/Socket/Server/ClientManager.php b/Socket/Server/ClientManager.php
index df2db085b..19d3b6724 100644
--- a/Socket/Server/ClientManager.php
+++ b/Socket/Server/ClientManager.php
@@ -28,7 +28,7 @@ class ClientManager
public function get($id)
{
- return $this->clients[$id] ?? new NullClientConnection(\uniqid(), null);
+ return $this->clients[$id] ?? new NullClientConnection($id, null);
}
public function getBySocket($socket)
@@ -39,7 +39,7 @@ class ClientManager
}
}
- return new NullClientConnection(\uniqid(), null);
+ return new NullClientConnection($id, null);
}
public function remove($id)
diff --git a/Socket/Server/Server.php b/Socket/Server/Server.php
index ea302ab78..d307c036e 100644
--- a/Socket/Server/Server.php
+++ b/Socket/Server/Server.php
@@ -14,10 +14,12 @@ declare(strict_types=1);
namespace phpOMS\Socket\Server;
+use phpOMS\Account\Account;
use phpOMS\Socket\Client\ClientConnection;
-use phpOMS\Socket\CommandManager;
-use phpOMS\Socket\Packets\PacketManager;
use phpOMS\Socket\SocketAbstract;
+use phpOMS\Message\Socket\PacketManager;
+use Socket\SocketApplication;
+use phpOMS\ApplicationAbstract;
/**
* Server class.
@@ -61,7 +63,7 @@ class Server extends SocketAbstract
/**
* Socket application.
*
- * @var \Socket\SocketApplication
+ * @var SocketApplication
* @since 1.0.0
*/
private $app = null;
@@ -69,15 +71,15 @@ class Server extends SocketAbstract
/**
* Constructor.
*
- * @param \Socket\SocketApplication $app socketApplication
+ * @param SocketApplication $app socketApplication
*
* @since 1.0.0
*/
- public function __construct($app)
+ public function __construct(ApplicationAbstract $app)
{
$this->app = $app;
$this->clientManager = new ClientManager();
- $this->packetManager = new PacketManager(new CommandManager(), new ClientManager());
+ $this->packetManager = new PacketManager($this->app->router, $this->app->dispatcher);
}
/**
@@ -180,7 +182,7 @@ class Server extends SocketAbstract
@\socket_set_nonblock($this->sock);
$this->conn[] = $this->sock;
- $this->app->logger->info('Start running...');
+ $this->app->logger->info('Is running...');
while ($this->run) {
$read = $this->conn;
@@ -201,31 +203,46 @@ class Server extends SocketAbstract
} else {
$client = $this->clientManager->getBySocket($socket);
$data = @\socket_read($socket, 1024, \PHP_NORMAL_READ);
- var_dump($data);
+
+ if ($data === false) {
+ \socket_close($socket);
+ }
+
+ $data = \is_string($data) ? \trim($data) : '';
if (!$client->getHandshake()) {
$this->app->logger->debug('Doing handshake...');
if ($this->handshake($client, $data)) {
+ $client->setHandshake(true);
$this->app->logger->debug('Handshake succeeded.');
} else {
$this->app->logger->debug('Handshake failed.');
$this->disconnectClient($client);
}
} else {
- // todo: maybe implement shutdown and help for testing?
- $this->packetManager->handle($this->unmask($data), $client);
+ $this->packetManager->handle($data, $client);
}
}
}
}
+ $this->app->logger->info('Is shutdown...');
$this->close();
}
+ public function shutdown($request) : void
+ {
+ $msg = 'shutdown' . "\n";
+ \socket_write($this->clientManager->get($request->getHeader()->getAccount())->getSocket(), $msg, \strlen($msg));
+
+ $this->run = false;
+ }
+
public function connectClient($socket) : void
{
$this->app->logger->debug('Connecting client...');
- $this->clientManager->add($client = new ClientConnection(\uniqid(), $socket));
+ $this->app->accountManager->add(new Account(1));
+ $this->clientManager->add($client = new ClientConnection(new Account(1), $socket));
$this->conn[$client->getId()] = $socket;
$this->app->logger->debug('Connected client.');
}
diff --git a/Socket/SocketType.php b/Socket/SocketType.php
deleted file mode 100644
index 78e6de30d..000000000
--- a/Socket/SocketType.php
+++ /dev/null
@@ -1,32 +0,0 @@
-app = new class() extends ApplicationAbstract
+ {
+ protected string $appName = 'Socket';
+ };
+
+ $this->app->logger = new FileLogger(__DIR__ . '/client.log', false);
+ $this->app->dbPool = $GLOBALS['dbpool'];
+ $this->app->orgId = 1;
+ $this->app->cachePool = new CachePool($this->app->dbPool);
+ $this->app->accountManager = new AccountManager($GLOBALS['session']);
+ $this->app->appSettings = new CoreSettings($this->app->dbPool->get());
+ $this->app->moduleManager = new ModuleManager($this->app, __DIR__ . '/../../../../Modules');
+ $this->app->dispatcher = new Dispatcher($this->app);
+ $this->app->eventManager = new EventManager($this->app->dispatcher);
+ $this->app->eventManager->importFromFile(__DIR__ . '/../../../Socket/Hooks.php');
+ $this->app->l11nManager = new L11nManager($this->app->appName);
+ $this->app->router = new SocketRouter();
+ }
+
+ protected function tearDown() : void
+ {
+ \unlink(__DIR__ . '/client.log');
+ \unlink(__DIR__ . '/server.log');
+ }
+
+ public function testSetupTCPSocket() : void
{
self::markTestIncomplete();
+ return;
+ $pipes = [];
+ $process = \proc_open('php ClientTestHelper.php', [1 => ['pipe', 'w'], 2 => ['pipe', 'w']], $pipes, __DIR__);
+
+ \sleep(5);
+
+ $socket = new Client($this->app);
+ $socket->create('127.0.0.1', $GLOBALS['CONFIG']['socket']['master']['port']);
+
+ $socket->addPacket('handshake' . "\r");
+ $socket->addPacket('help' . "\r");
+ $socket->addPacket('shutdown' . "\r");
+
+ $this->app->router->add('^shutdown$', function() use ($socket) { $socket->shutdown(); });
+
+ $socket->run();
+
+ self::assertEquals(
+ 'Creating socket...' . "\n"
+ . 'Binding socket...' . "\n"
+ . 'Start listening...' . "\n"
+ . 'Is running...' . "\n"
+ . 'Connecting client...' . "\n"
+ . 'Connected client.' . "\n"
+ . 'Doing handshake...' . "\n"
+ . 'Handshake succeeded.' . "\n"
+ . 'Is shutdown...' . "\n",
+ \file_get_contents(__DIR__ . '/server.log')
+ );
}
}
diff --git a/tests/Socket/Client/ClientTestHelper.php b/tests/Socket/Client/ClientTestHelper.php
new file mode 100644
index 000000000..7a054e46a
--- /dev/null
+++ b/tests/Socket/Client/ClientTestHelper.php
@@ -0,0 +1,71 @@
+create('admin', $config['db']['core']['masters']['admin']);
+$GLOBALS['dbpool']->create('select', $config['db']['core']['masters']['select']);
+$GLOBALS['dbpool']->create('update', $config['db']['core']['masters']['update']);
+$GLOBALS['dbpool']->create('insert', $config['db']['core']['masters']['insert']);
+$GLOBALS['dbpool']->create('schema', $config['db']['core']['masters']['schema']);
+
+$httpSession = new HttpSession();
+$GLOBALS['session'] = $httpSession;
+
+DataMapperAbstract::setConnection($GLOBALS['dbpool']->get());
+
+$app = new class() extends ApplicationAbstract
+{
+ protected string $appName = 'Socket';
+};
+
+$app->logger = FileLogger::getInstance(__DIR__ . '/server.log', true);
+$app->dbPool = $GLOBALS['dbpool'];
+$app->orgId = 1;
+$app->cachePool = new CachePool($app->dbPool);
+$app->accountManager = new AccountManager($GLOBALS['session']);
+$app->appSettings = new CoreSettings($app->dbPool->get());
+$app->moduleManager = new ModuleManager($app, __DIR__ . '/../../../../Modules');
+$app->dispatcher = new Dispatcher($app);
+$app->eventManager = new EventManager($app->dispatcher);
+$app->eventManager->importFromFile(__DIR__ . '/../../../Socket/Hooks.php');
+$app->l11nManager = new L11nManager($app->appName);
+$app->router = new SocketRouter();
+
+$socket = new Server($app);
+$socket->create('127.0.0.1', $config['socket']['master']['port']);
+$socket->setLimit(1);
+
+$app->router->add('^shutdown$', function($app, $request) use ($socket) { $socket->shutdown($request); });
+
+$socket->run();
\ No newline at end of file
diff --git a/tests/Socket/CommandManagerTest.php b/tests/Socket/CommandManagerTest.php
deleted file mode 100644
index 9037daf08..000000000
--- a/tests/Socket/CommandManagerTest.php
+++ /dev/null
@@ -1,28 +0,0 @@
-app->logger = FileLogger::getInstance(__DIR__ . '/server.log', true);
+ $this->app->logger = new FileLogger(__DIR__ . '/server.log', false);
$this->app->dbPool = $GLOBALS['dbpool'];
$this->app->orgId = 1;
$this->app->cachePool = new CachePool($this->app->dbPool);
@@ -54,29 +62,46 @@ class ServerTest extends \PHPUnit\Framework\TestCase
$this->app->eventManager = new EventManager($this->app->dispatcher);
$this->app->eventManager->importFromFile(__DIR__ . '/../../../Socket/Hooks.php');
$this->app->l11nManager = new L11nManager($this->app->appName);
- $this->app->router = new WebRouter();
+ $this->app->router = new SocketRouter();
}
protected function tearDown() : void
{
- /*
- \delete(__DIR__ . '/client.log');
- \delete(__DIR__ . '/server.log');*/
+ \unlink(__DIR__ . '/client.log');
+ \unlink(__DIR__ . '/server.log');
}
public function testSetupTCPSocket() : void
{
- self::markTestIncomplete();
- return;
$pipes = [];
- $process = \proc_open('php ServerTestHelper.php 127.0.0.1', [1 => ['pipe', 'w'], 2 => ['pipe', 'w']], $pipes, __DIR__);
+ $process = \proc_open('php ServerTestHelper.php', [1 => ['pipe', 'w'], 2 => ['pipe', 'w']], $pipes, __DIR__);
$socket = new Server($this->app);
$socket->create('127.0.0.1', $GLOBALS['CONFIG']['socket']['master']['port']);
$socket->setLimit(1);
+
+ $this->app->router->add('^shutdown$', function($app, $request) use ($socket) { $socket->shutdown($request); });
+
$socket->run();
- // todo: assert content of server.log
- // todo: assert content of client.log
+ self::assertTrue(\file_exists(__DIR__ . '/server.log'));
+ self::assertEquals(
+ 'Creating socket...' . "\n"
+ . 'Binding socket...' . "\n"
+ . 'Start listening...' . "\n"
+ . 'Is running...' . "\n"
+ . 'Connecting client...' . "\n"
+ . 'Connected client.' . "\n"
+ . 'Doing handshake...' . "\n"
+ . 'Handshake succeeded.' . "\n"
+ . 'Is shutdown...' . "\n",
+ \file_get_contents(__DIR__ . '/server.log')
+ );
+
+ self::assertTrue(\file_exists(__DIR__ . '/client.log'));
+ $client = \file_get_contents(__DIR__ . '/client.log');
+ self::assertStringContainsString('Sending: handshake', $client);
+ self::assertStringContainsString('Sending: help', $client);
+ self::assertStringContainsString('Sending: shutdown', $client);
}
}
diff --git a/tests/Socket/Server/ServerTestHelper.php b/tests/Socket/Server/ServerTestHelper.php
index f4a283fb6..3d6c455a8 100644
--- a/tests/Socket/Server/ServerTestHelper.php
+++ b/tests/Socket/Server/ServerTestHelper.php
@@ -36,12 +36,13 @@ try {
handleSocketError($sock);
$msgs = [
+ 'handshake' . "\r", // this needs to happen first (of course the submitted handshake data needs to be implemented correctl. just sending this is of course bad!)
'help' . "\r",
'shutdown' . "\r",
];
foreach ($msgs as $msg) {
- var_dump($msg);
+ \file_put_contents(__DIR__ . '/client.log', 'Sending: ' . $msg . "\n", \FILE_APPEND);
@\socket_write($sock, $msg, \strlen($msg));
handleSocketError($sock);
@@ -56,7 +57,7 @@ try {
/* Normalize */
$data = \trim($data);
- \file_put_contents(__DIR__ . '/client.log', $data, \FILE_APPEND);
+ \file_put_contents(__DIR__ . '/client.log', 'Receiving' . $data . "\n", \FILE_APPEND);
}
handleSocketError($sock);
@@ -66,4 +67,4 @@ try {
}
handleSocketError($sock);
-$sock = null;
\ No newline at end of file
+$sock = null;
diff --git a/tests/Socket/SocketTypeTest.php b/tests/Socket/SocketTypeTest.php
deleted file mode 100644
index c0f8fb1d3..000000000
--- a/tests/Socket/SocketTypeTest.php
+++ /dev/null
@@ -1,28 +0,0 @@
-