2007-08-30 02:11:09 +08:00
|
|
|
<?php
|
|
|
|
|
2009-04-03 20:10:54 +08:00
|
|
|
define( "MSG_TIMEOUT", ZM_WEB_AJAX_TIMEOUT );
|
2007-08-30 02:11:09 +08:00
|
|
|
define( "MSG_DATA_SIZE", 4+256 );
|
|
|
|
|
|
|
|
if ( !($_REQUEST['connkey'] && $_REQUEST['command']) )
|
|
|
|
{
|
2008-10-17 00:12:23 +08:00
|
|
|
ajaxError( "Unexpected received message type '$type'" );
|
2007-08-30 02:11:09 +08:00
|
|
|
}
|
|
|
|
|
2008-10-17 00:12:23 +08:00
|
|
|
if ( !($socket = @socket_create( AF_UNIX, SOCK_DGRAM, 0 )) )
|
2007-08-30 02:11:09 +08:00
|
|
|
{
|
2008-10-17 00:12:23 +08:00
|
|
|
ajaxError( "socket_create() failed: ".socket_strerror(socket_last_error()) );
|
2007-08-30 02:11:09 +08:00
|
|
|
}
|
2008-07-16 23:19:12 +08:00
|
|
|
$locSockFile = ZM_PATH_SOCKS.'/zms-'.sprintf("%06d",$_REQUEST['connkey']).'w.sock';
|
2008-10-17 00:12:23 +08:00
|
|
|
if ( !@socket_bind( $socket, $locSockFile ) )
|
2007-08-30 02:11:09 +08:00
|
|
|
{
|
2011-02-06 23:28:01 +08:00
|
|
|
ajaxError( "socket_bind( $locSockFile ) failed: ".socket_strerror(socket_last_error()) );
|
2007-08-30 02:11:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
switch ( $_REQUEST['command'] )
|
|
|
|
{
|
2008-11-03 21:16:47 +08:00
|
|
|
case CMD_VARPLAY :
|
2011-06-21 17:19:10 +08:00
|
|
|
Debug( "Varplaying to ".$_REQUEST['rate'] );
|
2008-12-06 07:56:15 +08:00
|
|
|
$msg = pack( "lcn", MSG_CMD, $_REQUEST['command'], $_REQUEST['rate']+32768 );
|
2008-11-03 21:16:47 +08:00
|
|
|
break;
|
2007-08-30 02:11:09 +08:00
|
|
|
case CMD_ZOOMIN :
|
2011-06-21 17:19:10 +08:00
|
|
|
Debug( "Zooming to ".$_REQUEST['x'].",".$_REQUEST['y'] );
|
2008-12-06 07:42:35 +08:00
|
|
|
$msg = pack( "lcnn", MSG_CMD, $_REQUEST['command'], $_REQUEST['x'], $_REQUEST['y'] );
|
2007-08-30 02:11:09 +08:00
|
|
|
break;
|
|
|
|
case CMD_PAN :
|
2011-06-21 17:19:10 +08:00
|
|
|
Debug( "Panning to ".$_REQUEST['x'].",".$_REQUEST['y'] );
|
2008-12-06 07:42:35 +08:00
|
|
|
$msg = pack( "lcnn", MSG_CMD, $_REQUEST['command'], $_REQUEST['x'], $_REQUEST['y'] );
|
2007-08-30 02:11:09 +08:00
|
|
|
break;
|
|
|
|
case CMD_SCALE :
|
2011-06-21 17:19:10 +08:00
|
|
|
Debug( "Scaling to ".$_REQUEST['scale'] );
|
2008-12-06 07:42:35 +08:00
|
|
|
$msg = pack( "lcn", MSG_CMD, $_REQUEST['command'], $_REQUEST['scale'] );
|
2007-08-30 02:11:09 +08:00
|
|
|
break;
|
|
|
|
case CMD_SEEK :
|
2011-06-21 17:19:10 +08:00
|
|
|
Debug( "Seeking to ".$_REQUEST['offset'] );
|
2008-12-06 07:42:35 +08:00
|
|
|
$msg = pack( "lcN", MSG_CMD, $_REQUEST['command'], $_REQUEST['offset'] );
|
2007-08-30 02:11:09 +08:00
|
|
|
break;
|
|
|
|
default :
|
2008-12-06 07:42:35 +08:00
|
|
|
$msg = pack( "lc", MSG_CMD, $_REQUEST['command'] );
|
2007-08-30 02:11:09 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-07-16 23:19:12 +08:00
|
|
|
$remSockFile = ZM_PATH_SOCKS.'/zms-'.sprintf("%06d",$_REQUEST['connkey']).'s.sock';
|
2014-07-11 22:51:24 +08:00
|
|
|
$max_socket_tries = 10;
|
2009-05-08 18:04:12 +08:00
|
|
|
while ( !file_exists($remSockFile) && $max_socket_tries-- ) //sometimes we are too fast for our own good, if it hasn't been setup yet give it a second.
|
2015-06-05 23:49:05 +08:00
|
|
|
usleep(200000);
|
2009-05-08 18:04:12 +08:00
|
|
|
|
2008-07-16 23:19:12 +08:00
|
|
|
if ( !@socket_sendto( $socket, $msg, strlen($msg), 0, $remSockFile ) )
|
2007-08-30 02:11:09 +08:00
|
|
|
{
|
2008-10-30 07:08:21 +08:00
|
|
|
ajaxError( "socket_sendto( $remSockFile ) failed: ".socket_strerror(socket_last_error()) );
|
2007-08-30 02:11:09 +08:00
|
|
|
}
|
|
|
|
|
2008-09-26 17:47:20 +08:00
|
|
|
$rSockets = array( $socket );
|
|
|
|
$wSockets = NULL;
|
2008-07-16 23:19:12 +08:00
|
|
|
$eSockets = NULL;
|
2011-06-28 00:15:16 +08:00
|
|
|
$numSockets = @socket_select( $rSockets, $wSockets, $eSockets, intval(MSG_TIMEOUT/1000), (MSG_TIMEOUT%1000)*1000 );
|
2007-08-30 02:11:09 +08:00
|
|
|
|
2010-11-03 23:45:44 +08:00
|
|
|
if ( $numSockets === false )
|
2009-04-02 03:39:23 +08:00
|
|
|
{
|
|
|
|
ajaxError( "socket_select failed: ".socket_strerror(socket_last_error()) );
|
|
|
|
}
|
2013-09-12 22:51:51 +08:00
|
|
|
else if ( $numSockets < 0 )
|
|
|
|
{
|
2014-07-24 23:05:35 +08:00
|
|
|
ajaxError( "Socket closed $remSockFile" );
|
2013-09-12 22:51:51 +08:00
|
|
|
}
|
2009-04-02 03:39:23 +08:00
|
|
|
else if ( $numSockets == 0 )
|
2007-08-30 02:11:09 +08:00
|
|
|
{
|
2014-07-24 23:05:35 +08:00
|
|
|
ajaxError( "Timed out waiting for msg $remSockFile" );
|
2007-08-30 02:11:09 +08:00
|
|
|
}
|
2008-07-16 23:19:12 +08:00
|
|
|
else if ( $numSockets > 0 )
|
2007-08-30 02:11:09 +08:00
|
|
|
{
|
2008-07-16 23:19:12 +08:00
|
|
|
if ( count($rSockets) != 1 )
|
2009-04-02 03:39:23 +08:00
|
|
|
ajaxError( "Bogus return from select, ".count($rSockets)." sockets available" );
|
2007-08-30 02:11:09 +08:00
|
|
|
}
|
|
|
|
|
2008-10-30 07:08:21 +08:00
|
|
|
switch( $nbytes = @socket_recvfrom( $socket, $msg, MSG_DATA_SIZE, 0, $remSockFile ) )
|
2007-08-30 02:11:09 +08:00
|
|
|
{
|
|
|
|
case -1 :
|
|
|
|
{
|
2008-10-30 07:08:21 +08:00
|
|
|
ajaxError( "socket_recvfrom( $remSockFile ) failed: ".socket_strerror(socket_last_error()) );
|
2008-10-17 00:12:23 +08:00
|
|
|
break;
|
2007-08-30 02:11:09 +08:00
|
|
|
}
|
|
|
|
case 0 :
|
|
|
|
{
|
2008-10-17 00:12:23 +08:00
|
|
|
ajaxError( "No data to read from socket" );
|
|
|
|
break;
|
2007-08-30 02:11:09 +08:00
|
|
|
}
|
|
|
|
default :
|
|
|
|
{
|
|
|
|
if ( $nbytes != MSG_DATA_SIZE )
|
2008-10-17 00:12:23 +08:00
|
|
|
ajaxError( "Got unexpected message size, got $nbytes, expected ".MSG_DATA_SIZE );
|
2007-08-30 02:11:09 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = unpack( "ltype", $msg );
|
|
|
|
switch ( $data['type'] )
|
|
|
|
{
|
|
|
|
case MSG_DATA_WATCH :
|
|
|
|
{
|
2009-03-08 22:34:09 +08:00
|
|
|
$data = unpack( "ltype/imonitor/istate/dfps/ilevel/irate/ddelay/izoom/Cdelayed/Cpaused/Cenabled/Cforced", $msg );
|
2007-08-30 02:11:09 +08:00
|
|
|
$data['fps'] = sprintf( "%.2f", $data['fps'] );
|
2008-12-06 07:42:35 +08:00
|
|
|
$data['rate'] /= RATE_BASE;
|
2007-08-30 02:11:09 +08:00
|
|
|
$data['delay'] = sprintf( "%.2f", $data['delay'] );
|
2008-12-06 07:42:35 +08:00
|
|
|
$data['zoom'] = sprintf( "%.1f", $data['zoom']/SCALE_BASE );
|
2008-10-17 00:12:23 +08:00
|
|
|
ajaxResponse( array( 'status'=>$data ) );
|
2007-08-30 02:11:09 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MSG_DATA_EVENT :
|
|
|
|
{
|
2009-03-08 22:34:09 +08:00
|
|
|
$data = unpack( "ltype/ievent/iprogress/irate/izoom/Cpaused", $msg );
|
2007-08-30 02:11:09 +08:00
|
|
|
//$data['progress'] = sprintf( "%.2f", $data['progress'] );
|
2008-12-06 07:42:35 +08:00
|
|
|
$data['rate'] /= RATE_BASE;
|
|
|
|
$data['zoom'] = sprintf( "%.1f", $data['zoom']/SCALE_BASE );
|
2008-10-17 00:12:23 +08:00
|
|
|
ajaxResponse( array( 'status'=>$data ) );
|
2007-08-30 02:11:09 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default :
|
|
|
|
{
|
2008-10-17 00:12:23 +08:00
|
|
|
ajaxError( "Unexpected received message type '$type'" );
|
2007-08-30 02:11:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-17 00:12:23 +08:00
|
|
|
ajaxError( 'Unrecognised action or insufficient permissions' );
|
2007-08-30 02:11:09 +08:00
|
|
|
|
2008-10-17 00:12:23 +08:00
|
|
|
function ajaxCleanup()
|
|
|
|
{
|
|
|
|
global $socket, $locSockFile;
|
|
|
|
if ( !empty( $socket ) )
|
|
|
|
@socket_close( $socket );
|
|
|
|
if ( !empty( $locSockFile ) )
|
|
|
|
@unlink( $locSockFile );
|
|
|
|
}
|
2007-08-30 02:11:09 +08:00
|
|
|
?>
|