-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSocket.php
139 lines (107 loc) · 3.24 KB
/
Socket.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
declare(strict_types=1);
require_once('VO/Server.php');
final class Socket
{
private $server;
private $resource;
private $connectionTimeout;
/**
* @throws RuntimeException
*/
private function __construct(Server $server, int $connectionTimeout = 5) {
$this->server = $server;
$this->connectionTimeout = $connectionTimeout;
if(!function_exists('fsockopen')) {
throw new RuntimeException('fsockopen is not available');
}
$this->resource = $this->getSocket();
}
public function __destruct() {
$this->closeSocket();
}
public static function open(Server $server, int $connectionTimeout = 5): self {
return new self($server, $connectionTimeout);
}
public function getServer(): Server {
return $this->server;
}
public function getConnectionTimeout(): int {
return $this->connectionTimeout;
}
/**
* @throws RuntimeException
*/
private function getSocket() {
$errno = 0;
$errstr = '';
$resource = fsockopen(
'udp://' . $this->server->getHost(),
$this->server->getPort(),
$errno,
$errstr,
$this->connectionTimeout
);
if($errno !== 0 || !is_resource($resource)) {
throw new RuntimeException(sprintf('Failed to open socket (error %d: %s).', $errno, $errstr));
}
stream_set_timeout($resource, $this->connectionTimeout);
return $resource;
}
private function closeSocket(): bool {
if(is_resource($this->resource)) {
return fclose($this->resource);
}
return false;
}
/**
* @throws RuntimeException
*/
public function send(Packet $packet): int {
$bytes = fwrite($this->resource, (string) $packet);
if($bytes === false) {
throw new RuntimeException('Failed to send packet.');
}
return $bytes;
}
public function readBool(int $bytes = 1): bool {
return (bool) $this->readInt($bytes);
}
/**
* @throws InvalidArgumentException
*/
public function readInt(int $bytes): int {
if($bytes < 1 || $bytes > 4) {
throw new InvalidArgumentException(sprintf('cannot read integer of %d bytes', $bytes));
}
$buffer = $this->readRaw($bytes);
$int = 0;
for($i = 0; $i < $bytes; $i++) {
$int |= ord($buffer{$i}) << ($i * 8);
}
return $int;
}
public function readInt8(): int {
return $this->readInt(1);
}
public function readInt16(): int {
return $this->readInt(2);
}
public function readInt32(): int {
return $this->readInt(4);
}
public function readString(int $lengthBytes = 4): string {
$strlen = $this->readInt($lengthBytes);
return ($strlen > 0) ? $this->readRaw($strlen) : '';
}
/**
* @throws RuntimeException
*/
public function readRaw(int $bytes): string {
$response = fread($this->resource, $bytes);
if($response === false) {
throw new RuntimeException(sprintf('Failed to read response (bytes: %d).', $bytes));
}
return $response;
}
}