@@ -146,15 +146,19 @@ private function receive($len = null)
146146 /**
147147 * Returns an stream socket to the desired server.
148148 *
149- * @param string $address Server url string.
150- *
149+ * @param string $address Server url string.
150+ * @param integer $timeout Number of seconds until the connect() system call should timeout.
151151 * @return resource
152+ * @throws \Exception Exception raised if connection fails.
152153 */
153- private function getStream ($ address )
154+ private function getStream ($ address, $ timeout = null )
154155 {
155- $ fp = stream_socket_client ($ address , $ errno , $ errstr , STREAM_CLIENT_CONNECT );
156+ if (is_null ($ timeout )) {
157+ $ timeout = intval (ini_get ('default_socket_timeout ' ));
158+ }
159+ $ fp = stream_socket_client ($ address , $ errno , $ errstr , $ timeout , STREAM_CLIENT_CONNECT );
156160 if (!$ fp ) {
157- echo ' !!!!!!! ' . $ errstr. ' - ' . $ errno ;
161+ throw new \ Exception ( $ errstr, $ errno) ;
158162 }
159163 //stream_set_blocking($fp, 0);
160164 return $ fp ;
@@ -173,11 +177,13 @@ public function isConnected()
173177 /**
174178 * Connect to server.
175179 *
180+ * @param integer $timeout Number of seconds until the connect() system call should timeout.
181+ * @throws \Exception Exception raised if connection fails.
176182 * @return void
177183 */
178- public function connect ()
184+ public function connect ($ timeout = null )
179185 {
180- $ this ->streamSocket = $ this ->getStream ($ this ->options ->getAddress ());
186+ $ this ->streamSocket = $ this ->getStream ($ this ->options ->getAddress (), $ timeout );
181187 $ msg = 'CONNECT ' .$ this ->options ;
182188 $ this ->send ($ msg );
183189 }
@@ -268,7 +274,7 @@ private function handleMSG($line)
268274 } else {
269275 return new \Exception ('not callable ' );
270276 }
271-
277+
272278 return ;
273279 }
274280
0 commit comments