@@ -190,14 +190,11 @@ private function receive($len = null)
190190 * @return resource
191191 * @throws \Exception Exception raised if connection fails.
192192 */
193- private function getStream ($ address , $ timeout = null )
193+ private function getStream ($ address , $ timeout )
194194 {
195- if (is_null ($ timeout )) {
196- $ timeout = intval (ini_get ('default_socket_timeout ' ));
197- }
198195 $ errno = null ;
199196 $ errstr = null ;
200-
197+
201198 $ fp = stream_socket_client ($ address , $ errno , $ errstr , $ timeout , STREAM_CLIENT_CONNECT );
202199 $ timeout = number_format ($ timeout , 3 );
203200 $ seconds = floor ($ timeout );
@@ -231,9 +228,13 @@ public function isConnected()
231228 */
232229 public function connect ($ timeout = null )
233230 {
231+ if ($ timeout === null ) {
232+ $ timeout = intval (ini_get ('default_socket_timeout ' ));
233+ }
234234
235235 $ this ->timeout = $ timeout ;
236236 $ this ->streamSocket = $ this ->getStream ($ this ->options ->getAddress (), $ timeout );
237+ $ this ->setStreamTimeout ($ timeout );
237238
238239 $ msg = 'CONNECT ' .$ this ->options ;
239240 $ this ->send ($ msg );
@@ -413,7 +414,8 @@ private function handleMSG($line)
413414 public function wait ($ quantity = 0 )
414415 {
415416 $ count = 0 ;
416- while (!feof ($ this ->streamSocket )) {
417+ $ info = stream_get_meta_data ($ this ->streamSocket );
418+ while (is_resource ($ this ->streamSocket ) && !feof ($ this ->streamSocket ) && !$ info ['timed_out ' ]) {
417419 $ line = $ this ->receive ();
418420
419421 if ($ line === false ) {
@@ -431,6 +433,7 @@ public function wait($quantity = 0)
431433 return $ this ;
432434 }
433435 }
436+ $ info = stream_get_meta_data ($ this ->streamSocket );
434437 }
435438 $ this ->close ();
436439
@@ -447,9 +450,12 @@ public function wait($quantity = 0)
447450 public function setStreamTimeout ($ seconds )
448451 {
449452 if ($ this ->isConnected ()) {
450- if (is_int ($ seconds )) {
453+ if (is_numeric ($ seconds )) {
451454 try {
452- return $ this ->streamWrapper ->setStreamTimeout ($ this ->streamSocket , $ seconds );
455+ $ timeout = number_format ($ seconds , 3 );
456+ $ seconds = floor ($ timeout );
457+ $ microseconds = ($ timeout - $ seconds ) * 1000 ;
458+ return stream_set_timeout ($ this ->streamSocket , $ seconds , $ microseconds );
453459 } catch (\Exception $ e ) {
454460 return false ;
455461 }
@@ -489,4 +495,13 @@ public function close()
489495 fclose ($ this ->streamSocket );
490496 $ this ->streamSocket = null ;
491497 }
498+
499+
500+ /**
501+ * @return resource
502+ */
503+ public function streamSocket ()
504+ {
505+ return $ this ->streamSocket ;
506+ }
492507}
0 commit comments