Socket.php
96 lines
| 1 | <?php |
| 2 | /* |
| 3 | * |
| 4 | * JetBackup @ package |
| 5 | * Created By Idan Ben-Ezra |
| 6 | * |
| 7 | * Copyrights @ JetApps |
| 8 | * https://www.jetapps.com |
| 9 | * |
| 10 | **/ |
| 11 | namespace JetBackup\SocketAPI\Socket; |
| 12 | |
| 13 | use JetBackup\SocketAPI\Exception\SocketException; |
| 14 | use JetBackup\SocketAPI\Message\MessageReader; |
| 15 | use JetBackup\SocketAPI\Message\MessageWriter; |
| 16 | |
| 17 | class Socket { |
| 18 | |
| 19 | private static int $_readTimeout = 120; |
| 20 | private static int $_writeTimeout = 30; |
| 21 | |
| 22 | private $_socket; |
| 23 | private $_stream; |
| 24 | private $_file; |
| 25 | |
| 26 | private $_reader; |
| 27 | private $_writer; |
| 28 | |
| 29 | public static function setReadTimeout(int $seconds):void { self::$_readTimeout = max($seconds, 5); } |
| 30 | public static function setWriteTimeout(int $seconds):void { self::$_writeTimeout = max($seconds, 5); } |
| 31 | |
| 32 | /** |
| 33 | * Socket constructor. |
| 34 | * @param $socket |
| 35 | * @throws SocketException |
| 36 | */ |
| 37 | public function __construct($socket=null) { |
| 38 | $this->_socket = $socket ?: socket_create(AF_UNIX, SOCK_STREAM, 0); |
| 39 | if(!$this->getSocketResource()) throw new SocketException("Could not create socket"); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * @param string $file |
| 44 | */ |
| 45 | public function setFile($file) { $this->_file = $file; } |
| 46 | |
| 47 | /** |
| 48 | * @return string |
| 49 | */ |
| 50 | public function getFile() { return $this->_file; } |
| 51 | |
| 52 | public function getSocketResource() { return $this->_socket; } |
| 53 | public function getStreamResource() { |
| 54 | if(!$this->_stream) $this->_stream = socket_export_stream($this->getSocketResource()); |
| 55 | return $this->_stream; |
| 56 | } |
| 57 | |
| 58 | |
| 59 | /** |
| 60 | * @return MessageReader |
| 61 | */ |
| 62 | public function getReader(): MessageReader { |
| 63 | if(!$this->_reader) $this->_reader = new MessageReader($this); |
| 64 | return $this->_reader; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * @return MessageWriter |
| 69 | */ |
| 70 | public function getWriter(): MessageWriter { |
| 71 | if(!$this->_writer) $this->_writer = new MessageWriter($this); |
| 72 | return $this->_writer; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * @param bool $isServer |
| 77 | * |
| 78 | * @throws SocketException |
| 79 | */ |
| 80 | public function connect( bool $isServer=false) { |
| 81 | if(!$this->getFile()) throw new SocketException("There is no socket file set"); |
| 82 | if($isServer && file_exists($this->getFile())) @unlink($this->getFile()); |
| 83 | if (!file_exists($this->getFile()) || !is_readable($this->getFile())) throw new SocketException("Could not connect to socket (not exist)."); |
| 84 | if(!@socket_connect($this->getSocketResource(), $this->getFile())) throw new SocketException("Could not connect to socket (socket error)."); |
| 85 | // Set socket timeouts to prevent blocking forever if the server doesn't respond |
| 86 | socket_set_option($this->getSocketResource(), SOL_SOCKET, SO_RCVTIMEO, ['sec' => self::$_readTimeout, 'usec' => 0]); |
| 87 | socket_set_option($this->getSocketResource(), SOL_SOCKET, SO_SNDTIMEO, ['sec' => self::$_writeTimeout, 'usec' => 0]); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * |
| 92 | */ |
| 93 | public function close() { |
| 94 | @socket_close($this->getSocketResource()); |
| 95 | } |
| 96 | } |