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
|
<?php
function startrailcontrol() {
exec('./startrailcontrol.sh');
// open control socket
$port = 2222;
$address = '::1';
$connection_wait = 100;
while (true) {
if ($connection_wait == 0) {
echo "Too many failers, exiting\n";
exit;
}
sleep(1);
$socket = socket_create(AF_INET6, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
//echo "Unable to create Socket: " . socket_strerror(socket_last_error()) . "\n";
$connection_wait--;
continue;
}
$connection = socket_connect($socket, $address, $port);
if ($connection === false) {
//echo "Unable to connect to [$address]:$port " . socket_strerror(socket_last_error($socket)) . "\n";
socket_close($socket);
$connection_wait--;
continue;
}
break;
}
// read wellcome message
$data = socket_read($socket, 128);
return $socket;
}
function stoprailcontrol($socket, $message = '') {
$ret = 0;
if ($message != '') {
echo "$message\n";
$ret = 1;
}
// close control socket
socket_write($socket, "q\n");
socket_close($socket);
exec('./stoprailcontrol.sh');
exit ($ret);
}
?>
|