PEAR2_Net_Transmitter
1.0.0a1
A wrapper for stream functionality
|
00001 <?php 00002 00022 namespace PEAR2\Net\Transmitter; 00023 00039 class SocketServerConnectionTransmitter extends StreamTransmitter 00040 { 00041 00045 protected $peerIP; 00046 00050 protected $peerPort; 00051 00059 public function __construct($server, $timeout = null) 00060 { 00061 if (!self::isServer($server)) { 00062 throw $this->createException('Invalid server supplied.', 8); 00063 } 00064 $timeout 00065 = null == $timeout ? ini_get('default_socket_timeout') : $timeout; 00066 00067 try { 00068 parent::__construct( 00069 @stream_socket_accept($server, $timeout, $peername) 00070 ); 00071 $hostPortCombo = explode(':', $peername); 00072 $this->peerIP = $hostPortCombo[0]; 00073 $this->peerPort = (int) $hostPortCombo[1]; 00074 } catch (\Exception $e) { 00075 throw $this->createException('Failed to initialize connection.', 9); 00076 } 00077 } 00078 00086 public static function isServer($var) 00087 { 00088 return is_resource($var) 00089 && (bool) preg_match('#\s?server\s?#sm', get_resource_type($var)); 00090 } 00091 00097 public function getPeerIP() 00098 { 00099 return $this->peerIP; 00100 } 00101 00107 public function getPeerPort() 00108 { 00109 return $this->peerPort; 00110 } 00111 00117 public function isDataAwaiting() 00118 { 00119 if (parent::isDataAwaiting()) { 00120 $meta = stream_get_meta_data($this->stream); 00121 return!$meta['timed_out'] && !$meta['eof']; 00122 } 00123 return false; 00124 } 00125 00136 protected function createException($message, $code = 0) 00137 { 00138 return new SocketException( 00139 $message, $code, null, $this->error_no, $this->error_str 00140 ); 00141 } 00142 00143 }