1 Star 0 Fork 347

YLMZ/swoole-src

forked from swoole/swoole-src 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
swoole_client.cc 65.70 KB
一键复制 编辑 原始数据 按行查看 历史
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277
/*
+----------------------------------------------------------------------+
| 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_cxx.h"
#include "socks5.h"
#include "mqtt.h"
#include <string>
#include <queue>
#include <unordered_map>
using namespace std;
#include "ext/standard/basic_functions.h"
typedef struct
{
zend_fcall_info_cache cache_onConnect;
zend_fcall_info_cache cache_onReceive;
zend_fcall_info_cache cache_onClose;
zend_fcall_info_cache cache_onError;
zend_fcall_info_cache cache_onBufferFull;
zend_fcall_info_cache cache_onBufferEmpty;
#ifdef SW_USE_OPENSSL
zend_fcall_info_cache cache_onSSLReady;
#endif
zval _object;
} client_callback;
enum client_property
{
client_property_callback = 0,
client_property_coroutine = 1,
client_property_socket = 2,
};
static PHP_METHOD(swoole_client, __construct);
static PHP_METHOD(swoole_client, __destruct);
static PHP_METHOD(swoole_client, set);
static PHP_METHOD(swoole_client, connect);
static PHP_METHOD(swoole_client, recv);
static PHP_METHOD(swoole_client, send);
static PHP_METHOD(swoole_client, pipe);
static PHP_METHOD(swoole_client, sendfile);
static PHP_METHOD(swoole_client, sendto);
static PHP_METHOD(swoole_client, sleep);
static PHP_METHOD(swoole_client, wakeup);
#ifdef SW_USE_OPENSSL
static PHP_METHOD(swoole_client, enableSSL);
static PHP_METHOD(swoole_client, getPeerCert);
static PHP_METHOD(swoole_client, verifyPeerCert);
#endif
static PHP_METHOD(swoole_client, isConnected);
static PHP_METHOD(swoole_client, getsockname);
static PHP_METHOD(swoole_client, getpeername);
static PHP_METHOD(swoole_client, close);
static PHP_METHOD(swoole_client, shutdown);
static PHP_METHOD(swoole_client, on);
#ifdef SWOOLE_SOCKETS_SUPPORT
static PHP_METHOD(swoole_client, getSocket);
#endif
#ifdef PHP_SWOOLE_CLIENT_USE_POLL
static int client_poll_add(zval *sock_array, int index, struct pollfd *fds, int maxevents, int event);
static int client_poll_wait(zval *sock_array, struct pollfd *fds, int maxevents, int n_event, int revent);
#else
static int client_select_add(zval *sock_array, fd_set *fds, int *max_fd);
static int client_select_wait(zval *sock_array, fd_set *fds);
#endif
static void client_onConnect(swClient *cli);
static void client_onReceive(swClient *cli, char *data, uint32_t length);
static void client_onClose(swClient *cli);
static void client_onError(swClient *cli);
static void client_onBufferFull(swClient *cli);
static void client_onBufferEmpty(swClient *cli);
static sw_inline void client_execute_callback(zval *zobject, enum php_swoole_client_callback_type type)
{
zval _retval, *retval = &_retval;
zval args[1];
client_callback *cb = (client_callback *) swoole_get_property(zobject, client_property_callback);
const char *callback_name;
zend_fcall_info_cache *fci_cache;
switch(type)
{
case SW_CLIENT_CB_onConnect:
callback_name = "onConnect";
fci_cache = &cb->cache_onConnect;
break;
case SW_CLIENT_CB_onError:
callback_name = "onError";
fci_cache = &cb->cache_onError;
break;
case SW_CLIENT_CB_onClose:
callback_name = "onClose";
fci_cache = &cb->cache_onClose;
break;
case SW_CLIENT_CB_onBufferFull:
callback_name = "onBufferFull";
fci_cache = &cb->cache_onBufferFull;
break;
case SW_CLIENT_CB_onBufferEmpty:
callback_name = "onBufferEmpty";
fci_cache = &cb->cache_onBufferEmpty;
break;
#ifdef SW_USE_OPENSSL
case SW_CLIENT_CB_onSSLReady:
callback_name = "onSSLReady";
fci_cache = &cb->cache_onSSLReady;
break;
#endif
default:
return;
}
if (!fci_cache)
{
swoole_php_fatal_error(E_WARNING, "object have not %s callback", callback_name);
return;
}
args[0] = *zobject;
if (sw_call_user_function_fast_ex(NULL, fci_cache, retval, 1, args) == FAILURE)
{
swoole_php_fatal_error(E_WARNING, "%s handler error", callback_name);
return;
}
if (UNEXPECTED(EG(exception)))
{
zend_exception_error(EG(exception), E_ERROR);
}
if (retval)
{
zval_ptr_dtor(retval);
}
}
static sw_inline swClient* client_get_ptr(zval *zobject)
{
swClient *cli = (swClient *) swoole_get_object(zobject);
if (cli && cli->socket && cli->socket->active == 1)
{
return cli;
}
else
{
SwooleG.error = SW_ERROR_CLIENT_NO_CONNECTION;
zend_update_property_long(swoole_client_ce, zobject, ZEND_STRL("errCode"), SwooleG.error);
swoole_php_error(E_WARNING, "client is not connected to server");
return NULL;
}
}
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_client_void, 0, 0, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_client_construct, 0, 0, 1)
ZEND_ARG_INFO(0, type)
ZEND_ARG_INFO(0, async)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_client_set, 0, 0, 1)
ZEND_ARG_ARRAY_INFO(0, settings, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_client_connect, 0, 0, 1)
ZEND_ARG_INFO(0, host)
ZEND_ARG_INFO(0, port)
ZEND_ARG_INFO(0, timeout)
ZEND_ARG_INFO(0, sock_flag)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_client_recv, 0, 0, 0)
ZEND_ARG_INFO(0, size)
ZEND_ARG_INFO(0, flag)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_client_send, 0, 0, 1)
ZEND_ARG_INFO(0, data)
ZEND_ARG_INFO(0, flag)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_client_pipe, 0, 0, 1)
ZEND_ARG_INFO(0, dst_socket)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_client_sendfile, 0, 0, 1)
ZEND_ARG_INFO(0, filename)
ZEND_ARG_INFO(0, offset)
ZEND_ARG_INFO(0, length)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_client_sendto, 0, 0, 3)
ZEND_ARG_INFO(0, ip)
ZEND_ARG_INFO(0, port)
ZEND_ARG_INFO(0, data)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_client_close, 0, 0, 0)
ZEND_ARG_INFO(0, force)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_client_shutdown, 0, 0, 1)
ZEND_ARG_INFO(0, how)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_client_on, 0, 0, 2)
ZEND_ARG_INFO(0, event_name)
ZEND_ARG_CALLABLE_INFO(0, callback, 0)
ZEND_END_ARG_INFO()
#ifdef SW_USE_OPENSSL
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_client_enableSSL, 0, 0, 0)
ZEND_ARG_CALLABLE_INFO(0, callback, 0)
ZEND_END_ARG_INFO()
#endif
static const zend_function_entry swoole_client_methods[] =
{
PHP_ME(swoole_client, __construct, arginfo_swoole_client_construct, ZEND_ACC_PUBLIC)
PHP_ME(swoole_client, __destruct, arginfo_swoole_client_void, ZEND_ACC_PUBLIC)
PHP_ME(swoole_client, set, arginfo_swoole_client_set, ZEND_ACC_PUBLIC)
PHP_ME(swoole_client, connect, arginfo_swoole_client_connect, ZEND_ACC_PUBLIC)
PHP_ME(swoole_client, recv, arginfo_swoole_client_recv, ZEND_ACC_PUBLIC)
PHP_ME(swoole_client, send, arginfo_swoole_client_send, ZEND_ACC_PUBLIC)
PHP_ME(swoole_client, pipe, arginfo_swoole_client_pipe, ZEND_ACC_PUBLIC)
PHP_ME(swoole_client, sendfile, arginfo_swoole_client_sendfile, ZEND_ACC_PUBLIC)
PHP_ME(swoole_client, sendto, arginfo_swoole_client_sendto, ZEND_ACC_PUBLIC)
PHP_ME(swoole_client, sleep, arginfo_swoole_client_void, ZEND_ACC_PUBLIC)
PHP_ME(swoole_client, wakeup, arginfo_swoole_client_void, ZEND_ACC_PUBLIC)
PHP_MALIAS(swoole_client, pause, sleep, arginfo_swoole_client_void, ZEND_ACC_PUBLIC)
PHP_MALIAS(swoole_client, resume, wakeup, arginfo_swoole_client_void, ZEND_ACC_PUBLIC)
PHP_ME(swoole_client, shutdown, arginfo_swoole_client_shutdown, ZEND_ACC_PUBLIC)
#ifdef SW_USE_OPENSSL
PHP_ME(swoole_client, enableSSL, arginfo_swoole_client_enableSSL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_client, getPeerCert, arginfo_swoole_client_void, ZEND_ACC_PUBLIC)
PHP_ME(swoole_client, verifyPeerCert, arginfo_swoole_client_void, ZEND_ACC_PUBLIC)
#endif
PHP_ME(swoole_client, isConnected, arginfo_swoole_client_void, ZEND_ACC_PUBLIC)
PHP_ME(swoole_client, getsockname, arginfo_swoole_client_void, ZEND_ACC_PUBLIC)
PHP_ME(swoole_client, getpeername, arginfo_swoole_client_void, ZEND_ACC_PUBLIC)
PHP_ME(swoole_client, close, arginfo_swoole_client_close, ZEND_ACC_PUBLIC)
PHP_ME(swoole_client, on, arginfo_swoole_client_on, ZEND_ACC_PUBLIC)
#ifdef SWOOLE_SOCKETS_SUPPORT
PHP_ME(swoole_client, getSocket, arginfo_swoole_client_void, ZEND_ACC_PUBLIC)
#endif
PHP_FE_END
};
static unordered_map<string, queue<swClient *> *> long_connections;
zend_class_entry *swoole_client_ce;
static zend_object_handlers swoole_client_handlers;
void swoole_client_init(int module_number)
{
SW_INIT_CLASS_ENTRY(swoole_client, "Swoole\\Client", "swoole_client", NULL, swoole_client_methods);
SW_SET_CLASS_SERIALIZABLE(swoole_client, zend_class_serialize_deny, zend_class_unserialize_deny);
SW_SET_CLASS_CLONEABLE(swoole_client, zend_class_clone_deny);
SW_SET_CLASS_UNSET_PROPERTY_HANDLER(swoole_client, zend_class_unset_property_deny);
zend_declare_property_long(swoole_client_ce, ZEND_STRL("errCode"), 0, ZEND_ACC_PUBLIC);
zend_declare_property_long(swoole_client_ce, ZEND_STRL("sock"), -1, ZEND_ACC_PUBLIC);
zend_declare_property_bool(swoole_client_ce, ZEND_STRL("reuse"), 0, ZEND_ACC_PUBLIC);
zend_declare_property_long(swoole_client_ce, ZEND_STRL("reuseCount"), 0, ZEND_ACC_PUBLIC);
zend_declare_property_long(swoole_client_ce, ZEND_STRL("type"), 0, ZEND_ACC_PUBLIC);
zend_declare_property_null(swoole_client_ce, ZEND_STRL("id"), ZEND_ACC_PUBLIC);
zend_declare_property_null(swoole_client_ce, ZEND_STRL("setting"), ZEND_ACC_PUBLIC);
/**
* event callback
*/
zend_declare_property_null(swoole_client_ce, ZEND_STRL("onConnect"), ZEND_ACC_PRIVATE);
zend_declare_property_null(swoole_client_ce, ZEND_STRL("onError"), ZEND_ACC_PRIVATE);
zend_declare_property_null(swoole_client_ce, ZEND_STRL("onReceive"), ZEND_ACC_PRIVATE);
zend_declare_property_null(swoole_client_ce, ZEND_STRL("onClose"), ZEND_ACC_PRIVATE);
zend_declare_property_null(swoole_client_ce, ZEND_STRL("onBufferFull"), ZEND_ACC_PRIVATE);
zend_declare_property_null(swoole_client_ce, ZEND_STRL("onBufferEmpty"), ZEND_ACC_PRIVATE);
#ifdef SW_USE_OPENSSL
zend_declare_property_null(swoole_client_ce, ZEND_STRL("onSSLReady"), ZEND_ACC_PRIVATE);
#endif
zend_declare_class_constant_long(swoole_client_ce, ZEND_STRL("MSG_OOB"), MSG_OOB);
zend_declare_class_constant_long(swoole_client_ce, ZEND_STRL("MSG_PEEK"), MSG_PEEK);
zend_declare_class_constant_long(swoole_client_ce, ZEND_STRL("MSG_DONTWAIT"), MSG_DONTWAIT);
zend_declare_class_constant_long(swoole_client_ce, ZEND_STRL("MSG_WAITALL"), MSG_WAITALL);
zend_declare_class_constant_long(swoole_client_ce, ZEND_STRL("SHUT_RDWR"), SHUT_RDWR);
zend_declare_class_constant_long(swoole_client_ce, ZEND_STRL("SHUT_RD"), SHUT_RD);
zend_declare_class_constant_long(swoole_client_ce, ZEND_STRL("SHUT_WR"), SHUT_WR);
}
static void client_onReceive(swClient *cli, char *data, uint32_t length)
{
zval *zobject = (zval *) cli->object;
zend_fcall_info_cache *fci_cache;
zval args[2];
zval _retval, *retval = &_retval;
args[0] = *zobject;
ZVAL_STRINGL(&args[1], data, length);
client_callback *cb = (client_callback *) swoole_get_property(zobject, 0);
fci_cache = &cb->cache_onReceive;
if (!fci_cache)
{
swoole_php_fatal_error(E_WARNING, "swoole_client object has no 'onReceive' callback function");
goto free_zdata;
}
if (sw_call_user_function_fast_ex(NULL, &cb->cache_onReceive, retval, 2, args) == FAILURE)
{
swoole_php_fatal_error(E_WARNING, "onReactorCallback handler error");
goto free_zdata;
}
if (UNEXPECTED(EG(exception)))
{
zend_exception_error(EG(exception), E_ERROR);
}
if (retval)
{
zval_ptr_dtor(retval);
}
free_zdata:
zval_ptr_dtor(&args[1]);
}
static void client_onConnect(swClient *cli)
{
zval *zobject = (zval *) cli->object;
#ifdef SW_USE_OPENSSL
if (cli->ssl_wait_handshake)
{
client_execute_callback(zobject, SW_CLIENT_CB_onSSLReady);
}
else
#endif
if (!cli->redirect)
{
client_execute_callback(zobject, SW_CLIENT_CB_onConnect);
}
else
{
client_callback *cb = (client_callback *) swoole_get_property(zobject, 0);
if (!cb || !cb->cache_onReceive.function_handler)
{
swoole_php_fatal_error(E_ERROR, "has no 'onReceive' callback function");
}
}
}
static void client_onClose(swClient *cli)
{
zval *zobject = (zval *) cli->object;
php_swoole_client_free(zobject, cli);
client_execute_callback(zobject, SW_CLIENT_CB_onClose);
zval_ptr_dtor(zobject);
}
static void client_onError(swClient *cli)
{
zval *zobject = (zval *) cli->object;
zend_update_property_long(swoole_client_ce, zobject, ZEND_STRL("errCode"), SwooleG.error);
php_swoole_client_free(zobject, cli);
client_execute_callback(zobject, SW_CLIENT_CB_onError);
zval_ptr_dtor(zobject);
}
static void client_onBufferFull(swClient *cli)
{
zval *zobject = (zval *) cli->object;
client_execute_callback(zobject, SW_CLIENT_CB_onBufferFull);
}
static void client_onBufferEmpty(swClient *cli)
{
zval *zobject = (zval *) cli->object;
client_execute_callback(zobject, SW_CLIENT_CB_onBufferEmpty);
}
#ifdef SW_USE_OPENSSL
void php_swoole_client_check_ssl_setting(swClient *cli, zval *zset)
{
HashTable *vht = Z_ARRVAL_P(zset);
zval *v;
if (php_swoole_array_get_value(vht, "ssl_method", v))
{
cli->ssl_option.method = (int) zval_get_long(v);
}
if (php_swoole_array_get_value(vht, "ssl_compress", v))
{
cli->ssl_option.disable_compress = !zval_is_true(v);
}
if (php_swoole_array_get_value(vht, "ssl_cert_file", v))
{
zend::string str_v(v);
if (access(str_v.val(), R_OK) < 0)
{
swoole_php_fatal_error(E_ERROR, "ssl cert file[%s] not found", str_v.val());
return;
}
cli->ssl_option.cert_file = sw_strdup(str_v.val());
}
if (php_swoole_array_get_value(vht, "ssl_key_file", v))
{
zend::string str_v(v);
if (access(str_v.val(), R_OK) < 0)
{
swoole_php_fatal_error(E_ERROR, "ssl key file[%s] not found", str_v.val());
return;
}
cli->ssl_option.key_file = sw_strdup(str_v.val());
}
if (php_swoole_array_get_value(vht, "ssl_passphrase", v))
{
zend::string str_v(v);
cli->ssl_option.passphrase = sw_strdup(str_v.val());
}
#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
if (php_swoole_array_get_value(vht, "ssl_host_name", v))
{
zend::string str_v(v);
cli->ssl_option.tls_host_name = sw_strdup(str_v.val());
}
#endif
if (php_swoole_array_get_value(vht, "ssl_verify_peer", v))
{
cli->ssl_option.verify_peer = zval_is_true(v);
}
if (php_swoole_array_get_value(vht, "ssl_allow_self_signed", v))
{
cli->ssl_option.allow_self_signed = zval_is_true(v);
}
if (php_swoole_array_get_value(vht, "ssl_cafile", v))
{
zend::string str_v(v);
cli->ssl_option.cafile = sw_strdup(str_v.val());
}
if (php_swoole_array_get_value(vht, "ssl_capath", v))
{
zend::string str_v(v);
cli->ssl_option.capath = sw_strdup(str_v.val());
}
if (php_swoole_array_get_value(vht, "ssl_verify_depth", v))
{
cli->ssl_option.verify_depth = (int) zval_get_long(v);
}
if (cli->ssl_option.cert_file && !cli->ssl_option.key_file)
{
swoole_php_fatal_error(E_ERROR, "ssl require key file");
return;
}
}
#endif
void php_swoole_client_check_setting(swClient *cli, zval *zset)
{
HashTable *vht;
zval *v;
int value = 1;
vht = Z_ARRVAL_P(zset);
//buffer: eof check
if (php_swoole_array_get_value(vht, "open_eof_check", v))
{
cli->open_eof_check = zval_is_true(v);
}
//buffer: split package with eof
if (php_swoole_array_get_value(vht, "open_eof_split", v))
{
cli->protocol.split_by_eof = zval_is_true(v);
if (cli->protocol.split_by_eof)
{
cli->open_eof_check = 1;
}
}
//package eof
if (php_swoole_array_get_value(vht, "package_eof", v))
{
zend::string str_v(v);
cli->protocol.package_eof_len = str_v.len();
if (cli->protocol.package_eof_len == 0)
{
swoole_php_fatal_error(E_ERROR, "pacakge_eof cannot be an empty string");
return;
}
else if (cli->protocol.package_eof_len > SW_DATA_EOF_MAXLEN)
{
swoole_php_fatal_error(E_ERROR, "pacakge_eof max length is %d", SW_DATA_EOF_MAXLEN);
return;
}
bzero(cli->protocol.package_eof, SW_DATA_EOF_MAXLEN);
memcpy(cli->protocol.package_eof, str_v.val(), str_v.len());
}
//open mqtt protocol
if (php_swoole_array_get_value(vht, "open_mqtt_protocol", v))
{
cli->open_length_check = zval_is_true(v);
cli->protocol.get_package_length = swMqtt_get_package_length;
}
//open length check
if (php_swoole_array_get_value(vht, "open_length_check", v))
{
cli->open_length_check = zval_is_true(v);
cli->protocol.get_package_length = swProtocol_get_package_length;
}
//package length size
if (php_swoole_array_get_value(vht, "package_length_type", v))
{
zend::string str_v(v);
cli->protocol.package_length_type = str_v.val()[0];
cli->protocol.package_length_size = swoole_type_size(cli->protocol.package_length_type);
if (cli->protocol.package_length_size == 0)
{
swoole_php_fatal_error(E_ERROR, "Unknown package_length_type name '%c', see pack(). Link: http://php.net/pack", cli->protocol.package_length_type);
return;
}
}
//package length offset
if (php_swoole_array_get_value(vht, "package_length_offset", v))
{
cli->protocol.package_length_offset = (int) zval_get_long(v);
}
//package body start
if (php_swoole_array_get_value(vht, "package_body_offset", v))
{
cli->protocol.package_body_offset = (int) zval_get_long(v);
}
//length function
if (php_swoole_array_get_value(vht, "package_length_func", v))
{
while(1)
{
if (Z_TYPE_P(v) == IS_STRING)
{
swProtocol_length_function func = (swProtocol_length_function) swoole_get_function(Z_STRVAL_P(v),
Z_STRLEN_P(v));
if (func != NULL)
{
cli->protocol.get_package_length = func;
break;
}
}
char *func_name = NULL;
if (!sw_zend_is_callable(v, 0, &func_name))
{
swoole_php_fatal_error(E_ERROR, "function '%s' is not callable", func_name);
return;
}
efree(func_name);
cli->protocol.get_package_length = php_swoole_length_func;
if (cli->protocol.private_data)
{
zval_ptr_dtor((zval *)cli->protocol.private_data);
efree(cli->protocol.private_data);
}
Z_TRY_ADDREF_P(v);
cli->protocol.private_data = sw_zval_dup(v);
break;
}
cli->protocol.package_length_size = 0;
cli->protocol.package_length_type = '\0';
cli->protocol.package_length_offset = SW_IPC_BUFFER_SIZE;
}
/**
* package max length
*/
if (php_swoole_array_get_value(vht, "package_max_length", v))
{
cli->protocol.package_max_length = (int) zval_get_long(v);
}
else
{
cli->protocol.package_max_length = SW_BUFFER_INPUT_SIZE;
}
/**
* socket send/recv buffer size
*/
if (php_swoole_array_get_value(vht, "socket_buffer_size", v))
{
value = (int) zval_get_long(v);
if (value <= 0)
{
value = INT_MAX;
}
swSocket_set_buffer_size(cli->socket->fd, value);
cli->socket->buffer_size = value;
}
if (php_swoole_array_get_value(vht, "buffer_high_watermark", v))
{
value = (int) zval_get_long(v);
cli->buffer_high_watermark = value;
}
if (php_swoole_array_get_value(vht, "buffer_low_watermark", v))
{
value = (int) zval_get_long(v);
cli->buffer_low_watermark = value;
}
/**
* bind port
*/
if (php_swoole_array_get_value(vht, "bind_port", v))
{
int bind_port = (int) zval_get_long(v);
/**
* bind address
*/
if (php_swoole_array_get_value(vht, "bind_address", v))
{
zend::string str_v(v);
swSocket_bind(cli->socket->fd, cli->type, str_v.val(), &bind_port);
}
}
/**
* client: tcp_nodelay
*/
if (php_swoole_array_get_value(vht, "open_tcp_nodelay", v))
{
if (zval_is_true(v))
{
goto _open_tcp_nodelay;
}
}
else
{
_open_tcp_nodelay:
if (cli->type == SW_SOCK_TCP || cli->type == SW_SOCK_TCP6)
{
value = 1;
if (setsockopt(cli->socket->fd, IPPROTO_TCP, TCP_NODELAY, &value, sizeof(value)) != 0)
{
swSysWarn("setsockopt(%d, TCP_NODELAY) failed", cli->socket->fd);
}
}
}
/**
* socks5 proxy
*/
if (php_swoole_array_get_value(vht, "socks5_host", v))
{
zend::string host(v);
if (php_swoole_array_get_value(vht, "socks5_port", v))
{
cli->socks5_proxy = (struct _swSocks5 *) ecalloc(1, sizeof(swSocks5));
cli->socks5_proxy->host = estrdup(host.val());
cli->socks5_proxy->port = zval_get_long(v);
cli->socks5_proxy->dns_tunnel = 1;
if (php_swoole_array_get_value(vht, "socks5_username", v))
{
zend::string username(v);
if (php_swoole_array_get_value(vht, "socks5_password", v))
{
zend::string password(v);
cli->socks5_proxy->method = 0x02;
cli->socks5_proxy->username = username.val();
cli->socks5_proxy->l_username = username.len();
cli->socks5_proxy->password = password.val();
cli->socks5_proxy->l_password = password.len();
}
else
{
swoole_php_fatal_error(E_WARNING, "socks5_password should not be null");
}
}
}
else
{
swoole_php_fatal_error(E_WARNING, "socks5_port should not be null");
}
}
/**
* http proxy
*/
else if (php_swoole_array_get_value(vht, "http_proxy_host", v))
{
zend::string host(v);
if (php_swoole_array_get_value(vht, "http_proxy_port", v))
{
cli->http_proxy = (struct _http_proxy*) ecalloc(1, sizeof(struct _http_proxy));
cli->http_proxy->proxy_host = estrdup(host.val());
cli->http_proxy->proxy_port = zval_get_long(v);
if (php_swoole_array_get_value(vht, "http_proxy_username", v) || php_swoole_array_get_value(vht, "http_proxy_user", v))
{
zend::string username(v);
if (php_swoole_array_get_value(vht, "http_proxy_password", v))
{
zend::string password(v);
cli->http_proxy->user = estrdup(username.val());
cli->http_proxy->l_user = username.len();
cli->http_proxy->password = estrdup(password.val());
cli->http_proxy->l_password = password.len();
}
else
{
swoole_php_fatal_error(E_WARNING, "http_proxy_password should not be null");
}
}
}
else
{
swoole_php_fatal_error(E_WARNING, "http_proxy_port should not be null");
}
}
/**
* ssl
*/
#ifdef SW_USE_OPENSSL
if (cli->open_ssl)
{
php_swoole_client_check_ssl_setting(cli, zset);
}
#endif
}
void php_swoole_client_free(zval *zobject, swClient *cli)
{
if (cli->timer)
{
swTimer_del(&SwooleG.timer, cli->timer);
cli->timer = NULL;
}
//socks5 proxy config
if (cli->socks5_proxy)
{
efree(cli->socks5_proxy->host);
if (cli->socks5_proxy->username)
{
efree(cli->socks5_proxy->username);
}
if (cli->socks5_proxy->password)
{
efree(cli->socks5_proxy->password);
}
efree(cli->socks5_proxy);
}
//http proxy config
if (cli->http_proxy)
{
efree(cli->http_proxy->proxy_host);
if (cli->http_proxy->user)
{
efree(cli->http_proxy->user);
}
if (cli->http_proxy->password)
{
efree(cli->http_proxy->password);
}
efree(cli->http_proxy);
}
if (cli->protocol.private_data)
{
zval *zcallback = (zval *) cli->protocol.private_data;
sw_zval_free(zcallback);
}
//long tcp connection, delete from php_sw_long_connections
if (cli->keep)
{
string conn_key = string(cli->server_str, cli->server_strlen);
auto i = long_connections.find(conn_key);
if (i != long_connections.end())
{
queue<swClient *> *q = i->second;
if (q->empty())
{
delete q;
long_connections.erase(string(cli->server_str, cli->server_strlen));
}
}
sw_free(cli->server_str);
swClient_free(cli);
pefree(cli, 1);
}
else
{
sw_free(cli->server_str);
swClient_free(cli);
efree(cli);
}
#ifdef SWOOLE_SOCKETS_SUPPORT
zval *zsocket = (zval *) swoole_get_property(zobject, client_property_socket);
if (zsocket)
{
sw_zval_free(zsocket);
swoole_set_property(zobject, client_property_socket, NULL);
}
#endif
//unset object
swoole_set_object(zobject, NULL);
}
swClient* php_swoole_client_new(zval *zobject, char *host, int host_len, int port)
{
zval *ztype;
int async = 0;
uint64_t tmp_buf;
int ret;
ztype = sw_zend_read_property(Z_OBJCE_P(zobject), zobject, ZEND_STRL("type"), 0);
if (ztype == NULL || ZVAL_IS_NULL(ztype))
{
swoole_php_fatal_error(E_ERROR, "failed to get swoole_client->type");
return NULL;
}
long type = Z_LVAL_P(ztype);
//new flag, swoole-1.6.12+
if (type & SW_FLAG_ASYNC)
{
async = 1;
}
int client_type = php_swoole_socktype(type);
if ((client_type == SW_SOCK_TCP || client_type == SW_SOCK_TCP6) && (port <= 0 || port > SW_CLIENT_MAX_PORT))
{
swoole_php_fatal_error(E_WARNING, "The port is invalid");
SwooleG.error = SW_ERROR_INVALID_PARAMS;
return NULL;
}
swClient *cli;
string conn_key;
zval *zconnection_id = sw_zend_read_property_not_null(Z_OBJCE_P(zobject), zobject, ZEND_STRL("id"), 1);
if (zconnection_id && Z_TYPE_P(zconnection_id) == IS_STRING && Z_STRLEN_P(zconnection_id) > 0)
{
conn_key = string(Z_STRVAL_P(zconnection_id), Z_STRLEN_P(zconnection_id));
}
else
{
size_t size = sw_snprintf(SwooleTG.buffer_stack->str, SwooleTG.buffer_stack->size, "%s:%d", host, port);
conn_key = string(SwooleTG.buffer_stack->str, size);
}
//keep the tcp connection
if (type & SW_FLAG_KEEP)
{
auto i = long_connections.find(conn_key);
if (i == long_connections.end() || i->second->empty())
{
cli = (swClient*) pemalloc(sizeof(swClient), 1);
goto create_socket;
}
else
{
queue<swClient*> *q = i->second;
cli = q->front();
q->pop();
//try recv, check connection status
ret = recv(cli->socket->fd, &tmp_buf, sizeof(tmp_buf), MSG_DONTWAIT | MSG_PEEK);
if (ret == 0 || (ret < 0 && swConnection_error(errno) == SW_CLOSE))
{
cli->close(cli);
php_swoole_client_free(zobject, cli);
cli = (swClient*) pemalloc(sizeof(swClient), 1);
goto create_socket;
}
cli->reuse_count++;
zend_update_property_long(Z_OBJCE_P(zobject), zobject, ZEND_STRL("reuseCount"), cli->reuse_count);
}
}
else
{
cli = (swClient*) emalloc(sizeof(swClient));
create_socket: if (swClient_create(cli, php_swoole_socktype(type), async) < 0)
{
swoole_php_sys_error(E_WARNING, "swClient_create() failed");
zend_update_property_long(Z_OBJCE_P(zobject), zobject, ZEND_STRL("errCode"), errno);
return NULL;
}
//don't forget free it
cli->server_str = sw_strndup(conn_key.c_str(), conn_key.length());
cli->server_strlen = conn_key.length();
}
zend_update_property_long(Z_OBJCE_P(zobject), zobject, ZEND_STRL("sock"), cli->socket->fd);
if (type & SW_FLAG_KEEP)
{
cli->keep = 1;
}
#ifdef SW_USE_OPENSSL
if (type & SW_SOCK_SSL)
{
cli->open_ssl = 1;
}
#endif
return cli;
}
static PHP_METHOD(swoole_client, __construct)
{
zend_long type = 0;
zend_long async = 0;
char *id = NULL;
size_t len = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l|bs", &type, &async, &id, &len) == FAILURE)
{
swoole_php_fatal_error(E_ERROR, "socket type param is required");
RETURN_FALSE;
}
if (async)
{
type |= SW_FLAG_ASYNC;
}
if ((type & SW_FLAG_ASYNC))
{
if ((type & SW_FLAG_KEEP) && SWOOLE_G(cli))
{
swoole_php_fatal_error(E_ERROR, "The 'SWOOLE_KEEP' flag can only be used in the php-fpm or apache environment");
}
php_swoole_check_reactor();
}
int client_type = php_swoole_socktype(type);
if (client_type < SW_SOCK_TCP || client_type > SW_SOCK_UNIX_STREAM)
{
const char *space, *class_name = get_active_class_name(&space);
zend_type_error(
"%s%s%s() expects parameter %d to be client type, unknown type " ZEND_LONG_FMT " given",
class_name, space, get_active_function_name(), 1, type
);
RETURN_FALSE;
}
zend_update_property_long(swoole_client_ce, getThis(), ZEND_STRL("type"), type);
if (id)
{
zend_update_property_stringl(swoole_client_ce, getThis(), ZEND_STRL("id"), id, len);
}
//init
swoole_set_object(getThis(), NULL);
swoole_set_property(getThis(), client_property_callback, NULL);
#ifdef SWOOLE_SOCKETS_SUPPORT
swoole_set_property(getThis(), client_property_socket, NULL);
#endif
RETURN_TRUE;
}
static PHP_METHOD(swoole_client, __destruct)
{
SW_PREVENT_USER_DESTRUCT();
swClient *cli = (swClient *) swoole_get_object(getThis());
//no keep connection
if (cli)
{
sw_zend_call_method_with_0_params(getThis(), swoole_client_ce, NULL, "close", NULL);
}
//free memory
client_callback *cb = (client_callback *) swoole_get_property(getThis(), client_property_callback);
if (cb)
{
efree(cb);
swoole_set_property(getThis(), client_property_callback, NULL);
}
}
static PHP_METHOD(swoole_client, set)
{
zval *zset;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &zset) == FAILURE)
{
RETURN_FALSE;
}
if (Z_TYPE_P(zset) != IS_ARRAY)
{
RETURN_FALSE;
}
zval *zsetting = sw_zend_read_property_array(swoole_client_ce, getThis(), ZEND_STRL("setting"), 1);
php_array_merge(Z_ARRVAL_P(zsetting), Z_ARRVAL_P(zset));
RETURN_TRUE;
}
static PHP_METHOD(swoole_client, connect)
{
char *host;
size_t host_len;
zend_long port = 0;
double timeout = SW_CLIENT_CONNECT_TIMEOUT;
zend_long sock_flag = 0;
ZEND_PARSE_PARAMETERS_START(1, 4)
Z_PARAM_STRING(host, host_len)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(port)
Z_PARAM_DOUBLE(timeout)
Z_PARAM_LONG(sock_flag)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
if (host_len == 0)
{
swoole_php_fatal_error(E_WARNING, "The host is empty");
RETURN_FALSE;
}
swClient *cli = (swClient *) swoole_get_object(getThis());
if (cli)
{
swoole_php_fatal_error(E_WARNING, "connection to the server has already been established");
RETURN_FALSE;
}
cli = php_swoole_client_new(getThis(), host, host_len, port);
if (cli == NULL)
{
RETURN_FALSE;
}
swoole_set_object(getThis(), cli);
if (cli->type == SW_SOCK_TCP || cli->type == SW_SOCK_TCP6)
{
if (cli->async == 1)
{
//for tcp: nonblock
//for udp: have udp connect
sock_flag = 1;
}
}
if (cli->keep == 1 && cli->socket->active == 1)
{
zend_update_property_bool(swoole_client_ce, getThis(), ZEND_STRL("reuse"), 1);
RETURN_TRUE;
}
else if (cli->socket->active == 1)
{
swoole_php_fatal_error(E_WARNING, "connection to the server has already been established");
RETURN_FALSE;
}
zval *zset = sw_zend_read_property(swoole_client_ce, getThis(), ZEND_STRL("setting"), 1);
if (zset && ZVAL_IS_ARRAY(zset))
{
php_swoole_client_check_setting(cli, zset);
}
//nonblock async
if (cli->async)
{
client_callback *cb = (client_callback *) swoole_get_property(getThis(), 0);
if (!cb)
{
swoole_php_fatal_error(E_ERROR, "no event callback function");
RETURN_FALSE;
}
if (swSocket_is_stream(cli->type))
{
if (!cb->cache_onConnect.function_handler)
{
swoole_php_fatal_error(E_ERROR, "no 'onConnect' callback function");
RETURN_FALSE;
}
if (!cb->cache_onError.function_handler)
{
swoole_php_fatal_error(E_ERROR, "no 'onError' callback function");
RETURN_FALSE;
}
if (!cb->cache_onClose.function_handler)
{
swoole_php_fatal_error(E_ERROR, "no 'onClose' callback function");
RETURN_FALSE;
}
cli->onConnect = client_onConnect;
cli->onClose = client_onClose;
cli->onError = client_onError;
cli->onReceive = client_onReceive;
cli->reactor_fdtype = PHP_SWOOLE_FD_STREAM_CLIENT;
if (cb->cache_onBufferFull.function_handler)
{
cli->onBufferFull = client_onBufferFull;
}
if (cb->cache_onBufferEmpty.function_handler)
{
cli->onBufferEmpty = client_onBufferEmpty;
}
}
else
{
if (!cb || !cb->cache_onReceive.function_handler)
{
swoole_php_fatal_error(E_ERROR, "no 'onReceive' callback function");
RETURN_FALSE;
}
if (cb->cache_onConnect.function_handler)
{
cli->onConnect = client_onConnect;
}
if (cb->cache_onClose.function_handler)
{
cli->onClose = client_onClose;
}
if (cb->cache_onError.function_handler)
{
cli->onError = client_onError;
}
cli->onReceive = client_onReceive;
cli->reactor_fdtype = PHP_SWOOLE_FD_DGRAM_CLIENT;
}
zval *zobject = getThis();
cli->object = zobject;
sw_copy_to_stack(cli->object, cb->_object);
Z_TRY_ADDREF_P(zobject);
}
//nonblock async
if (cli->connect(cli, host, port, timeout, sock_flag) < 0)
{
if (errno == 0)
{
if (SwooleG.error == SW_ERROR_DNSLOOKUP_RESOLVE_FAILED)
{
swoole_php_error(E_WARNING, "connect to server[%s:%d] failed. Error: %s[%d]", host, (int )port,
hstrerror(h_errno), h_errno);
}
zend_update_property_long(swoole_client_ce, getThis(), ZEND_STRL("errCode"), SwooleG.error);
}
else
{
swoole_php_sys_error(E_WARNING, "connect to server[%s:%d] failed", host, (int )port);
zend_update_property_long(swoole_client_ce, getThis(), ZEND_STRL("errCode"), errno);
}
if (cli->async && cli->onError == NULL)
{
php_swoole_client_free(getThis(), cli);
zval_ptr_dtor(getThis());
}
if (!cli->async)
{
php_swoole_client_free(getThis(), cli);
}
RETURN_FALSE;
}
RETURN_TRUE;
}
static PHP_METHOD(swoole_client, send)
{
char *data;
size_t data_len;
zend_long flags = 0;
ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_STRING(data, data_len)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(flags)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
if (data_len == 0)
{
swoole_php_fatal_error(E_WARNING, "data to send is empty");
RETURN_FALSE;
}
swClient *cli = client_get_ptr(getThis());
if (!cli)
{
RETURN_FALSE;
}
//clear errno
SwooleG.error = 0;
int ret = cli->send(cli, data, data_len, flags);
if (ret < 0)
{
swoole_php_sys_error(E_WARNING, "failed to send(%d) %zu bytes", cli->socket->fd, data_len);
zend_update_property_long(swoole_client_ce, getThis(), ZEND_STRL("errCode"), SwooleG.error);
RETVAL_FALSE;
}
else
{
RETURN_LONG(ret);
}
}
static PHP_METHOD(swoole_client, sendto)
{
char* ip;
size_t ip_len;
long port;
char *data;
size_t len;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "sls", &ip, &ip_len, &port, &data, &len) == FAILURE)
{
RETURN_FALSE;
}
if (len == 0)
{
swoole_php_error(E_WARNING, "data to send is empty");
RETURN_FALSE;
}
swClient *cli = (swClient *) swoole_get_object(getThis());
if (!cli)
{
cli = php_swoole_client_new(getThis(), ip, ip_len, port);
if (cli == NULL)
{
RETURN_FALSE;
}
cli->socket->active = 1;
swoole_set_object(getThis(), cli);
}
int ret;
if (cli->type == SW_SOCK_UDP)
{
ret = swSocket_udp_sendto(cli->socket->fd, ip, port, data, len);
}
else if (cli->type == SW_SOCK_UDP6)
{
ret = swSocket_udp_sendto6(cli->socket->fd, ip, port, data, len);
}
else
{
swoole_php_fatal_error(E_WARNING, "only supports SWOOLE_SOCK_UDP or SWOOLE_SOCK_UDP6");
RETURN_FALSE;
}
SW_CHECK_RETURN(ret);
}
static PHP_METHOD(swoole_client, sendfile)
{
char *file;
size_t file_len;
zend_long offset = 0;
zend_long length = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|ll", &file, &file_len, &offset, &length) == FAILURE)
{
RETURN_FALSE;
}
if (file_len == 0)
{
swoole_php_fatal_error(E_WARNING, "file to send is empty");
RETURN_FALSE;
}
swClient *cli = client_get_ptr(getThis());
if (!cli)
{
RETURN_FALSE;
}
//only stream socket can sendfile
if (!(cli->type == SW_SOCK_TCP || cli->type == SW_SOCK_TCP6 || cli->type == SW_SOCK_UNIX_STREAM))
{
swoole_php_error(E_WARNING, "dgram socket cannot use sendfile");
RETURN_FALSE;
}
//clear errno
SwooleG.error = 0;
int ret = cli->sendfile(cli, file, offset, length);
if (ret < 0)
{
SwooleG.error = errno;
swoole_php_fatal_error(E_WARNING, "sendfile() failed. Error: %s [%d]", strerror(SwooleG.error), SwooleG.error);
zend_update_property_long(swoole_client_ce, getThis(), ZEND_STRL("errCode"), SwooleG.error);
RETVAL_FALSE;
}
else
{
RETVAL_TRUE;
}
}
static PHP_METHOD(swoole_client, recv)
{
zend_long buf_len = SW_PHP_CLIENT_BUFFER_SIZE;
zend_long flags = 0;
int ret;
char *buf = NULL;
ZEND_PARSE_PARAMETERS_START(0, 2)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(buf_len)
Z_PARAM_LONG(flags)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
//waitall
if (flags == 1)
{
flags = MSG_WAITALL;
}
swClient *cli = client_get_ptr(getThis());
if (!cli)
{
RETURN_FALSE;
}
swProtocol *protocol = &cli->protocol;
if (cli->open_eof_check)
{
if (cli->buffer == NULL)
{
cli->buffer = swString_new(SW_BUFFER_SIZE_BIG);
}
swString *buffer = cli->buffer;
int eof = -1;
if (buffer->length > 0)
{
goto find_eof;
}
while (1)
{
buf = buffer->str + buffer->length;
buf_len = buffer->size - buffer->length;
if (buf_len > SW_BUFFER_SIZE_BIG)
{
buf_len = SW_BUFFER_SIZE_BIG;
}
ret = cli->recv(cli, buf, buf_len, 0);
if (ret < 0)
{
swoole_php_sys_error(E_WARNING, "recv() failed");
buffer->length = 0;
RETURN_FALSE;
}
else if (ret == 0)
{
buffer->length = 0;
RETURN_EMPTY_STRING();
}
buffer->length += ret;
if (buffer->length < protocol->package_eof_len)
{
continue;
}
find_eof: eof = swoole_strnpos(buffer->str, buffer->length, protocol->package_eof, protocol->package_eof_len);
if (eof >= 0)
{
eof += protocol->package_eof_len;
RETVAL_STRINGL(buffer->str, eof);
if ((int) buffer->length > eof)
{
swString_pop_front(buffer, eof);
}
else
{
buffer->length = 0;
}
return;
}
else
{
if (buffer->length == protocol->package_max_length)
{
swoole_php_error(E_WARNING, "no package eof");
buffer->length = 0;
RETURN_FALSE;
}
else if (buffer->length == buffer->size)
{
if (buffer->size < protocol->package_max_length)
{
uint32_t new_size = buffer->size * 2;
if (new_size > protocol->package_max_length)
{
new_size = protocol->package_max_length;
}
if (swString_extend(buffer, new_size) < 0)
{
buffer->length = 0;
RETURN_FALSE;
}
}
}
}
}
buffer->length = 0;
RETURN_FALSE;
}
else if (cli->open_length_check)
{
if (cli->buffer == NULL)
{
cli->buffer = swString_new(SW_BUFFER_SIZE_STD);
}
else
{
swString_clear(cli->buffer);
}
uint32_t header_len = protocol->package_length_offset + protocol->package_length_size;
while (1)
{
int retval = cli->recv(cli, cli->buffer->str + cli->buffer->length, header_len - cli->buffer->length, 0);
if (retval <= 0)
{
break;
}
cli->buffer->length += retval;
buf_len = protocol->get_package_length(protocol, cli->socket, cli->buffer->str, cli->buffer->length);
if (buf_len == 0)
{
continue;
}
else if (buf_len < 0)
{
break;
}
else
{
break;
}
}
//error package
if (buf_len < 0)
{
RETURN_EMPTY_STRING();
}
//empty package
else if (buf_len == header_len)
{
RETURN_STRINGL(cli->buffer->str, header_len);
}
else if (buf_len > protocol->package_max_length)
{
swoole_error_log(SW_LOG_WARNING, SW_ERROR_PACKAGE_LENGTH_TOO_LARGE, "Package is too big. package_length=%d", (int )buf_len);
RETURN_EMPTY_STRING();
}
else if (buf_len == (zend_long) cli->buffer->length)
{
RETURN_STRINGL(cli->buffer->str, cli->buffer->length);
}
buf = (char *) emalloc(buf_len + 1);
memcpy(buf, cli->buffer->str, cli->buffer->length);
SwooleG.error = 0;
ret = cli->recv(cli, buf + header_len, buf_len - cli->buffer->length, MSG_WAITALL);
if (ret > 0)
{
ret += header_len;
if (ret != buf_len)
{
ret = 0;
}
}
}
else
{
if (!(flags & MSG_WAITALL) && buf_len > SW_PHP_CLIENT_BUFFER_SIZE)
{
buf_len = SW_PHP_CLIENT_BUFFER_SIZE;
}
buf = (char *) emalloc(buf_len + 1);
SwooleG.error = 0;
ret = cli->recv(cli, buf, buf_len, flags);
}
if (ret < 0)
{
SwooleG.error = errno;
swoole_php_error(E_WARNING, "recv() failed. Error: %s [%d]", strerror(SwooleG.error), SwooleG.error);
zend_update_property_long(swoole_client_ce, getThis(), ZEND_STRL("errCode"), SwooleG.error);
if (buf)
{
efree(buf);
}
RETURN_FALSE;
}
else
{
if (ret == 0)
{
if (buf)
{
efree(buf);
}
RETURN_EMPTY_STRING();
}
else
{
buf[ret] = 0;
RETVAL_STRINGL(buf, ret);
efree(buf);
}
}
}
static PHP_METHOD(swoole_client, isConnected)
{
swClient *cli = (swClient *) swoole_get_object(getThis());
if (!cli)
{
RETURN_FALSE;
}
if (!cli->socket)
{
RETURN_FALSE;
}
RETURN_BOOL(cli->socket->active);
}
static PHP_METHOD(swoole_client, getsockname)
{
swClient *cli = client_get_ptr(getThis());
if (!cli)
{
RETURN_FALSE;
}
if (cli->type == SW_SOCK_UNIX_STREAM || cli->type == SW_SOCK_UNIX_DGRAM)
{
swoole_php_fatal_error(E_WARNING, "getsockname() only support AF_INET family socket");
RETURN_FALSE;
}
cli->socket->info.len = sizeof(cli->socket->info.addr);
if (getsockname(cli->socket->fd, (struct sockaddr*) &cli->socket->info.addr, &cli->socket->info.len) < 0)
{
swoole_php_sys_error(E_WARNING, "getsockname() failed");
RETURN_FALSE;
}
array_init(return_value);
if (cli->type == SW_SOCK_UDP6 || cli->type == SW_SOCK_TCP6)
{
add_assoc_long(return_value, "port", ntohs(cli->socket->info.addr.inet_v6.sin6_port));
char tmp[INET6_ADDRSTRLEN];
if (inet_ntop(AF_INET6, &cli->socket->info.addr.inet_v6.sin6_addr, tmp, sizeof(tmp)))
{
add_assoc_string(return_value, "host", tmp);
}
else
{
swoole_php_fatal_error(E_WARNING, "inet_ntop() failed");
}
}
else
{
add_assoc_long(return_value, "port", ntohs(cli->socket->info.addr.inet_v4.sin_port));
add_assoc_string(return_value, "host", inet_ntoa(cli->socket->info.addr.inet_v4.sin_addr));
}
}
#ifdef SWOOLE_SOCKETS_SUPPORT
static PHP_METHOD(swoole_client, getSocket)
{
zval *zsocket = (zval *) swoole_get_property(getThis(), client_property_socket);
if (zsocket)
{
RETURN_ZVAL(zsocket, 1, NULL);
}
swClient *cli = client_get_ptr(getThis());
if (!cli)
{
RETURN_FALSE;
}
if (cli->keep)
{
swoole_php_fatal_error(E_WARNING, "the 'getSocket' method can't be used on persistent connection");
RETURN_FALSE;
}
php_socket *socket_object = swoole_convert_to_socket(cli->socket->fd);
if (!socket_object)
{
RETURN_FALSE;
}
SW_ZEND_REGISTER_RESOURCE(return_value, (void * ) socket_object, php_sockets_le_socket());
zsocket = sw_zval_dup(return_value);
Z_TRY_ADDREF_P(zsocket);
swoole_set_property(getThis(), client_property_socket, zsocket);
}
#endif
static PHP_METHOD(swoole_client, getpeername)
{
swClient *cli = client_get_ptr(getThis());
if (!cli)
{
RETURN_FALSE;
}
if (cli->type == SW_SOCK_UDP)
{
array_init(return_value);
add_assoc_long(return_value, "port", ntohs(cli->remote_addr.addr.inet_v4.sin_port));
add_assoc_string(return_value, "host", inet_ntoa(cli->remote_addr.addr.inet_v4.sin_addr));
}
else if (cli->type == SW_SOCK_UDP6)
{
array_init(return_value);
add_assoc_long(return_value, "port", ntohs(cli->remote_addr.addr.inet_v6.sin6_port));
char tmp[INET6_ADDRSTRLEN];
if (inet_ntop(AF_INET6, &cli->remote_addr.addr.inet_v6.sin6_addr, tmp, sizeof(tmp)))
{
add_assoc_string(return_value, "host", tmp);
}
else
{
swoole_php_fatal_error(E_WARNING, "inet_ntop() failed");
}
}
else
{
swoole_php_fatal_error(E_WARNING, "only supports SWOOLE_SOCK_UDP or SWOOLE_SOCK_UDP6");
RETURN_FALSE;
}
}
static PHP_METHOD(swoole_client, close)
{
int ret = 1;
zend_bool force = 0;
ZEND_PARSE_PARAMETERS_START(0, 1)
Z_PARAM_OPTIONAL
Z_PARAM_BOOL(force)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
swClient *cli = (swClient *) swoole_get_object(getThis());
if (!cli || !cli->socket)
{
swoole_php_fatal_error(E_WARNING, "client is not connected to the server");
RETURN_FALSE;
}
if (cli->socket->closed)
{
swoole_php_error(E_WARNING, "client socket is closed");
RETURN_FALSE;
}
if (cli->async && cli->socket->active == 0)
{
zval *zobject = getThis();
zval_ptr_dtor(zobject);
}
//Connection error, or short tcp connection.
//No keep connection
if (force || !cli->keep || swConnection_error(SwooleG.error) == SW_CLOSE)
{
uint8_t need_free = !cli->async;
ret = cli->close(cli);
if (need_free)
{
php_swoole_client_free(getThis(), cli);
}
}
else
{
if (cli->keep)
{
string conn_key(cli->server_str, cli->server_strlen);
queue<swClient*> *q;
auto i = long_connections.find(conn_key);
if (i == long_connections.end())
{
q = new queue<swClient*>;
long_connections[conn_key] = q;
}
else
{
q = i->second;
}
q->push(cli);
}
//unset object
swoole_set_object(getThis(), NULL);
}
SW_CHECK_RETURN(ret);
}
static PHP_METHOD(swoole_client, on)
{
char *cb_name;
size_t cb_name_len;
zval *zcallback;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "sz", &cb_name, &cb_name_len, &zcallback) == FAILURE)
{
RETURN_FALSE;
}
zval *ztype = sw_zend_read_property(swoole_client_ce, getThis(), ZEND_STRL("type"), 0);
if (ztype == NULL || ZVAL_IS_NULL(ztype))
{
swoole_php_fatal_error(E_ERROR, "get swoole_client->type failed");
return;
}
if (!(Z_LVAL_P(ztype) & SW_FLAG_ASYNC))
{
swoole_php_fatal_error(E_ERROR, "can't register event callback functions in SYNC mode");
return;
}
client_callback *cb = (client_callback *) swoole_get_property(getThis(), client_property_callback);
if (!cb)
{
cb = (client_callback *) emalloc(sizeof(client_callback));
bzero(cb, sizeof(client_callback));
swoole_set_property(getThis(), client_property_callback, cb);
}
char *func_name = NULL;
zend_fcall_info_cache func_cache;
if (!sw_zend_is_callable_ex(zcallback, NULL, 0, &func_name, NULL, &func_cache, NULL))
{
swoole_php_fatal_error(E_ERROR, "function '%s' is not callable", func_name);
return;
}
efree(func_name);
if (strncasecmp("connect", cb_name, cb_name_len) == 0)
{
zend_update_property(swoole_client_ce, getThis(), ZEND_STRL("onConnect"), zcallback);
cb->cache_onConnect = func_cache;
}
else if (strncasecmp("receive", cb_name, cb_name_len) == 0)
{
zend_update_property(swoole_client_ce, getThis(), ZEND_STRL("onReceive"), zcallback);
cb->cache_onReceive = func_cache;
}
else if (strncasecmp("close", cb_name, cb_name_len) == 0)
{
zend_update_property(swoole_client_ce, getThis(), ZEND_STRL("onClose"), zcallback);
cb->cache_onClose = func_cache;
}
else if (strncasecmp("error", cb_name, cb_name_len) == 0)
{
zend_update_property(swoole_client_ce, getThis(), ZEND_STRL("onError"), zcallback);
cb->cache_onError = func_cache;
}
else if (strncasecmp("bufferFull", cb_name, cb_name_len) == 0)
{
zend_update_property(swoole_client_ce, getThis(), ZEND_STRL("onBufferFull"), zcallback);
cb->cache_onBufferFull = func_cache;
}
else if (strncasecmp("bufferEmpty", cb_name, cb_name_len) == 0)
{
zend_update_property(swoole_client_ce, getThis(), ZEND_STRL("onBufferEmpty"), zcallback);
cb->cache_onBufferEmpty = func_cache;
}
else
{
swoole_php_fatal_error(E_WARNING, "Unknown event callback type name '%s'", cb_name);
RETURN_FALSE;
}
RETURN_TRUE;
}
static PHP_METHOD(swoole_client, sleep)
{
swClient *cli = client_get_ptr(getThis());
if (!cli)
{
RETURN_FALSE;
}
SW_CHECK_RETURN(swClient_sleep(cli));
}
static PHP_METHOD(swoole_client, wakeup)
{
swClient *cli = client_get_ptr(getThis());
if (!cli)
{
RETURN_FALSE;
}
SW_CHECK_RETURN(swClient_wakeup(cli));
}
#ifdef SW_USE_OPENSSL
static PHP_METHOD(swoole_client, enableSSL)
{
swClient *cli = client_get_ptr(getThis());
if (!cli)
{
RETURN_FALSE;
}
if (cli->type != SW_SOCK_TCP && cli->type != SW_SOCK_TCP6)
{
swoole_php_fatal_error(E_WARNING, "cannot use enableSSL");
RETURN_FALSE;
}
if (cli->socket->ssl)
{
swoole_php_fatal_error(E_WARNING, "SSL has been enabled");
RETURN_FALSE;
}
cli->open_ssl = 1;
zval *zset = sw_zend_read_property(swoole_client_ce, getThis(), ZEND_STRL("setting"), 1);
if (zset && ZVAL_IS_ARRAY(zset))
{
php_swoole_client_check_ssl_setting(cli, zset);
}
if (swClient_enable_ssl_encrypt(cli) < 0)
{
RETURN_FALSE;
}
if (cli->async)
{
zval *zcallback;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &zcallback) == FAILURE)
{
RETURN_FALSE;
}
char *func_name = NULL;
zend_fcall_info_cache func_cache;
if (!sw_zend_is_callable_ex(zcallback, NULL, 0, &func_name, NULL, &func_cache, NULL))
{
swoole_php_fatal_error(E_ERROR, "function '%s' is not callable", func_name);
return;
}
efree(func_name);
client_callback *cb = (client_callback *) swoole_get_property(getThis(), client_property_callback);
if (!cb)
{
swoole_php_fatal_error(E_WARNING, "the object is not an instance of swoole_client");
RETURN_FALSE;
}
zend_update_property(swoole_client_ce, getThis(), ZEND_STRL("onSSLReady"), zcallback);
cb->cache_onSSLReady = func_cache;
cli->ssl_wait_handshake = 1;
cli->socket->ssl_state = SW_SSL_STATE_WAIT_STREAM;
SwooleG.main_reactor->set(SwooleG.main_reactor, cli->socket->fd, SW_FD_STREAM_CLIENT | SW_EVENT_WRITE);
}
else
{
if (swClient_ssl_handshake(cli) < 0)
{
RETURN_FALSE;
}
}
RETURN_TRUE;
}
static PHP_METHOD(swoole_client, getPeerCert)
{
swClient *cli = client_get_ptr(getThis());
if (!cli)
{
RETURN_FALSE;
}
if (!cli->socket->ssl)
{
swoole_php_fatal_error(E_WARNING, "SSL is not ready");
RETURN_FALSE;
}
char buf[8192];
int n = swSSL_get_client_certificate(cli->socket->ssl, buf, sizeof(buf));
if (n < 0)
{
RETURN_FALSE;
}
RETURN_STRINGL(buf, n);
}
static PHP_METHOD(swoole_client, verifyPeerCert)
{
swClient *cli = client_get_ptr(getThis());
if (!cli)
{
RETURN_FALSE;
}
if (!cli->socket->ssl)
{
swoole_php_fatal_error(E_WARNING, "SSL is not ready");
RETURN_FALSE;
}
zend_bool allow_self_signed = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &allow_self_signed) == FAILURE)
{
RETURN_FALSE;
}
SW_CHECK_RETURN(swClient_ssl_verify(cli, allow_self_signed));
}
#endif
static PHP_METHOD(swoole_client, pipe)
{
swClient *cli = client_get_ptr(getThis());
if (!cli)
{
RETURN_FALSE;
}
zval *write_socket;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &write_socket) == FAILURE)
{
RETURN_FALSE;
}
int fd;
int flags = 0;
//server session id
if (Z_TYPE_P(write_socket) == IS_LONG)
{
fd = Z_LVAL_P(write_socket);
swConnection *conn = swWorker_get_connection(SwooleG.serv, fd);
if (conn == NULL)
{
RETURN_FALSE;
}
flags = SW_CLIENT_PIPE_TCP_SESSION;
}
else
{
fd = swoole_convert_to_fd(write_socket);
if (fd < 0)
{
RETURN_FALSE;
}
}
SW_CHECK_RETURN(cli->pipe(cli, fd, flags));
}
static PHP_METHOD(swoole_client, shutdown)
{
swClient *cli = client_get_ptr(getThis());
if (!cli)
{
RETURN_FALSE;
}
long __how;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &__how) == FAILURE)
{
RETURN_FALSE;
}
SW_CHECK_RETURN(swClient_shutdown(cli, __how));
}
PHP_FUNCTION(swoole_client_select)
{
#ifdef PHP_SWOOLE_CLIENT_USE_POLL
zval *r_array, *w_array, *e_array;
int retval, index = 0;
double timeout = SW_CLIENT_CONNECT_TIMEOUT;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "a!a!a!|d", &r_array, &w_array, &e_array, &timeout) == FAILURE)
{
RETURN_FALSE;
}
int maxevents = MAX(MAX(php_swoole_array_length(r_array), php_swoole_array_length(w_array)),
php_swoole_array_length(e_array));
struct pollfd *fds = (struct pollfd *) ecalloc(maxevents, sizeof(struct pollfd));
if (r_array != NULL && php_swoole_array_length(r_array) > 0)
{
index = client_poll_add(r_array, index, fds, maxevents, POLLIN);
}
if (w_array != NULL && php_swoole_array_length(w_array) > 0)
{
index = client_poll_add(w_array, index, fds, maxevents, POLLOUT);
}
if (e_array != NULL && php_swoole_array_length(w_array) > 0)
{
index = client_poll_add(e_array, index, fds, maxevents, POLLHUP);
}
if (index == 0)
{
efree(fds);
swoole_php_fatal_error(E_WARNING, "no resource arrays were passed to select");
RETURN_FALSE;
}
retval = poll(fds, maxevents, (int) timeout * 1000);
if (retval == -1)
{
efree(fds);
swoole_php_sys_error(E_WARNING, "unable to poll()");
RETURN_FALSE;
}
if (r_array != NULL && php_swoole_array_length(r_array) > 0)
{
client_poll_wait(r_array, fds, maxevents, retval, POLLIN);
}
if (w_array != NULL && php_swoole_array_length(w_array) > 0)
{
client_poll_wait(w_array, fds, maxevents, retval, POLLOUT);
}
if (e_array != NULL && php_swoole_array_length(e_array) > 0)
{
client_poll_wait(e_array, fds, maxevents, retval, POLLHUP);
}
efree(fds);
RETURN_LONG(retval);
#else
zval *r_array, *w_array, *e_array;
fd_set rfds, wfds, efds;
int max_fd = 0;
int retval, sets = 0;
double timeout = SW_CLIENT_CONNECT_TIMEOUT;
struct timeval timeo;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "a!a!a!|d", &r_array, &w_array, &e_array, &timeout) == FAILURE)
{
RETURN_FALSE;
}
FD_ZERO(&rfds);
FD_ZERO(&wfds);
FD_ZERO(&efds);
if (r_array != NULL) sets += client_select_add(r_array, &rfds, &max_fd);
if (w_array != NULL) sets += client_select_add(w_array, &wfds, &max_fd);
if (e_array != NULL) sets += client_select_add(e_array, &efds, &max_fd);
if (!sets)
{
swoole_php_fatal_error(E_WARNING, "no resource arrays were passed to select");
RETURN_FALSE;
}
if (max_fd >= FD_SETSIZE)
{
swoole_php_fatal_error(E_WARNING, "select max_fd > FD_SETSIZE[%d]", FD_SETSIZE);
RETURN_FALSE;
}
timeo.tv_sec = (int) timeout;
timeo.tv_usec = (int) ((timeout - timeo.tv_sec) * 1000 * 1000);
retval = select(max_fd + 1, &rfds, &wfds, &efds, &timeo);
if (retval == -1)
{
swoole_php_sys_error(E_WARNING, "unable to select");
RETURN_FALSE;
}
if (r_array != NULL)
{
client_select_wait(r_array, &rfds);
}
if (w_array != NULL)
{
client_select_wait(w_array, &wfds);
}
if (e_array != NULL)
{
client_select_wait(e_array, &efds);
}
RETURN_LONG(retval);
#endif
}
#ifdef PHP_SWOOLE_CLIENT_USE_POLL
static inline int client_poll_get(struct pollfd *fds, int maxevents, int fd)
{
int i;
for (i = 0; i < maxevents; i++)
{
if (fds[i].fd == fd)
{
return i;
}
}
return -1;
}
static int client_poll_wait(zval *sock_array, struct pollfd *fds, int maxevents, int n_event, int revent)
{
zval *element = NULL;
int sock;
ulong_t num = 0;
if (Z_TYPE_P(sock_array) != IS_ARRAY)
{
return 0;
}
zval new_array;
array_init(&new_array);
zend_ulong num_key;
zend_string *key;
zval *dest_element;
int poll_key;
ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(sock_array), num_key, key, element)
{
sock = swoole_convert_to_fd(element);
if (sock < 0)
{
continue;
}
poll_key = client_poll_get(fds, maxevents, sock);
if (poll_key == -1)
{
swoole_php_fatal_error(E_WARNING, "bad fd[%d]", sock);
continue;
}
if (!(fds[poll_key].revents & revent))
{
continue;
}
if (key)
{
dest_element = zend_hash_add(Z_ARRVAL(new_array), key, element);
}
else
{
dest_element = zend_hash_index_update(Z_ARRVAL(new_array), num_key, element);
}
if (dest_element)
{
Z_ADDREF_P(dest_element);
}
num++;
} ZEND_HASH_FOREACH_END();
zval_ptr_dtor(sock_array);
ZVAL_COPY_VALUE(sock_array, &new_array);
return num ? 1 : 0;
}
static int client_poll_add(zval *sock_array, int index, struct pollfd *fds, int maxevents, int event)
{
zval *element = NULL;
if (Z_TYPE_P(sock_array) != IS_ARRAY)
{
return -1;
}
int sock;
int key = -1;
SW_HASHTABLE_FOREACH_START(Z_ARRVAL_P(sock_array), element)
sock = swoole_convert_to_fd(element);
if (sock < 0)
{
continue;
}
if (event != POLLIN)
{
key = client_poll_get(fds, maxevents, sock);
}
if (key < 0)
{
fds[index].fd = sock;
fds[index].events = event;
index++;
}
else
{
fds[key].fd = sock;
fds[key].events |= event;
}
SW_HASHTABLE_FOREACH_END();
return index;
}
#else
static int client_select_wait(zval *sock_array, fd_set *fds)
{
zval *element = NULL;
int sock;
ulong_t num = 0;
if (Z_TYPE_P(sock_array) != IS_ARRAY)
{
return 0;
}
zval new_array;
array_init(&new_array);
zend_ulong num_key;
zend_string *key;
zval *dest_element;
ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(sock_array), num_key, key, element)
{
sock = swoole_convert_to_fd(element);
if (sock < 0)
{
continue;
}
if ((sock < FD_SETSIZE) && FD_ISSET(sock, fds))
{
if (key)
{
dest_element = zend_hash_add(Z_ARRVAL(new_array), key, element);
}
else
{
dest_element = zend_hash_index_update(Z_ARRVAL(new_array), num_key, element);
}
if (dest_element)
{
Z_ADDREF_P(dest_element);
}
}
num++;
} ZEND_HASH_FOREACH_END();
zval_ptr_dtor(sock_array);
ZVAL_COPY_VALUE(sock_array, &new_array);
return num ? 1 : 0;
}
static int client_select_add(zval *sock_array, fd_set *fds, int *max_fd)
{
zval *element = NULL;
if (Z_TYPE_P(sock_array) != IS_ARRAY)
{
return 0;
}
int sock;
int num = 0;
SW_HASHTABLE_FOREACH_START(Z_ARRVAL_P(sock_array), element)
sock = swoole_convert_to_fd(element);
if (sock < 0)
{
continue;
}
if (sock < FD_SETSIZE)
{
FD_SET(sock, fds);
}
else
{
swoole_php_fatal_error(E_WARNING, "socket[%d] > FD_SETSIZE[%d]", sock, FD_SETSIZE);
continue;
}
if (sock > *max_fd)
{
*max_fd = sock;
}
num ++;
SW_HASHTABLE_FOREACH_END();
return num ? 1 : 0;
}
#endif
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/cooly_admin/swoole.git
git@gitee.com:cooly_admin/swoole.git
cooly_admin
swoole
swoole-src
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385