@@ -47,10 +47,6 @@ static const zend_function_entry swoole_coroutine_system_methods[] =
47
47
PHP_ME (swoole_coroutine_system, waitPid, arginfo_class_Swoole_Coroutine_System_waitPid, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
48
48
PHP_ME (swoole_coroutine_system, waitSignal, arginfo_class_Swoole_Coroutine_System_waitSignal, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
49
49
PHP_ME (swoole_coroutine_system, waitEvent, arginfo_class_Swoole_Coroutine_System_waitEvent, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
50
- /* Deprecated file methods */
51
- PHP_ME (swoole_coroutine_system, fread , arginfo_class_Swoole_Coroutine_System_fread, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC | ZEND_ACC_DEPRECATED)
52
- PHP_ME (swoole_coroutine_system, fwrite , arginfo_class_Swoole_Coroutine_System_fwrite, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC | ZEND_ACC_DEPRECATED)
53
- PHP_ME (swoole_coroutine_system, fgets , arginfo_class_Swoole_Coroutine_System_fgets, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC | ZEND_ACC_DEPRECATED)
54
50
PHP_FE_END
55
51
};
56
52
@@ -76,214 +72,6 @@ PHP_METHOD(swoole_coroutine_system, sleep) {
76
72
RETURN_BOOL (System::sleep (seconds) == 0 );
77
73
}
78
74
79
- static void co_socket_read (int fd, zend_long length, INTERNAL_FUNCTION_PARAMETERS) {
80
- php_swoole_check_reactor ();
81
- Socket _socket (fd, SW_SOCK_RAW);
82
-
83
- zend_string *buf = zend_string_alloc (length + 1 , 0 );
84
- size_t nbytes = length <= 0 ? SW_BUFFER_SIZE_STD : length;
85
- ssize_t n = _socket.read (ZSTR_VAL (buf), nbytes);
86
- if (n < 0 ) {
87
- ZVAL_FALSE (return_value);
88
- zend_string_free (buf);
89
- } else if (n == 0 ) {
90
- ZVAL_EMPTY_STRING (return_value);
91
- zend_string_free (buf);
92
- } else {
93
- ZSTR_VAL (buf)[n] = 0 ;
94
- ZSTR_LEN (buf) = n;
95
- ZVAL_STR (return_value, buf);
96
- }
97
- _socket.move_fd ();
98
- }
99
-
100
- static void co_socket_write (int fd, char *str, size_t l_str, INTERNAL_FUNCTION_PARAMETERS) {
101
- php_swoole_check_reactor ();
102
- Socket _socket (fd, SW_SOCK_RAW);
103
-
104
- ssize_t n = _socket.write (str, l_str);
105
- if (n < 0 ) {
106
- swoole_set_last_error (errno);
107
- ZVAL_FALSE (return_value);
108
- } else {
109
- ZVAL_LONG (return_value, n);
110
- }
111
- _socket.move_fd ();
112
- }
113
-
114
- PHP_METHOD (swoole_coroutine_system, fread) {
115
- Coroutine::get_current_safe ();
116
-
117
- zval *handle;
118
- zend_long length = 0 ;
119
-
120
- ZEND_PARSE_PARAMETERS_START (1 , 2 )
121
- Z_PARAM_RESOURCE (handle)
122
- Z_PARAM_OPTIONAL
123
- Z_PARAM_LONG (length)
124
- ZEND_PARSE_PARAMETERS_END_EX (RETURN_FALSE);
125
-
126
- int async;
127
- int fd = php_swoole_convert_to_fd_ex (handle, &async);
128
- if (fd < 0 ) {
129
- RETURN_FALSE;
130
- }
131
-
132
- if (async) {
133
- co_socket_read (fd, length, INTERNAL_FUNCTION_PARAM_PASSTHRU);
134
- return ;
135
- }
136
-
137
- if (length <= 0 ) {
138
- struct stat file_stat;
139
- if (swoole_coroutine_fstat (fd, &file_stat) < 0 ) {
140
- swoole_set_last_error (errno);
141
- RETURN_FALSE;
142
- }
143
- off_t _seek = swoole_coroutine_lseek (fd, 0 , SEEK_CUR);
144
- if (_seek < 0 ) {
145
- swoole_set_last_error (errno);
146
- RETURN_FALSE;
147
- }
148
- if (_seek >= file_stat.st_size ) {
149
- length = SW_BUFFER_SIZE_STD;
150
- } else {
151
- length = file_stat.st_size - _seek;
152
- }
153
- }
154
-
155
- zend_string *buf = zend_string_alloc (length, 0 );
156
- ssize_t ret = -1 ;
157
- swoole_trace (" fd=%d, length=" ZEND_LONG_FMT, fd, length);
158
- php_swoole_check_reactor ();
159
- bool async_success = swoole::coroutine::async ([&]() {
160
- while (1 ) {
161
- ret = read (fd, buf->val , length);
162
- if (ret < 0 && errno == EINTR) {
163
- continue ;
164
- }
165
- break ;
166
- }
167
- });
168
-
169
- if (async_success && ret >= 0 ) {
170
- buf->len = ret;
171
- buf->val [buf->len ] = 0 ;
172
- RETURN_STR (buf);
173
- } else {
174
- zend_string_release (buf);
175
- RETURN_FALSE;
176
- }
177
- }
178
-
179
- PHP_METHOD (swoole_coroutine_system, fgets) {
180
- Coroutine::get_current_safe ();
181
-
182
- zval *handle;
183
- php_stream *stream;
184
-
185
- ZEND_PARSE_PARAMETERS_START (1 , 1 )
186
- Z_PARAM_RESOURCE (handle)
187
- ZEND_PARSE_PARAMETERS_END_EX (RETURN_FALSE);
188
-
189
- int async;
190
- int fd = php_swoole_convert_to_fd_ex (handle, &async);
191
- if (fd < 0 ) {
192
- RETURN_FALSE;
193
- }
194
-
195
- if (async == 1 ) {
196
- php_swoole_fatal_error (E_WARNING, " only support file resources" );
197
- RETURN_FALSE;
198
- }
199
-
200
- php_stream_from_res (stream, Z_RES_P (handle));
201
-
202
- FILE *file;
203
- if (stream->stdiocast ) {
204
- file = stream->stdiocast ;
205
- } else {
206
- if (php_stream_cast (stream, PHP_STREAM_AS_STDIO, (void **) &file, 1 ) != SUCCESS || file == nullptr ) {
207
- RETURN_FALSE;
208
- }
209
- }
210
-
211
- if (stream->readbuf == nullptr ) {
212
- stream->readbuflen = stream->chunk_size ;
213
- stream->readbuf = (uchar *) emalloc (stream->chunk_size );
214
- }
215
-
216
- if (!stream->readbuf ) {
217
- RETURN_FALSE;
218
- }
219
-
220
- int ret = 0 ;
221
- swoole_trace (" fd=%d, length=%ld" , fd, stream->readbuflen );
222
- php_swoole_check_reactor ();
223
- bool async_success = swoole::coroutine::async ([&]() {
224
- char *data = fgets ((char *) stream->readbuf , stream->readbuflen , file);
225
- if (data == nullptr ) {
226
- ret = -1 ;
227
- stream->eof = 1 ;
228
- }
229
- });
230
-
231
- if (async_success && ret != -1 ) {
232
- ZVAL_STRING (return_value, (char *) stream->readbuf );
233
- } else {
234
- ZVAL_FALSE (return_value);
235
- }
236
- }
237
-
238
- PHP_METHOD (swoole_coroutine_system, fwrite) {
239
- Coroutine::get_current_safe ();
240
-
241
- zval *handle;
242
- char *str;
243
- size_t l_str;
244
- zend_long length = 0 ;
245
-
246
- ZEND_PARSE_PARAMETERS_START (2 , 3 )
247
- Z_PARAM_RESOURCE (handle)
248
- Z_PARAM_STRING (str, l_str)
249
- Z_PARAM_OPTIONAL
250
- Z_PARAM_LONG (length)
251
- ZEND_PARSE_PARAMETERS_END_EX (RETURN_FALSE);
252
-
253
- int async;
254
- int fd = php_swoole_convert_to_fd_ex (handle, &async);
255
- if (fd < 0 ) {
256
- RETURN_FALSE;
257
- }
258
- if (length <= 0 || (size_t ) length > l_str) {
259
- length = l_str;
260
- }
261
- if (async) {
262
- co_socket_write (fd, str, length, INTERNAL_FUNCTION_PARAM_PASSTHRU);
263
- return ;
264
- }
265
-
266
- zend::CharPtr buf (str, length);
267
- ssize_t ret = -1 ;
268
- swoole_trace (" fd=%d, length=" ZEND_LONG_FMT, fd, length);
269
- php_swoole_check_reactor ();
270
- bool async_success = swoole::coroutine::async ([&]() {
271
- while (1 ) {
272
- ret = write (fd, buf.get (), length);
273
- if (ret < 0 && errno == EINTR) {
274
- continue ;
275
- }
276
- break ;
277
- }
278
- });
279
-
280
- if (async_success && ret >= 0 ) {
281
- ZVAL_LONG (return_value, ret);
282
- } else {
283
- ZVAL_FALSE (return_value);
284
- }
285
- }
286
-
287
75
PHP_METHOD (swoole_coroutine_system, readFile) {
288
76
char *filename;
289
77
size_t l_filename;
0 commit comments