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 / Message / MessageReader.php
backup / src / JetBackup / 3rdparty / SocketAPI / Message Last commit date
.htaccess 1 year ago Message.php 1 year ago MessageReader.php 1 year ago MessageWriter.php 1 year ago index.html 1 year ago web.config 1 year ago
MessageReader.php
122 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\Message;
12
13 use JetBackup\SocketAPI\Exception\MessageException;
14 use JetBackup\SocketAPI\Socket\Socket;
15
16 class MessageReader extends Message {
17
18 const READ_CHUNK = 1024;
19
20 /** @var Socket */
21 private $_socket;
22 private $_buffer;
23
24 public function __construct(Socket $socket) {
25 parent::__construct();
26 $this->_buffer = '';
27 $this->_socket = $socket;
28 }
29
30 /**
31 * @param $length
32 *
33 * @return int
34 * @throws MessageException
35 */
36 public function read($length=self::READ_CHUNK) {
37 if(feof($this->_socket->getStreamResource()))
38 throw new MessageException("Nothing to read from socket");
39
40 if(($data = socket_read($this->_socket->getSocketResource(), $length)) === false) {
41 $code = socket_last_error($this->_socket->getSocketResource());
42 $message = socket_strerror($code);
43 throw new MessageException("Failed reading from socket. Error: {$message} (error code {$code})");
44 }
45
46 $this->append($data);
47
48 return strlen($data);
49 }
50
51 /**
52 * @param string $message
53 */
54 public function append($message) { $this->_buffer .= $message; }
55
56 /**
57 * @throws MessageException
58 */
59 public function checkMessage() {
60
61 // can't find delimiter - wait for more data
62 if(!$this->_findDelimiter()) return;
63
64 // must receive more than 12 bytes - wait for more data
65 if(strlen($this->_buffer) < 13) return;
66
67 $length = self::unpackInt(substr($this->_buffer, 4));
68
69 if(!$length || $length > self::MAX_MESSAGE_LENGTH) {
70 // Can't find length or length is too long - move buffer pointer 1 byte forward and go to the next delimiter
71 $this->_buffer = substr($this->_buffer, 1);
72
73 if(!$length) throw new MessageException("Invalid message length provided");
74 else throw new MessageException("The maximum message length is " . self::MAX_MESSAGE_LENGTH . " bytes. You sent " . $length . " bytes");
75 }
76
77 $msglen = strlen(substr($this->_buffer, 8));
78
79 // not all message was received - wait for more data
80 if($msglen < ($length+4)) return;
81
82 $length_end = self::unpackInt(substr($this->_buffer, $length+8));
83
84 if(!$length_end || $length != $length_end) {
85 // start length and end length is not equal - move buffer pointer 1 byte forward and go to the next delimiter
86 $this->_buffer = substr($this->_buffer, 1);
87
88 if(!$length_end) throw new MessageException("Invalid message protocol");
89 else throw new MessageException("The start message length and end message length doesn't match");
90 }
91
92 // remove the first 8 bytes (delimiter and length) and add this message to the iterator
93 $message = substr($this->_buffer, 8, $length);
94
95 // move the buffer pointer to the next message
96 $this->_buffer = substr($this->_buffer, $length+12);
97
98 foreach($this->_protocol_listener as $listener) $listener->onMessageReady($message);
99 }
100
101 /**
102 * @return bool
103 */
104 private function _findDelimiter() {
105
106 // must receive more than 12 bytes - wait for more data
107 if(strlen($this->_buffer) < 13) return false;
108
109 $d = self::unpackInt($this->_buffer);
110 $o = 4;
111
112 while(self::DELIMITER != $d) {
113 $s = substr($this->_buffer, $o++, 1);
114 if($s === false) return false;
115 $s = (int)ord($s);
116 $d = (($d << 8) | $s) & self::MAX_UINT32;
117 }
118
119 $this->_buffer = substr($this->_buffer, $o-4);
120 return true;
121 }
122 }