153
184
int i_fd = luaL_checkint( L, 1 );
154
185
size_t i_len = luaL_optint( L, 2, 1 );
155
186
char psz_buffer[i_len];
156
i_len = recv( i_fd, psz_buffer, i_len, 0 );
157
lua_pushlstring( L, psz_buffer, i_len );
187
ssize_t i_ret = recv( i_fd, psz_buffer, i_len, 0 );
188
lua_pushlstring( L, psz_buffer, (i_ret >= 0) ? i_ret : 0 );
161
192
/*****************************************************************************
163
194
*****************************************************************************/
164
static int vlclua_net_select( lua_State *L )
195
/* Takes a { fd : events } table as first arg and modifies it to { fd : revents } */
196
static int vlclua_net_poll( lua_State *L )
167
size_t i_nfds = luaL_checkint( L, 1 );
168
fd_set *fds_read = (fd_set*)luaL_checkudata( L, 2, "fd_set" );
169
fd_set *fds_write = (fd_set*)luaL_checkudata( L, 3, "fd_set" );
170
double f_timeout = luaL_checknumber( L, 4 );
171
struct timeval timeout;
174
if( i_nfds > FD_SETSIZE )
177
timeout.tv_sec = (int)f_timeout;
178
timeout.tv_usec = (int)(1e6*(f_timeout-(double)((int)f_timeout)));
179
i_ret = select( i_nfds, fds_read, fds_write, 0, &timeout );
198
luaL_checktype( L, 1, LUA_TTABLE );
199
double f_timeout = luaL_optnumber( L, 2, -1. );
203
while( lua_next( L, 1 ) )
208
struct pollfd *p_fds = malloc( i_fds * sizeof( struct pollfd ) );
209
vlc_cleanup_push( free, p_fds );
212
while( lua_next( L, 1 ) )
214
p_fds[i].fd = luaL_checkinteger( L, -2 );
215
p_fds[i].events = luaL_checkinteger( L, -1 );
216
p_fds[i].revents = 0;
221
int i_ret = poll( p_fds, i_fds, f_timeout < 0. ? -1 : (int)(f_timeout*1000) );
222
for( i = 0; i < i_fds; i++ )
224
lua_pushinteger( L, p_fds[i].fd );
225
lua_pushinteger( L, p_fds[i].revents );
226
lua_settable( L, 1 );
180
228
lua_pushinteger( L, i_ret );
181
lua_pushinteger( L, (double)timeout.tv_sec+((double)timeout.tv_usec)/1e-6 );
185
/*****************************************************************************
187
*****************************************************************************/
188
static int vlclua_fd_clr( lua_State * );
189
static int vlclua_fd_isset( lua_State * );
190
static int vlclua_fd_set( lua_State * );
191
static int vlclua_fd_zero( lua_State * );
193
static const luaL_Reg vlclua_fd_set_reg[] = {
194
{ "clr", vlclua_fd_clr },
195
{ "isset", vlclua_fd_isset },
196
{ "set", vlclua_fd_set },
197
{ "zero", vlclua_fd_zero },
201
static int vlclua_fd_set_new( lua_State *L )
203
fd_set *fds = (fd_set*)lua_newuserdata( L, sizeof( fd_set ) );
206
if( luaL_newmetatable( L, "fd_set" ) )
209
luaL_register( L, NULL, vlclua_fd_set_reg );
210
lua_setfield( L, -2, "__index" );
213
lua_setmetatable( L, -2 );
217
static int vlclua_fd_clr( lua_State *L )
219
fd_set *fds = (fd_set*)luaL_checkudata( L, 1, "fd_set" );
220
int i_fd = luaL_checkint( L, 2 );
225
static int vlclua_fd_isset( lua_State *L )
227
fd_set *fds = (fd_set*)luaL_checkudata( L, 1, "fd_set" );
228
int i_fd = luaL_checkint( L, 2 );
229
lua_pushboolean( L, FD_ISSET( i_fd, fds ) );
233
static int vlclua_fd_set( lua_State *L )
235
fd_set *fds = (fd_set*)luaL_checkudata( L, 1, "fd_set" );
236
size_t i_fd = luaL_checkint( L, 2 );
237
/* FIXME: we should really use poll() instead here, but that breaks the
238
* VLC/LUA API. On Windows, overflow protection is built-in FD_SET, not
239
* on POSIX. In both cases, run-time behavior will however be wrong. */
241
if( i_fd < FD_SETSIZE )
247
static int vlclua_fd_zero( lua_State *L )
249
fd_set *fds = (fd_set*)luaL_checkudata( L, 1, "fd_set" );
254
233
/*****************************************************************************
290
269
#ifdef HAVE_SYS_STAT_H
291
270
const char *psz_path = luaL_checkstring( L, 1 );
293
if( utf8_stat( psz_path, &s ) )
272
if( vlc_stat( psz_path, &s ) )
295
274
//return luaL_error( L, "Couldn't stat %s.", psz_path );
296
275
lua_newtable( L );
297
276
if( S_ISREG( s.st_mode ) )
298
lua_pushstring( L, "file" );
277
lua_pushliteral( L, "file" );
299
278
else if( S_ISDIR( s.st_mode ) )
300
lua_pushstring( L, "dir" );
279
lua_pushliteral( L, "dir" );
302
281
else if( S_ISCHR( s.st_mode ) )
303
lua_pushstring( L, "character device" );
282
lua_pushliteral( L, "character device" );
306
285
else if( S_ISBLK( s.st_mode ) )
307
lua_pushstring( L, "block device" );
286
lua_pushliteral( L, "block device" );
310
289
else if( S_ISFIFO( s.st_mode ) )
311
lua_pushstring( L, "fifo" );
290
lua_pushliteral( L, "fifo" );
314
293
else if( S_ISLNK( s.st_mode ) )
315
lua_pushstring( L, "symbolic link" );
294
lua_pushliteral( L, "symbolic link" );
318
297
else if( S_ISSOCK( s.st_mode ) )
319
lua_pushstring( L, "socket" );
298
lua_pushliteral( L, "socket" );
322
lua_pushstring( L, "unknown" );
301
lua_pushliteral( L, "unknown" );
323
302
lua_setfield( L, -2, "type" );
324
303
lua_pushinteger( L, s.st_mode );
325
304
lua_setfield( L, -2, "mode" );
371
350
static const luaL_Reg vlclua_net_reg[] = {
372
351
{ "url_parse", vlclua_url_parse },
373
352
{ "listen_tcp", vlclua_net_listen_tcp },
353
{ "connect_tcp", vlclua_net_connect_tcp },
374
354
{ "close", vlclua_net_close },
375
355
{ "send", vlclua_net_send },
376
356
{ "recv", vlclua_net_recv },
377
{ "select", vlclua_net_select },
378
{ "fd_set_new", vlclua_fd_set_new },
357
{ "poll", vlclua_net_poll },
379
358
{ "read", vlclua_fd_read },
380
359
{ "write", vlclua_fd_write },
381
360
{ "stat", vlclua_stat }, /* Not really "net" */
388
367
lua_newtable( L );
389
368
luaL_register( L, NULL, vlclua_net_reg );
369
#define ADD_CONSTANT( name, value ) \
370
lua_pushinteger( L, value ); \
371
lua_setfield( L, -2, name );
372
ADD_CONSTANT( "POLLIN", POLLIN )
373
ADD_CONSTANT( "POLLPRI", POLLPRI )
374
ADD_CONSTANT( "POLLOUT", POLLOUT )
375
ADD_CONSTANT( "POLLERR", POLLERR )
376
ADD_CONSTANT( "POLLHUP", POLLHUP )
377
ADD_CONSTANT( "POLLNVAL", POLLNVAL )
390
378
lua_setfield( L, -2, "net" );