Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 29 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| Client | |
0.00% |
0 / 29 |
|
0.00% |
0 / 5 |
182 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| disconnect | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| run | |
0.00% |
0 / 23 |
|
0.00% |
0 / 1 |
90 | |||
| shutdown | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| addPacket | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Jingga |
| 4 | * |
| 5 | * PHP Version 8.1 |
| 6 | * |
| 7 | * @package phpOMS\Socket\Client |
| 8 | * @copyright Dennis Eichhorn |
| 9 | * @license OMS License 2.0 |
| 10 | * @version 1.0.0 |
| 11 | * @link https://jingga.app |
| 12 | */ |
| 13 | declare(strict_types=1); |
| 14 | |
| 15 | namespace phpOMS\Socket\Client; |
| 16 | |
| 17 | use phpOMS\Application\ApplicationAbstract; |
| 18 | use phpOMS\Message\Socket\PacketManager; |
| 19 | use phpOMS\Socket\Server\ClientManager; |
| 20 | use phpOMS\Socket\SocketAbstract; |
| 21 | |
| 22 | /** |
| 23 | * Client socket class. |
| 24 | * |
| 25 | * @package phpOMS\Socket\Client |
| 26 | * @license OMS License 2.0 |
| 27 | * @link https://jingga.app |
| 28 | * @since 1.0.0 |
| 29 | */ |
| 30 | class Client extends SocketAbstract |
| 31 | { |
| 32 | /** |
| 33 | * Packet manager. |
| 34 | * |
| 35 | * @var PacketManager |
| 36 | * @since 1.0.0 |
| 37 | */ |
| 38 | private $packetManager = null; |
| 39 | |
| 40 | /** |
| 41 | * Socket application. |
| 42 | * |
| 43 | * @var SocketApplication |
| 44 | * @since 1.0.0 |
| 45 | */ |
| 46 | private $app = null; |
| 47 | |
| 48 | private $clientManager = null; |
| 49 | |
| 50 | private array $packets = []; |
| 51 | |
| 52 | /** |
| 53 | * Constructor. |
| 54 | * |
| 55 | * @since 1.0.0 |
| 56 | */ |
| 57 | public function __construct(ApplicationAbstract $app) |
| 58 | { |
| 59 | $this->app = $app; |
| 60 | $this->clientManager = new ClientManager(); |
| 61 | $this->packetManager = new PacketManager($this->app->router, $this->app->dispatcher); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Disconnect from server. |
| 66 | * |
| 67 | * @return void |
| 68 | * |
| 69 | * @since 1.0.0 |
| 70 | */ |
| 71 | private function disconnect() : void |
| 72 | { |
| 73 | $this->run = false; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * {@inheritdoc} |
| 78 | */ |
| 79 | public function run() : void |
| 80 | { |
| 81 | \socket_connect($this->sock, $this->ip, $this->port); |
| 82 | |
| 83 | $errorCounter = 0; |
| 84 | |
| 85 | while ($this->run) { |
| 86 | try { |
| 87 | if (!empty($this->packets)) { |
| 88 | $msg = \array_shift($this->packets); |
| 89 | |
| 90 | \socket_write($this->sock, $msg, \strlen($msg)); |
| 91 | } |
| 92 | |
| 93 | $read = [$this->sock]; |
| 94 | |
| 95 | if (\socket_last_error() !== 0) { |
| 96 | ++$errorCounter; |
| 97 | } |
| 98 | |
| 99 | // todo: create reset condition for errorCounter. Probably if a successful read happened |
| 100 | |
| 101 | //if (socket_select($read, $write = null, $except = null, 0) < 1) { |
| 102 | // error |
| 103 | // socket_last_error(); |
| 104 | // socket_strerror(socket_last_error()); |
| 105 | //} |
| 106 | |
| 107 | if (!empty($read)) { |
| 108 | $data = \socket_read($this->sock, 1024); |
| 109 | |
| 110 | \var_dump($data); |
| 111 | |
| 112 | /* Server no data */ |
| 113 | if ($data === false) { |
| 114 | continue; |
| 115 | } |
| 116 | |
| 117 | /* Normalize */ |
| 118 | $data = \trim($data); |
| 119 | |
| 120 | if (!empty($data)) { |
| 121 | $data = \explode(' ', $data); |
| 122 | $this->commands->trigger($data[0], 0, $data); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | if ($errorCounter > 10) { |
| 127 | $this->run = false; |
| 128 | } |
| 129 | } catch (\Throwable $_) { |
| 130 | $this->run = false; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | $this->close(); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Stop the socket connection to the server |
| 139 | * |
| 140 | * @return void |
| 141 | * |
| 142 | * @since 1.0.0 |
| 143 | */ |
| 144 | public function shutdown() : void |
| 145 | { |
| 146 | $this->run = false; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Add packet to be handeld |
| 151 | * |
| 152 | * @param mixed $packet Packet to handle |
| 153 | * |
| 154 | * @return void |
| 155 | * |
| 156 | * @since 1.0.0 |
| 157 | */ |
| 158 | public function addPacket($packet) : void |
| 159 | { |
| 160 | $this->packets[] = $packet; |
| 161 | } |
| 162 | } |