| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * Async sockets | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @copyright Copyright (c) 2015-2016, Efimov Evgenij <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * This source file is subject to the MIT license that is bundled | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * with this source code in the file LICENSE. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | namespace AsyncSockets\Socket\Io; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use AsyncSockets\Frame\FramePickerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * Class StreamedClientIo | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | class StreamedClientIo extends AbstractClientIo | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * Read attempts count | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     const READ_ATTEMPTS = 2; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * Amount of read attempts | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @var int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     private $readAttempts = self::READ_ATTEMPTS; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * Remote socket address | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     private $remoteAddress; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     /** {@inheritdoc} */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 25 |  |     protected function readRawDataIntoPicker(FramePickerInterface $picker) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         // work-around https://bugs.php.net/bug.php?id=52602 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 25 |  |         $resource         = $this->socket->getStreamResource(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $readContext      = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 25 |  |             'countCycles'       => 0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 25 |  |             'dataBeforeIo'      => $this->getDataInSocket(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 25 |  |             'isStreamDataEmpty' => false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 25 |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         do { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 25 |  |             $data = fread($resource, self::SOCKET_BUFFER_SIZE); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 25 |  |             $this->throwNetworkSocketExceptionIf($data === false, 'Failed to read data.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 24 |  |             $isDataEmpty = $data === ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 24 |  |             $result      = $picker->pickUpData($data, $this->getRemoteAddress()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 24 |  |             $readContext['countCycles']      += 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 24 |  |             $readContext['isStreamDataEmpty'] = $this->isReadDataActuallyEmpty($data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 24 |  |             $this->readAttempts               = $this->resolveReadAttempts($readContext, $this->readAttempts); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 24 |  |         } while (!$picker->isEof() && !$isDataEmpty); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 24 |  |         return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * Calculate attempts value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * @param array $context Read context | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * @param int   $currentAttempts Current attempts counter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * @return int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 24 |  |     private function resolveReadAttempts(array $context, $currentAttempts) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 24 |  |         return ($context['countCycles'] === 1 && empty($context['dataBeforeIo'])) || | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 11 |  |                ($context['countCycles'] > 1   && $context['isStreamDataEmpty']) ? | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 24 |  |             $currentAttempts - 1 : | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 24 |  |             self::READ_ATTEMPTS; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 80 |  |  |     /** {@inheritdoc} */ | 
            
                                                                        
                            
            
                                    
            
            
                | 81 | 4 |  |     protected function writeRawData($data) | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 83 | 4 |  |         $resource = $this->socket->getStreamResource(); | 
            
                                                                        
                            
            
                                    
            
            
                | 84 | 4 |  |         $test     = stream_socket_sendto($resource, ''); | 
            
                                                                        
                            
            
                                    
            
            
                | 85 | 4 |  |         $this->throwNetworkSocketExceptionIf($test !== 0, 'Failed to send data.'); | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 87 | 3 |  |         $written = fwrite($resource, $data, strlen($data)); | 
            
                                                                        
                            
            
                                    
            
            
                | 88 | 3 |  |         $this->throwNetworkSocketExceptionIf($written === false, 'Failed to send data.'); | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 90 | 2 |  |         if ($written === 0) { | 
            
                                                                        
                            
            
                                    
            
            
                | 91 | 2 |  |             $this->throwExceptionIfNotConnected('Remote connection has been lost.'); | 
            
                                                                        
                            
            
                                    
            
            
                | 92 | 1 |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 94 | 1 |  |         return $written; | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     /** {@inheritdoc} */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 31 |  |     protected function isConnected() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 31 |  |         return $this->resolveRemoteAddress() !== false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     /** {@inheritdoc} */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 24 |  |     protected function getRemoteAddress() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 24 |  |         if ($this->remoteAddress === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 24 |  |             $this->remoteAddress = $this->resolveRemoteAddress(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 24 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 24 |  |         return $this->remoteAddress; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      * Checks whether data read from stream buffer can be filled later | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      * @param string $data Read data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 24 |  |     private function isReadDataActuallyEmpty($data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 24 |  |         $result = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 | 24 |  |         if ($data === '') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 5 |  |             $dataInSocket = $this->getDataInSocket(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 5 |  |             $result       = $dataInSocket === '' || $dataInSocket === false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 5 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 | 24 |  |         return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     /** {@inheritdoc} */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 | 2 |  |     protected function canReachFrame() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 | 2 |  |         return $this->readAttempts > 0 && $this->isConnected(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |      * Return first byte from socket buffer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 | 25 |  |     private function getDataInSocket() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 | 25 |  |         return stream_socket_recvfrom($this->socket->getStreamResource(), 1, STREAM_PEEK); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |      * Return remote address if we connected or false otherwise | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |      * @return string|bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 | 31 |  |     private function resolveRemoteAddress() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 | 31 |  |         return stream_socket_get_name($this->socket->getStreamResource(), true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 156 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 157 |  |  |  |