-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstatsapi.php
More file actions
39 lines (29 loc) · 929 Bytes
/
statsapi.php
File metadata and controls
39 lines (29 loc) · 929 Bytes
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
<?
class Stats {
private static $collector = false;
private static $socket = null;
// Expects array('host' => '...', 'port' => '...')
public static function bind($collector, $resolve = true) {
if (is_null(self::$socket)) {
self::$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
}
self::$collector = $collector;
if ($resolve) {
// resolve the hostname so we don't have to do this on each message we send
$address = gethostbyname(self::$collector['host']);
if (ip2long($address) !== false) {
self::$collector['host'] = $address;
}
}
}
public static function send($message) {
if (self::$collector === false) {
return;
}
settype($message, 'string');
if (strlen($message) == 0) {
return;
}
socket_sendto(self::$socket, $message, strlen($message), 0, self::$collector['host'], self::$collector['port']);
}
}