| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * Async sockets | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @copyright Copyright (c) 2015, 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 |  |  | use AsyncSockets\Socket\SocketInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * Class DatagramClientIo | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | class DatagramClientIo extends AbstractClientIo | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * Destination address | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     protected $remoteAddress; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * Constructor | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @param SocketInterface $socket Socket object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @param string|null     $remoteAddress Destination address in form scheme://host:port or null for local files io | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 34 |  |     public function __construct(SocketInterface $socket, $remoteAddress) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 34 |  |         parent::__construct($socket); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 34 |  |         if ($remoteAddress) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 32 |  |             $components = parse_url($remoteAddress); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 32 |  |             $this->remoteAddress = $components['host'] . ':' . $components['port']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 32 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 34 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     /** {@inheritdoc} */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 14 |  |     protected function readRawDataIntoPicker(FramePickerInterface $picker) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 14 |  |         $size     = self::SOCKET_BUFFER_SIZE; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 14 |  |         $resource = $this->socket->getStreamResource(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         do { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 14 |  |             $data = stream_socket_recvfrom($resource, $size, STREAM_PEEK); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 14 |  |             if (strlen($data) < $size) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 14 |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 4 |  |             $size += $size; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 4 |  |         } while (true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 14 |  |         $data = stream_socket_recvfrom($resource, $size, 0, $actualRemoteAddress); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 14 |  |         return $picker->pickUpData($data, $actualRemoteAddress); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     /** {@inheritdoc} */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 8 |  |     protected function writeRawData($data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 8 |  |         $result = stream_socket_sendto($this->socket->getStreamResource(), $data, 0, $this->remoteAddress); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 8 |  |         $this->throwNetworkSocketExceptionIf($result < 0, 'Failed to send data.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 4 |  |         return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 69 |  |  |     /** {@inheritdoc} */ | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |     protected function getRemoteAddress() | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |         return $this->remoteAddress; | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     /** {@inheritdoc} */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 23 |  |     protected function isConnected() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 23 |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     /** {@inheritdoc} */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 15 |  |     protected function isEndOfTransfer() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 15 |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     /** {@inheritdoc} */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 4 |  |     protected function canReachFrame() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         // in datagram we have no second chance to receive the frame | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 4 |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 93 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 94 |  |  |  |