-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathretrieve.class.php
85 lines (76 loc) · 1.85 KB
/
retrieve.class.php
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
namespace FreedomForged\XPlaneData;
require('dataLabels.php');
/**
*
* X-Plane 10 UDP Parsing class for dummies (or something)
* @author Jason Gillman Jr. jason@rrfaae.com
*
*/
class retrieve
{
/**
*
* @var string $listenAddr The address you want to listen on (0.0.0.0 for all available, 127.0.0.1 default)
*
*/
private $listenAddr;
/**
*
* @var integer $listenPort The port you want to listen on (49002 default)
*
*/
private $listenPort;
/**
*
* @var resource $sock The created socket resource
*
*/
private $sock;
/**
*
* Construct
*
*/
public function __construct($listenAddr = '127.0.0.1', $listenPort = 49002)
{
$this->listenAddr = $listenAddr;
$this->listenPort = $listenPort;
}
/**
*
* @return array The unpacked data
* Pull all the available dataz
*
*/
public function getData()
{
/**
* The creation and closing of the socket at the time of the method call is required.
* If this isn't done, the socket queue fills up, so subsequent calls will show stale data.
* If someone can figure out a more elegant way of doing this, please let me know!
*/
$this->sock = socket_create(AF_INET, SOCK_DGRAM, 17); // 17 == udp
socket_bind($this->sock, $this->listenAddr, $this->listenPort);
$pkgSize = socket_recv($this->sock, $pkg, 65500, 0); // Flags param is required. Send it 0 to indicate no flags?
$sentenceCount = (strlen($pkg) - 5) / 36;
$formatBase = '@5/Isentence 1/@9/f8data 1 - ';
if($sentenceCount > 1)
{
$format = $formatBase;
$i = 2;
for($i = 2; $i <= $sentenceCount; $i++)
{
$format .= '/Isentence ' . $i . '/f8data ' . $i . ' - ';
}
}
else
{
$format = $formatBase;
}
$rawData = unpack($format, $pkg);
socket_close($this->sock);
return convertToLabels($rawData);
}
}
?>