|
| 1 | +<?php |
| 2 | + |
| 3 | + |
| 4 | +namespace Bolt\connection; |
| 5 | + |
| 6 | +use Bolt\Bolt; |
| 7 | +use Exception; |
| 8 | + |
| 9 | +/** |
| 10 | + * Stream socket class |
| 11 | + * |
| 12 | + * @author Michal Stefanak |
| 13 | + * @link https://github.com/stefanak-michal/Bolt |
| 14 | + * @package Bolt\connection |
| 15 | + */ |
| 16 | +class StreamSocket implements IConnection |
| 17 | +{ |
| 18 | + |
| 19 | + /** |
| 20 | + * @var string |
| 21 | + */ |
| 22 | + private $ip; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var int |
| 26 | + */ |
| 27 | + private $port; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var int |
| 31 | + */ |
| 32 | + private $timeout; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var array |
| 36 | + */ |
| 37 | + private $sslContextOptions = []; |
| 38 | + |
| 39 | + /** |
| 40 | + * @var resource |
| 41 | + */ |
| 42 | + private $stream; |
| 43 | + |
| 44 | + /** |
| 45 | + * StreamSocket constructor. |
| 46 | + * @param string $ip |
| 47 | + * @param int $port |
| 48 | + * @param int $timeout |
| 49 | + */ |
| 50 | + public function __construct(string $ip = '127.0.0.1', int $port = 7687, int $timeout = 15) |
| 51 | + { |
| 52 | + $this->ip = $ip; |
| 53 | + $this->port = $port; |
| 54 | + $this->timeout = $timeout; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Set SSL Context options |
| 59 | + * @link https://www.php.net/manual/en/context.ssl.php |
| 60 | + * @param array $options |
| 61 | + */ |
| 62 | + public function setSslContextOptions(array $options) |
| 63 | + { |
| 64 | + $this->sslContextOptions = $options; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Connect |
| 69 | + * @return bool |
| 70 | + * @throws Exception |
| 71 | + */ |
| 72 | + public function connect(): bool |
| 73 | + { |
| 74 | + $context = stream_context_create([ |
| 75 | + 'socket' => [ |
| 76 | + 'tcp_nodelay' => true, |
| 77 | + ], |
| 78 | + 'ssl' => $this->sslContextOptions |
| 79 | + ]); |
| 80 | + |
| 81 | + $this->stream = stream_socket_client( 'tcp://' . $this->ip . ':' . $this->port, $errno, $errstr, $this->timeout, STREAM_CLIENT_CONNECT, $context); |
| 82 | + |
| 83 | + if ($this->stream === false) { |
| 84 | + Bolt::error($errstr . ' (' . $errno . ')'); |
| 85 | + return false; |
| 86 | + } |
| 87 | + |
| 88 | + if (!stream_set_blocking($this->stream, true)) { |
| 89 | + Bolt::error('Cannot set socket into blocking mode'); |
| 90 | + return false; |
| 91 | + } |
| 92 | + |
| 93 | + if (!empty($this->sslContextOptions)) { |
| 94 | + if (stream_socket_enable_crypto($this->stream, true, STREAM_CRYPTO_METHOD_ANY_CLIENT) !== true) { |
| 95 | + Bolt::error('Enable encryption error'); |
| 96 | + return false; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + return true; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Write to connection |
| 105 | + * @param string $buffer |
| 106 | + * @throws Exception |
| 107 | + */ |
| 108 | + public function write(string $buffer) |
| 109 | + { |
| 110 | + if (Bolt::$debug) |
| 111 | + $this->printHex($buffer); |
| 112 | + |
| 113 | + |
| 114 | + if (fwrite($this->stream, $buffer) === false) |
| 115 | + Bolt::error('Write error'); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Read from connection |
| 120 | + * @param int $length |
| 121 | + * @return string |
| 122 | + * @throws Exception |
| 123 | + */ |
| 124 | + public function read(int $length = 2048): string |
| 125 | + { |
| 126 | + $res = fread($this->stream, $length); |
| 127 | + if (empty($res)) |
| 128 | + Bolt::error('Read error'); |
| 129 | + |
| 130 | + if (Bolt::$debug) |
| 131 | + $this->printHex($res, false); |
| 132 | + |
| 133 | + return (string)$res; |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Close connection |
| 138 | + */ |
| 139 | + public function disconnect() |
| 140 | + { |
| 141 | + if (is_resource($this->stream)) |
| 142 | + stream_socket_shutdown($this->stream, STREAM_SHUT_RDWR); |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Print buffer as HEX |
| 147 | + * @param string $str |
| 148 | + * @param bool $write |
| 149 | + */ |
| 150 | + private function printHex(string $str, bool $write = true) |
| 151 | + { |
| 152 | + $str = implode(unpack('H*', $str)); |
| 153 | + echo '<pre>'; |
| 154 | + echo $write ? '> ' : '< '; |
| 155 | + foreach (str_split($str, 8) as $chunk) { |
| 156 | + echo implode(' ', str_split($chunk, 2)); |
| 157 | + echo ' '; |
| 158 | + } |
| 159 | + echo '</pre>'; |
| 160 | + } |
| 161 | + |
| 162 | +} |
0 commit comments