PEAR2_Net_Transmitter
1.0.0a1
A wrapper for stream functionality
|
00001 <?php 00002 00022 namespace PEAR2\Net\Transmitter; 00023 00037 class SocketClientTransmitter extends StreamTransmitter 00038 { 00039 00043 protected $error_no = null; 00044 00048 protected $error_str = null; 00049 00062 public function __construct($host, $port = 8728, $persist = false, 00063 $timeout = null, $key = '', $context = null 00064 ) { 00065 $flags = STREAM_CLIENT_CONNECT; 00066 if ($persist) { 00067 $flags |= STREAM_CLIENT_PERSISTENT; 00068 } 00069 00070 $timeout 00071 = null == $timeout ? ini_get('default_socket_timeout') : $timeout; 00072 00073 $key = rawurlencode($key); 00074 00075 if (null === $context) { 00076 $context = stream_context_get_default(); 00077 } elseif ( 00078 (!is_resource($context)) 00079 || ('stream-context' !== get_resource_type($context)) 00080 ) { 00081 throw $this->createException('Invalid context supplied.', 6); 00082 } 00083 00084 try { 00085 parent::__construct( 00086 @stream_socket_client( 00087 "tcp://{$host}:{$port}/{$key}", $this->error_no, 00088 $this->error_str, $timeout, $flags, $context 00089 ) 00090 ); 00091 } catch (\Exception $e) { 00092 throw $this->createException('Failed to initialize socket.', 7); 00093 } 00094 } 00095 00101 public function isDataAwaiting() 00102 { 00103 if (parent::isDataAwaiting()) { 00104 $meta = stream_get_meta_data($this->stream); 00105 return!$meta['timed_out'] && !$meta['eof']; 00106 } 00107 return false; 00108 } 00109 00120 protected function createException($message, $code = 0) 00121 { 00122 return new SocketException( 00123 $message, $code, null, $this->error_no, $this->error_str 00124 ); 00125 } 00126 00127 }