mirror of
https://github.com/Karaka-Management/phpOMS.git
synced 2026-01-11 17:58:41 +00:00
133 lines
2.9 KiB
PHP
133 lines
2.9 KiB
PHP
<?php
|
|
/**
|
|
* Orange Management
|
|
*
|
|
* PHP Version 7.1
|
|
*
|
|
* @category TBD
|
|
* @package TBD
|
|
* @author OMS Development Team <dev@oms.com>
|
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
|
* @copyright 2013 Dennis Eichhorn
|
|
* @license OMS License 1.0
|
|
* @version 1.0.0
|
|
* @link http://orange-management.com
|
|
*/
|
|
namespace phpOMS\Socket\Client;
|
|
|
|
use phpOMS\Socket\CommandManager;
|
|
use phpOMS\Socket\SocketAbstract;
|
|
|
|
/**
|
|
* Client socket class.
|
|
*
|
|
* @category Framework
|
|
* @package phpOMS\Socket\Client
|
|
* @author OMS Development Team <dev@oms.com>
|
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
|
* @license OMS License 1.0
|
|
* @link http://orange-management.com
|
|
* @since 1.0.0
|
|
*/
|
|
class Client extends SocketAbstract
|
|
{
|
|
private $commands;
|
|
|
|
/**
|
|
* Constructor.
|
|
*
|
|
* @since 1.0.0
|
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
|
*/
|
|
public function __construct()
|
|
{
|
|
$this->commands = new CommandManager();
|
|
|
|
/** @noinspection PhpUnusedParameterInspection */
|
|
$this->commands->attach('disconnect', function ($conn, $para) {
|
|
$this->disconnect();
|
|
}, $this);
|
|
}
|
|
|
|
/**
|
|
* Disconnect from server.
|
|
*
|
|
* @since 1.0.0
|
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
|
*/
|
|
private function disconnect()
|
|
{
|
|
$this->run = false;
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function create(string $ip, int $port)
|
|
{
|
|
parent::create($ip, $port);
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function run()
|
|
{
|
|
socket_connect($this->sock, $this->ip, $this->port);
|
|
$i = 0;
|
|
|
|
while ($this->run) {
|
|
try {
|
|
$i++;
|
|
$msg = 'disconnect';
|
|
socket_write($this->sock, $msg, strlen($msg));
|
|
|
|
$read = [$this->sock];
|
|
|
|
//if(socket_select($read, $write = null, $except = null, 0) < 1) {
|
|
// error
|
|
// socket_last_error();
|
|
// socket_strerror(socket_last_error());
|
|
//}
|
|
|
|
if (count($read) > 0) {
|
|
$data = socket_read($this->sock, 1024);
|
|
|
|
/* Server no data */
|
|
if ($data === false) {
|
|
continue;
|
|
}
|
|
|
|
/* Normalize */
|
|
$data = trim($data);
|
|
|
|
if (!empty($data)) {
|
|
$data = explode(' ', $data);
|
|
$this->commands->trigger($data[0], 0, $data);
|
|
}
|
|
}
|
|
} catch (\Error $e) {
|
|
$this->run = false;
|
|
}
|
|
}
|
|
|
|
$this->close();
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function close()
|
|
{
|
|
parent::close();
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function __destruct()
|
|
{
|
|
parent::__destruct();
|
|
}
|
|
}
|