Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/lcmulti.c
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ static int lcurl_multi_timeout(lua_State *L){

static int lcurl_multi_socket_action(lua_State *L){
lcurl_multi_t *p = lcurl_getmulti(L);
curl_socket_t s = lutil_optint64(L, 2, CURL_SOCKET_TIMEOUT);
curl_socket_t s = lcurl_opt_os_socket(L, 2, CURL_SOCKET_TIMEOUT);
CURLMcode code; int n, mask;
lua_State *curL;

Expand Down Expand Up @@ -551,7 +551,7 @@ static int lcurl_multi_socket_callback(CURL *easy, curl_socket_t s, int what, vo
lua_rawgetp(L, -1, easy);
e = lcurl_geteasy_at(L, -1);
lua_remove(L, -2);
lutil_pushint64(L, s);
lcurl_push_os_socket(L, s);
lua_pushinteger(L, what);

if(lua_pcall(L, n+2, 0, 0)){
Expand Down
56 changes: 55 additions & 1 deletion src/lcutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -332,4 +332,58 @@ void lcurl_stack_dump (lua_State *L){
i++;
}
fprintf(stderr, " ------------ Stack Dump Finished ------------\n" );
}
}

curl_socket_t lcurl_opt_os_socket(lua_State *L, int idx, curl_socket_t def) {
if (lua_islightuserdata(L, idx))
return (curl_socket_t)lua_touserdata(L, idx);

return (curl_socket_t)lutil_optint64(L, idx, def);
}

void lcurl_push_os_socket(lua_State *L, curl_socket_t fd) {
#if !defined(_WIN32)
lutil_pushint64(L, fd);
#else /*_WIN32*/
/* Assumes that compiler can optimize constant conditions. MSVC do this. */

/*On Lua 5.3 lua_Integer type can be represented exactly*/
#if LUA_VERSION_NUM >= 503
if (sizeof(curl_socket_t) <= sizeof(lua_Integer)) {
lua_pushinteger(L, (lua_Integer)fd);
return;
}
#endif

#if defined(LUA_NUMBER_DOUBLE) || defined(LUA_NUMBER_FLOAT)
/*! @todo test DBL_MANT_DIG, FLT_MANT_DIG */

if (sizeof(lua_Number) == 8) { /*we have 53 bits for integer*/
if ((sizeof(curl_socket_t) <= 6)) {
lua_pushnumber(L, (lua_Number)fd);
return;
}

if(((UINT_PTR)fd & 0x1FFFFFFFFFFFFF) == (UINT_PTR)fd)
lua_pushnumber(L, (lua_Number)fd);
else
lua_pushlightuserdata(L, (void*)fd);

return;
}

if (sizeof(lua_Number) == 4) { /*we have 24 bits for integer*/
if (((UINT_PTR)fd & 0xFFFFFF) == (UINT_PTR)fd)
lua_pushnumber(L, (lua_Number)fd);
else
lua_pushlightuserdata(L, (void*)fd);
return;
}
#endif

lutil_pushint64(L, fd);
if (lcurl_opt_os_socket(L, -1, 0) != fd)
lua_pushlightuserdata(L, (void*)fd);

#endif /*_WIN32*/
}
4 changes: 4 additions & 0 deletions src/lcutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,8 @@ int lcurl_utils_apply_options(lua_State *L, int opt, int obj, int do_close,

void lcurl_stack_dump (lua_State *L);

curl_socket_t lcurl_opt_os_socket(lua_State *L, int idx, curl_socket_t def);

void lcurl_push_os_socket(lua_State *L, curl_socket_t fd);

#endif