Commit bfe17fd3 authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

lua: vector out files descriptable table code

parent 6da72c20
...@@ -223,8 +223,6 @@ static int Start_LuaIntf( vlc_object_t *p_this, const char *name ) ...@@ -223,8 +223,6 @@ static int Start_LuaIntf( vlc_object_t *p_this, const char *name )
} }
p_intf->p_sys = p_sys; p_intf->p_sys = p_sys;
vlclua_fd_init( p_sys );
p_sys->psz_filename = vlclua_find_file( "intf", name ); p_sys->psz_filename = vlclua_find_file( "intf", name );
if( !p_sys->psz_filename ) if( !p_sys->psz_filename )
{ {
...@@ -255,7 +253,11 @@ static int Start_LuaIntf( vlc_object_t *p_this, const char *name ) ...@@ -255,7 +253,11 @@ static int Start_LuaIntf( vlc_object_t *p_this, const char *name )
luaopen_input( L ); luaopen_input( L );
luaopen_msg( L ); luaopen_msg( L );
luaopen_misc( L ); luaopen_misc( L );
luaopen_net_intf( L ); if( vlclua_fd_init( L, &p_sys->dtable ) )
{
lua_close( L );
goto error;
}
luaopen_object( L ); luaopen_object( L );
luaopen_osd( L ); luaopen_osd( L );
luaopen_playlist( L ); luaopen_playlist( L );
...@@ -364,20 +366,9 @@ static int Start_LuaIntf( vlc_object_t *p_this, const char *name ) ...@@ -364,20 +366,9 @@ static int Start_LuaIntf( vlc_object_t *p_this, const char *name )
p_sys->L = L; p_sys->L = L;
#ifndef _WIN32
if( vlc_pipe( p_sys->fd ) )
{
lua_close( p_sys->L );
goto error;
}
#else
# define close(fd) (void)0
#endif
if( vlc_clone( &p_sys->thread, Run, p_intf, VLC_THREAD_PRIORITY_LOW ) ) if( vlc_clone( &p_sys->thread, Run, p_intf, VLC_THREAD_PRIORITY_LOW ) )
{ {
close( p_sys->fd[1] ); vlclua_fd_cleanup( &p_sys->dtable );
close( p_sys->fd[0] );
lua_close( p_sys->L ); lua_close( p_sys->L );
goto error; goto error;
} }
...@@ -396,12 +387,11 @@ void Close_LuaIntf( vlc_object_t *p_this ) ...@@ -396,12 +387,11 @@ void Close_LuaIntf( vlc_object_t *p_this )
intf_thread_t *p_intf = (intf_thread_t*)p_this; intf_thread_t *p_intf = (intf_thread_t*)p_this;
intf_sys_t *p_sys = p_intf->p_sys; intf_sys_t *p_sys = p_intf->p_sys;
close( p_sys->fd[1] ); vlclua_fd_interrupt( &p_sys->dtable );
vlc_join( p_sys->thread, NULL ); vlc_join( p_sys->thread, NULL );
vlclua_fd_cleanup( &p_sys->dtable );
lua_close( p_sys->L ); lua_close( p_sys->L );
close( p_sys->fd[0] );
vlclua_fd_destroy( p_sys );
free( p_sys->psz_filename ); free( p_sys->psz_filename );
free( p_sys ); free( p_sys );
} }
......
...@@ -31,7 +31,6 @@ void luaopen_input( lua_State * ); ...@@ -31,7 +31,6 @@ void luaopen_input( lua_State * );
void luaopen_msg( lua_State * ); void luaopen_msg( lua_State * );
void luaopen_misc( lua_State * ); void luaopen_misc( lua_State * );
void luaopen_net_generic( lua_State * ); void luaopen_net_generic( lua_State * );
void luaopen_net_intf( lua_State * );
void luaopen_object( lua_State * ); void luaopen_object( lua_State * );
void luaopen_osd( lua_State * ); void luaopen_osd( lua_State * );
void luaopen_playlist( lua_State * ); void luaopen_playlist( lua_State * );
......
...@@ -42,50 +42,40 @@ ...@@ -42,50 +42,40 @@
#include <vlc_network.h> #include <vlc_network.h>
#include <vlc_url.h> #include <vlc_url.h>
#include <vlc_fs.h> #include <vlc_fs.h>
#include <vlc_interface.h>
#include "../vlc.h" #include "../vlc.h"
#include "../libs.h" #include "../libs.h"
#include "misc.h"
void vlclua_fd_init( intf_sys_t *sys ) static vlclua_dtable_t *vlclua_get_dtable( lua_State *L )
{ {
sys->fdv = NULL; return vlclua_get_object( L, vlclua_get_dtable );
sys->fdc = 0;
}
/** Releases all (leaked) VLC Lua file descriptors. */
void vlclua_fd_destroy( intf_sys_t *sys )
{
for( unsigned i = 0; i < sys->fdc; i++ )
net_Close( sys->fdv[i] );
free( sys->fdv );
} }
/** Maps an OS file descriptor to a VLC Lua file descriptor */ /** Maps an OS file descriptor to a VLC Lua file descriptor */
static int vlclua_fd_map( lua_State *L, int fd ) static int vlclua_fd_map( lua_State *L, int fd )
{ {
intf_thread_t *intf = (intf_thread_t *)vlclua_get_this( L ); vlclua_dtable_t *dt = vlclua_get_dtable( L );
intf_sys_t *sys = intf->p_sys;
if( (unsigned)fd < 3u ) if( (unsigned)fd < 3u )
return -1; return -1;
#ifndef NDEBUG #ifndef NDEBUG
for( unsigned i = 0; i < sys->fdc; i++ ) for( unsigned i = 0; i < dt->fdc; i++ )
assert( sys->fdv[i] != fd ); assert( dt->fdv[i] != fd );
#endif #endif
if( sys->fdc >= 64 ) if( dt->fdc >= 64 )
return -1; return -1;
int *fdv = realloc( sys->fdv, (sys->fdc + 1) * sizeof (sys->fdv[0]) ); int *fdv = realloc( dt->fdv, (dt->fdc + 1) * sizeof (dt->fdv[0]) );
if( unlikely(fdv == NULL) ) if( unlikely(fdv == NULL) )
return -1; return -1;
sys->fdv = fdv; dt->fdv = fdv;
sys->fdv[sys->fdc] = fd; dt->fdv[dt->fdc] = fd;
fd = 3 + sys->fdc; fd = 3 + dt->fdc;
sys->fdc++; dt->fdc++;
return fd; return fd;
} }
...@@ -100,25 +90,23 @@ static int vlclua_fd_map_safe( lua_State *L, int fd ) ...@@ -100,25 +90,23 @@ static int vlclua_fd_map_safe( lua_State *L, int fd )
/** Gets the OS file descriptor mapped to a VLC Lua file descriptor */ /** Gets the OS file descriptor mapped to a VLC Lua file descriptor */
static int vlclua_fd_get( lua_State *L, unsigned idx ) static int vlclua_fd_get( lua_State *L, unsigned idx )
{ {
intf_thread_t *intf = (intf_thread_t *)vlclua_get_this( L ); vlclua_dtable_t *dt = vlclua_get_dtable( L );
intf_sys_t *sys = intf->p_sys;
if( idx < 3u ) if( idx < 3u )
return idx; return idx;
idx -= 3; idx -= 3;
return (idx < sys->fdc) ? sys->fdv[idx] : -1; return (idx < dt->fdc) ? dt->fdv[idx] : -1;
} }
/** Gets the VLC Lua file descriptor mapped from an OS file descriptor */ /** Gets the VLC Lua file descriptor mapped from an OS file descriptor */
static int vlclua_fd_get_lua( lua_State *L, int fd ) static int vlclua_fd_get_lua( lua_State *L, int fd )
{ {
intf_thread_t *intf = (intf_thread_t *)vlclua_get_this( L ); vlclua_dtable_t *dt = vlclua_get_dtable( L );
intf_sys_t *sys = intf->p_sys;
if( (unsigned)fd < 3u ) if( (unsigned)fd < 3u )
return fd; return fd;
for( unsigned i = 0; i < sys->fdc; i++ ) for( unsigned i = 0; i < dt->fdc; i++ )
if( sys->fdv[i] == fd ) if( dt->fdv[i] == fd )
return 3 + i; return 3 + i;
return -1; return -1;
} }
...@@ -126,25 +114,24 @@ static int vlclua_fd_get_lua( lua_State *L, int fd ) ...@@ -126,25 +114,24 @@ static int vlclua_fd_get_lua( lua_State *L, int fd )
/** Unmaps an OS file descriptor from VLC Lua */ /** Unmaps an OS file descriptor from VLC Lua */
static void vlclua_fd_unmap( lua_State *L, unsigned idx ) static void vlclua_fd_unmap( lua_State *L, unsigned idx )
{ {
intf_thread_t *intf = (intf_thread_t *)vlclua_get_this( L ); vlclua_dtable_t *dt = vlclua_get_dtable( L );
intf_sys_t *sys = intf->p_sys;
int fd = -1; int fd = -1;
if( idx < 3u ) if( idx < 3u )
return; /* Never close stdin/stdout/stderr. */ return; /* Never close stdin/stdout/stderr. */
idx -= 3; idx -= 3;
if( idx >= sys->fdc ) if( idx >= dt->fdc )
return; return;
fd = sys->fdv[idx]; fd = dt->fdv[idx];
sys->fdc--; dt->fdc--;
memmove( sys->fdv + idx, sys->fdv + idx + 1, memmove( dt->fdv + idx, dt->fdv + idx + 1,
(sys->fdc - idx) * sizeof (sys->fdv[0]) ); (dt->fdc - idx) * sizeof (dt->fdv[0]) );
/* realloc() not really needed */ /* realloc() not really needed */
#ifndef NDEBUG #ifndef NDEBUG
for( unsigned i = 0; i < sys->fdc; i++ ) for( unsigned i = 0; i < dt->fdc; i++ )
assert( sys->fdv[i] != fd ); assert( dt->fdv[i] != fd );
#endif #endif
} }
...@@ -322,8 +309,7 @@ static int vlclua_net_recv( lua_State *L ) ...@@ -322,8 +309,7 @@ static int vlclua_net_recv( lua_State *L )
/* Takes a { fd : events } table as first arg and modifies it to { fd : revents } */ /* Takes a { fd : events } table as first arg and modifies it to { fd : revents } */
static int vlclua_net_poll( lua_State *L ) static int vlclua_net_poll( lua_State *L )
{ {
intf_thread_t *intf = (intf_thread_t *)vlclua_get_this( L ); vlclua_dtable_t *dt = vlclua_get_dtable( L );
intf_sys_t *sys = intf->p_sys;
luaL_checktype( L, 1, LUA_TTABLE ); luaL_checktype( L, 1, LUA_TTABLE );
...@@ -340,7 +326,7 @@ static int vlclua_net_poll( lua_State *L ) ...@@ -340,7 +326,7 @@ static int vlclua_net_poll( lua_State *L )
lua_pushnil( L ); lua_pushnil( L );
int i = 1; int i = 1;
p_fds[0].fd = sys->fd[0]; p_fds[0].fd = dt->fd[0];
p_fds[0].events = POLLIN; p_fds[0].events = POLLIN;
while( lua_next( L, 1 ) ) while( lua_next( L, 1 ) )
{ {
...@@ -509,7 +495,7 @@ static const luaL_Reg vlclua_net_intf_reg[] = { ...@@ -509,7 +495,7 @@ static const luaL_Reg vlclua_net_intf_reg[] = {
{ NULL, NULL } { NULL, NULL }
}; };
void luaopen_net_intf( lua_State *L ) static void luaopen_net_intf( lua_State *L )
{ {
lua_newtable( L ); lua_newtable( L );
luaL_register( L, NULL, vlclua_net_intf_reg ); luaL_register( L, NULL, vlclua_net_intf_reg );
...@@ -525,6 +511,40 @@ void luaopen_net_intf( lua_State *L ) ...@@ -525,6 +511,40 @@ void luaopen_net_intf( lua_State *L )
lua_setfield( L, -2, "net" ); lua_setfield( L, -2, "net" );
} }
int vlclua_fd_init( lua_State *L, vlclua_dtable_t *dt )
{
#ifndef _WIN32
if( vlc_pipe( dt->fd ) )
return -1;
#endif
dt->fdv = NULL;
dt->fdc = 0;
vlclua_set_object( L, vlclua_get_dtable, dt );
luaopen_net_intf( L );
return 0;
}
void vlclua_fd_interrupt( vlclua_dtable_t *dt )
{
#ifndef _WIN32
close( dt->fd[1] );
dt->fd[1] = -1;
#endif
}
/** Releases all (leaked) VLC Lua file descriptors. */
void vlclua_fd_cleanup( vlclua_dtable_t *dt )
{
for( unsigned i = 0; i < dt->fdc; i++ )
net_Close( dt->fdv[i] );
free( dt->fdv );
#ifndef _WIN32
if( dt->fd[1] != -1 )
close( dt->fd[1] );
close( dt->fd[0] );
#endif
}
static const luaL_Reg vlclua_net_generic_reg[] = { static const luaL_Reg vlclua_net_generic_reg[] = {
{ "url_parse", vlclua_url_parse }, { "url_parse", vlclua_url_parse },
{ "stat", vlclua_stat }, /* Not really "net" */ { "stat", vlclua_stat }, /* Not really "net" */
......
...@@ -155,23 +155,31 @@ int vlclua_playlist_add_internal( vlc_object_t *, lua_State *, playlist_t *, ...@@ -155,23 +155,31 @@ int vlclua_playlist_add_internal( vlc_object_t *, lua_State *, playlist_t *,
int vlclua_add_modules_path( lua_State *, const char *psz_filename ); int vlclua_add_modules_path( lua_State *, const char *psz_filename );
/** /**
* Per-interface private state * File descriptors table
*/ */
struct intf_sys_t typedef struct
{ {
char *psz_filename; int *fdv;
lua_State *L; unsigned fdc;
#ifndef _WIN32 #ifndef _WIN32
int fd[2]; int fd[2];
#endif #endif
int *fdv; } vlclua_dtable_t;
unsigned fdc;
int vlclua_fd_init( lua_State *, vlclua_dtable_t * );
void vlclua_fd_interrupt( vlclua_dtable_t * );
void vlclua_fd_cleanup( vlclua_dtable_t * );
/**
* Per-interface private state
*/
struct intf_sys_t
{
char *psz_filename;
lua_State *L;
vlc_thread_t thread; vlc_thread_t thread;
vlclua_dtable_t dtable;
}; };
void vlclua_fd_init( struct intf_sys_t * );
void vlclua_fd_destroy( struct intf_sys_t * );
#endif /* VLC_LUA_H */ #endif /* VLC_LUA_H */
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment