forked from pmmp/RakLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRakLib.php
192 lines (163 loc) · 4.01 KB
/
RakLib.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
/*
* RakLib network library
*
*
* This project is not affiliated with Jenkins Software LLC nor RakNet.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*/
declare(strict_types=1);
namespace raklib;
const MIN_PHP_VERSION = "7.2.0RC3";
//Dependencies check
$errors = 0;
if(version_compare(MIN_PHP_VERSION, PHP_VERSION) > 0){
echo "[CRITICAL] Use PHP >= " . MIN_PHP_VERSION . PHP_EOL;
++$errors;
}
$exts = [
"bcmath" => "BC Math",
"pthreads" => "pthreads",
"sockets" => "Sockets"
];
foreach($exts as $ext => $name){
if(!extension_loaded($ext)){
echo "[CRITICAL] Unable to find the $name ($ext) extension." . PHP_EOL;
++$errors;
}
}
if(extension_loaded("pthreads")){
$pthreads_version = phpversion("pthreads");
if(substr_count($pthreads_version, ".") < 2){
$pthreads_version = "0.$pthreads_version";
}
if(version_compare($pthreads_version, "3.1.7dev") < 0){
echo "[CRITICAL] pthreads >= 3.1.7dev is required, while you have $pthreads_version.";
++$errors;
}
}
if($errors > 0){
exit(1); //Exit with error
}
unset($errors, $exts);
abstract class RakLib{
const VERSION = "0.9.0";
const PROTOCOL = 6;
const MAGIC = "\x00\xff\xff\x00\xfe\xfe\xfe\xfe\xfd\xfd\xfd\xfd\x12\x34\x56\x78";
const PRIORITY_NORMAL = 0;
const PRIORITY_IMMEDIATE = 1;
const FLAG_NEED_ACK = 0b00001000;
/*
* These internal "packets" DO NOT exist in the RakNet protocol. They are used by the RakLib API to communicate
* messages between the RakLib thread and the implementation's thread.
*
* Internal Packet:
* int32 (length without this field)
* byte (packet ID)
* payload
*/
/*
* ENCAPSULATED payload:
* byte (identifier length)
* byte[] (identifier)
* byte (flags, last 3 bits, priority)
* payload (binary internal EncapsulatedPacket)
*/
const PACKET_ENCAPSULATED = 0x01;
/*
* OPEN_SESSION payload:
* byte (identifier length)
* byte[] (identifier)
* byte (address length)
* byte[] (address)
* short (port)
* long (clientID)
*/
const PACKET_OPEN_SESSION = 0x02;
/*
* CLOSE_SESSION payload:
* byte (identifier length)
* byte[] (identifier)
* string (reason)
*/
const PACKET_CLOSE_SESSION = 0x03;
/*
* INVALID_SESSION payload:
* byte (identifier length)
* byte[] (identifier)
*/
const PACKET_INVALID_SESSION = 0x04;
/* TODO: implement this
* SEND_QUEUE payload:
* byte (identifier length)
* byte[] (identifier)
*/
const PACKET_SEND_QUEUE = 0x05;
/*
* ACK_NOTIFICATION payload:
* byte (identifier length)
* byte[] (identifier)
* int (identifierACK)
*/
const PACKET_ACK_NOTIFICATION = 0x06;
/*
* SET_OPTION payload:
* byte (option name length)
* byte[] (option name)
* byte[] (option value)
*/
const PACKET_SET_OPTION = 0x07;
/*
* RAW payload:
* byte (address length)
* byte[] (address from/to)
* short (port)
* byte[] (payload)
*/
const PACKET_RAW = 0x08;
/*
* BLOCK_ADDRESS payload:
* byte (address length)
* byte[] (address)
* int (timeout)
*/
const PACKET_BLOCK_ADDRESS = 0x09;
/*
* UNBLOCK_ADDRESS payload:
* byte (address length)
* byte[] (address)
*/
const PACKET_UNBLOCK_ADDRESS = 0x10;
/*
* REPORT_PING payload:
* byte (identifier length)
* byte[] (identifier)
* int32 (measured latency in MS)
*/
const PACKET_REPORT_PING = 0x11;
/*
* No payload
*
* Sends the disconnect message, removes sessions correctly, closes sockets.
*/
const PACKET_SHUTDOWN = 0x7e;
/*
* No payload
*
* Leaves everything as-is and halts, other Threads can be in a post-crash condition.
*/
const PACKET_EMERGENCY_SHUTDOWN = 0x7f;
/**
* Regular RakNet uses 10 by default. MCPE uses 20. Configure this value as appropriate.
* @var int
*/
public static $SYSTEM_ADDRESS_COUNT = 20;
public static function bootstrap(\ClassLoader $loader){
$loader->addPath(dirname(__FILE__) . DIRECTORY_SEPARATOR . "..");
}
}