PluginProbe ʕ •ᴥ•ʔ
JetBackup – Backup, Restore & Migrate / trunk
JetBackup – Backup, Restore & Migrate vtrunk
3.1.22.3 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.8.1 1.4.9 1.5.0 1.5.1 1.5.1.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 1.6.10 1.6.11 1.6.12 1.6.13 1.6.15 1.6.5.1 1.6.8.8 1.6.9 1.6.9.1 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7.5 2.0.8.7 2.0.9.11 2.0.9.14 2.0.9.15 2.0.9.6 2.0.9.7 2.0.9.9 3.1.10.7 3.1.11.1 3.1.12.3 3.1.13.4 3.1.14.17 3.1.15.4 3.1.16.1 3.1.17.5 3.1.18.10 3.1.18.8 3.1.18.9 3.1.19.8 3.1.20.3 3.1.21.3 3.1.7.9 3.1.9.2 trunk 1.1.90 1.1.91 1.2.0 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.4.2
backup / src / JetBackup / 3rdparty / SocketAPI / Socket / Socket.php
backup / src / JetBackup / 3rdparty / SocketAPI / Socket Last commit date
.htaccess 1 year ago Socket.php 4 months ago index.html 1 year ago web.config 1 year ago
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 }