@@ -549,32 +549,7 @@ static void php_mrloop_add_read_stream(INTERNAL_FUNCTION_PARAMETERS)
549
549
this = PHP_MRLOOP_OBJ (obj );
550
550
551
551
// convert resource to PHP stream
552
- if ((stream = (php_stream * )zend_fetch_resource_ex (res , NULL , php_file_le_stream ())))
553
- {
554
- // extract file descriptor from resource stream
555
- if (php_stream_cast (stream , PHP_STREAM_AS_FD | PHP_STREAM_CAST_INTERNAL , (void * )& fd , 1 ) == FAILURE || fd < 0 )
556
- {
557
- PHP_MRLOOP_THROW ("Passed resource without file descriptor" );
558
- RETURN_NULL ();
559
-
560
- close (fd );
561
-
562
- return ;
563
- }
564
- }
565
-
566
- // set non-blocking mode
567
- if (fcntl (fd , F_SETFL , fcntl (fd , F_GETFL , 0 ) | O_NONBLOCK ) < 0 )
568
- {
569
- close (fd );
570
-
571
- char * error = strerror (errno );
572
- PHP_MRLOOP_THROW (error );
573
-
574
- mr_stop (this -> loop );
575
-
576
- return ;
577
- }
552
+ PHP_STREAM_TO_FD (stream , res , fd );
578
553
579
554
fnbytes = (size_t )(nbytes_null == true ? DEFAULT_STREAM_BUFF_LEN : nbytes );
580
555
@@ -618,30 +593,7 @@ static void php_mrloop_add_write_stream(INTERNAL_FUNCTION_PARAMETERS)
618
593
619
594
this = PHP_MRLOOP_OBJ (obj );
620
595
621
- if ((stream = (php_stream * )zend_fetch_resource_ex (res , NULL , php_file_le_stream ())))
622
- {
623
- if (php_stream_cast (stream , PHP_STREAM_AS_FD | PHP_STREAM_CAST_INTERNAL , (void * )& fd , 1 ) == FAILURE || fd < 0 )
624
- {
625
- PHP_MRLOOP_THROW ("Passed resource without file descriptor" );
626
- RETURN_NULL ();
627
-
628
- close (fd );
629
-
630
- return ;
631
- }
632
- }
633
-
634
- if (fcntl (fd , F_SETFL , fcntl (fd , F_GETFL , 0 ) | O_NONBLOCK ) < 0 )
635
- {
636
- close (fd );
637
-
638
- char * error = strerror (errno );
639
- PHP_MRLOOP_THROW (error );
640
-
641
- mr_stop (this -> loop );
642
-
643
- return ;
644
- }
596
+ PHP_STREAM_TO_FD (stream , res , fd );
645
597
646
598
nbytes = ZSTR_LEN (contents );
647
599
iov = emalloc (sizeof (php_iovec_t ));
@@ -662,28 +614,44 @@ static void php_mrloop_add_write_stream(INTERNAL_FUNCTION_PARAMETERS)
662
614
}
663
615
static void php_mrloop_writev (INTERNAL_FUNCTION_PARAMETERS )
664
616
{
665
- zend_long fd ;
666
617
zend_string * contents ;
667
618
php_iovec_t iov ;
668
619
php_mrloop_t * this ;
669
- zval * obj ;
620
+ zval * obj , * res ;
670
621
size_t nbytes ;
622
+ php_stream * stream ;
623
+ int fd ;
671
624
672
625
obj = getThis ();
626
+ fd = -1 ;
673
627
674
628
ZEND_PARSE_PARAMETERS_START (2 , 2 )
675
- Z_PARAM_LONG ( fd )
629
+ Z_PARAM_ZVAL ( res )
676
630
Z_PARAM_STR (contents )
677
631
ZEND_PARSE_PARAMETERS_END ();
678
632
679
633
this = PHP_MRLOOP_OBJ (obj );
680
634
681
- if (fcntl (( int ) fd , F_GETFD ) < 0 )
635
+ if (Z_TYPE_P ( res ) == IS_RESOURCE )
682
636
{
683
- PHP_MRLOOP_THROW ("Detected invalid file descriptor" );
684
- mr_stop (this -> loop );
637
+ PHP_STREAM_TO_FD (stream , res , fd );
638
+ }
639
+ else if (Z_TYPE_P (res ) == IS_LONG )
640
+ {
641
+ fd = Z_LVAL_P (res );
685
642
686
- return ;
643
+ if (fcntl (fd , F_GETFD ) < 0 )
644
+ {
645
+ PHP_MRLOOP_THROW ("Detected invalid file descriptor" );
646
+ mr_stop (this -> loop );
647
+
648
+ return ;
649
+ }
650
+ }
651
+ else
652
+ {
653
+ PHP_MRLOOP_THROW ("Detected invalid file descriptor" );
654
+ RETURN_NULL ();
687
655
}
688
656
689
657
nbytes = ZSTR_LEN (contents );
0 commit comments