forked from pmmp/RakLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RakLib.php
160 lines (136 loc) · 3.25 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
<?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.
*
*/
namespace raklib;
//Dependencies check
$errors = 0;
if(version_compare("7.0", PHP_VERSION) > 0){
echo "[CRITICAL] Use PHP >= 7.0" . PHP_EOL;
++$errors;
}
if(!extension_loaded("sockets")){
echo "[CRITICAL] Unable to find the Socket extension." . PHP_EOL;
++$errors;
}
if(!extension_loaded("pthreads")){
echo "[CRITICAL] Unable to find the pthreads extension." . PHP_EOL;
++$errors;
}else{
$pthreads_version = phpversion("pthreads");
if(substr_count($pthreads_version, ".") < 2){
$pthreads_version = "0.$pthreads_version";
}
if(version_compare($pthreads_version, "3.0.0") < 0){
echo "[CRITICAL] pthreads >= 3.0.0 is required, while you have $pthreads_version.";
++$errors;
}
}
if($errors > 0){
exit(1); //Exit with error
}
unset($errors);
abstract class RakLib{
const VERSION = "0.8.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;
/*
* 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;
/*
* RAW payload:
* byte (address length)
* byte[] (address)
* int (timeout)
*/
const PACKET_BLOCK_ADDRESS = 0x09;
/*
* 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;
public static function bootstrap(\ClassLoader $loader){
$loader->addPath(dirname(__FILE__) . DIRECTORY_SEPARATOR . "..");
}
}