You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

103 lines
2.2 KiB

<?php
namespace RouterOS;
use RouterOS\Exceptions\ClientException;
trait SocketTrait
{
/**
* Socket resource
*
* @var resource|null
*/
private $_socket;
/**
* Code of error
*
* @var int
*/
private $_socket_err_num;
/**
* Description of socket error
*
* @var string
*/
private $_socket_err_str;
/**
* Initiate socket session
*
* @return void
* @throws \RouterOS\Exceptions\ClientException
* @throws \RouterOS\Exceptions\ConfigException
*/
private function openSocket()
{
// Default: Context for ssl
$context = stream_context_create([
'ssl' => [
'ciphers' => 'ADH:ALL',
'verify_peer' => false,
'verify_peer_name' => false
]
]);
// Default: Proto tcp:// but for ssl we need ssl://
$proto = $this->config('ssl') ? 'ssl://' : '';
// Initiate socket client
$socket = @stream_socket_client(
$proto . $this->config('host') . ':' . $this->config('port'),
$this->_socket_err_num,
$this->_socket_err_str,
$this->config('timeout'),
STREAM_CLIENT_CONNECT,
$context
);
// Throw error is socket is not initiated
if (false === $socket) {
throw new ClientException('Unable to establish socket session, ' . $this->_socket_err_str);
}
//Timeout read
stream_set_timeout($socket, $this->config('timeout'));
// Save socket to static variable
$this->setSocket($socket);
}
/**
* Close socket session
*
* @return bool
*/
private function closeSocket(): bool
{
return fclose($this->_socket);
}
/**
* Save socket resource to static variable
*
* @param resource $socket
* @return void
*/
private function setSocket($socket)
{
$this->_socket = $socket;
}
/**
* Return socket resource if is exist
*
* @return resource
*/
public function getSocket()
{
return $this->_socket;
}
}