加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
swoole_async.c 33.87 KB
一键复制 编辑 原始数据 按行查看 历史
韩天峰 提交于 2018-02-05 16:08 . added stack_size option for coroutine.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337
/*
+----------------------------------------------------------------------+
| Swoole |
+----------------------------------------------------------------------+
| This source file is subject to version 2.0 of the Apache license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.apache.org/licenses/LICENSE-2.0.html |
| If you did not receive a copy of the Apache2.0 license and are unable|
| to obtain it through the world-wide-web, please send a note to |
| license@swoole.com so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Tianfeng Han <mikan.tenny@gmail.com> |
+----------------------------------------------------------------------+
*/
#include "php_swoole.h"
#include "php_streams.h"
#include "php_network.h"
#include "ext/standard/file.h"
#ifdef SW_COROUTINE
#include "swoole_coroutine.h"
#include "ext/standard/basic_functions.h"
#include <setjmp.h>
#endif
typedef struct
{
#if PHP_MAJOR_VERSION >= 7
zval _callback;
zval _filename;
#endif
zval *callback;
zval *filename;
int fd;
off_t offset;
uint16_t type;
uint8_t once;
char *content;
uint32_t length;
} file_request;
typedef struct
{
int fd;
off_t offset;
} file_handle;
typedef struct
{
#if PHP_MAJOR_VERSION >= 7
zval _callback;
zval _domain;
#endif
zval *callback;
zval *domain;
#ifdef SW_COROUTINE
php_context *context; //add for coro
uint8_t useless; //1 代表没有用
swTimer_node *timer;
#endif
} dns_request;
typedef struct
{
swString *zaddress;
int64_t update_time;
} dns_cache;
typedef struct
{
zval *callback;
#if PHP_MAJOR_VERSION >= 7
zval _callback;
#endif
pid_t pid;
int fd;
swString *buffer;
} process_stream;
static void php_swoole_aio_onComplete(swAio_event *event);
static void php_swoole_dns_callback(char *domain, swDNSResolver_result *result, void *data);
#ifdef SW_COROUTINE
static void php_swoole_dns_callback_coro(char *domain, swDNSResolver_result *result, void *data);
static void php_swoole_dns_timeout_coro(swTimer *timer, swTimer_node *tnode);
#endif
static void php_swoole_file_request_free(void *data);
static swHashMap *php_swoole_open_files;
static swHashMap *php_swoole_aio_request;
#ifdef SW_COROUTINE
static swHashMap *request_cache_map = NULL; //以domin为区分
#endif
static sw_inline int64_t swTimer_get_now_msec()
{
struct timeval now;
if (swTimer_now(&now) < 0)
{
return SW_ERR;
}
int64_t msec1 = (now.tv_sec) * 1000;
int64_t msec2 = (now.tv_usec) / 1000;
return msec1 + msec2;
}
static sw_inline void swoole_aio_free(void *ptr)
{
if (SwooleAIO.mode == SW_AIO_LINUX)
{
free(ptr);
}
else
{
efree(ptr);
}
}
static sw_inline void* swoole_aio_malloc(size_t __size)
{
#ifdef HAVE_LINUX_AIO
void *memory;
if (SwooleAIO.mode == SW_AIO_LINUX)
{
size_t buf_len = __size + (sysconf(_SC_PAGESIZE) - (__size % sysconf(_SC_PAGESIZE)));
if (posix_memalign((void **) &memory, sysconf(_SC_PAGESIZE), buf_len) != 0)
{
return NULL;
}
else
{
return memory;
}
}
else
#endif
{
return emalloc(__size);
}
}
static void php_swoole_file_request_free(void *data)
{
file_request *file_req = data;
if (file_req->callback)
{
sw_zval_ptr_dtor(&file_req->callback);
}
swoole_aio_free(file_req->content);
sw_zval_ptr_dtor(&file_req->filename);
efree(file_req);
}
void swoole_async_init(int module_number TSRMLS_DC)
{
bzero(&SwooleAIO, sizeof(SwooleAIO));
REGISTER_LONG_CONSTANT("SWOOLE_AIO_BASE", SW_AIO_BASE, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SWOOLE_AIO_LINUX", SW_AIO_LINUX, CONST_CS | CONST_PERSISTENT);
php_swoole_open_files = swHashMap_new(SW_HASHMAP_INIT_BUCKET_N, NULL);
if (php_swoole_open_files == NULL)
{
swoole_php_fatal_error(E_ERROR, "create hashmap[1] failed.");
}
php_swoole_aio_request = swHashMap_new(SW_HASHMAP_INIT_BUCKET_N, php_swoole_file_request_free);
if (php_swoole_aio_request == NULL)
{
swoole_php_fatal_error(E_ERROR, "create hashmap[2] failed.");
}
}
void php_swoole_check_aio()
{
if (SwooleAIO.init == 0)
{
php_swoole_check_reactor();
swAio_init();
}
if (!SwooleAIO.callback)
{
SwooleAIO.callback = php_swoole_aio_onComplete;
}
}
static void php_swoole_dns_callback(char *domain, swDNSResolver_result *result, void *data)
{
SWOOLE_GET_TSRMLS;
dns_request *req = data;
zval *retval = NULL;
zval *zaddress;
zval **args[2];
char *address;
SW_MAKE_STD_ZVAL(zaddress);
if (result->num > 0)
{
if (SwooleG.dns_lookup_random)
{
address = result->hosts[rand() % result->num].address;
}
else
{
address = result->hosts[0].address;
}
SW_ZVAL_STRING(zaddress, address, 1);
}
else
{
SW_ZVAL_STRING(zaddress, "", 1);
}
args[0] = &req->domain;
args[1] = &zaddress;
zval *zcallback = req->callback;
if (sw_call_user_function_ex(EG(function_table), NULL, zcallback, &retval, 2, args, 0, NULL TSRMLS_CC) == FAILURE)
{
swoole_php_fatal_error(E_WARNING, "swoole_asyns_dns_lookup handler error.");
return;
}
if (EG(exception))
{
zend_exception_error(EG(exception), E_ERROR TSRMLS_CC);
}
sw_zval_ptr_dtor(&req->callback);
sw_zval_ptr_dtor(&req->domain);
efree(req);
if (retval)
{
sw_zval_ptr_dtor(&retval);
}
sw_zval_ptr_dtor(&zaddress);
}
//用于coro 回调
#ifdef SW_COROUTINE
static void php_swoole_dns_callback_coro(char *domain, swDNSResolver_result *result, void *data)
{
SWOOLE_GET_TSRMLS;
dns_request *req = data;
zval *retval = NULL;
zval *zaddress;
char *address;
SW_MAKE_STD_ZVAL(zaddress);
if (result->num > 0)
{
if (SwooleG.dns_lookup_random)
{
address = result->hosts[rand() % result->num].address;
}
else
{
address = result->hosts[0].address;
}
SW_ZVAL_STRING(zaddress, address, 1);
}
else
{
SW_ZVAL_STRING(zaddress, "", 1);
}
//update cache
dns_cache *cache = swHashMap_find(request_cache_map, Z_STRVAL_P(req->domain), Z_STRLEN_P(req->domain));
if (cache == NULL )
{
cache = emalloc(sizeof(dns_cache));
swHashMap_add(request_cache_map, Z_STRVAL_P(req->domain), Z_STRLEN_P(req->domain), cache);
cache->zaddress = swString_new(20);
}
swString_write_ptr(cache->zaddress, 0, Z_STRVAL_P(zaddress), Z_STRLEN_P(zaddress));
cache->update_time = (int64_t) swTimer_get_now_msec + (int64_t) (SwooleG.dns_cache_refresh_time * 1000);
//timeout
if (req->timer)
{
swTimer_del(&SwooleG.timer, req->timer);
req->timer = NULL;
}
if (req->useless)
{
efree(req);
return;
}
int ret = coro_resume(req->context, zaddress, &retval);
if (ret > 0)
{
goto free_zdata;
}
if (retval != NULL)
{
sw_zval_ptr_dtor(&retval);
}
//说明已经yield走了
free_zdata:
// free 上下文
sw_zval_ptr_dtor(&zaddress);
efree(req->context);
efree(req);
}
//用于timeout
static void php_swoole_dns_timeout_coro(swTimer *timer, swTimer_node *tnode)
{
zval *retval = NULL;
zval *zaddress;
php_context *cxt = (php_context *) tnode->data;
#if PHP_MAJOR_VERSION < 7
dns_request *req =(dns_request *) cxt->coro_params;
#else
dns_request *req = (dns_request *) cxt->coro_params.value.ptr;
#endif
SW_MAKE_STD_ZVAL(zaddress);
dns_cache *cache = swHashMap_find(request_cache_map, Z_STRVAL_P(req->domain), Z_STRLEN_P(req->domain));
if (cache != NULL && cache->update_time > (int64_t) swTimer_get_now_msec)
{
SW_ZVAL_STRINGL(zaddress, (*cache->zaddress).str, (*cache->zaddress).length, 1);
}
else
{
SW_ZVAL_STRING(zaddress, "", 1);
}
int ret = coro_resume(req->context, zaddress, &retval);
if (ret > 0)
{
goto free_zdata;
}
if (retval != NULL)
{
sw_zval_ptr_dtor(&retval);
}
free_zdata:
sw_zval_ptr_dtor(&zaddress);
efree(req->context);
req->useless = 1;
}
#endif
static void php_swoole_aio_onComplete(swAio_event *event)
{
int isEOF = SW_FALSE;
int64_t ret;
zval *retval = NULL, *zcallback = NULL, *zwriten = NULL;
zval *zcontent = NULL;
zval **args[2];
file_request *file_req = NULL;
dns_request *dns_req = NULL;
#if PHP_MAJOR_VERSION < 7
TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL);
#else
zval _zcontent;
zval _zwriten;
bzero(&_zcontent, sizeof(zval));
bzero(&_zwriten, sizeof(zval));
#endif
if (event->type == SW_AIO_DNS_LOOKUP)
{
dns_req = (dns_request *) event->req;
if (dns_req->callback == NULL)
{
swoole_php_error(E_WARNING, "swoole_async: onAsyncComplete callback not found[0]");
return;
}
zcallback = dns_req->callback;
}
else
{
file_req = swHashMap_find_int(php_swoole_aio_request, event->task_id);
if (!file_req)
{
swoole_php_fatal_error(E_WARNING, "swoole_async: onAsyncComplete callback not found[1]");
return;
}
if (file_req->callback == NULL && file_req->type == SW_AIO_READ)
{
swoole_php_fatal_error(E_WARNING, "swoole_async: onAsyncComplete callback not found[2]");
return;
}
zcallback = file_req->callback;
}
ret = event->ret;
if (ret < 0)
{
SwooleG.error = event->error;
swoole_php_error(E_WARNING, "Aio Error: %s[%d]", strerror(event->error), event->error);
}
else if (file_req != NULL)
{
if (ret == 0)
{
bzero(event->buf, event->nbytes);
isEOF = SW_TRUE;
}
else if (file_req->once == 1)
{
if (SwooleAIO.mode != SW_AIO_LINUX && ret < file_req->length)
{
swoole_php_fatal_error(E_WARNING, "swoole_async: ret_length[%d] < req->length[%d].", (int ) ret, file_req->length);
}
}
else if (event->type == SW_AIO_READ)
{
file_req->offset += event->ret;
}
}
if (event->type == SW_AIO_READ)
{
args[0] = &file_req->filename;
args[1] = &zcontent;
#if PHP_MAJOR_VERSION < 7
SW_MAKE_STD_ZVAL(zcontent);
#else
zcontent = &_zcontent;
#endif
if (ret < 0)
{
SW_ZVAL_STRING(zcontent, "", 1);
}
else
{
SW_ZVAL_STRINGL(zcontent, event->buf, ret, 1);
}
}
else if (event->type == SW_AIO_WRITE)
{
#if PHP_MAJOR_VERSION < 7
SW_MAKE_STD_ZVAL(zwriten);
#else
zwriten = &_zwriten;
#endif
args[0] = &file_req->filename;
args[1] = &zwriten;
ZVAL_LONG(zwriten, ret);
}
else if(event->type == SW_AIO_DNS_LOOKUP)
{
args[0] = &dns_req->domain;
#if PHP_MAJOR_VERSION < 7
SW_MAKE_STD_ZVAL(zcontent);
#else
zcontent = &_zcontent;
#endif
if (ret < 0)
{
SW_ZVAL_STRING(zcontent, "", 1);
}
else
{
SW_ZVAL_STRING(zcontent, event->buf, 1);
}
args[1] = &zcontent;
}
else
{
swoole_php_fatal_error(E_WARNING, "swoole_async: onAsyncComplete unknown event type[%d].", event->type);
return;
}
if (zcallback)
{
if (sw_call_user_function_ex(EG(function_table), NULL, zcallback, &retval, 2, args, 0, NULL TSRMLS_CC) == FAILURE)
{
swoole_php_fatal_error(E_WARNING, "swoole_async: onAsyncComplete handler error");
return;
}
if (EG(exception))
{
zend_exception_error(EG(exception), E_ERROR TSRMLS_CC);
}
}
//file io
if (file_req)
{
if (file_req->once == 1)
{
close_file:
close(event->fd);
swHashMap_del_int(php_swoole_aio_request, event->task_id);
}
else if(file_req->type == SW_AIO_WRITE)
{
if (retval != NULL && !ZVAL_IS_NULL(retval) && !Z_BVAL_P(retval))
{
swHashMap_del(php_swoole_open_files, Z_STRVAL_P(file_req->filename), Z_STRLEN_P(file_req->filename));
goto close_file;
}
else
{
swHashMap_del_int(php_swoole_aio_request, event->task_id);
}
}
else
{
if ((retval != NULL && !ZVAL_IS_NULL(retval) && !Z_BVAL_P(retval)) || isEOF)
{
goto close_file;
}
//Less than expected, at the end of the file
else if (event->ret < event->nbytes)
{
event->ret = 0;
php_swoole_aio_onComplete(event);
}
//continue to read
else
{
int ret = SwooleAIO.read(event->fd, event->buf, event->nbytes, file_req->offset);
if (ret < 0)
{
swoole_php_fatal_error(E_WARNING, "swoole_async: continue to read failed. Error: %s[%d]", strerror(event->error), event->error);
goto close_file;
}
else
{
swHashMap_move_int(php_swoole_aio_request, event->task_id, ret);
}
}
}
}
else if (dns_req)
{
sw_zval_ptr_dtor(&dns_req->callback);
sw_zval_ptr_dtor(&dns_req->domain);
efree(dns_req);
efree(event->buf);
}
if (zcontent)
{
sw_zval_ptr_dtor(&zcontent);
}
if (zwriten)
{
sw_zval_ptr_dtor(&zwriten);
}
if (retval)
{
sw_zval_ptr_dtor(&retval);
}
}
PHP_FUNCTION(swoole_async_read)
{
zval *callback;
zval *filename;
long buf_size = SW_AIO_DEFAULT_CHUNK_SIZE;
long offset = 0;
int open_flag = O_RDONLY;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz|ll", &filename, &callback, &buf_size, &offset) == FAILURE)
{
return;
}
if (offset < 0)
{
swoole_php_fatal_error(E_WARNING, "offset must be greater than 0.");
RETURN_FALSE;
}
if (buf_size > SW_AIO_MAX_CHUNK_SIZE)
{
buf_size = SW_AIO_MAX_CHUNK_SIZE;
}
#ifdef HAVE_LINUX_AIO
if (SwooleAIO.mode == SW_AIO_LINUX && (buf_size % SW_AIO_MIN_UNIT_SIZE) != 0)
{
swoole_php_fatal_error(E_WARNING, "the chunk buffer size must be an integer multiple of %d.", SW_AIO_MIN_UNIT_SIZE);
RETURN_FALSE;
}
#endif
convert_to_string(filename);
if (SwooleAIO.mode == SW_AIO_LINUX)
{
open_flag |= O_DIRECT;
}
int fd = open(Z_STRVAL_P(filename), open_flag, 0644);
if (fd < 0)
{
swoole_php_sys_error(E_WARNING, "open(%s, O_RDONLY) failed.", Z_STRVAL_P(filename));
RETURN_FALSE;
}
struct stat file_stat;
if (fstat(fd, &file_stat) < 0)
{
swoole_php_sys_error(E_WARNING, "fstat(%s) failed.", Z_STRVAL_P(filename));
close(fd);
RETURN_FALSE;
}
if (offset >= file_stat.st_size)
{
swoole_php_fatal_error(E_WARNING, "offset must be less than file_size[=%ld].", file_stat.st_size);
close(fd);
RETURN_FALSE;
}
void *fcnt = swoole_aio_malloc(buf_size);
if (fcnt == NULL)
{
swoole_php_sys_error(E_WARNING, "malloc failed.");
close(fd);
RETURN_FALSE;
}
file_request *req = emalloc(sizeof(file_request));
req->fd = fd;
req->filename = filename;
sw_zval_add_ref(&filename);
sw_copy_to_stack(req->filename, req->_filename);
if (callback && !ZVAL_IS_NULL(callback))
{
req->callback = callback;
sw_zval_add_ref(&callback);
sw_copy_to_stack(req->callback, req->_callback);
}
req->content = fcnt;
req->once = 0;
req->type = SW_AIO_READ;
req->length = buf_size;
req->offset = offset;
php_swoole_check_aio();
int ret = SwooleAIO.read(fd, fcnt, buf_size, offset);
if (ret == SW_ERR)
{
RETURN_FALSE;
}
else
{
swHashMap_add_int(php_swoole_aio_request, ret, req);
RETURN_TRUE;
}
}
PHP_FUNCTION(swoole_async_write)
{
zval *callback = NULL;
zval *filename;
char *fcnt;
zend_size_t fcnt_len = 0;
off_t offset = -1;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zs|lz", &filename, &fcnt, &fcnt_len, &offset, &callback) == FAILURE)
{
return;
}
if (fcnt_len <= 0)
{
RETURN_FALSE;
}
#ifdef HAVE_LINUX_AIO
if (SwooleAIO.mode == SW_AIO_LINUX && (fcnt_len % SW_AIO_MIN_UNIT_SIZE) != 0)
{
swoole_php_fatal_error(E_WARNING, "the length must be an integer multiple of %d.", SW_AIO_MIN_UNIT_SIZE);
RETURN_FALSE;
}
#endif
if (callback && !ZVAL_IS_NULL(callback))
{
char *func_name = NULL;
if (!sw_zend_is_callable(callback, 0, &func_name TSRMLS_CC))
{
swoole_php_fatal_error(E_WARNING, "Function '%s' is not callable", func_name);
efree(func_name);
RETURN_FALSE;
}
efree(func_name);
}
convert_to_string(filename);
long fd = (long) swHashMap_find(php_swoole_open_files, Z_STRVAL_P(filename), Z_STRLEN_P(filename));
if (fd == 0)
{
int open_flag = O_WRONLY | O_CREAT;
#ifdef HAVE_LINUX_AIO
if (SwooleAIO.mode == SW_AIO_LINUX)
{
open_flag |= O_DIRECT;
if (offset < 0)
{
swoole_php_fatal_error(E_WARNING, "cannot use FILE_APPEND with linux native aio.");
RETURN_FALSE;
}
}
else
#endif
if (offset < 0)
{
open_flag |= O_APPEND;
}
fd = open(Z_STRVAL_P(filename), open_flag, 0644);
if (fd < 0)
{
swoole_php_fatal_error(E_WARNING, "open(%s, %d) failed. Error: %s[%d]", Z_STRVAL_P(filename), open_flag, strerror(errno), errno);
RETURN_FALSE;
}
swHashMap_add(php_swoole_open_files, Z_STRVAL_P(filename), Z_STRLEN_P(filename), (void*) fd);
}
if (offset < 0)
{
offset = 0;
}
file_request *req = emalloc(sizeof(file_request));
char *wt_cnt = swoole_aio_malloc(fcnt_len);
req->fd = fd;
req->content = wt_cnt;
req->once = 0;
req->type = SW_AIO_WRITE;
req->length = fcnt_len;
req->offset = offset;
req->filename = filename;
sw_zval_add_ref(&filename);
sw_copy_to_stack(req->filename, req->_filename);
if (callback && !ZVAL_IS_NULL(callback))
{
req->callback = callback;
sw_zval_add_ref(&callback);
sw_copy_to_stack(req->callback, req->_callback);
}
else
{
req->callback = NULL;
}
memcpy(wt_cnt, fcnt, fcnt_len);
php_swoole_check_aio();
int ret = SwooleAIO.write(fd, wt_cnt, fcnt_len, offset);
if (ret == SW_ERR)
{
RETURN_FALSE;
}
else
{
swHashMap_add_int(php_swoole_aio_request, ret, req);
RETURN_TRUE;
}
}
PHP_FUNCTION(swoole_async_readfile)
{
zval *callback;
zval *filename;
int open_flag = O_RDONLY;
if (SwooleAIO.mode == SW_AIO_LINUX)
{
open_flag |= O_DIRECT;
}
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz", &filename, &callback) == FAILURE)
{
return;
}
convert_to_string(filename);
int fd = open(Z_STRVAL_P(filename), open_flag, 0644);
if (fd < 0)
{
swoole_php_fatal_error(E_WARNING, "open file[%s] failed. Error: %s[%d]", Z_STRVAL_P(filename), strerror(errno), errno);
RETURN_FALSE;
}
struct stat file_stat;
if (fstat(fd, &file_stat) < 0)
{
swoole_php_fatal_error(E_WARNING, "fstat failed. Error: %s[%d]", strerror(errno), errno);
close(fd);
RETURN_FALSE;
}
if (file_stat.st_size <= 0)
{
swoole_php_fatal_error(E_WARNING, "file is empty.");
close(fd);
RETURN_FALSE;
}
if (file_stat.st_size > SW_AIO_MAX_FILESIZE)
{
swoole_php_fatal_error(E_WARNING, "file_size[size=%ld|max_size=%d] is too big. Please use swoole_async_read.",
(long int) file_stat.st_size, SW_AIO_MAX_FILESIZE);
close(fd);
RETURN_FALSE;
}
size_t length = file_stat.st_size;
#ifdef HAVE_LINUX_AIO
if (SwooleAIO.mode == SW_AIO_LINUX && length % SwooleG.pagesize > 0)
{
length += SwooleG.pagesize - (length % SwooleG.pagesize);
}
#endif
file_request *req = emalloc(sizeof(file_request));
req->fd = fd;
req->filename = filename;
sw_zval_add_ref(&filename);
sw_copy_to_stack(req->filename, req->_filename);
if (callback && !ZVAL_IS_NULL(callback))
{
req->callback = callback;
sw_zval_add_ref(&callback);
sw_copy_to_stack(req->callback, req->_callback);
}
req->content = swoole_aio_malloc(length);
req->once = 1;
req->type = SW_AIO_READ;
req->length = length;
req->offset = 0;
php_swoole_check_aio();
int ret = SwooleAIO.read(fd, req->content, length, 0);
if (ret == SW_ERR)
{
RETURN_FALSE;
}
else
{
swHashMap_add_int(php_swoole_aio_request, ret, req);
RETURN_TRUE;
}
}
PHP_FUNCTION(swoole_async_writefile)
{
zval *callback = NULL;
zval *filename;
char *fcnt;
zend_size_t fcnt_len;
long flags = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS()TSRMLS_CC, "zs|zl", &filename, &fcnt, &fcnt_len, &callback, &flags) == FAILURE)
{
return;
}
int open_flag = O_CREAT | O_WRONLY;
#ifdef HAVE_LINUX_AIO
if (SwooleAIO.mode == SW_AIO_LINUX)
{
open_flag |= O_DIRECT;
if (flags & PHP_FILE_APPEND)
{
swoole_php_fatal_error(E_WARNING, "cannot use FILE_APPEND with linux native aio.");
RETURN_FALSE;
}
}
else
#endif
if (flags & PHP_FILE_APPEND)
{
open_flag |= O_APPEND;
}
if (fcnt_len <= 0)
{
RETURN_FALSE;
}
if (fcnt_len > SW_AIO_MAX_FILESIZE)
{
swoole_php_fatal_error(E_WARNING, "file_size[size=%d|max_size=%d] is too big. Please use swoole_async_write.",
fcnt_len, SW_AIO_MAX_FILESIZE);
RETURN_FALSE;
}
if (callback && !ZVAL_IS_NULL(callback))
{
char *func_name = NULL;
if (!sw_zend_is_callable(callback, 0, &func_name TSRMLS_CC))
{
swoole_php_fatal_error(E_WARNING, "Function '%s' is not callable", func_name);
efree(func_name);
RETURN_FALSE;
}
efree(func_name);
}
convert_to_string(filename);
int fd = open(Z_STRVAL_P(filename), open_flag, 0644);
if (fd < 0)
{
swoole_php_fatal_error(E_WARNING, "open file failed. Error: %s[%d]", strerror(errno), errno);
RETURN_FALSE;
}
size_t memory_size = fcnt_len;
#ifdef HAVE_LINUX_AIO
if (SwooleAIO.mode == SW_AIO_LINUX && memory_size % SwooleG.pagesize > 0)
{
memory_size += SwooleG.pagesize - (memory_size % SwooleG.pagesize);
}
#endif
char *wt_cnt = swoole_aio_malloc(memory_size);
file_request *req = emalloc(sizeof(file_request));
req->filename = filename;
sw_zval_add_ref(&filename);
sw_copy_to_stack(req->filename, req->_filename);
if (callback && !ZVAL_IS_NULL(callback))
{
req->callback = callback;
sw_zval_add_ref(&callback);
sw_copy_to_stack(req->callback, req->_callback);
}
else
{
req->callback = NULL;
}
req->fd = fd;
req->type = SW_AIO_WRITE;
req->content = wt_cnt;
req->once = 1;
req->length = fcnt_len;
req->offset = 0;
memcpy(wt_cnt, fcnt, fcnt_len);
#ifdef HAVE_LINUX_AIO
if (SwooleAIO.mode == SW_AIO_LINUX && memory_size != fcnt_len)
{
memset(wt_cnt + fcnt_len, 0, memory_size - fcnt_len);
}
#endif
php_swoole_check_aio();
int ret = SwooleAIO.write(fd, wt_cnt, memory_size, 0);
if (ret == SW_ERR)
{
RETURN_FALSE;
}
else
{
swHashMap_add_int(php_swoole_aio_request, ret, req);
RETURN_TRUE;
}
}
PHP_FUNCTION(swoole_async_set)
{
if (SwooleG.main_reactor != NULL)
{
swoole_php_fatal_error(E_ERROR, "eventLoop has already been created. unable to create swoole_server.");
RETURN_FALSE;
}
zval *zset = NULL;
HashTable *vht;
zval *v;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zset) == FAILURE)
{
return;
}
php_swoole_array_separate(zset);
vht = Z_ARRVAL_P(zset);
if (php_swoole_array_get_value(vht, "aio_mode", v))
{
convert_to_long(v);
SwooleAIO.mode = (uint8_t) Z_LVAL_P(v);
}
if (php_swoole_array_get_value(vht, "thread_num", v))
{
convert_to_long(v);
SwooleAIO.thread_num = (uint8_t) Z_LVAL_P(v);
}
if (php_swoole_array_get_value(vht, "enable_signalfd", v))
{
convert_to_boolean(v);
SwooleG.enable_signalfd = Z_BVAL_P(v);
}
if (php_swoole_array_get_value(vht, "dns_cache_refresh_time", v))
{
convert_to_double(v);
SwooleG.dns_cache_refresh_time = Z_DVAL_P(v);
}
if (php_swoole_array_get_value(vht, "socket_buffer_size", v))
{
convert_to_long(v);
SwooleG.socket_buffer_size = Z_LVAL_P(v);
if (SwooleG.socket_buffer_size <= 0 || SwooleG.socket_buffer_size > SW_MAX_INT)
{
SwooleG.socket_buffer_size = SW_MAX_INT;
}
}
if (php_swoole_array_get_value(vht, "log_level", v))
{
convert_to_long(v);
SwooleG.log_level = Z_LVAL_P(v);
}
if (php_swoole_array_get_value(vht, "display_errors", v))
{
convert_to_boolean(v);
SWOOLE_G(display_errors) = 0;
}
if (php_swoole_array_get_value(vht, "socket_dontwait", v))
{
convert_to_boolean(v);
SwooleG.socket_dontwait = Z_BVAL_P(v);
}
if (php_swoole_array_get_value(vht, "dns_lookup_random", v))
{
convert_to_boolean(v);
SwooleG.dns_lookup_random = Z_BVAL_P(v);
}
if (php_swoole_array_get_value(vht, "dns_server", v))
{
convert_to_string(v);
SwooleG.dns_server_v4 = sw_strndup(Z_STRVAL_P(v), Z_STRLEN_P(v));
}
if (php_swoole_array_get_value(vht, "use_async_resolver", v))
{
convert_to_boolean(v);
SwooleG.use_async_resolver = Z_BVAL_P(v);
}
#if defined(HAVE_REUSEPORT) && defined(HAVE_EPOLL)
//reuse port
if (php_swoole_array_get_value(vht, "enable_reuse_port", v))
{
convert_to_boolean(v);
if (Z_BVAL_P(v) && swoole_version_compare(SwooleG.uname.release, "3.9.0") >= 0)
{
SwooleG.reuse_port = 1;
}
}
#endif
sw_zval_ptr_dtor(&zset);
}
PHP_FUNCTION(swoole_async_dns_lookup)
{
zval *domain;
zval *cb;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz", &domain, &cb) == FAILURE)
{
return;
}
if (Z_TYPE_P(domain) != IS_STRING)
{
swoole_php_fatal_error(E_WARNING, "invalid domain name.");
RETURN_FALSE;
}
if (Z_STRLEN_P(domain) == 0)
{
swoole_php_fatal_error(E_WARNING, "domain name empty.");
RETURN_FALSE;
}
dns_request *req = emalloc(sizeof(dns_request));
req->callback = cb;
sw_copy_to_stack(req->callback, req->_callback);
sw_zval_add_ref(&req->callback);
req->domain = domain;
sw_copy_to_stack(req->domain, req->_domain);
sw_zval_add_ref(&req->domain);
/**
* Use asynchronous IO
*/
if (SwooleG.use_async_resolver)
{
php_swoole_check_reactor();
SW_CHECK_RETURN(swDNSResolver_request(Z_STRVAL_P(domain), php_swoole_dns_callback, (void *) req));
}
if (SwooleAIO.mode == SW_AIO_LINUX)
{
SwooleAIO.mode = SW_AIO_BASE;
SwooleAIO.init = 0;
}
php_swoole_check_aio();
/**
* Use thread pool
*/
int buf_size;
if (Z_STRLEN_P(domain) < SW_IP_MAX_LENGTH)
{
buf_size = SW_IP_MAX_LENGTH + 1;
}
else
{
buf_size = Z_STRLEN_P(domain) + 1;
}
void *buf = emalloc(buf_size);
bzero(buf, buf_size);
memcpy(buf, Z_STRVAL_P(domain), Z_STRLEN_P(domain));
php_swoole_check_aio();
SW_CHECK_RETURN(swAio_dns_lookup(req, buf, buf_size));
}
static int process_stream_onRead(swReactor *reactor, swEvent *event)
{
SWOOLE_GET_TSRMLS;
process_stream *ps = event->socket->object;
char *buf = ps->buffer->str + ps->buffer->length;
size_t len = ps->buffer->size - ps->buffer->length;
int ret = read(event->fd, buf, len);
if (ret > 0)
{
ps->buffer->length += ret;
if (ps->buffer->length == ps->buffer->size)
{
swString_extend(ps->buffer, ps->buffer->size * 2);
}
}
else if (ret == 0)
{
zval *zcallback = ps->callback;
zval *retval = NULL;
zval **args[2];
zval *zdata;
SW_MAKE_STD_ZVAL(zdata);
SW_ZVAL_STRINGL(zdata, ps->buffer->str, ps->buffer->length, 1);
SwooleG.main_reactor->del(SwooleG.main_reactor, ps->fd);
swString_free(ps->buffer);
args[0] = &zdata;
int status;
zval *zstatus;
SW_MAKE_STD_ZVAL(zstatus);
pid_t pid = swWaitpid(ps->pid, &status, WNOHANG);
if (pid > 0)
{
array_init(zstatus);
add_assoc_long(zstatus, "code", WEXITSTATUS(status));
add_assoc_long(zstatus, "signal", WTERMSIG(status));
}
else
{
ZVAL_FALSE(zstatus);
}
args[1] = &zstatus;
if (sw_call_user_function_ex(EG(function_table), NULL, zcallback, &retval, 2, args, 0, NULL TSRMLS_CC) == FAILURE)
{
swoole_php_fatal_error(E_WARNING, "swoole_async: onAsyncComplete handler error");
}
if (EG(exception))
{
zend_exception_error(EG(exception), E_ERROR TSRMLS_CC);
}
if (retval != NULL)
{
sw_zval_ptr_dtor(&retval);
}
sw_zval_ptr_dtor(&zdata);
sw_zval_ptr_dtor(&zstatus);
sw_zval_ptr_dtor(&zcallback);
close(ps->fd);
efree(ps);
}
else
{
swSysError("read() failed.");
}
return SW_OK;
}
PHP_METHOD(swoole_async, exec)
{
char *command;
zend_size_t command_len;
zval *callback;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz", &command, &command_len, &callback) == FAILURE)
{
return;
}
php_swoole_check_reactor();
if (!swReactor_handle_isset(SwooleG.main_reactor, PHP_SWOOLE_FD_MYSQL))
{
SwooleG.main_reactor->setHandle(SwooleG.main_reactor, PHP_SWOOLE_FD_PROCESS_STREAM | SW_EVENT_READ, process_stream_onRead);
SwooleG.main_reactor->setHandle(SwooleG.main_reactor, PHP_SWOOLE_FD_PROCESS_STREAM | SW_EVENT_ERROR, process_stream_onRead);
}
pid_t pid;
int fd = swoole_shell_exec(command, &pid);
if (fd < 0)
{
swoole_php_error(E_WARNING, "Unable to execute '%s'", command);
RETURN_FALSE;
}
swString *buffer = swString_new(1024);
if (buffer == NULL)
{
RETURN_FALSE;
}
process_stream *ps = emalloc(sizeof(process_stream));
ps->callback = callback;
sw_copy_to_stack(ps->callback, ps->_callback);
sw_zval_add_ref(&ps->callback);
ps->fd = fd;
ps->pid = pid;
ps->buffer = buffer;
if (SwooleG.main_reactor->add(SwooleG.main_reactor, ps->fd, PHP_SWOOLE_FD_PROCESS_STREAM | SW_EVENT_READ) < 0)
{
sw_zval_ptr_dtor(&ps->callback);
efree(ps);
RETURN_FALSE;
}
else
{
swConnection *_socket = swReactor_get(SwooleG.main_reactor, ps->fd);
_socket->object = ps;
RETURN_LONG(pid);
}
}
#ifdef SW_COROUTINE
PHP_FUNCTION(swoole_async_dns_lookup_coro)
{
zval *domain;
double timeout = SW_CLIENT_DEFAULT_TIMEOUT;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|d", &domain, &timeout) == FAILURE)
{
RETURN_FALSE;
}
coro_check(TSRMLS_C);
if (Z_TYPE_P(domain) != IS_STRING)
{
swoole_php_fatal_error(E_WARNING, "invalid domain name.");
RETURN_FALSE;
}
if (Z_STRLEN_P(domain) == 0)
{
swoole_php_fatal_error(E_WARNING, "domain name empty.");
RETURN_FALSE;
}
if (!request_cache_map)
{
request_cache_map = swHashMap_new(256, NULL);
}
//find cache
dns_cache *cache = swHashMap_find(request_cache_map, Z_STRVAL_P(domain), Z_STRLEN_P(domain));
if (cache != NULL && cache->update_time > (int64_t)swTimer_get_now_msec )
{
SW_RETURN_STRINGL((*cache->zaddress).str,(*cache->zaddress).length,1);
}
dns_request *req = emalloc(sizeof(dns_request));
req->domain = domain;
sw_copy_to_stack(req->domain, req->_domain);
req->useless = 0;
php_context *sw_current_context = emalloc(sizeof(php_context));
sw_current_context->onTimeout = NULL;
sw_current_context->state = SW_CORO_CONTEXT_RUNNING;
#if PHP_MAJOR_VERSION < 7
sw_current_context->coro_params = req;
#else
sw_current_context->coro_params.value.ptr = (void *) req;
#endif
req->context = sw_current_context;
php_swoole_check_reactor();
int ret = swDNSResolver_request(Z_STRVAL_P(domain), php_swoole_dns_callback_coro, (void *) req);
if (ret == SW_ERR)
{
SW_CHECK_RETURN(ret);
}
//add timeout
php_swoole_check_timer(timeout);
req->timer = SwooleG.timer.add(&SwooleG.timer, (int) (timeout * 1000), 0, sw_current_context, php_swoole_dns_timeout_coro);
if (req->timer)
{
sw_current_context->state = SW_CORO_CONTEXT_IN_DELAYED_TIMEOUT_LIST;
}
coro_save(sw_current_context);
coro_yield();
}
#endif
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化