xref: /php-uv/php_uv.h (revision 5f5281da)
1 #ifndef PHP_UV_H
2 
3 #define PHP_UV_H
4 
5 #define PHP_UV_EXTNAME "uv"
6 #define PHP_UV_VERSION "0.3.0"
7 
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif
11 
12 #ifdef PHP_WIN32
13 #include <Winsock2.h>
14 #include <Mswsock.h>
15 #include <psapi.h>
16 #include <Iphlpapi.h>
17 #endif
18 
19 #ifndef PHP_UV_DTRACE
20 #define PHP_UV_DTRACE 0
21 #endif
22 
23 #if PHP_UV_DTRACE >= 1
24 #include <dtrace.h>
25 #include <sys/sdt.h>
26 #include "phpuv_dtrace.h"
27 #define PHP_UV_PROBE(PROBE) PHPUV_TRACE_##PROBE();
28 #else
29 #define PHP_UV_PROBE(PROBE)
30 #endif
31 
32 #include "php.h"
33 #include "uv.h"
34 
35 #include "php_network.h"
36 #include "php_streams.h"
37 
38 #if defined(HAVE_SOCKETS) && !defined(COMPILE_DL_SOCKETS)
39 #include "ext/sockets/php_sockets.h"
40 #elif !defined(PHP_WIN32)
41 typedef struct {
42 	int bsd_socket;
43 	int type;
44 	int error;
45 	int blocking;
46 	zval zstream;
47 	zend_object std;
48 } php_socket;
49 #endif
50 
51 #include <Zend/zend.h>
52 #include <Zend/zend_compile.h>
53 #include <Zend/zend_exceptions.h>
54 #include <Zend/zend_extensions.h>
55 #include <Zend/zend_globals.h>
56 #include <Zend/zend_hash.h>
57 #include <Zend/zend_interfaces.h>
58 #include <Zend/zend_list.h>
59 #include <Zend/zend_object_handlers.h>
60 #include <Zend/zend_variables.h>
61 #include <Zend/zend_vm.h>
62 
63 #if PHP_VERSION_ID >= 80000
64 #define TSRMLS_C
65 #define TSRMLS_CC
66 #define TSRMLS_D
67 #define TSRMLS_DC
68 #define TSRMLS_FETCH_FROM_CTX(ctx)
69 #define TSRMLS_SET_CTX(ctx)
70 #endif
71 
72 /* Define the entry point symbol
73  * Zend will use when loading this module
74  */
75 extern zend_module_entry uv_module_entry;
76 #define phpext_uv_ptr &uv_module_entry
77 
78 extern zend_class_entry *uv_class_entry;
79 
80 enum php_uv_lock_type {
81 	IS_UV_RWLOCK = 1,
82 	IS_UV_RWLOCK_RD = 2,
83 	IS_UV_RWLOCK_WR = 3,
84 	IS_UV_MUTEX = 4,
85 	IS_UV_SEMAPHORE = 5,
86 };
87 
88 enum php_uv_resource_type{
89 	IS_UV_TCP      = 0,
90 	IS_UV_UDP      = 1,
91 	IS_UV_PIPE     = 2,
92 	IS_UV_IDLE     = 3,
93 	IS_UV_TIMER    = 4,
94 	IS_UV_ASYNC    = 5,
95 	IS_UV_LOOP     = 6,
96 	IS_UV_HANDLE   = 7,
97 	IS_UV_STREAM   = 8,
98 	IS_UV_ADDRINFO = 9,
99 	IS_UV_PROCESS  = 10,
100 	IS_UV_PREPARE  = 11,
101 	IS_UV_CHECK    = 12,
102 	IS_UV_WORK     = 13,
103 	IS_UV_FS       = 14,
104 	IS_UV_FS_EVENT = 15,
105 	IS_UV_TTY      = 16,
106 	IS_UV_FS_POLL  = 17,
107 	IS_UV_POLL     = 18,
108 	IS_UV_SIGNAL   = 19,
109 	IS_UV_MAX      = 20
110 };
111 
112 enum php_uv_callback_type{
113 	PHP_UV_LISTEN_CB       = 0,
114 	PHP_UV_READ_CB         = 1,
115 	PHP_UV_READ2_CB        = 2,
116 	PHP_UV_WRITE_CB        = 3,
117 	PHP_UV_SHUTDOWN_CB     = 4,
118 	PHP_UV_CLOSE_CB        = 5,
119 	PHP_UV_TIMER_CB        = 6,
120 	PHP_UV_IDLE_CB         = 7,
121 	PHP_UV_CONNECT_CB      = 8,
122 	PHP_UV_GETADDR_CB      = 9,
123 	PHP_UV_RECV_CB         = 10,
124 	PHP_UV_SEND_CB         = 11,
125 	PHP_UV_PIPE_CONNECT_CB = 12,
126 	PHP_UV_PROC_CLOSE_CB   = 13,
127 	PHP_UV_PREPARE_CB      = 14,
128 	PHP_UV_CHECK_CB        = 15,
129 	PHP_UV_ASYNC_CB        = 16,
130 	PHP_UV_WORK_CB         = 17,
131 	PHP_UV_AFTER_WORK_CB   = 18,
132 	PHP_UV_FS_CB           = 19,
133 	PHP_UV_FS_EVENT_CB     = 20,
134 	PHP_UV_FS_POLL_CB      = 21,
135 	PHP_UV_POLL_CB         = 22,
136 	PHP_UV_SIGNAL_CB       = 23,
137 	PHP_UV_CB_MAX          = 24
138 };
139 
140 typedef struct {
141     zend_fcall_info fci;
142     zend_fcall_info_cache fcc;
143 } php_uv_cb_t;
144 
145 typedef struct {
146 	zend_object std;
147 
148 #if defined(ZTS) && PHP_VERSION_ID < 80000
149 	void ***thread_ctx;
150 #endif
151 	int type;
152 	uv_os_sock_t sock;
153 	union {
154 		uv_tcp_t tcp;
155 		uv_udp_t udp;
156 		uv_pipe_t pipe;
157 		uv_idle_t idle;
158 		uv_timer_t timer;
159 		uv_async_t async;
160 		uv_loop_t loop;
161 		uv_handle_t handle;
162 		uv_req_t req;
163 		uv_stream_t stream;
164 		uv_shutdown_t shutdown;
165 		uv_udp_send_t udp_send;
166 		uv_connect_t connect;
167 		uv_getaddrinfo_t addrinfo;
168 		uv_prepare_t prepare;
169 		uv_check_t check;
170 		uv_process_t process;
171 		uv_work_t work;
172 		uv_fs_t fs;
173 		uv_fs_event_t fs_event;
174 		uv_tty_t tty;
175 		uv_fs_poll_t fs_poll;
176 		uv_poll_t poll;
177 		uv_signal_t signal;
178 	} uv;
179 	char *buffer;
180 	php_uv_cb_t *callback[PHP_UV_CB_MAX];
181 	zval gc_data[PHP_UV_CB_MAX * 2];
182 	zval fs_fd;
183 	zval fs_fd_alt;
184 } php_uv_t;
185 
186 typedef struct {
187 	zend_object std;
188 
189 	union {
190 		struct sockaddr_in ipv4;
191 		struct sockaddr_in6 ipv6;
192 	} addr;
193 } php_uv_sockaddr_t;
194 
195 typedef struct {
196 	zend_object std;
197 
198 	int locked;
199 	enum php_uv_lock_type type;
200 	union {
201 		uv_rwlock_t rwlock;
202 		uv_mutex_t mutex;
203 		uv_sem_t semaphore;
204 	} lock;
205 } php_uv_lock_t;
206 
207 typedef struct {
208 	zend_object std;
209 
210 	int fd;
211 	zval stream;
212 	int flags;
213 } php_uv_stdio_t;
214 
215 typedef struct {
216 	zend_object std;
217 
218 	uv_loop_t loop;
219 
220 	size_t gc_buffer_size;
221 	zval *gc_buffer;
222 } php_uv_loop_t;
223 
224 /* File/directory stat mode constants*/
225 #ifdef PHP_WIN32
226 #define S_IFDIR _S_IFDIR
227 #define S_IFREG _S_IFREG
228 #else
229 #ifndef S_IFDIR
230 #define S_IFDIR 0040000
231 #endif
232 #ifndef S_IFREG
233 #define S_IFREG 0100000
234 #endif
235 #endif
236 
237 ZEND_BEGIN_MODULE_GLOBALS(uv)
238 	php_uv_loop_t *default_loop;
239 ZEND_END_MODULE_GLOBALS(uv)
240 
241 #ifdef ZTS
242 #ifdef COMPILE_DL_UV
243 ZEND_TSRMLS_CACHE_EXTERN()
244 #endif
245 
246 #define UV_G(v) TSRMG(uv_globals_id, zend_uv_globals *, v)
247 #else
248 #define UV_G(v) (uv_globals.v)
249 #endif
250 
251 #endif /* PHP_UV_H */
252