Skip to content

Commit 619fb1b

Browse files
committed
Add. Support to socket_action interface.
1 parent 8a9d0a0 commit 619fb1b

5 files changed

Lines changed: 121 additions & 21 deletions

File tree

src/l52util.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,27 @@ void *lutil_newudatap_impl(lua_State *L, size_t size, const void *p){
155155
lutil_setmetatablep(L, p);
156156
return obj;
157157
}
158+
159+
void lutil_pushint64(lua_State *L, int64_t v){
160+
if(sizeof(lua_Integer) >= sizeof(int64_t)){
161+
lua_pushinteger(L, (lua_Integer)v);
162+
return;
163+
}
164+
lua_pushnumber(L, (lua_Number)v);
165+
}
166+
167+
int64_t lutil_checkint64(lua_State *L, int idx){
168+
if(sizeof(lua_Integer) >= sizeof(int64_t))
169+
return luaL_checkinteger(L, idx);
170+
return (int64_t)luaL_checknumber(L, idx);
171+
}
172+
173+
int64_t lutil_optint64(lua_State *L, int idx, int64_t v){
174+
if(sizeof(lua_Integer) >= sizeof(int64_t))
175+
return luaL_optinteger(L, idx, v);
176+
return (int64_t)luaL_optnumber(L, idx, v);
177+
}
178+
179+
void lutil_pushnvalues(lua_State *L, int n){
180+
for(;n;--n) lua_pushvalue(L, -n);
181+
}

src/l52util.h

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,47 +13,52 @@
1313

1414
#include "lua.h"
1515
#include "lauxlib.h"
16+
#include <stdint.h>
1617

1718
#if LUA_VERSION_NUM >= 503 /* Lua 5.3 */
1819

19-
#ifndef luaL_optint
20-
# define luaL_optint luaL_optinteger
21-
#endif
22-
2320
#ifndef luaL_checkint
24-
# define luaL_checkint luaL_checkinteger
21+
#define luaL_checkint luaL_checkinteger
2522
#endif
2623

2724
#ifndef luaL_checklong
28-
# define luaL_checklong luaL_checkinteger
25+
#define luaL_checklong luaL_checkinteger
2926
#endif
3027

28+
#ifndef luaL_optint
29+
#define luaL_optint luaL_optinteger
3130
#endif
3231

32+
#ifndef luaL_optlong
33+
#define luaL_optlong luaL_optinteger
34+
#endif
3335

34-
#if LUA_VERSION_NUM >= 502 // lua 5.2
36+
#endif
3537

36-
// lua_rawgetp
37-
// lua_rawsetp
38-
// luaL_setfuncs
39-
// lua_absindex
40-
#ifndef lua_objlen
38+
#if LUA_VERSION_NUM >= 502 /* Lua 5.2 */
4139

42-
#define lua_objlen lua_rawlen
40+
/* lua_rawgetp */
41+
/* lua_rawsetp */
42+
/* luaL_setfuncs */
43+
/* lua_absindex */
4344

45+
#ifndef lua_objlen
46+
#define lua_objlen lua_rawlen
4447
#endif
4548

4649
int luaL_typerror (lua_State *L, int narg, const char *tname);
4750

4851
#ifndef luaL_register
49-
5052
void luaL_register (lua_State *L, const char *libname, const luaL_Reg *l);
53+
#endif
5154

55+
#ifndef lua_equal
56+
#define lua_equal(L,idx1,idx2) lua_compare(L,(idx1),(idx2),LUA_OPEQ)
5257
#endif
5358

54-
#else // lua 5.1
59+
#else /* Lua 5.1 */
5560

56-
// functions form lua 5.2
61+
/* functions form lua 5.2 */
5762

5863
# define lua_absindex(L, i) (((i)>0)?(i):((i)<=LUA_REGISTRYINDEX?(i):(lua_gettop(L)+(i)+1)))
5964
# define lua_rawlen lua_objlen
@@ -78,5 +83,12 @@ int lutil_createmetap (lua_State *L, const void *p, const luaL_Reg *methods,
7883

7984
void *lutil_newudatap_impl (lua_State *L, size_t size, const void *p);
8085

81-
#endif
86+
void lutil_pushint64(lua_State *L, int64_t v);
8287

88+
int64_t lutil_checkint64(lua_State *L, int idx);
89+
90+
int64_t lutil_optint64(lua_State *L, int idx, int64_t v);
91+
92+
void lutil_pushnvalues(lua_State *L, int n);
93+
94+
#endif

src/lcmulti.c

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,18 @@ static int lcurl_multi_timeout(lua_State *L){
226226
return 1;
227227
}
228228

229+
static int lcurl_multi_socket_action(lua_State *L){
230+
lcurl_multi_t *p = lcurl_getmulti(L);
231+
curl_socket_t s = lutil_checkint64(L, 2);
232+
int n, mask = lutil_checkint64(L, 3);
233+
CURLMcode code = curl_multi_socket_action(p->curl, s, mask, &n);
234+
if(code != CURLM_OK){
235+
lcurl_fail_ex(L, p->err_mode, LCURL_ERROR_MULTI, code);
236+
}
237+
lua_pushinteger(L, n);
238+
return 1;
239+
}
240+
229241
//{ OPTIONS
230242
static int lcurl_opt_set_long_(lua_State *L, int opt){
231243
lcurl_multi_t *p = lcurl_getmulti(L);
@@ -313,7 +325,7 @@ static int lcurl_multi_set_callback(lua_State *L,
313325
return 1;
314326
}
315327

316-
//{Timer
328+
//{ Timer
317329

318330
int lcurl_multi_timer_callback(CURLM *multi, long ms, void *arg){
319331
lcurl_multi_t *p = arg;
@@ -347,7 +359,6 @@ int lcurl_multi_timer_callback(CURLM *multi, long ms, void *arg){
347359
return ret;
348360
}
349361

350-
351362
static int lcurl_multi_set_TIMERFUNCTION(lua_State *L){
352363
lcurl_multi_t *p = lcurl_getmulti(L);
353364
return lcurl_multi_set_callback(L, p, &p->tm,
@@ -358,6 +369,43 @@ static int lcurl_multi_set_TIMERFUNCTION(lua_State *L){
358369

359370
//}
360371

372+
//{ Socket
373+
374+
static int lcurl_multi_socket_callback(CURL *easy, curl_socket_t s, int what, void *arg, void *socketp){
375+
lcurl_multi_t *p = arg;
376+
lua_State *L = p->L;
377+
lcurl_easy_t *e;
378+
int n, top = lua_gettop(L);
379+
380+
n = lcurl_util_push_cb(L, &p->sc);
381+
382+
lua_rawgeti(L, LCURL_LUA_REGISTRY, p->h_ref);
383+
lua_rawgetp(L, -1, easy);
384+
e = lcurl_geteasy_at(L, -1);
385+
lua_remove(L, -2);
386+
lutil_pushint64(L, s);
387+
lua_pushinteger(L, what);
388+
389+
if(lua_pcall(L, n+2, 0, 0)){
390+
assert(lua_gettop(L) >= top);
391+
lua_settop(L, top);
392+
return -1; //! @todo break perform
393+
}
394+
395+
lua_settop(L, top);
396+
return 0;
397+
}
398+
399+
static int lcurl_multi_set_SOCKETFUNCTION(lua_State *L){
400+
lcurl_multi_t *p = lcurl_getmulti(L);
401+
return lcurl_multi_set_callback(L, p, &p->sc,
402+
CURLMOPT_SOCKETFUNCTION, CURLMOPT_SOCKETDATA,
403+
"socket", lcurl_multi_socket_callback
404+
);
405+
}
406+
407+
//}
408+
361409
//}
362410

363411
static int lcurl_multi_setopt(lua_State *L){
@@ -396,10 +444,12 @@ static const struct luaL_Reg lcurl_multi_methods[] = {
396444
{"setopt", lcurl_multi_setopt },
397445
{"wait", lcurl_multi_wait },
398446
{"timeout", lcurl_multi_timeout },
447+
{"socket_action", lcurl_multi_socket_action },
399448

400449
#define OPT_ENTRY(L, N, T, S) { "setopt_"#L, lcurl_multi_set_##N },
401450
#include "lcoptmulti.h"
402-
OPT_ENTRY(timerfunction, TIMERFUNCTION, TTT, 0)
451+
OPT_ENTRY(timerfunction, TIMERFUNCTION, TTT, 0)
452+
OPT_ENTRY(socketfunction, SOCKETFUNCTION, TTT, 0)
403453
#undef OPT_ENTRY
404454

405455
{"close", lcurl_multi_cleanup },
@@ -411,7 +461,8 @@ static const struct luaL_Reg lcurl_multi_methods[] = {
411461
static const lcurl_const_t lcurl_multi_opt[] = {
412462
#define OPT_ENTRY(L, N, T, S) { "OPT_MULTI_"#N, CURLMOPT_##N },
413463
#include "lcoptmulti.h"
414-
OPT_ENTRY(timerfunction, TIMERFUNCTION, TTT, 0)
464+
OPT_ENTRY(timerfunction, TIMERFUNCTION, TTT, 0)
465+
OPT_ENTRY(socketfunction, SOCKETFUNCTION, TTT, 0)
415466
#undef OPT_ENTRY
416467

417468
{NULL, 0}

src/lcmulti.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ typedef struct lcurl_multi_tag{
2020
int err_mode;
2121
int h_ref;
2222
lcurl_callback_t tm;
23+
lcurl_callback_t sc;
2324
}lcurl_multi_t;
2425

2526
int lcurl_multi_create(lua_State *L, int error_mode);

src/lcopteasy.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,10 +264,22 @@ FLG_ENTRY( HTTP_VERSION_2_0 )
264264
FLG_ENTRY( READFUNC_PAUSE ) /*7.18.0*/
265265
FLG_ENTRY( WRITEFUNC_PAUSE ) /*7.18.0*/
266266

267+
FLG_ENTRY( POLL_IN ) /*7.14.0*/
268+
FLG_ENTRY( POLL_INOUT ) /*7.14.0*/
269+
FLG_ENTRY( POLL_NONE ) /*7.14.0*/
270+
FLG_ENTRY( POLL_OUT ) /*7.14.0*/
271+
FLG_ENTRY( POLL_REMOVE ) /*7.14.0*/
272+
FLG_ENTRY( SOCKET_TIMEOUT ) /*7.14.0*/
273+
274+
FLG_ENTRY( CSELECT_ERR ) /*7.16.3*/
275+
FLG_ENTRY( CSELECT_IN ) /*7.16.3*/
276+
FLG_ENTRY( CSELECT_OUT ) /*7.16.3*/
277+
267278
#ifdef OPT_ENTRY_IS_NULL
268279
# undef OPT_ENTRY
269280
#endif
270281

271282
#ifdef FLG_ENTRY_IS_NULL
272283
# undef FLG_ENTRY
273284
#endif
285+

0 commit comments

Comments
 (0)