-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.php
More file actions
executable file
·69 lines (61 loc) · 2.03 KB
/
timer.php
File metadata and controls
executable file
·69 lines (61 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/php
<?php
error_reporting(E_ALL);
date_default_timezone_set('Europe/Berlin');
function logger($message)
{
global $key, $action;
file_put_contents(dirname(__FILE__).'/timer.log', date('Y.m.d H:i:s') . ': Timer '.$key.' for '.$action.' > '.$message."\n", FILE_APPEND);
}
$script = array_shift($argv);
$address = array_shift($argv);
$port = array_shift($argv);
$key = array_shift($argv);
$interval = array_shift($argv);
$action = array_shift($argv);
$interval = (int) $interval / 1000;
$nextCall = microtime(true)+$interval;
if (!$address || !$port) {
echo 'address or port missing!';
exit;
}
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_connect($socket, $address, $port);
$i = 0;
logger('started...');
$read = '';
do {
$wait = $nextCall - microtime(true);
//echo 'WAITING '.(int)($wait * 1000000).' microseconds';
if ($wait > 0) usleep((int)($wait * 1000000));
$sockets = array($socket);
$message = 'TIMER '.$action."\r\n\r\n".'key='.$key;
$len=strlen($message);
$offset = 0;
while ($offset < $len) {
$sent = socket_write($socket, substr($message, $offset), $len-$offset);
if ($sent === false) {
//echo 'ERROR WHILE SENDING'."\n";
// Error occurred, break the while loop
break;
}
$offset += $sent;
}
if ($offset < $len) {
$read = 'sending error: (' . socket_last_error() . ') '. socket_strerror(socket_last_error());
} else {
$read = '';
if (!socket_select($sockets, $w=null, $e=null, null)) {
$read = 'reading error: (' . socket_last_error() . ') '. socket_strerror(socket_last_error());
} else {
socket_set_nonblock($socket);
while (($buffer = @socket_read($sockets[0],96, PHP_BINARY_READ))) {
$read .= $buffer;
}
socket_set_block($socket);
}
}
$nextCall += $interval;
//var_dump($nextCall);
} while($read == 'ok' && ++$i);
logger('shutdown after '.$i.' runs / message: ' .$read);