Browse Source

some methods for work with socket moved to SocketTrait

tags/0.8
Paul Rock 7 years ago
parent
commit
ab74b7f788
  1. 55
      src/Client.php
  2. 2
      src/Config.php
  3. 57
      src/SocketTrait.php

55
src/Client.php

@ -15,6 +15,8 @@ use RouterOS\Helpers\ArrayHelper;
*/
class Client implements Interfaces\ClientInterface
{
use SocketTrait;
/**
* Socket resource
*
@ -431,10 +433,12 @@ class Client implements Interfaces\ClientInterface
* Save socket resource to static variable
*
* @param resource $socket
* @return Client
*/
private function setSocket($socket)
private function setSocket($socket): Client
{
$this->_socket = $socket;
return $this;
}
/**
@ -446,53 +450,4 @@ class Client implements Interfaces\ClientInterface
{
return $this->_socket;
}
/**
* Initiate socket session
*
* @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 (!$socket) {
throw new ClientException('Unable to establish socket session, ' . $this->_socket_err_str);
}
// Save socket to static variable
return $this->setSocket($socket);
}
/**
* Close socket session
*
* @return bool
*/
private function closeSocket(): bool
{
return fclose($this->_socket);
}
}

2
src/Config.php

@ -111,7 +111,7 @@ class Config implements ConfigInterface
*
* @param string $name
* @return mixed
* @throws ConfigException
* @throws \RouterOS\Exceptions\ConfigException
*/
public function get(string $name)
{

57
src/SocketTrait.php

@ -0,0 +1,57 @@
<?php
namespace RouterOS;
use RouterOS\Exceptions\ClientException;
trait SocketTrait
{
/**
* Initiate socket session
*
* @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 (!$socket) {
throw new ClientException('Unable to establish socket session, ' . $this->_socket_err_str);
}
// Save socket to static variable
return $this->setSocket($socket);
}
/**
* Close socket session
*
* @return bool
*/
private function closeSocket(): bool
{
return fclose($this->_socket);
}
}
Loading…
Cancel
Save