forked from 137-rick/Dora-RPC
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPacket.php
More file actions
81 lines (64 loc) · 2.14 KB
/
Copy pathPacket.php
File metadata and controls
81 lines (64 loc) · 2.14 KB
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
<?php
namespace DoraRPC;
class Packet
{
public static function packFormat($msg = "OK", $code = 0, $data = array())
{
$pack = array(
"code" => $code,
"msg" => $msg,
"data" => $data,
);
return $pack;
}
public static function packEncode($data, $type = "tcp")
{
if ($type == "tcp") {
$sendStr = serialize($data);
//if compress the packet
if (DoraConst::SW_DATACOMPRESS_FLAG == true) {
$sendStr = gzencode($sendStr, 4);
}
if (DoraConst::SW_DATASIGEN_FLAG == true) {
$signedcode = pack('N', crc32($sendStr . DoraConst::SW_DATASIGEN_SALT));
$sendStr = pack('N', strlen($sendStr) + 4) . $signedcode . $sendStr;
} else {
$sendStr = pack('N', strlen($sendStr)) . $sendStr;
}
return $sendStr;
} else if ($type == "http") {
$sendStr = json_encode($data);
return $sendStr;
} else {
return self::packFormat("packet type wrong", 100006);
}
}
public static function packDecode($str)
{
$header = substr($str, 0, 4);
$len = unpack("Nlen", $header);
$len = $len["len"];
if (DoraConst::SW_DATASIGEN_FLAG == true) {
$signedcode = substr($str, 4, 4);
$result = substr($str, 8);
//check signed
if (pack("N", crc32($result . DoraConst::SW_DATASIGEN_SALT)) != $signedcode) {
return self::packFormat("Signed check error!", 100005);
}
$len = $len - 4;
} else {
$result = substr($str, 4);
}
if ($len != strlen($result)) {
//结果长度不对
echo "error length...\n";
return self::packFormat("packet length invalid 包长度非法", 100007);
}
//if compress the packet
if (DoraConst::SW_DATACOMPRESS_FLAG == true) {
$result = gzdecode($result);
}
$result = unserialize($result);
return self::packFormat("OK", 0, $result);
}
}