Skip to content

Commit c2928e3

Browse files
author
ace411
committed
feat: create PHP_STREAM_TO_FD macro
1 parent 6d683da commit c2928e3

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/loop.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,4 +176,23 @@ zend_class_entry *php_mrloop_ce, *php_mrloop_exception_ce;
176176
GC_ADDREF(mrloop_cb->fci.object); \
177177
}
178178

179+
/* extract file descriptor from PHP stream */
180+
#define PHP_STREAM_TO_FD(fd_stream, fd_resource, fd) \
181+
if ((fd_stream = (php_stream *)zend_fetch_resource_ex( \
182+
fd_resource, NULL, php_file_le_stream()))) { \
183+
if (php_stream_cast(fd_stream, \
184+
PHP_STREAM_AS_FD | PHP_STREAM_CAST_INTERNAL, \
185+
(void *)&fd, 1) == FAILURE || \
186+
fd < 0) { \
187+
PHP_MRLOOP_THROW("Passed resource without file descriptor"); \
188+
RETURN_NULL(); \
189+
} \
190+
} \
191+
if (fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK) < 0) { \
192+
close(fd); \
193+
char *error = strerror(errno); \
194+
PHP_MRLOOP_THROW(error); \
195+
mr_stop(this->loop); \
196+
}
197+
179198
#endif

0 commit comments

Comments
 (0)