diff --git a/src/l52util.c b/src/l52util.c index 70dc7b9..af64402 100644 --- a/src/l52util.c +++ b/src/l52util.c @@ -155,3 +155,27 @@ void *lutil_newudatap_impl(lua_State *L, size_t size, const void *p){ lutil_setmetatablep(L, p); return obj; } + +void lutil_pushint64(lua_State *L, int64_t v){ + if(sizeof(lua_Integer) >= sizeof(int64_t)){ + lua_pushinteger(L, (lua_Integer)v); + return; + } + lua_pushnumber(L, (lua_Number)v); +} + +int64_t lutil_checkint64(lua_State *L, int idx){ + if(sizeof(lua_Integer) >= sizeof(int64_t)) + return luaL_checkinteger(L, idx); + return (int64_t)luaL_checknumber(L, idx); +} + +int64_t lutil_optint64(lua_State *L, int idx, int64_t v){ + if(sizeof(lua_Integer) >= sizeof(int64_t)) + return luaL_optinteger(L, idx, v); + return (int64_t)luaL_optnumber(L, idx, v); +} + +void lutil_pushnvalues(lua_State *L, int n){ + for(;n;--n) lua_pushvalue(L, -n); +} diff --git a/src/l52util.h b/src/l52util.h index 560fe10..84bc8ce 100644 --- a/src/l52util.h +++ b/src/l52util.h @@ -13,47 +13,52 @@ #include "lua.h" #include "lauxlib.h" +#include #if LUA_VERSION_NUM >= 503 /* Lua 5.3 */ -#ifndef luaL_optint -# define luaL_optint luaL_optinteger -#endif - #ifndef luaL_checkint -# define luaL_checkint luaL_checkinteger +#define luaL_checkint luaL_checkinteger #endif #ifndef luaL_checklong -# define luaL_checklong luaL_checkinteger +#define luaL_checklong luaL_checkinteger +#endif + +#ifndef luaL_optint +#define luaL_optint luaL_optinteger +#endif + +#ifndef luaL_optlong +#define luaL_optlong luaL_optinteger #endif #endif +#if LUA_VERSION_NUM >= 502 /* Lua 5.2 */ -#if LUA_VERSION_NUM >= 502 // lua 5.2 +/* lua_rawgetp */ +/* lua_rawsetp */ +/* luaL_setfuncs */ +/* lua_absindex */ -// lua_rawgetp -// lua_rawsetp -// luaL_setfuncs -// lua_absindex #ifndef lua_objlen - #define lua_objlen lua_rawlen - #endif int luaL_typerror (lua_State *L, int narg, const char *tname); #ifndef luaL_register - void luaL_register (lua_State *L, const char *libname, const luaL_Reg *l); - #endif -#else // lua 5.1 +#ifndef lua_equal +#define lua_equal(L,idx1,idx2) lua_compare(L,(idx1),(idx2),LUA_OPEQ) +#endif -// functions form lua 5.2 +#else /* Lua 5.1 */ + +/* functions form lua 5.2 */ # define lua_absindex(L, i) (((i)>0)?(i):((i)<=LUA_REGISTRYINDEX?(i):(lua_gettop(L)+(i)+1))) # define lua_rawlen lua_objlen @@ -78,5 +83,12 @@ int lutil_createmetap (lua_State *L, const void *p, const luaL_Reg *methods, void *lutil_newudatap_impl (lua_State *L, size_t size, const void *p); -#endif +void lutil_pushint64(lua_State *L, int64_t v); +int64_t lutil_checkint64(lua_State *L, int idx); + +int64_t lutil_optint64(lua_State *L, int idx, int64_t v); + +void lutil_pushnvalues(lua_State *L, int n); + +#endif diff --git a/src/lcmulti.c b/src/lcmulti.c index c2c017c..34f5b81 100644 --- a/src/lcmulti.c +++ b/src/lcmulti.c @@ -226,6 +226,18 @@ static int lcurl_multi_timeout(lua_State *L){ return 1; } +static int lcurl_multi_socket_action(lua_State *L){ + lcurl_multi_t *p = lcurl_getmulti(L); + curl_socket_t s = lutil_checkint64(L, 2); + int n, mask = lutil_checkint64(L, 3); + CURLMcode code = curl_multi_socket_action(p->curl, s, mask, &n); + if(code != CURLM_OK){ + lcurl_fail_ex(L, p->err_mode, LCURL_ERROR_MULTI, code); + } + lua_pushinteger(L, n); + return 1; +} + //{ OPTIONS static int lcurl_opt_set_long_(lua_State *L, int opt){ lcurl_multi_t *p = lcurl_getmulti(L); @@ -313,7 +325,7 @@ static int lcurl_multi_set_callback(lua_State *L, return 1; } -//{Timer +//{ Timer int lcurl_multi_timer_callback(CURLM *multi, long ms, void *arg){ lcurl_multi_t *p = arg; @@ -347,7 +359,6 @@ int lcurl_multi_timer_callback(CURLM *multi, long ms, void *arg){ return ret; } - static int lcurl_multi_set_TIMERFUNCTION(lua_State *L){ lcurl_multi_t *p = lcurl_getmulti(L); return lcurl_multi_set_callback(L, p, &p->tm, @@ -358,6 +369,43 @@ static int lcurl_multi_set_TIMERFUNCTION(lua_State *L){ //} +//{ Socket + +static int lcurl_multi_socket_callback(CURL *easy, curl_socket_t s, int what, void *arg, void *socketp){ + lcurl_multi_t *p = arg; + lua_State *L = p->L; + lcurl_easy_t *e; + int n, top = lua_gettop(L); + + n = lcurl_util_push_cb(L, &p->sc); + + lua_rawgeti(L, LCURL_LUA_REGISTRY, p->h_ref); + lua_rawgetp(L, -1, easy); + e = lcurl_geteasy_at(L, -1); + lua_remove(L, -2); + lutil_pushint64(L, s); + lua_pushinteger(L, what); + + if(lua_pcall(L, n+2, 0, 0)){ + assert(lua_gettop(L) >= top); + lua_settop(L, top); + return -1; //! @todo break perform + } + + lua_settop(L, top); + return 0; +} + +static int lcurl_multi_set_SOCKETFUNCTION(lua_State *L){ + lcurl_multi_t *p = lcurl_getmulti(L); + return lcurl_multi_set_callback(L, p, &p->sc, + CURLMOPT_SOCKETFUNCTION, CURLMOPT_SOCKETDATA, + "socket", lcurl_multi_socket_callback + ); +} + +//} + //} static int lcurl_multi_setopt(lua_State *L){ @@ -396,10 +444,12 @@ static const struct luaL_Reg lcurl_multi_methods[] = { {"setopt", lcurl_multi_setopt }, {"wait", lcurl_multi_wait }, {"timeout", lcurl_multi_timeout }, + {"socket_action", lcurl_multi_socket_action }, #define OPT_ENTRY(L, N, T, S) { "setopt_"#L, lcurl_multi_set_##N }, #include "lcoptmulti.h" - OPT_ENTRY(timerfunction, TIMERFUNCTION, TTT, 0) + OPT_ENTRY(timerfunction, TIMERFUNCTION, TTT, 0) + OPT_ENTRY(socketfunction, SOCKETFUNCTION, TTT, 0) #undef OPT_ENTRY {"close", lcurl_multi_cleanup }, @@ -411,7 +461,8 @@ static const struct luaL_Reg lcurl_multi_methods[] = { static const lcurl_const_t lcurl_multi_opt[] = { #define OPT_ENTRY(L, N, T, S) { "OPT_MULTI_"#N, CURLMOPT_##N }, #include "lcoptmulti.h" - OPT_ENTRY(timerfunction, TIMERFUNCTION, TTT, 0) + OPT_ENTRY(timerfunction, TIMERFUNCTION, TTT, 0) + OPT_ENTRY(socketfunction, SOCKETFUNCTION, TTT, 0) #undef OPT_ENTRY {NULL, 0} diff --git a/src/lcmulti.h b/src/lcmulti.h index 3e866de..73d767f 100644 --- a/src/lcmulti.h +++ b/src/lcmulti.h @@ -20,6 +20,7 @@ typedef struct lcurl_multi_tag{ int err_mode; int h_ref; lcurl_callback_t tm; + lcurl_callback_t sc; }lcurl_multi_t; int lcurl_multi_create(lua_State *L, int error_mode); diff --git a/src/lcopteasy.h b/src/lcopteasy.h index 1d77f52..4da9095 100644 --- a/src/lcopteasy.h +++ b/src/lcopteasy.h @@ -264,6 +264,17 @@ FLG_ENTRY( HTTP_VERSION_2_0 ) FLG_ENTRY( READFUNC_PAUSE ) /*7.18.0*/ FLG_ENTRY( WRITEFUNC_PAUSE ) /*7.18.0*/ +FLG_ENTRY( POLL_IN ) /*7.14.0*/ +FLG_ENTRY( POLL_INOUT ) /*7.14.0*/ +FLG_ENTRY( POLL_NONE ) /*7.14.0*/ +FLG_ENTRY( POLL_OUT ) /*7.14.0*/ +FLG_ENTRY( POLL_REMOVE ) /*7.14.0*/ +FLG_ENTRY( SOCKET_TIMEOUT ) /*7.14.0*/ + +FLG_ENTRY( CSELECT_ERR ) /*7.16.3*/ +FLG_ENTRY( CSELECT_IN ) /*7.16.3*/ +FLG_ENTRY( CSELECT_OUT ) /*7.16.3*/ + #ifdef OPT_ENTRY_IS_NULL # undef OPT_ENTRY #endif @@ -271,3 +282,4 @@ FLG_ENTRY( WRITEFUNC_PAUSE ) /*7.18.0*/ #ifdef FLG_ENTRY_IS_NULL # undef FLG_ENTRY #endif +