1 Star 0 Fork 347

杜子课堂/swoole-src

forked from swoole/swoole-src 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
swoole_server.c 84.39 KB
一键复制 编辑 原始数据 按行查看 历史
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175
/*
+----------------------------------------------------------------------+
| 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 "module.h"
#include "Connection.h"
#include "ext/standard/php_var.h"
#if PHP_MAJOR_VERSION < 7
#include "ext/standard/php_smart_str.h"
#else
#include "zend_smart_str.h"
#endif
static int php_swoole_task_id;
static int udp_server_socket;
static int dgram_server_socket;
static struct
{
zval *zobjects[SW_MAX_LISTEN_PORT];
zval *zports;
uint8_t num;
} server_port_list;
zval *php_sw_server_callbacks[PHP_SERVER_CALLBACK_NUM];
static swHashMap *task_callbacks;
#if PHP_MAJOR_VERSION >= 7
zval _php_sw_server_callbacks[PHP_SERVER_CALLBACK_NUM];
#endif
static int php_swoole_task_finish(swServer *serv, zval *data TSRMLS_DC);
static void php_swoole_onPipeMessage(swServer *serv, swEventData *req);
static void php_swoole_onStart(swServer *);
static void php_swoole_onShutdown(swServer *);
static int php_swoole_onPacket(swServer *, swEventData *);
static void php_swoole_onWorkerStart(swServer *, int worker_id);
static void php_swoole_onWorkerStop(swServer *, int worker_id);
static void php_swoole_onUserWorkerStart(swServer *serv, swWorker *worker);
static int php_swoole_onTask(swServer *, swEventData *task);
static int php_swoole_onFinish(swServer *, swEventData *task);
static void php_swoole_onWorkerError(swServer *serv, int worker_id, pid_t worker_pid, int exit_code, int signo);
static void php_swoole_onManagerStart(swServer *serv);
static void php_swoole_onManagerStop(swServer *serv);
static zval* php_swoole_server_add_port(swListenPort *port TSRMLS_DC);
int php_swoole_task_pack(swEventData *task, zval *data TSRMLS_DC)
{
smart_str serialized_data = { 0 };
php_serialize_data_t var_hash;
task->info.type = SW_EVENT_TASK;
//field fd save task_id
task->info.fd = php_swoole_task_id++;
//field from_id save the worker_id
task->info.from_id = SwooleWG.id;
swTask_type(task) = 0;
char *task_data_str;
int task_data_len = 0;
//need serialize
if (SW_Z_TYPE_P(data) != IS_STRING)
{
//serialize
swTask_type(task) |= SW_TASK_SERIALIZE;
PHP_VAR_SERIALIZE_INIT(var_hash);
sw_php_var_serialize(&serialized_data, data, &var_hash TSRMLS_CC);
PHP_VAR_SERIALIZE_DESTROY(var_hash);
#if PHP_MAJOR_VERSION<7
task_data_str = serialized_data.c;
task_data_len = serialized_data.len;
#else
task_data_str = serialized_data.s->val;
task_data_len = serialized_data.s->len;
#endif
}
else
{
task_data_str = Z_STRVAL_P(data);
task_data_len = Z_STRLEN_P(data);
}
if (task_data_len >= SW_IPC_MAX_SIZE - sizeof(task->info))
{
if (swTaskWorker_large_pack(task, task_data_str, task_data_len) < 0)
{
smart_str_free(&serialized_data);
swoole_php_fatal_error(E_WARNING, "large task pack failed()");
return SW_ERR;
}
}
else
{
memcpy(task->data, task_data_str, task_data_len);
task->info.len = task_data_len;
}
smart_str_free(&serialized_data);
return task->info.fd;
}
void php_swoole_get_recv_data(zval *zdata, swEventData *req, char *header, uint32_t header_length)
{
char *data_ptr = NULL;
int data_len;
#ifdef SW_USE_RINGBUFFER
swPackage package;
if (req->info.type == SW_EVENT_PACKAGE)
{
memcpy(&package, req->data, sizeof (package));
data_ptr = package.data;
data_len = package.length;
}
#else
if (req->info.type == SW_EVENT_PACKAGE_END)
{
swString *worker_buffer = swWorker_get_buffer(SwooleG.serv, req->info.from_id);
data_ptr = worker_buffer->str;
data_len = worker_buffer->length;
}
#endif
else
{
data_ptr = req->data;
data_len = req->info.len;
}
if (header_length >= data_len)
{
SW_ZVAL_STRING(zdata, "", 1);
}
else
{
SW_ZVAL_STRINGL(zdata, data_ptr + header_length, data_len - header_length, 1);
}
if (header_length > 0)
{
memcpy(header, data_ptr, header_length);
}
#ifdef SW_USE_RINGBUFFER
if (req->info.type == SW_EVENT_PACKAGE)
{
swReactorThread *thread = swServer_get_thread(SwooleG.serv, req->info.from_id);
thread->buffer_input->free(thread->buffer_input, data_ptr);
}
#endif
}
int php_swoole_get_send_data(zval *zdata, char **str TSRMLS_DC)
{
int length;
if (SW_Z_TYPE_P(zdata) == IS_OBJECT)
{
if (!instanceof_function(Z_OBJCE_P(zdata), swoole_buffer_class_entry_ptr TSRMLS_CC))
{
goto convert;
}
swString *str_buffer = swoole_get_object(zdata);
if (!str_buffer->str)
{
swoole_php_fatal_error(E_WARNING, "swoole_buffer object is empty.");
return SW_ERR;
}
length = str_buffer->length - str_buffer->offset;
*str = str_buffer->str + str_buffer->offset;
}
else
{
convert:
convert_to_string(zdata);
length = Z_STRLEN_P(zdata);
*str = Z_STRVAL_P(zdata);
}
if (length >= SwooleG.serv->buffer_output_size)
{
swoole_php_fatal_error(E_WARNING, "send %d byte data fail, max_size is %d.", length, SwooleG.serv->buffer_output_size);
return SW_ERR;
}
return length;
}
static sw_inline int php_swoole_check_task_param(int dst_worker_id TSRMLS_DC)
{
if (SwooleG.task_worker_num < 1)
{
swoole_php_fatal_error(E_WARNING, "Task method cannot use, Please set task_worker_num.");
return SW_ERR;
}
if (dst_worker_id >= SwooleG.task_worker_num)
{
swoole_php_fatal_error(E_WARNING, "worker_id must be less than serv->task_worker_num.");
return SW_ERR;
}
if (!swIsWorker())
{
swoole_php_fatal_error(E_WARNING, "The method can only be used in the worker process.");
return SW_ERR;
}
return SW_OK;
}
zval* php_swoole_task_unpack(swEventData *task_result TSRMLS_DC)
{
zval *result_data, *result_unserialized_data;
char *result_data_str;
int result_data_len = 0;
php_unserialize_data_t var_hash;
swString *large_packet;
/**
* Large result package
*/
if (swTask_type(task_result) & SW_TASK_TMPFILE)
{
large_packet = swTaskWorker_large_unpack(task_result);
/**
* unpack failed
*/
if (large_packet == NULL)
{
return NULL;
}
result_data_str = large_packet->str;
result_data_len = large_packet->length;
}
else
{
result_data_str = task_result->data;
result_data_len = task_result->info.len;
}
if (swTask_type(task_result) & SW_TASK_SERIALIZE)
{
PHP_VAR_UNSERIALIZE_INIT(var_hash);
SW_ALLOC_INIT_ZVAL(result_unserialized_data);
if (sw_php_var_unserialize(&result_unserialized_data, (const unsigned char **) &result_data_str,
(const unsigned char *) (result_data_str + result_data_len), &var_hash TSRMLS_CC))
{
result_data = result_unserialized_data;
}
else
{
SW_ALLOC_INIT_ZVAL(result_data);
SW_ZVAL_STRINGL(result_data, result_data_str, result_data_len, 1);
}
PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
}
else
{
SW_ALLOC_INIT_ZVAL(result_data);
SW_ZVAL_STRINGL(result_data, result_data_str, result_data_len, 1);
}
return result_data;
}
static zval* php_swoole_server_add_port(swListenPort *port TSRMLS_DC)
{
zval *port_object;
SW_ALLOC_INIT_ZVAL(port_object);
object_init_ex(port_object, swoole_server_port_class_entry_ptr);
server_port_list.zobjects[server_port_list.num++] = port_object;
swoole_server_port_property *property = emalloc(sizeof(swoole_server_port_property));
bzero(property, sizeof(swoole_server_port_property));
swoole_set_property(port_object, 0, property);
swoole_set_object(port_object, port);
zend_update_property_string(swoole_server_port_class_entry_ptr, port_object, ZEND_STRL("host"), port->host TSRMLS_CC);
zend_update_property_long(swoole_server_port_class_entry_ptr, port_object, ZEND_STRL("port"), port->port TSRMLS_CC);
zend_update_property_long(swoole_server_port_class_entry_ptr, port_object, ZEND_STRL("type"), port->type TSRMLS_CC);
add_next_index_zval(server_port_list.zports, port_object);
return port_object;
}
void php_swoole_server_before_start(swServer *serv, zval *zobject TSRMLS_DC)
{
/**
* create swoole server
*/
if (swServer_create(serv) < 0)
{
swoole_php_fatal_error(E_ERROR, "create server failed. Error: %s", sw_error);
return;
}
swTrace("Create swoole_server host=%s, port=%d, mode=%d, type=%d", serv->listen_list->host, (int) serv->listen_list->port, serv->factory_mode, (int) serv->listen_list->type);
zval *zsetting = sw_zend_read_property(swoole_server_class_entry_ptr, zobject, ZEND_STRL("setting"), 1 TSRMLS_CC);
if (zsetting == NULL || ZVAL_IS_NULL(zsetting))
{
SW_MAKE_STD_ZVAL(zsetting);
array_init(zsetting);
zend_update_property(swoole_server_class_entry_ptr, zobject, ZEND_STRL("setting"), zsetting TSRMLS_CC);
}
if (!sw_zend_hash_exists(Z_ARRVAL_P(zsetting), ZEND_STRL("worker_num")))
{
add_assoc_long(zsetting, "worker_num", serv->worker_num);
}
if (!sw_zend_hash_exists(Z_ARRVAL_P(zsetting), ZEND_STRL("task_worker_num")))
{
add_assoc_long(zsetting, "task_worker_num", SwooleG.task_worker_num);
}
if (!sw_zend_hash_exists(Z_ARRVAL_P(zsetting), ZEND_STRL("pipe_buffer_size")))
{
add_assoc_long(zsetting, "pipe_buffer_size", serv->pipe_buffer_size);
}
if (!sw_zend_hash_exists(Z_ARRVAL_P(zsetting), ZEND_STRL("buffer_output_size")))
{
add_assoc_long(zsetting, "buffer_output_size", serv->buffer_output_size);
}
if (!sw_zend_hash_exists(Z_ARRVAL_P(zsetting), ZEND_STRL("max_connection")))
{
add_assoc_long(zsetting, "max_connection", serv->max_connection);
}
int i;
zval *retval = NULL;
zval *port_object;
zval *port_setting;
for (i = 1; i < server_port_list.num; i++)
{
port_object = server_port_list.zobjects[i];
port_setting = sw_zend_read_property(swoole_server_port_class_entry_ptr, port_object, ZEND_STRL("setting"), 1 TSRMLS_CC);
//use swoole_server->setting
if (port_setting == NULL || ZVAL_IS_NULL(port_setting))
{
sw_zval_add_ref(&port_setting);
sw_zval_add_ref(&port_object);
sw_zend_call_method_with_1_params(&port_object, swoole_server_port_class_entry_ptr, NULL, "set", &retval, zsetting);
if (retval != NULL)
{
sw_zval_ptr_dtor(&retval);
}
}
}
}
void php_swoole_register_callback(swServer *serv)
{
/*
* optional callback
*/
if (php_sw_server_callbacks[SW_SERVER_CB_onStart] != NULL)
{
serv->onStart = php_swoole_onStart;
}
serv->onShutdown = php_swoole_onShutdown;
/**
* require callback, set the master/manager/worker PID
*/
serv->onWorkerStart = php_swoole_onWorkerStart;
if (php_sw_server_callbacks[SW_SERVER_CB_onWorkerStop] != NULL)
{
serv->onWorkerStop = php_swoole_onWorkerStop;
}
/**
* UDP Packet
*/
if (php_sw_server_callbacks[SW_SERVER_CB_onPacket] != NULL)
{
serv->onPacket = php_swoole_onPacket;
}
/**
* Task Worker
*/
if (php_sw_server_callbacks[SW_SERVER_CB_onTask] != NULL)
{
serv->onTask = php_swoole_onTask;
}
if (php_sw_server_callbacks[SW_SERVER_CB_onFinish] != NULL)
{
serv->onFinish = php_swoole_onFinish;
}
if (php_sw_server_callbacks[SW_SERVER_CB_onWorkerError] != NULL)
{
serv->onWorkerError = php_swoole_onWorkerError;
}
if (php_sw_server_callbacks[SW_SERVER_CB_onManagerStart] != NULL)
{
serv->onManagerStart = php_swoole_onManagerStart;
}
if (php_sw_server_callbacks[SW_SERVER_CB_onManagerStop] != NULL)
{
serv->onManagerStop = php_swoole_onManagerStop;
}
if (php_sw_server_callbacks[SW_SERVER_CB_onPipeMessage] != NULL)
{
serv->onPipeMessage = php_swoole_onPipeMessage;
}
if (php_sw_server_callbacks[SW_SERVER_CB_onBufferFull] != NULL)
{
serv->onBufferFull = php_swoole_onBufferFull;
}
if (php_sw_server_callbacks[SW_SERVER_CB_onBufferEmpty] != NULL)
{
serv->onBufferEmpty = php_swoole_onBufferEmpty;
}
}
static int php_swoole_task_finish(swServer *serv, zval *data TSRMLS_DC)
{
int flags = 0;
smart_str serialized_data = {0};
php_serialize_data_t var_hash;
char *data_str;
int data_len = 0;
int ret;
//need serialize
if (SW_Z_TYPE_P(data) != IS_STRING)
{
//serialize
flags |= SW_TASK_SERIALIZE;
PHP_VAR_SERIALIZE_INIT(var_hash);
sw_php_var_serialize(&serialized_data, data, &var_hash TSRMLS_CC);
PHP_VAR_SERIALIZE_DESTROY(var_hash);
#if PHP_MAJOR_VERSION<7
data_str = serialized_data.c;
data_len = serialized_data.len;
#else
data_str = serialized_data.s->val;
data_len = serialized_data.s->len;
#endif
}
else
{
data_str = Z_STRVAL_P(data);
data_len = Z_STRLEN_P(data);
}
ret = swTaskWorker_finish(serv, data_str, data_len, flags);
smart_str_free(&serialized_data);
return ret;
}
static void php_swoole_onPipeMessage(swServer *serv, swEventData *req)
{
SWOOLE_GET_TSRMLS;
zval *zserv = (zval *) serv->ptr2;
zval *zworker_id;
zval *retval = NULL;
SW_MAKE_STD_ZVAL(zworker_id);
ZVAL_LONG(zworker_id, (long) req->info.from_id);
zval *zdata = php_swoole_task_unpack(req TSRMLS_CC);
zval **args[3];
args[0] = &zserv;
args[1] = &zworker_id;
args[2] = &zdata;
swTrace("PipeMessage: fd=%d|len=%d|from_id=%d|data=%s\n", req->info.fd, req->info.len, req->info.from_id, req->data);
if (sw_call_user_function_ex(EG(function_table), NULL, php_sw_server_callbacks[SW_SERVER_CB_onPipeMessage], &retval, 3, args, 0, NULL TSRMLS_CC) == FAILURE)
{
swoole_php_fatal_error(E_WARNING, "onPipeMessage handler error.");
}
if (EG(exception))
{
zend_exception_error(EG(exception), E_ERROR TSRMLS_CC);
}
sw_zval_ptr_dtor(&zworker_id);
sw_zval_ptr_dtor(&zdata);
if (retval != NULL)
{
sw_zval_ptr_dtor(&retval);
}
}
int php_swoole_onReceive(swServer *serv, swEventData *req)
{
swFactory *factory = &serv->factory;
zval *zserv = (zval *) serv->ptr2;
zval **args[4];
zval *zfd;
zval *zfrom_id;
zval *zdata;
zval *retval = NULL;
SWOOLE_GET_TSRMLS;
zval *callback = php_swoole_server_get_callback(serv, req->info.from_fd, SW_SERVER_CB_onReceive);
if (callback == NULL || ZVAL_IS_NULL(callback))
{
swoole_php_fatal_error(E_WARNING, "onReceive callback is null.");
return SW_OK;
}
//UDP使用from_id作为port,fd做为ip
php_swoole_udp_t udp_info;
swDgramPacket *packet;
SW_MAKE_STD_ZVAL(zfd);
SW_MAKE_STD_ZVAL(zfrom_id);
SW_MAKE_STD_ZVAL(zdata);
//dgram
if (swEventData_is_dgram(req->info.type))
{
swoole_php_error(E_DEPRECATED, "The udp onReceive callback is deprecated, use onPacket instead.");
swString *buffer = swWorker_get_buffer(serv, req->info.from_id);
packet = (swDgramPacket*) buffer->str;
//udp ipv4
if (req->info.type == SW_EVENT_UDP)
{
udp_info.from_fd = req->info.from_fd;
udp_info.port = packet->port;
memcpy(&udp_server_socket, &udp_info, sizeof(udp_server_socket));
factory->last_from_id = udp_server_socket;
swTrace("SendTo: from_id=%d|from_fd=%d", (uint16_t) req->info.from_id, req->info.from_fd);
SW_ZVAL_STRINGL(zdata, packet->data, packet->length, 1);
ZVAL_LONG(zfrom_id, (long ) udp_server_socket);
ZVAL_LONG(zfd, (long ) packet->addr.v4.s_addr);
}
//udp ipv6
else if (req->info.type == SW_EVENT_UDP6)
{
udp_info.from_fd = req->info.from_fd;
udp_info.port = packet->port;
memcpy(&dgram_server_socket, &udp_info, sizeof(udp_server_socket));
factory->last_from_id = dgram_server_socket;
swTrace("SendTo: from_id=%d|from_fd=%d", (uint16_t) req->info.from_id, req->info.from_fd);
ZVAL_LONG(zfrom_id, (long ) dgram_server_socket);
char tmp[INET6_ADDRSTRLEN];
inet_ntop(AF_INET6, &packet->addr.v6, tmp, sizeof(tmp));
SW_ZVAL_STRING(zfd, tmp, 1);
SW_ZVAL_STRINGL(zdata, packet->data, packet->length, 1);
}
//unix dgram
else
{
SW_ZVAL_STRINGL(zfd, packet->data, packet->addr.un.path_length, 1);
SW_ZVAL_STRINGL(zdata, packet->data + packet->addr.un.path_length, packet->length - packet->addr.un.path_length, 1);
ZVAL_LONG(zfrom_id, (long ) req->info.from_fd);
dgram_server_socket = req->info.from_fd;
}
}
//stream
else
{
ZVAL_LONG(zfrom_id, (long ) req->info.from_id);
ZVAL_LONG(zfd, (long ) req->info.fd);
php_swoole_get_recv_data(zdata, req, NULL, 0);
}
args[0] = &zserv;
args[1] = &zfd;
args[2] = &zfrom_id;
args[3] = &zdata;
if (sw_call_user_function_ex(EG(function_table), NULL, callback, &retval, 4, args, 0, NULL TSRMLS_CC) == FAILURE)
{
swoole_php_fatal_error(E_WARNING, "onReceive handler error.");
}
if (EG(exception))
{
zend_exception_error(EG(exception), E_ERROR TSRMLS_CC);
}
sw_zval_ptr_dtor(&zfd);
sw_zval_ptr_dtor(&zfrom_id);
sw_zval_ptr_dtor(&zdata);
if (retval != NULL)
{
sw_zval_ptr_dtor(&retval);
}
return SW_OK;
}
static int php_swoole_onPacket(swServer *serv, swEventData *req)
{
zval *zserv = (zval *) serv->ptr2;
zval **args[3];
zval *zdata;
zval *zaddr;
zval *retval = NULL;
swDgramPacket *packet;
SWOOLE_GET_TSRMLS;
SW_MAKE_STD_ZVAL(zdata);
SW_MAKE_STD_ZVAL(zaddr);
array_init(zaddr);
swString *buffer = swWorker_get_buffer(serv, req->info.from_id);
packet = (swDgramPacket*) buffer->str;
add_assoc_long(zaddr, "server_socket", req->info.from_fd);
swConnection *from_sock = swServer_connection_get(serv, req->info.from_fd);
if (from_sock)
{
add_assoc_long(zaddr, "server_port", swConnection_get_port(from_sock));
}
zval *callback = php_swoole_server_get_callback(serv, req->info.from_fd, SW_SERVER_CB_onPacket);
if (callback == NULL || ZVAL_IS_NULL(callback))
{
swoole_php_fatal_error(E_WARNING, "onPacket callback is null.");
return SW_OK;
}
char address[INET6_ADDRSTRLEN];
//udp ipv4
if (req->info.type == SW_EVENT_UDP)
{
inet_ntop(AF_INET, &packet->addr.v4, address, sizeof(address));
sw_add_assoc_string(zaddr, "address", address, 1);
add_assoc_long(zaddr, "port", packet->port);
SW_ZVAL_STRINGL(zdata, packet->data, packet->length, 1);
}
//udp ipv6
else if (req->info.type == SW_EVENT_UDP6)
{
inet_ntop(AF_INET6, &packet->addr.v6, address, sizeof(address));
sw_add_assoc_string(zaddr, "address", address, 1);
add_assoc_long(zaddr, "port", packet->port);
SW_ZVAL_STRINGL(zdata, packet->data, packet->length, 1);
}
//unix dgram
else if (req->info.type == SW_EVENT_UNIX_DGRAM)
{
sw_add_assoc_stringl(zaddr, "address", packet->data, packet->addr.un.path_length, 1);
SW_ZVAL_STRINGL(zdata, packet->data + packet->addr.un.path_length, packet->length - packet->addr.un.path_length, 1);
dgram_server_socket = req->info.from_fd;
}
args[0] = &zserv;
args[1] = &zdata;
args[2] = &zaddr;
if (sw_call_user_function_ex(EG(function_table), NULL, callback, &retval, 3, args, 0, NULL TSRMLS_CC) == FAILURE)
{
swoole_php_fatal_error(E_WARNING, "onPacket handler error.");
}
if (EG(exception))
{
zend_exception_error(EG(exception), E_ERROR TSRMLS_CC);
}
sw_zval_ptr_dtor(&zaddr);
sw_zval_ptr_dtor(&zdata);
if (retval != NULL)
{
sw_zval_ptr_dtor(&retval);
}
return SW_OK;
}
static int php_swoole_onTask(swServer *serv, swEventData *req)
{
zval *zserv = (zval *) serv->ptr2;
zval **args[4];
zval *zfd;
zval *zfrom_id;
sw_atomic_fetch_sub(&SwooleStats->tasking_num, 1);
zval *retval = NULL;
SWOOLE_GET_TSRMLS;
SW_MAKE_STD_ZVAL(zfd);
ZVAL_LONG(zfd, (long) req->info.fd);
SW_MAKE_STD_ZVAL(zfrom_id);
ZVAL_LONG(zfrom_id, (long) req->info.from_id);
zval *zdata = php_swoole_task_unpack(req TSRMLS_CC);
args[0] = &zserv;
args[1] = &zfd;
args[2] = &zfrom_id;
args[3] = &zdata;
if (sw_call_user_function_ex(EG(function_table), NULL, php_sw_server_callbacks[SW_SERVER_CB_onTask], &retval, 4, args, 0, NULL TSRMLS_CC) == FAILURE)
{
swoole_php_fatal_error(E_WARNING, "onTask handler error.");
}
if (EG(exception))
{
zend_exception_error(EG(exception), E_ERROR TSRMLS_CC);
}
sw_zval_ptr_dtor(&zfd);
sw_zval_ptr_dtor(&zfrom_id);
sw_zval_ptr_dtor(&zdata);
if (retval)
{
if (SW_Z_TYPE_P(retval) != IS_NULL)
{
php_swoole_task_finish(serv, retval TSRMLS_CC);
}
sw_zval_ptr_dtor(&retval);
}
return SW_OK;
}
static int php_swoole_onFinish(swServer *serv, swEventData *req)
{
zval *zserv = (zval *) serv->ptr2;
zval **args[3];
zval *ztask_id;
zval *zdata;
zval *retval = NULL;
SWOOLE_GET_TSRMLS;
SW_MAKE_STD_ZVAL(ztask_id);
ZVAL_LONG(ztask_id, (long) req->info.fd);
zdata = php_swoole_task_unpack(req TSRMLS_CC);
args[0] = &zserv;
args[1] = &ztask_id;
args[2] = &zdata;
zval *callback = NULL;
if (swTask_type(req) & SW_TASK_CALLBACK)
{
callback = swHashMap_find_int(task_callbacks, req->info.fd);
}
if (callback == NULL)
{
callback = php_sw_server_callbacks[SW_SERVER_CB_onFinish];
}
if (sw_call_user_function_ex(EG(function_table), NULL, callback, &retval, 3, args, 0, NULL TSRMLS_CC) == FAILURE)
{
swoole_php_fatal_error(E_WARNING, "onFinish handler error.");
}
if (EG(exception))
{
zend_exception_error(EG(exception), E_ERROR TSRMLS_CC);
}
sw_zval_ptr_dtor(&ztask_id);
sw_zval_ptr_dtor(&zdata);
#if PHP_MAJOR_VERSION >= 7
efree(zdata);
#endif
if (retval != NULL)
{
sw_zval_ptr_dtor(&retval);
}
if (swTask_type(req) & SW_TASK_CALLBACK)
{
swHashMap_del_int(task_callbacks, req->info.fd);
sw_zval_ptr_dtor(&callback);
#if PHP_MAJOR_VERSION >= 7
efree(callback);
#endif
}
return SW_OK;
}
static void php_swoole_onStart(swServer *serv)
{
SwooleG.lock.lock(&SwooleG.lock);
SWOOLE_GET_TSRMLS;
zval *zserv = (zval *) serv->ptr2;
zval **args[1];
zval *retval = NULL;
pid_t manager_pid = serv->factory_mode == SW_MODE_PROCESS ? SwooleGS->manager_pid : 0;
zend_update_property_long(swoole_server_class_entry_ptr, zserv, ZEND_STRL("master_pid"), SwooleGS->master_pid TSRMLS_CC);
zend_update_property_long(swoole_server_class_entry_ptr, zserv, ZEND_STRL("manager_pid"), manager_pid TSRMLS_CC);
args[0] = &zserv;
sw_zval_add_ref(&zserv);
if (sw_call_user_function_ex(EG(function_table), NULL, php_sw_server_callbacks[SW_SERVER_CB_onStart], &retval, 1, args, 0, NULL TSRMLS_CC) == FAILURE)
{
swoole_php_fatal_error(E_WARNING, "onStart handler error.");
}
if (EG(exception))
{
zend_exception_error(EG(exception), E_ERROR TSRMLS_CC);
}
if (retval != NULL)
{
sw_zval_ptr_dtor(&retval);
}
SwooleG.lock.unlock(&SwooleG.lock);
}
static void php_swoole_onManagerStart(swServer *serv)
{
SWOOLE_GET_TSRMLS;
zval *zserv = (zval *) serv->ptr2;
zval **args[1];
zval *retval = NULL;
pid_t manager_pid = serv->factory_mode == SW_MODE_PROCESS ? SwooleGS->manager_pid : 0;
zend_update_property_long(swoole_server_class_entry_ptr, zserv, ZEND_STRL("master_pid"), SwooleGS->master_pid TSRMLS_CC);
zend_update_property_long(swoole_server_class_entry_ptr, zserv, ZEND_STRL("manager_pid"), manager_pid TSRMLS_CC);
args[0] = &zserv;
sw_zval_add_ref(&zserv);
if (sw_call_user_function_ex(EG(function_table), NULL, php_sw_server_callbacks[SW_SERVER_CB_onManagerStart], &retval, 1, args, 0, NULL TSRMLS_CC) == FAILURE)
{
swoole_php_fatal_error(E_WARNING, "onManagerStart handler error.");
}
if (EG(exception))
{
zend_exception_error(EG(exception), E_ERROR TSRMLS_CC);
}
if (retval != NULL)
{
sw_zval_ptr_dtor(&retval);
}
}
static void php_swoole_onManagerStop(swServer *serv)
{
SWOOLE_GET_TSRMLS;
zval *zserv = (zval *) serv->ptr2;
zval **args[1];
zval *retval = NULL;
args[0] = &zserv;
sw_zval_add_ref(&zserv);
if (sw_call_user_function_ex(EG(function_table), NULL, php_sw_server_callbacks[SW_SERVER_CB_onManagerStop], &retval, 1, args, 0, NULL TSRMLS_CC) == FAILURE)
{
swoole_php_fatal_error(E_WARNING, "onManagerStop handler error.");
}
if (EG(exception))
{
zend_exception_error(EG(exception), E_ERROR TSRMLS_CC);
}
if (retval != NULL)
{
sw_zval_ptr_dtor(&retval);
}
}
static void php_swoole_onShutdown(swServer *serv)
{
SwooleG.lock.lock(&SwooleG.lock);
zval *zserv = (zval *) serv->ptr2;
zval **args[1];
zval *retval = NULL;
args[0] = &zserv;
SWOOLE_GET_TSRMLS;
if (php_sw_server_callbacks[SW_SERVER_CB_onShutdown] != NULL)
{
if (sw_call_user_function_ex(EG(function_table), NULL, php_sw_server_callbacks[SW_SERVER_CB_onShutdown], &retval, 1, args, 0, NULL TSRMLS_CC) == FAILURE)
{
swoole_php_fatal_error(E_WARNING, "onShutdown handler error.");
}
if (EG(exception))
{
zend_exception_error(EG(exception), E_ERROR TSRMLS_CC);
}
if (retval != NULL)
{
sw_zval_ptr_dtor(&retval);
}
}
SwooleG.lock.unlock(&SwooleG.lock);
}
static void php_swoole_onWorkerStart(swServer *serv, int worker_id)
{
zval *zserv = (zval *) serv->ptr2;
zval *zworker_id;
zval **args[2];
zval *retval = NULL;
SWOOLE_GET_TSRMLS;
SW_MAKE_STD_ZVAL(zworker_id);
ZVAL_LONG(zworker_id, worker_id);
args[0] = &zserv;
args[1] = &zworker_id;
/**
* Master Process ID
*/
zend_update_property_long(swoole_server_class_entry_ptr, zserv, ZEND_STRL("master_pid"), SwooleGS->master_pid TSRMLS_CC);
/**
* Manager Process ID
*/
zend_update_property_long(swoole_server_class_entry_ptr, zserv, ZEND_STRL("manager_pid"), SwooleGS->manager_pid TSRMLS_CC);
/**
* Worker ID
*/
zend_update_property(swoole_server_class_entry_ptr, zserv, ZEND_STRL("worker_id"), zworker_id TSRMLS_CC);
/**
* Is a task worker?
*/
if (worker_id >= serv->worker_num)
{
zend_update_property_bool(swoole_server_class_entry_ptr, zserv, ZEND_STRL("taskworker"), 1 TSRMLS_CC);
}
else
{
zend_update_property_bool(swoole_server_class_entry_ptr, zserv, ZEND_STRL("taskworker"), 0 TSRMLS_CC);
}
/**
* Worker Process ID
*/
zend_update_property_long(swoole_server_class_entry_ptr, zserv, ZEND_STRL("worker_pid"), getpid() TSRMLS_CC);
sw_zval_ptr_dtor(&zworker_id);
/**
* Have not set the event callback
*/
if (php_sw_server_callbacks[SW_SERVER_CB_onWorkerStart] == NULL)
{
return;
}
if (sw_call_user_function_ex(EG(function_table), NULL, php_sw_server_callbacks[SW_SERVER_CB_onWorkerStart], &retval, 2, args, 0, NULL TSRMLS_CC) == FAILURE)
{
swoole_php_fatal_error(E_WARNING, "onWorkerStart handler error.");
}
if (EG(exception))
{
zend_exception_error(EG(exception), E_ERROR TSRMLS_CC);
}
if (retval)
{
sw_zval_ptr_dtor(&retval);
}
}
static void php_swoole_onWorkerStop(swServer *serv, int worker_id)
{
if (SwooleWG.shutdown)
{
return;
}
SwooleWG.shutdown = 1;
zval *zobject = (zval *) serv->ptr2;
zval *zworker_id;
zval **args[2];
zval *retval = NULL;
SW_MAKE_STD_ZVAL(zworker_id);
ZVAL_LONG(zworker_id, worker_id);
sw_zval_add_ref(&zobject);
SWOOLE_GET_TSRMLS;
args[0] = &zobject;
args[1] = &zworker_id;
if (sw_call_user_function_ex(EG(function_table), NULL, php_sw_server_callbacks[SW_SERVER_CB_onWorkerStop], &retval, 2, args, 0,
NULL TSRMLS_CC) == FAILURE)
{
swoole_php_fatal_error(E_WARNING, "onWorkerStop handler error.");
}
if (EG(exception))
{
zend_exception_error(EG(exception), E_ERROR TSRMLS_CC);
}
sw_zval_ptr_dtor(&zworker_id);
if (retval != NULL)
{
sw_zval_ptr_dtor(&retval);
}
}
static void php_swoole_onUserWorkerStart(swServer *serv, swWorker *worker)
{
SWOOLE_GET_TSRMLS;
zval *object = worker->ptr;
zend_update_property_long(swoole_process_class_entry_ptr, object, ZEND_STRL("id"), SwooleWG.id TSRMLS_CC);
php_swoole_process_start(worker, object TSRMLS_CC);
}
static void php_swoole_onWorkerError(swServer *serv, int worker_id, pid_t worker_pid, int exit_code, int signo)
{
zval *zobject = (zval *) serv->ptr2;
zval *zworker_id, *zworker_pid, *zexit_code, *zsigno;
zval **args[5];
zval *retval = NULL;
SW_MAKE_STD_ZVAL(zworker_id);
ZVAL_LONG(zworker_id, worker_id);
SW_MAKE_STD_ZVAL(zworker_pid);
ZVAL_LONG(zworker_pid, worker_pid);
SW_MAKE_STD_ZVAL(zexit_code);
ZVAL_LONG(zexit_code, exit_code);
SW_MAKE_STD_ZVAL(zsigno);
ZVAL_LONG(zsigno, signo);
sw_zval_add_ref(&zobject);
SWOOLE_GET_TSRMLS;
args[0] = &zobject;
args[1] = &zworker_id;
args[2] = &zworker_pid;
args[3] = &zexit_code;
args[4] = &zsigno;
if (sw_call_user_function_ex(EG(function_table), NULL, php_sw_server_callbacks[SW_SERVER_CB_onWorkerError], &retval, 5, args, 0, NULL TSRMLS_CC) == FAILURE)
{
swoole_php_fatal_error(E_WARNING, "onWorkerError handler error.");
}
if (EG(exception))
{
zend_exception_error(EG(exception), E_ERROR TSRMLS_CC);
}
sw_zval_ptr_dtor(&zworker_id);
sw_zval_ptr_dtor(&zworker_pid);
sw_zval_ptr_dtor(&zexit_code);
sw_zval_ptr_dtor(&zsigno);
if (retval != NULL)
{
sw_zval_ptr_dtor(&retval);
}
}
void php_swoole_onConnect(swServer *serv, swDataHead *info)
{
zval *zserv = (zval *) serv->ptr2;
zval *zfd;
zval *zfrom_id;
zval **args[3];
zval *retval = NULL;
SWOOLE_GET_TSRMLS;
zval *callback = php_swoole_server_get_callback(serv, info->from_fd, SW_SERVER_CB_onConnect);
if (callback == NULL || ZVAL_IS_NULL(callback))
{
return;
}
SW_MAKE_STD_ZVAL(zfd);
ZVAL_LONG(zfd, info->fd);
SW_MAKE_STD_ZVAL(zfrom_id);
ZVAL_LONG(zfrom_id, info->from_id);
args[0] = &zserv;
args[1] = &zfd;
args[2] = &zfrom_id;
if (sw_call_user_function_ex(EG(function_table), NULL, callback, &retval, 3, args, 0, NULL TSRMLS_CC) == FAILURE)
{
swoole_php_error(E_WARNING, "onConnect handler error.");
}
if (EG(exception))
{
zend_exception_error(EG(exception), E_ERROR TSRMLS_CC);
}
sw_zval_ptr_dtor(&zfd);
sw_zval_ptr_dtor(&zfrom_id);
if (retval != NULL)
{
sw_zval_ptr_dtor(&retval);
}
}
void php_swoole_onClose(swServer *serv, swDataHead *info)
{
zval *zserv = (zval *) serv->ptr2;
zval *zfd;
zval *zfrom_id;
zval **args[3];
zval *retval = NULL;
SWOOLE_GET_TSRMLS;
zval *callback = php_swoole_server_get_callback(serv, info->from_fd, SW_SERVER_CB_onClose);
if (callback == NULL || ZVAL_IS_NULL(callback))
{
return;
}
SW_MAKE_STD_ZVAL(zfd);
ZVAL_LONG(zfd, info->fd);
SW_MAKE_STD_ZVAL(zfrom_id);
ZVAL_LONG(zfrom_id, info->from_id);
args[0] = &zserv;
args[1] = &zfd;
args[2] = &zfrom_id;
if (sw_call_user_function_ex(EG(function_table), NULL, callback, &retval, 3, args, 0, NULL TSRMLS_CC) == FAILURE)
{
swoole_php_fatal_error(E_WARNING, "onClose handler error.");
}
if (EG(exception))
{
zend_exception_error(EG(exception), E_ERROR TSRMLS_CC);
}
sw_zval_ptr_dtor(&zfd);
sw_zval_ptr_dtor(&zfrom_id);
if (retval != NULL)
{
sw_zval_ptr_dtor(&retval);
}
}
void php_swoole_onBufferFull(swServer *serv, swDataHead *info)
{
zval *zserv = (zval *) serv->ptr2;
zval *zfd;
zval **args[2];
zval *retval = NULL;
zval *callback = php_swoole_server_get_callback(serv, info->from_fd, SW_SERVER_CB_onBufferFull);
if (!callback)
{
return;
}
SWOOLE_GET_TSRMLS;
SW_MAKE_STD_ZVAL(zfd);
ZVAL_LONG(zfd, info->fd);
args[0] = &zserv;
args[1] = &zfd;
if (sw_call_user_function_ex(EG(function_table), NULL, callback, &retval, 2, args, 0, NULL TSRMLS_CC) == FAILURE)
{
swoole_php_error(E_WARNING, "onBufferFull handler error.");
}
if (EG(exception))
{
zend_exception_error(EG(exception), E_ERROR TSRMLS_CC);
}
sw_zval_ptr_dtor(&zfd);
if (retval != NULL)
{
sw_zval_ptr_dtor(&retval);
}
}
void php_swoole_onBufferEmpty(swServer *serv, swDataHead *info)
{
zval *zserv = (zval *) serv->ptr2;
zval *zfd;
zval **args[2];
zval *retval = NULL;
zval *callback = php_swoole_server_get_callback(serv, info->from_fd, SW_SERVER_CB_onBufferEmpty);
if (!callback)
{
return;
}
SWOOLE_GET_TSRMLS;
SW_MAKE_STD_ZVAL(zfd);
ZVAL_LONG(zfd, info->fd);
args[0] = &zserv;
args[1] = &zfd;
if (sw_call_user_function_ex(EG(function_table), NULL, callback, &retval, 2, args, 0, NULL TSRMLS_CC) == FAILURE)
{
swoole_php_error(E_WARNING, "onBufferEmpty handler error.");
}
if (EG(exception))
{
zend_exception_error(EG(exception), E_ERROR TSRMLS_CC);
}
sw_zval_ptr_dtor(&zfd);
if (retval != NULL)
{
sw_zval_ptr_dtor(&retval);
}
}
PHP_METHOD(swoole_server, __construct)
{
zend_size_t host_len = 0;
char *serv_host;
long sock_type = SW_SOCK_TCP;
long serv_port = 0;
long serv_mode = SW_MODE_PROCESS;
//only cli env
if (strcasecmp("cli", sapi_module.name) != 0)
{
swoole_php_fatal_error(E_ERROR, "swoole_server must run at php_cli environment.");
RETURN_FALSE;
}
if (SwooleG.main_reactor != NULL)
{
swoole_php_fatal_error(E_ERROR, "eventLoop has been created. Unable to create swoole_server.");
RETURN_FALSE;
}
if (SwooleGS->start > 0)
{
swoole_php_fatal_error(E_WARNING, "server is already running. Unable to create swoole_server.");
RETURN_FALSE;
}
swServer *serv = sw_malloc(sizeof (swServer));
swServer_init(serv);
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|lll", &serv_host, &host_len, &serv_port, &serv_mode, &sock_type) == FAILURE)
{
swoole_php_fatal_error(E_ERROR, "invalid parameters.");
return;
}
#ifdef __CYGWIN__
serv_mode = SW_MODE_SINGLE;
#elif !defined(SW_USE_THREAD)
if (serv_mode == SW_MODE_THREAD || serv_mode == SW_MODE_BASE)
{
serv_mode = SW_MODE_SINGLE;
swoole_php_fatal_error(E_WARNING, "PHP can not running at multi-threading. Reset mode to SWOOLE_MODE_BASE");
}
#endif
serv->factory_mode = serv_mode;
if (serv->factory_mode == SW_MODE_SINGLE)
{
serv->worker_num = 1;
serv->max_request = 0;
}
bzero(php_sw_server_callbacks, sizeof (zval*) * PHP_SERVER_CALLBACK_NUM);
swListenPort *port = swServer_add_port(serv, sock_type, serv_host, serv_port);
if (!port)
{
swoole_php_fatal_error(E_ERROR, "listen server port failed.");
return;
}
zval *server_object = getThis();
#ifdef HAVE_PCRE
zval *connection_iterator_object;
SW_MAKE_STD_ZVAL(connection_iterator_object);
object_init_ex(connection_iterator_object, swoole_connection_iterator_class_entry_ptr);
zend_update_property(swoole_server_class_entry_ptr, server_object, ZEND_STRL("connections"), connection_iterator_object TSRMLS_CC);
#endif
zend_update_property_stringl(swoole_server_class_entry_ptr, server_object, ZEND_STRL("host"), serv_host, host_len TSRMLS_CC);
zend_update_property_long(swoole_server_class_entry_ptr, server_object, ZEND_STRL("port"), (long) port->port TSRMLS_CC);
zend_update_property_long(swoole_server_class_entry_ptr, server_object, ZEND_STRL("mode"), serv->factory_mode TSRMLS_CC);
zend_update_property_long(swoole_server_class_entry_ptr, server_object, ZEND_STRL("type"), sock_type TSRMLS_CC);
swoole_set_object(server_object, serv);
zval *ports;
SW_ALLOC_INIT_ZVAL(ports);
array_init(ports);
zend_update_property(swoole_server_class_entry_ptr, server_object, ZEND_STRL("ports"), ports TSRMLS_CC);
server_port_list.zports = ports;
php_swoole_server_add_port(port TSRMLS_CC);
}
PHP_METHOD(swoole_server, set)
{
zval *zset = NULL;
zval *zobject = getThis();
HashTable *vht;
zval *v;
if (SwooleGS->start > 0)
{
swoole_php_fatal_error(E_WARNING, "Server is running. Unable to execute swoole_server_set now.");
RETURN_FALSE;
}
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zset) == FAILURE)
{
return;
}
php_swoole_array_separate(zset);
swServer *serv = swoole_get_object(zobject);
vht = Z_ARRVAL_P(zset);
//chroot
if (php_swoole_array_get_value(vht, "chroot", v))
{
convert_to_string(v);
SwooleG.chroot = strndup(Z_STRVAL_P(v), Z_STRLEN_P(v));
}
//user
if (php_swoole_array_get_value(vht, "user", v))
{
convert_to_string(v);
SwooleG.user = strndup(Z_STRVAL_P(v), Z_STRLEN_P(v));
}
//group
if (php_swoole_array_get_value(vht, "group", v))
{
convert_to_string(v);
SwooleG.group = strndup(Z_STRVAL_P(v), Z_STRLEN_P(v));
}
//daemonize
if (php_swoole_array_get_value(vht, "daemonize", v))
{
convert_to_boolean(v);
serv->daemonize = Z_BVAL_P(v);
}
//pid file
if (php_swoole_array_get_value(vht, "pid_file", v))
{
convert_to_string(v);
serv->pid_file = strndup(Z_STRVAL_P(v), Z_STRLEN_P(v));
}
//reactor thread num
if (php_swoole_array_get_value(vht, "reactor_num", v))
{
convert_to_long(v);
serv->reactor_num = (int) Z_LVAL_P(v);
if (serv->reactor_num <= 0)
{
serv->reactor_num = SwooleG.cpu_num;
}
}
//worker_num
if (php_swoole_array_get_value(vht, "worker_num", v))
{
convert_to_long(v);
serv->worker_num = (int) Z_LVAL_P(v);
if (serv->worker_num <= 0)
{
serv->worker_num = SwooleG.cpu_num;
}
}
//dispatch_mode
if (php_swoole_array_get_value(vht, "dispatch_mode", v))
{
convert_to_long(v);
serv->dispatch_mode = (int) Z_LVAL_P(v);
}
//c/c++ function
if (php_swoole_array_get_value(vht, "dispatch_func", v))
{
convert_to_string(v);
swServer_dispatch_function func = swModule_get_global_function(Z_STRVAL_P(v), Z_STRLEN_P(v));
if (func == NULL)
{
swoole_php_fatal_error(E_ERROR, "extension module function '%s' is undefined.", Z_STRVAL_P(v));
return;
}
serv->dispatch_mode = 0;
serv->dispatch_func = func;
}
//log_file
if (php_swoole_array_get_value(vht, "log_file", v))
{
convert_to_string(v);
SwooleG.log_file = strndup(Z_STRVAL_P(v), Z_STRLEN_P(v));
}
//log_level
if (php_swoole_array_get_value(vht, "log_level", v))
{
convert_to_long(v);
SwooleG.log_level = (int) Z_LVAL_P(v);
}
/**
* for dispatch_mode = 1/3
*/
if (php_swoole_array_get_value(vht, "discard_timeout_request", v))
{
convert_to_boolean(v);
serv->discard_timeout_request = Z_BVAL_P(v);
}
//onConnect/onClose event
if (php_swoole_array_get_value(vht, "enable_unsafe_event", v))
{
convert_to_boolean(v);
serv->enable_unsafe_event = Z_BVAL_P(v);
}
//delay receive
if (php_swoole_array_get_value(vht, "enable_delay_receive", v))
{
convert_to_boolean(v);
serv->enable_delay_receive = Z_BVAL_P(v);
}
//task_worker_num
if (php_swoole_array_get_value(vht, "task_worker_num", v))
{
convert_to_long(v);
SwooleG.task_worker_num = (int) Z_LVAL_P(v);
task_callbacks = swHashMap_new(1024, NULL);
}
//task ipc mode, 1,2,3
if (php_swoole_array_get_value(vht, "task_ipc_mode", v))
{
convert_to_long(v);
SwooleG.task_ipc_mode = (int) Z_LVAL_P(v);
}
/**
* Temporary file directory for task_worker
*/
if (php_swoole_array_get_value(vht, "task_tmpdir", v))
{
convert_to_string(v);
if (Z_STRLEN_P(v) > SW_TASK_TMPDIR_SIZE - 30)
{
swoole_php_fatal_error(E_ERROR, "task_tmpdir is too long, max size is %d.", SW_TASK_TMPDIR_SIZE - 1);
return;
}
SwooleG.task_tmpdir = emalloc(SW_TASK_TMPDIR_SIZE);
SwooleG.task_tmpdir_len = snprintf(SwooleG.task_tmpdir, SW_TASK_TMPDIR_SIZE, "%s/swoole.task.XXXXXX", Z_STRVAL_P(v)) + 1;
}
//task_max_request
if (php_swoole_array_get_value(vht, "task_max_request", v))
{
convert_to_long(v);
SwooleG.task_max_request = (int) Z_LVAL_P(v);
}
//max_connection
if (php_swoole_array_get_value(vht, "max_connection", v) || php_swoole_array_get_value(vht, "max_conn", v))
{
convert_to_long(v);
serv->max_connection = (int) Z_LVAL_P(v);
}
//heartbeat_check_interval
if (php_swoole_array_get_value(vht, "heartbeat_check_interval", v))
{
convert_to_long(v);
serv->heartbeat_check_interval = (int) Z_LVAL_P(v);
}
//heartbeat idle time
if (php_swoole_array_get_value(vht, "heartbeat_idle_time", v))
{
convert_to_long(v);
serv->heartbeat_idle_time = (int) Z_LVAL_P(v);
if (serv->heartbeat_check_interval > serv->heartbeat_idle_time)
{
swoole_php_fatal_error(E_WARNING, "heartbeat_idle_time must be greater than heartbeat_check_interval.");
serv->heartbeat_check_interval = serv->heartbeat_idle_time / 2;
}
}
else if (serv->heartbeat_check_interval > 0)
{
serv->heartbeat_idle_time = serv->heartbeat_check_interval * 2;
}
//max_request
if (php_swoole_array_get_value(vht, "max_request", v))
{
convert_to_long(v);
serv->max_request = (int) Z_LVAL_P(v);
}
//cpu affinity
if (php_swoole_array_get_value(vht, "open_cpu_affinity", v))
{
convert_to_boolean(v);
serv->open_cpu_affinity = Z_BVAL_P(v);
}
//cpu affinity set
if (php_swoole_array_get_value(vht, "cpu_affinity_ignore", v))
{
int ignore_num = zend_hash_num_elements(Z_ARRVAL_P(v));
if (ignore_num >= SW_CPU_NUM)
{
swoole_php_fatal_error(E_ERROR, "cpu_affinity_ignore num must be less than cpu num (%d)", SW_CPU_NUM);
RETURN_FALSE;
}
int available_num = SW_CPU_NUM - ignore_num;
int *available_cpu = (int *) sw_malloc(sizeof(int) * available_num);
int flag, i, available_i = 0;
zval *zval_core = NULL;
for (i = 0; i < SW_CPU_NUM; i++)
{
flag = 1;
SW_HASHTABLE_FOREACH_START(Z_ARRVAL_P(v), zval_core)
int core = (int) Z_LVAL_P(zval_core);
if (i == core)
{
flag = 0;
break;
}
SW_HASHTABLE_FOREACH_END();
if (flag)
{
available_cpu[available_i] = i;
available_i++;
}
}
serv->cpu_affinity_available_num = available_num;
serv->cpu_affinity_available = available_cpu;
}
//paser x-www-form-urlencoded form data
if (php_swoole_array_get_value(vht, "http_parse_post", v))
{
convert_to_boolean(v);
serv->http_parse_post = Z_BVAL_P(v);
}
//temporary directory for HTTP uploaded file.
if (php_swoole_array_get_value(vht, "upload_tmp_dir", v))
{
convert_to_string(v);
if (Z_STRLEN_P(v) >= SW_HTTP_UPLOAD_TMPDIR_SIZE - 22)
{
swoole_php_fatal_error(E_ERROR, "option upload_tmp_dir [%s] is too long.", Z_STRVAL_P(v));
RETURN_FALSE;
}
serv->upload_tmp_dir = strndup(Z_STRVAL_P(v), Z_STRLEN_P(v));
}
/**
* buffer input size
*/
if (php_swoole_array_get_value(vht, "buffer_input_size", v))
{
convert_to_long(v);
serv->buffer_input_size = (int) Z_LVAL_P(v);
}
/**
* buffer output size
*/
if (php_swoole_array_get_value(vht, "buffer_output_size", v))
{
convert_to_long(v);
serv->buffer_output_size = (int) Z_LVAL_P(v);
}
/**
* set pipe memory buffer size
*/
if (php_swoole_array_get_value(vht, "pipe_buffer_size", v))
{
convert_to_long(v);
serv->pipe_buffer_size = (int) Z_LVAL_P(v);
}
//message queue key
if (php_swoole_array_get_value(vht, "message_queue_key", v))
{
convert_to_long(v);
serv->message_queue_key = (int) Z_LVAL_P(v);
}
zval *retval = NULL;
zval *port_object = server_port_list.zobjects[0];
sw_zval_add_ref(&port_object);
sw_zval_add_ref(&zset);
sw_zval_add_ref(&zobject);
sw_zend_call_method_with_1_params(&port_object, swoole_server_port_class_entry_ptr, NULL, "set", &retval, zset);
zend_update_property(swoole_server_class_entry_ptr, zobject, ZEND_STRL("setting"), zset TSRMLS_CC);
RETURN_TRUE;
}
PHP_METHOD(swoole_server, on)
{
zval *name;
zval *cb;
if (SwooleGS->start > 0)
{
swoole_php_fatal_error(E_WARNING, "Server is running. Unable to set event callback now.");
RETURN_FALSE;
}
if (zend_parse_parameters(ZEND_NUM_ARGS()TSRMLS_CC, "zz", &name, &cb) == FAILURE)
{
return;
}
#ifdef PHP_SWOOLE_CHECK_CALLBACK
char *func_name = NULL;
if (!sw_zend_is_callable(cb, 0, &func_name TSRMLS_CC))
{
swoole_php_fatal_error(E_ERROR, "Function '%s' is not callable", func_name);
efree(func_name);
return;
}
efree(func_name);
#endif
convert_to_string(name);
char *callback_name[PHP_SERVER_CALLBACK_NUM] = {
"Connect",
"Receive",
"Close",
"Packet",
"Start",
"Shutdown",
"WorkerStart",
"WorkerStop",
"Task",
"Finish",
"WorkerError",
"ManagerStart",
"ManagerStop",
"PipeMessage",
NULL,
NULL,
NULL,
NULL,
"BufferFull",
"BufferEmpty",
};
int i;
char property_name[128];
int l_property_name = 0;
memcpy(property_name, "on", 2);
for (i = 0; i < PHP_SERVER_CALLBACK_NUM; i++)
{
if (callback_name[i] == NULL)
{
continue;
}
if (strncasecmp(callback_name[i], Z_STRVAL_P(name), Z_STRLEN_P(name)) == 0)
{
memcpy(property_name + 2, callback_name[i], Z_STRLEN_P(name));
l_property_name = Z_STRLEN_P(name) + 2;
property_name[l_property_name] = '\0';
zend_update_property(swoole_server_class_entry_ptr, getThis(), property_name, l_property_name, cb TSRMLS_CC);
php_sw_server_callbacks[i] = sw_zend_read_property(swoole_server_class_entry_ptr, getThis(), property_name, l_property_name, 0 TSRMLS_CC);
sw_copy_to_stack(php_sw_server_callbacks[i], _php_sw_server_callbacks[i]);
break;
}
}
if (l_property_name == 0)
{
swoole_php_error(E_WARNING, "Unknown event types[%s]", Z_STRVAL_P(name));
RETURN_FALSE;
}
if (i < SW_SERVER_CB_onStart)
{
zval *port_object = server_port_list.zobjects[0];
zval *retval = NULL;
sw_zval_add_ref(&port_object);
sw_zend_call_method_with_2_params(&port_object, swoole_server_port_class_entry_ptr, NULL, "on", &retval, name, cb);
}
else
{
RETURN_TRUE;
}
}
PHP_METHOD(swoole_server, listen)
{
char *host;
zend_size_t host_len;
long sock_type;
long port;
if (SwooleGS->start > 0)
{
swoole_php_fatal_error(E_WARNING, "Server is running. cannot add listener.");
RETURN_FALSE;
}
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sll", &host, &host_len, &port, &sock_type) == FAILURE)
{
return;
}
swServer *serv = swoole_get_object(getThis());
swListenPort *ls = swServer_add_port(serv, (int) sock_type, host, (int) port);
if (!ls)
{
RETURN_FALSE;
}
zval *port_object = php_swoole_server_add_port(ls TSRMLS_CC);
RETURN_ZVAL(port_object, 1, NULL);
}
PHP_METHOD(swoole_server, addProcess)
{
if (SwooleGS->start > 0)
{
swoole_php_fatal_error(E_WARNING, "Server is running. cannot add process.");
RETURN_FALSE;
}
zval *process = NULL;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &process) == FAILURE)
{
return;
}
if (ZVAL_IS_NULL(process))
{
swoole_php_fatal_error(E_WARNING, "parameter 1 cannot be empty.");
RETURN_FALSE;
}
swServer *serv = swoole_get_object(getThis());
if (!instanceof_function(Z_OBJCE_P(process), swoole_process_class_entry_ptr TSRMLS_CC))
{
swoole_php_fatal_error(E_ERROR, "object is not instanceof swoole_process.");
RETURN_FALSE;
}
if (serv->onUserWorkerStart == NULL)
{
serv->onUserWorkerStart = php_swoole_onUserWorkerStart;
}
#if PHP_MAJOR_VERSION >= 7
zval *tmp_process = emalloc(sizeof(zval));
memcpy(tmp_process, process, sizeof(zval));
process = tmp_process;
#endif
sw_zval_add_ref(&process);
swWorker *worker = swoole_get_object(process);
worker->ptr = process;
int id = swServer_add_worker(serv, worker);
if (id < 0)
{
swoole_php_fatal_error(E_WARNING, "swServer_add_worker failed.");
RETURN_FALSE;
}
zend_update_property_long(swoole_process_class_entry_ptr, getThis(), ZEND_STRL("id"), id TSRMLS_CC);
RETURN_LONG(id);
}
PHP_METHOD(swoole_server, start)
{
zval *zobject = getThis();
int ret;
if (SwooleGS->start > 0)
{
swoole_php_fatal_error(E_WARNING, "Server is running. Unable to execute swoole_server::start.");
RETURN_FALSE;
}
swServer *serv = swoole_get_object(zobject);
php_swoole_register_callback(serv);
if (php_sw_server_callbacks[SW_SERVER_CB_onReceive] == NULL && php_sw_server_callbacks[SW_SERVER_CB_onPacket] == NULL)
{
swoole_php_fatal_error(E_ERROR, "require onReceive/onPacket callback");
RETURN_FALSE;
}
//-------------------------------------------------------------
serv->onReceive = php_swoole_onReceive;
serv->ptr2 = zobject;
sw_zval_add_ref(&zobject);
php_swoole_server_before_start(serv, zobject TSRMLS_CC);
ret = swServer_start(serv);
if (ret < 0)
{
swoole_php_fatal_error(E_ERROR, "start server failed. Error: %s", sw_error);
RETURN_LONG(ret);
}
RETURN_TRUE;
}
PHP_METHOD(swoole_server, send)
{
zval *zobject = getThis();
int ret;
zval *zfd;
zval *zdata;
long server_socket = -1;
if (SwooleGS->start == 0)
{
swoole_php_fatal_error(E_WARNING, "Server is not running.");
RETURN_FALSE;
}
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz|l", &zfd, &zdata, &server_socket) == FAILURE)
{
return;
}
char *data;
int length = php_swoole_get_send_data(zdata, &data TSRMLS_CC);
if (length < 0)
{
RETURN_FALSE;
}
else if (length == 0)
{
swoole_php_fatal_error(E_WARNING, "data is empty.");
RETURN_FALSE;
}
swServer *serv = swoole_get_object(zobject);
if (serv->have_udp_sock && SW_Z_TYPE_P(zfd) == IS_STRING)
{
if (server_socket == -1)
{
server_socket = dgram_server_socket;
}
//UDP IPv6
if (strchr(Z_STRVAL_P(zfd), ':'))
{
php_swoole_udp_t udp_info;
memcpy(&udp_info, &server_socket, sizeof(udp_info));
ret = swSocket_udp_sendto6(udp_info.from_fd, Z_STRVAL_P(zfd), udp_info.port, data, length);
}
//UNIX DGRAM
else if (Z_STRVAL_P(zfd)[0] == '/')
{
struct sockaddr_un addr_un;
memcpy(addr_un.sun_path, Z_STRVAL_P(zfd), Z_STRLEN_P(zfd));
addr_un.sun_family = AF_UNIX;
addr_un.sun_path[Z_STRLEN_P(zfd)] = 0;
ret = swSocket_sendto_blocking(server_socket, data, length, 0, (struct sockaddr *) &addr_un, sizeof(addr_un));
}
else
{
goto convert;
}
SW_CHECK_RETURN(ret);
}
convert: convert_to_long(zfd);
uint32_t fd = (uint32_t) Z_LVAL_P(zfd);
//UDP
if (swServer_is_udp(fd))
{
if (server_socket == -1)
{
server_socket = udp_server_socket;
}
php_swoole_udp_t udp_info;
memcpy(&udp_info, &server_socket, sizeof(udp_info));
struct sockaddr_in addr_in;
addr_in.sin_family = AF_INET;
addr_in.sin_port = htons(udp_info.port);
addr_in.sin_addr.s_addr = fd;
ret = swSocket_sendto_blocking(udp_info.from_fd, data, length, 0, (struct sockaddr *) &addr_in, sizeof(addr_in));
SW_CHECK_RETURN(ret);
}
//TCP
else
{
SW_CHECK_RETURN(swServer_tcp_send(serv, fd, data, length));
}
}
PHP_METHOD(swoole_server, sendto)
{
zval *zobject = getThis();
char *ip;
char *data;
zend_size_t len, ip_len;
long port;
long server_socket = -1;
zend_bool ipv6 = 0;
if (SwooleGS->start == 0)
{
swoole_php_fatal_error(E_WARNING, "Server is not running.");
RETURN_FALSE;
}
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sls|l", &ip, &ip_len, &port, &data, &len, &server_socket) == FAILURE)
{
return;
}
if (len <= 0)
{
swoole_php_fatal_error(E_WARNING, "data is empty.");
RETURN_FALSE;
}
swServer *serv = swoole_get_object(zobject);
if (strchr(ip, ':'))
{
ipv6 = 1;
}
if (ipv6 == 0 && serv->udp_socket_ipv4 <= 0)
{
swoole_php_fatal_error(E_WARNING, "You must add an UDP listener to server before using sendto.");
RETURN_FALSE;
}
else if (ipv6 == 1 && serv->udp_socket_ipv6 <= 0)
{
swoole_php_fatal_error(E_WARNING, "You must add an UDP6 listener to server before using sendto.");
RETURN_FALSE;
}
if (server_socket < 0)
{
server_socket = ipv6 ? serv->udp_socket_ipv6 : serv->udp_socket_ipv4;
}
int ret;
if (ipv6)
{
ret = swSocket_udp_sendto6(server_socket, ip, port, data, len);
}
else
{
ret = swSocket_udp_sendto(server_socket, ip, port, data, len);
}
SW_CHECK_RETURN(ret);
}
PHP_METHOD(swoole_server, sendfile)
{
zval *zobject = getThis();
zend_size_t len;
char *filename;
long fd;
long offset = 0;
if (SwooleGS->start == 0)
{
swoole_php_fatal_error(E_WARNING, "Server is not running.");
RETURN_FALSE;
}
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ls|l", &fd, &filename, &len, &offset) == FAILURE)
{
return;
}
//check fd
if (fd <= 0 || fd > SW_MAX_SOCKET_ID)
{
swoole_error_log(SW_LOG_WARNING, SW_ERROR_SESSION_INVALID_ID, "invalid fd[%ld].", fd);
RETURN_FALSE;
}
struct stat file_stat;
if (stat(filename, &file_stat) < 0)
{
swoole_php_sys_error(E_WARNING, "stat(%s) failed.", filename);
RETURN_FALSE;
}
if (file_stat.st_size <= offset)
{
swoole_php_error(E_WARNING, "file[offset=%ld] is empty.", offset);
RETURN_FALSE;
}
swServer *serv = swoole_get_object(zobject);
SW_CHECK_RETURN(swServer_tcp_sendfile(serv, (int) fd, filename, len, offset));
}
PHP_METHOD(swoole_server, close)
{
zval *zobject = getThis();
zend_bool reset = SW_FALSE;
long fd;
if (SwooleGS->start == 0)
{
swoole_php_fatal_error(E_WARNING, "Server is not running.");
RETURN_FALSE;
}
if (swIsMaster())
{
swoole_php_fatal_error(E_WARNING, "Cannot close connection in master process.");
RETURN_FALSE;
}
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|b", &fd, &reset) == FAILURE)
{
return;
}
swServer *serv = swoole_get_object(zobject);
SW_CHECK_RETURN(serv->close(serv, (int )fd, (int )reset));
}
PHP_METHOD(swoole_server, confirm)
{
zval *zobject = getThis();
long fd;
if (SwooleGS->start == 0)
{
swoole_php_fatal_error(E_WARNING, "Server is not running.");
RETURN_FALSE;
}
if (swIsMaster())
{
swoole_php_fatal_error(E_WARNING, "Cannot confirm connection in master process.");
RETURN_FALSE;
}
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &fd) == FAILURE)
{
return;
}
swServer *serv = swoole_get_object(zobject);
SW_CHECK_RETURN(swServer_confirm(serv, fd));
}
PHP_METHOD(swoole_server, pause)
{
zval *zobject = getThis();
long fd;
if (SwooleGS->start == 0)
{
swoole_php_fatal_error(E_WARNING, "Server is not running.");
RETURN_FALSE;
}
swServer *serv = swoole_get_object(zobject);
if (serv->factory_mode != SW_MODE_SINGLE || swIsTaskWorker())
{
swoole_php_fatal_error(E_WARNING, "cannot pause method.");
RETURN_FALSE;
}
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|b", &fd) == FAILURE)
{
return;
}
swConnection *conn = swServer_connection_verify(serv, fd);
if (!conn || conn->removed)
{
RETURN_FALSE;
}
int ret;
if (conn->events & SW_EVENT_WRITE)
{
ret = SwooleG.main_reactor->set(SwooleG.main_reactor, conn->fd, conn->fdtype | SW_EVENT_WRITE);
}
else
{
ret = SwooleG.main_reactor->del(SwooleG.main_reactor, conn->fd);
}
SW_CHECK_RETURN(ret);
}
PHP_METHOD(swoole_server, resume)
{
zval *zobject = getThis();
long fd;
if (SwooleGS->start == 0)
{
swoole_php_fatal_error(E_WARNING, "Server is not running.");
RETURN_FALSE;
}
swServer *serv = swoole_get_object(zobject);
if (serv->factory_mode != SW_MODE_SINGLE || swIsTaskWorker())
{
swoole_php_fatal_error(E_WARNING, "cannot resume method.");
RETURN_FALSE;
}
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &fd) == FAILURE)
{
return;
}
swConnection *conn = swServer_connection_verify(serv, fd);
if (!conn || !conn->removed)
{
RETURN_FALSE;
}
int ret;
if (conn->events & SW_EVENT_WRITE)
{
ret = SwooleG.main_reactor->set(SwooleG.main_reactor, conn->fd, conn->fdtype | SW_EVENT_READ | SW_EVENT_WRITE);
}
else
{
ret = SwooleG.main_reactor->add(SwooleG.main_reactor, conn->fd, conn->fdtype | SW_EVENT_READ);
}
SW_CHECK_RETURN(ret);
}
PHP_METHOD(swoole_server, stats)
{
if (SwooleGS->start == 0)
{
swoole_php_fatal_error(E_WARNING, "Server is not running.");
RETURN_FALSE;
}
array_init(return_value);
sw_add_assoc_long_ex(return_value, ZEND_STRS("start_time"), SwooleStats->start_time);
sw_add_assoc_long_ex(return_value, ZEND_STRS("connection_num"), SwooleStats->connection_num);
sw_add_assoc_long_ex(return_value, ZEND_STRS("accept_count"), SwooleStats->accept_count);
sw_add_assoc_long_ex(return_value, ZEND_STRS("close_count"), SwooleStats->close_count);
sw_add_assoc_long_ex(return_value, ZEND_STRS("tasking_num"), SwooleStats->tasking_num);
sw_add_assoc_long_ex(return_value, ZEND_STRS("request_count"), SwooleStats->request_count);
sw_add_assoc_long_ex(return_value, ZEND_STRS("worker_request_count"), SwooleWG.request_count);
if (SwooleG.task_ipc_mode > SW_TASK_IPC_UNIXSOCK && SwooleGS->task_workers.queue)
{
int queue_num = -1;
int queue_bytes = -1;
if (swMsgQueue_stat(SwooleGS->task_workers.queue, &queue_num, &queue_bytes) == 0)
{
sw_add_assoc_long_ex(return_value, ZEND_STRS("task_queue_num"), queue_num);
sw_add_assoc_long_ex(return_value, ZEND_STRS("task_queue_bytes"), queue_bytes);
}
}
}
PHP_METHOD(swoole_server, reload)
{
zend_bool only_reload_taskworker = 0;
if (SwooleGS->start == 0)
{
swoole_php_fatal_error(E_WARNING, "Server is not running.");
RETURN_FALSE;
}
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &only_reload_taskworker) == FAILURE)
{
return;
}
int sig = only_reload_taskworker ? SIGUSR2 : SIGUSR1;
if (kill(SwooleGS->manager_pid, sig) < 0)
{
swoole_php_fatal_error(E_WARNING, "kill() failed. Error: %s[%d]", strerror(errno), errno);
RETURN_FALSE;
}
RETURN_TRUE;
}
PHP_METHOD(swoole_server, heartbeat)
{
zval *zobject = getThis();
zend_bool close_connection = 0;
if (SwooleGS->start == 0)
{
swoole_php_fatal_error(E_WARNING, "Server is not running.");
RETURN_FALSE;
}
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &close_connection) == FAILURE)
{
return;
}
swServer *serv = swoole_get_object(zobject);
if (serv->heartbeat_idle_time < 1)
{
RETURN_FALSE;
}
int serv_max_fd = swServer_get_maxfd(serv);
int serv_min_fd = swServer_get_minfd(serv);
array_init(return_value);
int fd;
int checktime = (int) SwooleGS->now - serv->heartbeat_idle_time;
swConnection *conn;
for (fd = serv_min_fd; fd <= serv_max_fd; fd++)
{
swTrace("heartbeat check fd=%d", fd);
conn = &serv->connection_list[fd];
if (1 == conn->active && conn->last_time < checktime)
{
conn->close_force = 1;
/**
* Close the connection
*/
if (close_connection)
{
serv->factory.end(&serv->factory, fd);
}
#ifdef SW_REACTOR_USE_SESSION
add_next_index_long(return_value, conn->session_id);
#else
add_next_index_long(return_value, fd);
#endif
}
}
}
PHP_METHOD(swoole_server, taskwait)
{
swEventData buf;
zval *data;
double timeout = SW_TASKWAIT_TIMEOUT;
long dst_worker_id = -1;
if (SwooleGS->start == 0)
{
swoole_php_fatal_error(E_WARNING, "server is not running.");
RETURN_FALSE;
}
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|dl", &data, &timeout, &dst_worker_id) == FAILURE)
{
return;
}
if (php_swoole_check_task_param(dst_worker_id TSRMLS_CC) < 0)
{
RETURN_FALSE;
}
php_swoole_task_pack(&buf, data TSRMLS_CC);
uint64_t notify;
swEventData *task_result = &(SwooleG.task_result[SwooleWG.id]);
bzero(task_result, sizeof(swEventData));
swPipe *task_notify_pipe = &SwooleG.task_notify[SwooleWG.id];
int efd = task_notify_pipe->getFd(task_notify_pipe, 0);
//clear history task
while (read(efd, &notify, sizeof(notify)) > 0);
sw_atomic_fetch_add(&SwooleStats->tasking_num, 1);
if (swProcessPool_dispatch_blocking(&SwooleGS->task_workers, &buf, (int*) &dst_worker_id) >= 0)
{
task_notify_pipe->timeout = timeout;
int ret = task_notify_pipe->read(task_notify_pipe, &notify, sizeof(notify));
if (ret > 0)
{
zval *task_notify_data = php_swoole_task_unpack(task_result TSRMLS_CC);
RETURN_ZVAL(task_notify_data, 0, 0);
}
else
{
swoole_php_fatal_error(E_WARNING, "taskwait failed. Error: %s[%d]", strerror(errno), errno);
}
}
else
{
sw_atomic_fetch_sub(&SwooleStats->tasking_num, 1);
}
RETURN_FALSE;
}
PHP_METHOD(swoole_server, taskWaitMulti)
{
swEventData buf;
zval *tasks;
zval *task;
double timeout = SW_TASKWAIT_TIMEOUT;
if (SwooleGS->start == 0)
{
swoole_php_fatal_error(E_WARNING, "server is not running.");
RETURN_FALSE;
}
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|d", &tasks, &timeout) == FAILURE)
{
return;
}
swServer *serv = swoole_get_object(getThis());
array_init(return_value);
int dst_worker_id;
int task_id;
int i = 0;
int n_task = Z_ARRVAL_P(tasks)->nNumOfElements;
int list_of_id[1024];
uint64_t notify;
swEventData *task_result = &(SwooleG.task_result[SwooleWG.id]);
bzero(task_result, sizeof(swEventData));
swPipe *task_notify_pipe = &SwooleG.task_notify[SwooleWG.id];
swWorker *worker = swServer_get_worker(serv, SwooleWG.id);
char _tmpfile[sizeof(SW_TASK_TMP_FILE)] = SW_TASK_TMP_FILE;
int _tmpfile_fd = swoole_tmpfile(_tmpfile);
if (_tmpfile_fd < 0)
{
RETURN_FALSE;
}
close(_tmpfile_fd);
int *finish_count = (int *) task_result->data;
worker->lock.lock(&worker->lock);
*finish_count = 0;
memcpy(task_result->data + 4, _tmpfile, sizeof(_tmpfile));
worker->lock.unlock(&worker->lock);
//clear history task
int efd = task_notify_pipe->getFd(task_notify_pipe, 0);
while (read(efd, &notify, sizeof(notify)) > 0);
SW_HASHTABLE_FOREACH_START(Z_ARRVAL_P(tasks), task)
task_id = php_swoole_task_pack(&buf, task TSRMLS_CC);
if (task_id < 0)
{
swoole_php_fatal_error(E_WARNING, "task pack failed.");
goto fail;
}
swTask_type(&buf) |= SW_TASK_WAITALL;
dst_worker_id = -1;
sw_atomic_fetch_add(&SwooleStats->tasking_num, 1);
if (swProcessPool_dispatch_blocking(&SwooleGS->task_workers, &buf, (int*) &dst_worker_id) >= 0)
{
list_of_id[i] = task_id;
}
else
{
sw_atomic_fetch_sub(&SwooleStats->tasking_num, 1);
swoole_php_fatal_error(E_WARNING, "taskwait failed. Error: %s[%d]", strerror(errno), errno);
fail:
add_index_bool(return_value, i, 0);
n_task --;
}
i++;
SW_HASHTABLE_FOREACH_END();
double _now = swoole_microtime();
while (n_task > 0)
{
task_notify_pipe->timeout = timeout;
int ret = task_notify_pipe->read(task_notify_pipe, &notify, sizeof(notify));
if (ret > 0 && *finish_count < n_task)
{
if (swoole_microtime() - _now < timeout)
{
continue;
}
}
break;
}
worker->lock.lock(&worker->lock);
swString *content = swoole_file_get_contents(_tmpfile);
worker->lock.unlock(&worker->lock);
if (content == NULL)
{
RETURN_FALSE;
}
swEventData *result;
zval *zdata;
int j;
do
{
result = (swEventData *) (content->str + content->offset);
task_id = result->info.fd;
zdata = php_swoole_task_unpack(result TSRMLS_CC);
for (j = 0; j < n_task; j++)
{
if (list_of_id[j] == task_id)
{
break;
}
}
add_index_zval(return_value, j, zdata);
content->offset += sizeof(swDataHead) + result->info.len;
}
while(content->offset < content->length);
//free memory
swString_free(content);
//delete tmp file
unlink(_tmpfile);
}
PHP_METHOD(swoole_server, task)
{
swEventData buf;
zval *data;
zval *callback = NULL;
long dst_worker_id = -1;
if (SwooleGS->start == 0)
{
swoole_php_fatal_error(E_WARNING, "Server is not running.");
RETURN_FALSE;
}
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|lz", &data, &dst_worker_id, &callback) == FAILURE)
{
return;
}
if (php_swoole_check_task_param(dst_worker_id TSRMLS_CC) < 0)
{
RETURN_FALSE;
}
if (php_swoole_task_pack(&buf, data TSRMLS_CC) < 0)
{
RETURN_FALSE;
}
if (callback && !ZVAL_IS_NULL(callback))
{
#ifdef PHP_SWOOLE_CHECK_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;
}
efree(func_name);
#endif
swTask_type(&buf) |= SW_TASK_CALLBACK;
sw_zval_add_ref(&callback);
swHashMap_add_int(task_callbacks, buf.info.fd, sw_zval_dup(callback));
}
swTask_type(&buf) |= SW_TASK_NONBLOCK;
sw_atomic_fetch_add(&SwooleStats->tasking_num, 1);
if (swProcessPool_dispatch(&SwooleGS->task_workers, &buf, (int*) &dst_worker_id) >= 0)
{
RETURN_LONG(buf.info.fd);
}
else
{
sw_atomic_fetch_sub(&SwooleStats->tasking_num, 1);
RETURN_FALSE;
}
}
PHP_METHOD(swoole_server, sendMessage)
{
zval *zobject = getThis();
swEventData buf;
zval *message;
long worker_id = -1;
if (SwooleGS->start == 0)
{
swoole_php_fatal_error(E_WARNING, "Server is not running.");
RETURN_FALSE;
}
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zl", &message, &worker_id) == FAILURE)
{
return;
}
if (worker_id == SwooleWG.id)
{
swoole_php_fatal_error(E_WARNING, "cannot send message to self.");
RETURN_FALSE;
}
swServer *serv = swoole_get_object(zobject);
if (worker_id >= serv->worker_num + SwooleG.task_worker_num)
{
swoole_php_fatal_error(E_WARNING, "worker_id[%d] is invalid.", (int) worker_id);
RETURN_FALSE;
}
if (!serv->onPipeMessage)
{
swoole_php_fatal_error(E_WARNING, "onPipeMessage is null, cannot use sendMessage.");
RETURN_FALSE;
}
if (php_swoole_task_pack(&buf, message TSRMLS_CC) < 0)
{
RETURN_FALSE;
}
buf.info.type = SW_EVENT_PIPE_MESSAGE;
buf.info.from_id = SwooleWG.id;
swWorker *to_worker = swServer_get_worker(serv, worker_id);
SW_CHECK_RETURN(swWorker_send2worker(to_worker, &buf, sizeof(buf.info) + buf.info.len, SW_PIPE_MASTER | SW_PIPE_NONBLOCK));
}
PHP_METHOD(swoole_server, finish)
{
zval *zobject = getThis();
zval *data;
if (SwooleGS->start == 0)
{
swoole_php_fatal_error(E_WARNING, "Server is not running.");
RETURN_FALSE;
}
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &data) == FAILURE)
{
return;
}
swServer *serv = swoole_get_object(zobject);
SW_CHECK_RETURN(php_swoole_task_finish(serv, data TSRMLS_CC));
}
PHP_METHOD(swoole_server, bind)
{
zval *zobject = getThis();
long fd = 0;
long uid = 0;
if (SwooleGS->start == 0)
{
swoole_php_fatal_error(E_WARNING, "Server is not running.");
RETURN_FALSE;
}
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll", &fd, &uid) == FAILURE)
{
return;
}
swServer *serv = swoole_get_object(zobject);
swConnection *conn = swWorker_get_connection(serv, fd);
//udp client
if (conn == NULL)
{
swTrace("%ld conn error", fd);
RETURN_FALSE;
}
//connection is closed
if (conn->active == 0)
{
swTrace("fd:%ld a:%d, uid: %ld", fd, conn->active, conn->uid);
RETURN_FALSE;
}
if (conn->uid != 0)
{
RETURN_FALSE;
}
int ret = 0;
SwooleGS->lock.lock(&SwooleGS->lock);
if (conn->uid == 0)
{
conn->uid = (uint32_t) uid;
ret = 1;
}
SwooleGS->lock.unlock(&SwooleGS->lock);
SW_CHECK_RETURN(ret);
}
#ifdef SWOOLE_SOCKETS_SUPPORT
PHP_METHOD(swoole_server, getSocket)
{
long port = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &port) == FAILURE)
{
return;
}
zval *zobject = getThis();
swServer *serv = swoole_get_object(zobject);
int sock = swServer_get_socket(serv, port);
php_socket *socket_object = swoole_convert_to_socket(sock);
if (!socket_object)
{
RETURN_FALSE;
}
SW_ZEND_REGISTER_RESOURCE(return_value, (void *) socket_object, php_sockets_le_socket());
zval *zsocket = sw_zval_dup(return_value);
sw_zval_add_ref(&zsocket);
}
#endif
PHP_METHOD(swoole_server, connection_info)
{
zval *zobject = getThis();
zend_bool noCheckConnection = 0;
zval *zfd;
long from_id = -1;
if (SwooleGS->start == 0)
{
swoole_php_fatal_error(E_WARNING, "Server is not running.");
RETURN_FALSE;
}
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|lb", &zfd, &from_id, &noCheckConnection) == FAILURE)
{
return;
}
swServer *serv = swoole_get_object(zobject);
long fd = 0;
zend_bool ipv6_udp = 0;
//ipv6 udp
if (SW_Z_TYPE_P(zfd) == IS_STRING)
{
if (is_numeric_string(Z_STRVAL_P(zfd), Z_STRLEN_P(zfd), &fd, NULL, 0))
{
ipv6_udp = 0;
}
else
{
fd = 0;
ipv6_udp = 1;
}
}
else
{
convert_to_long(zfd);
fd = Z_LVAL_P(zfd);
}
//udp
if (ipv6_udp || swServer_is_udp(fd))
{
array_init(return_value);
swoole_php_error(E_DEPRECATED, "The udp connection_info is deprecated, use onPacket instead.");
if (ipv6_udp)
{
add_assoc_zval(return_value, "remote_ip", zfd);
}
else
{
struct in_addr sin_addr;
sin_addr.s_addr = fd;
sw_add_assoc_string(return_value, "remote_ip", inet_ntoa(sin_addr), 1);
}
if (from_id == 0)
{
return;
}
php_swoole_udp_t udp_info;
memcpy(&udp_info, &from_id, sizeof(udp_info));
//server socket
swConnection *from_sock = swServer_connection_get(serv, udp_info.from_fd);
if (from_sock)
{
add_assoc_long(return_value, "server_fd", from_sock->fd);
add_assoc_long(return_value, "socket_type", from_sock->socket_type);
add_assoc_long(return_value, "server_port", swConnection_get_port(from_sock));
}
add_assoc_long(return_value, "remote_port", udp_info.port);
return;
}
swConnection *conn = swServer_connection_verify(serv, fd);
if (!conn)
{
RETURN_FALSE;
}
//connection is closed
if (conn->active == 0 && !noCheckConnection)
{
RETURN_FALSE;
}
else
{
array_init(return_value);
if (serv->dispatch_mode == SW_DISPATCH_UIDMOD)
{
add_assoc_long(return_value, "uid", conn->uid);
}
swListenPort *port = swServer_get_port(serv, conn->fd);
if (port && port->open_websocket_protocol)
{
add_assoc_long(return_value, "websocket_status", conn->websocket_status);
}
#ifdef SW_USE_OPENSSL
if (conn->ssl_client_cert.length > 0)
{
sw_add_assoc_stringl(return_value, "ssl_client_cert", conn->ssl_client_cert.str, conn->ssl_client_cert.length - 1, 1);
}
#endif
//server socket
swConnection *from_sock = swServer_connection_get(serv, conn->from_fd);
if (from_sock)
{
add_assoc_long(return_value, "server_port", swConnection_get_port(from_sock));
}
add_assoc_long(return_value, "server_fd", conn->from_fd);
add_assoc_long(return_value, "socket_type", conn->socket_type);
add_assoc_long(return_value, "remote_port", swConnection_get_port(conn));
sw_add_assoc_string(return_value, "remote_ip", swConnection_get_ip(conn), 1);
add_assoc_long(return_value, "from_id", conn->from_id);
add_assoc_long(return_value, "connect_time", conn->connect_time);
add_assoc_long(return_value, "last_time", conn->last_time);
add_assoc_long(return_value, "close_errno", conn->close_errno);
}
}
PHP_METHOD(swoole_server, connection_list)
{
zval *zobject = getThis();
long start_fd = 0;
long find_count = 10;
if (SwooleGS->start == 0)
{
swoole_php_fatal_error(E_WARNING, "Server is not running.");
RETURN_FALSE;
}
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|ll", &start_fd, &find_count) == FAILURE)
{
return;
}
swServer *serv = swoole_get_object(zobject);
//超过最大查找数量
if (find_count > SW_MAX_FIND_COUNT)
{
swoole_php_fatal_error(E_WARNING, "swoole_connection_list max_find_count=%d", SW_MAX_FIND_COUNT);
RETURN_FALSE;
}
//复制出来避免被其他进程改写
int serv_max_fd = swServer_get_maxfd(serv);
if (start_fd == 0)
{
start_fd = swServer_get_minfd(serv);
}
#ifdef SW_REACTOR_USE_SESSION
else
{
swConnection *conn = swWorker_get_connection(serv, start_fd);
if (!conn)
{
RETURN_FALSE;
}
start_fd = conn->fd;
}
#endif
//达到最大,表示已经取完了
if ((int) start_fd >= serv_max_fd)
{
RETURN_FALSE;
}
array_init(return_value);
int fd = start_fd + 1;
swConnection *conn;
for (; fd <= serv_max_fd; fd++)
{
swTrace("maxfd=%d, fd=%d, find_count=%ld, start_fd=%ld", serv_max_fd, fd, find_count, start_fd);
conn = &serv->connection_list[fd];
if (conn->active && !conn->closed)
{
#ifdef SW_USE_OPENSSL
if (conn->ssl && conn->ssl_state != SW_SSL_STATE_READY)
{
continue;
}
#endif
#ifdef SW_REACTOR_USE_SESSION
add_next_index_long(return_value, conn->session_id);
#else
add_next_index_long(return_value, fd);
#endif
find_count--;
}
//finish fetch
if (find_count <= 0)
{
break;
}
}
}
PHP_METHOD(swoole_server, sendwait)
{
zval *zobject = getThis();
long fd;
zval *zdata;
if (SwooleGS->start == 0)
{
swoole_php_fatal_error(E_WARNING, "Server is not running.");
RETURN_FALSE;
}
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lz", &fd, &zdata) == FAILURE)
{
return;
}
char *data;
int length = php_swoole_get_send_data(zdata, &data TSRMLS_CC);
if (length < 0)
{
RETURN_FALSE;
}
else if (length == 0)
{
swoole_php_fatal_error(E_WARNING, "data is empty.");
RETURN_FALSE;
}
swServer *serv = swoole_get_object(zobject);
if (serv->factory_mode != SW_MODE_SINGLE || swIsTaskWorker())
{
swoole_php_fatal_error(E_WARNING, "cannot sendwait.");
RETURN_FALSE;
}
//UDP
if (swServer_is_udp(fd))
{
swoole_php_fatal_error(E_WARNING, "cannot sendwait.");
RETURN_FALSE;
}
//TCP
else
{
SW_CHECK_RETURN(swServer_tcp_sendwait(serv, fd, data, length));
}
}
PHP_METHOD(swoole_server, exist)
{
zval *zobject = getThis();
long fd;
if (SwooleGS->start == 0)
{
swoole_php_fatal_error(E_WARNING, "Server is not running.");
RETURN_FALSE;
}
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &fd) == FAILURE)
{
return;
}
swServer *serv = swoole_get_object(zobject);
swConnection *conn = swWorker_get_connection(serv, fd);
if (!conn)
{
RETURN_FALSE;
}
//connection is closed
if (conn->active == 0 || conn->closed)
{
RETURN_FALSE;
}
else
{
RETURN_TRUE;
}
}
PHP_METHOD(swoole_server, protect)
{
long fd;
zend_bool value = 1;
if (SwooleGS->start == 0)
{
swoole_php_fatal_error(E_WARNING, "Server is not running.");
RETURN_FALSE;
}
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|b", &fd, &value) == FAILURE)
{
return;
}
swServer *serv = swoole_get_object(getThis());
swConnection *conn = swWorker_get_connection(serv, fd);
if (!conn)
{
RETURN_FALSE;
}
//connection is closed
if (conn->active == 0 || conn->closed)
{
RETURN_FALSE;
}
else
{
conn->protect = value;
RETURN_TRUE;
}
}
PHP_METHOD(swoole_server, shutdown)
{
if (SwooleGS->start == 0)
{
swoole_php_fatal_error(E_WARNING, "Server is not running.");
RETURN_FALSE;
}
if (kill(SwooleGS->master_pid, SIGTERM) < 0)
{
swoole_php_sys_error(E_WARNING, "shutdown failed. kill(%d, SIGTERM) failed.", SwooleGS->master_pid);
RETURN_FALSE;
}
else
{
RETURN_TRUE;
}
}
PHP_METHOD(swoole_server, stop)
{
if (SwooleGS->start == 0)
{
swoole_php_fatal_error(E_WARNING, "Server is not running.");
RETURN_FALSE;
}
long worker_id = SwooleWG.id;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &worker_id) == FAILURE)
{
return;
}
if (worker_id == SwooleWG.id)
{
SwooleG.main_reactor->running = 0;
SwooleG.running = 0;
}
else
{
swWorker *worker = swServer_get_worker(SwooleG.serv, worker_id);
if (worker == NULL)
{
RETURN_FALSE;
}
else if (kill(worker->pid, SIGTERM) < 0)
{
swoole_php_sys_error(E_WARNING, "kill(%d, SIGTERM) failed.", worker->pid);
RETURN_FALSE;
}
}
RETURN_TRUE;
}
#ifdef HAVE_PCRE
static struct
{
int current_fd;
int max_fd;
uint32_t session_id;
int end;
int index;
} server_itearator;
PHP_METHOD(swoole_connection_iterator, rewind)
{
bzero(&server_itearator, sizeof(server_itearator));
server_itearator.current_fd = swServer_get_minfd(SwooleG.serv);
}
PHP_METHOD(swoole_connection_iterator, valid)
{
int fd = server_itearator.current_fd;
swConnection *conn;
int max_fd = swServer_get_maxfd(SwooleG.serv);
for (; fd <= max_fd; fd++)
{
conn = &SwooleG.serv->connection_list[fd];
if (conn->active && !conn->closed)
{
#ifdef SW_USE_OPENSSL
if (conn->ssl && conn->ssl_state != SW_SSL_STATE_READY)
{
continue;
}
#endif
server_itearator.session_id = conn->session_id;
server_itearator.current_fd = fd;
server_itearator.index++;
RETURN_TRUE;
}
}
RETURN_FALSE;
}
PHP_METHOD(swoole_connection_iterator, current)
{
RETURN_LONG(server_itearator.session_id);
}
PHP_METHOD(swoole_connection_iterator, next)
{
server_itearator.current_fd ++;
}
PHP_METHOD(swoole_connection_iterator, key)
{
RETURN_LONG(server_itearator.index);
}
PHP_METHOD(swoole_connection_iterator, count)
{
RETURN_LONG(SwooleStats->connection_num);
}
PHP_METHOD(swoole_connection_iterator, offsetExists)
{
zval *zobject = (zval *) SwooleG.serv->ptr2;
zval *retval = NULL;
zval *zfd;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zfd) == FAILURE)
{
return;
}
sw_zend_call_method_with_1_params(&zobject, swoole_server_class_entry_ptr, NULL, "exist", &retval, zfd);
if (retval)
{
RETVAL_BOOL(Z_BVAL_P(retval));
sw_zval_ptr_dtor(&retval);
}
}
PHP_METHOD(swoole_connection_iterator, offsetGet)
{
zval *zobject = (zval *) SwooleG.serv->ptr2;
zval *retval = NULL;
zval *zfd;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zfd) == FAILURE)
{
return;
}
sw_zend_call_method_with_1_params(&zobject, swoole_server_class_entry_ptr, NULL, "connection_info", &retval, zfd);
if (retval)
{
RETVAL_ZVAL(retval, 0, 0);
}
}
PHP_METHOD(swoole_connection_iterator, offsetSet)
{
return;
}
PHP_METHOD(swoole_connection_iterator, offsetUnset)
{
return;
}
#endif
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/null_026_8798/swoole.git
git@gitee.com:null_026_8798/swoole.git
null_026_8798
swoole
swoole-src
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385