[vlc-devel] commit: Fix blocking/non-blocking accept in lua interface. ( Antoine Cellerier )
git version control
git at videolan.org
Mon Dec 21 21:56:12 CET 2009
vlc | branch: master | Antoine Cellerier <antoine at macbook.(none)> | Sun Dec 20 14:24:37 2009 +0100| [8241e58419c8eb84bb02117467b6e8957e1ab0ae] | committer: Antoine Cellerier
Fix blocking/non-blocking accept in lua interface.
> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=8241e58419c8eb84bb02117467b6e8957e1ab0ae
---
modules/misc/lua/intf.c | 2 ++
modules/misc/lua/libs/net.c | 35 ++++++++++++++++++++++++++++++++++-
2 files changed, 36 insertions(+), 1 deletions(-)
diff --git a/modules/misc/lua/intf.c b/modules/misc/lua/intf.c
index f9f0dce..d7430bc 100644
--- a/modules/misc/lua/intf.c
+++ b/modules/misc/lua/intf.c
@@ -298,6 +298,8 @@ void Close_LuaIntf( vlc_object_t *p_this )
intf_thread_t *p_intf = (intf_thread_t*)p_this;
intf_sys_t *p_sys = p_intf->p_sys;
+ vlc_cancel( p_sys->thread );
+
if( !p_sys->exiting ) /* <- Read-only here and in thread: no locking */
{
vlc_mutex_lock( &p_sys->lock );
diff --git a/modules/misc/lua/libs/net.c b/modules/misc/lua/libs/net.c
index 321da9e..4e4b578 100644
--- a/modules/misc/lua/libs/net.c
+++ b/modules/misc/lua/libs/net.c
@@ -39,6 +39,10 @@
#include <lua.h> /* Low level lua C API */
#include <lauxlib.h> /* Higher level C API */
+#ifdef HAVE_POLL
+#include <poll.h> /* poll structures and defines */
+#endif
+
#include "../vlc.h"
#include "../libs.h"
@@ -121,7 +125,36 @@ static int vlclua_net_accept( lua_State *L )
{
vlc_object_t *p_this = vlclua_get_this( L );
int **ppi_fd = (int**)luaL_checkudata( L, 1, "net_listen" );
- int i_fd = net_Accept( p_this, *ppi_fd );
+ int *pi_fd = *ppi_fd;
+ int i_timeout = luaL_optint( L, 2, -1 ); /* block is default */
+
+ /* Implement net_Accept with timeout */
+ int i_fd = -1;
+
+ unsigned int i_count = 1;
+ while( pi_fd[0][i_count] != -1 )
+ i_count++;
+
+ struct pollfd ufd[i_count+1];
+ unsigned int i;
+ for( i = 0; i < i_count; i++ )
+ {
+ ufd[i].fd = pi_fd[i];
+ ufd[i].events = POLLIN;
+ }
+
+ if( poll( ufd, i_count, i_timeout ) > 0 )
+ {
+ for( i = 0; i < i_count; i++ )
+ {
+ if( !ufd[i].revents ) continue;
+ i_fd = net_AcceptSingle( p_this, ufd[i].fd );
+ if( i_fd == -1 ) continue;
+ memmove( pi_fd + i, pi_fd + i + 1, i_count - (i + 1) );
+ pi_fd[i_count - 1] = ufd[i].fd;
+ }
+ }
+
lua_pushinteger( L, i_fd );
return 1;
}
More information about the vlc-devel
mailing list