.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 | } |