forked from linxbin/pmir2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestPack.php
More file actions
138 lines (109 loc) · 5.36 KB
/
testPack.php
File metadata and controls
138 lines (109 loc) · 5.36 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
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
#!/usr/bin/env php
<?php
ini_set('display_errors', 'on');
ini_set('display_startup_errors', 'on');
error_reporting(E_ALL);
date_default_timezone_set('Asia/Shanghai');
!defined('BASE_PATH') && define('BASE_PATH', __DIR__ . '/');
!defined('SWOOLE_HOOK_FLAGS') && define('SWOOLE_HOOK_FLAGS', SWOOLE_HOOK_ALL);
require BASE_PATH . '/vendor/autoload.php';
//测试重写env
function env($conf, $default)
{
$config = [
'DEBUG' => true,
];
return $config[$conf] ?? $default;
}
use App\Controller\Packet\CodeMap;
use Hyperf\Di\Annotation\Inject;
/**
*
*/
class Client
{
/**
* @Inject
* @var CodeMap
*/
protected $CodeMap;
protected $size = '';
public function __construct()
{
$this->CodeMap = new CodeMap;
$this->run();
}
public function run()
{
//18 00 00 00 10 00 00 00 3d de 31 5a 3f d2 d1 d5 39 29 a8 a3 e3 62 bc 79
//12 00 05 00 06 66 61 6e 66 61 6e 06 66 61 6e 66 61 6e
//08 00 08 00 01 00 00 00
//05 00 0b 00 02
// var_dump(BigInteger(bytesToString([24,0,0,0,16,0,0,0,61,222,49,90,63,210,209,213,57,41,168,163,227,98,188,121]), 256)->toHex());
// var_dump(BigInteger(bytesToString([18,0,5,0,6,102,97,110,102,97,110,6,102,97,110,102,97,110]), 256)->toHex());
// var_dump(BigInteger(bytesToString([8,0,8,0,1,0,0,0]), 256)->toHex());
// var_dump(BigInteger(bytesToString([5,0,11,0,2]), 256)->toHex());
// var_dump(BigInteger(bytesToString([16,0,41,0,162,134,1,0,7,91,64,77,97,105,110,93]), 256)->toHex());
// var_dump(String2Hex(bytesToString([24, 0, 0, 0, 16, 0, 0, 0, 170, 124, 11, 209, 41, 241, 81, 142, 137, 41, 214, 160, 138, 169, 152, 239])));
$client = new Swoole\Client(SWOOLE_SOCK_TCP);
if (!$client->connect('127.0.0.1', 7000, -1)) {
EchoLog("connect failed. Error: {$client->errCode}");
}
//CLIENT_VERSION
//{"len":24,"cmd":1000,"packet":[24,0,0,0,16,0,0,0,170,124,11,209,41,241,81,142,137,41,214,160,138,169,152,239],"cmdName":"CLIENT_VERSION","res":{"VersionHash":[16,0,0,0,-86,124,11,-47,41,-15,81,-114,-119,41,-42,-96,-118,-87,-104,-17]}}
$client->send(bytesToString([24, 0, 0, 0, 16, 0, 0, 0, 170, 124, 11, 209, 41, 241, 81, 142, 137, 41, 214, 160, 138, 169, 152, 239]));
while ($packet = $client->recv()) {
if ($packet) {
$strlen = strlen($packet);
$data = [];
$i = 0;
while ($i < $strlen) {
$size = unpack('s', substr($packet, $i))[1];
$data[] = substr($packet, $i, $size);
$i += $size;
}
foreach ($data as $k => $v) {
$cmd = $this->unPacketData($v);
switch ($cmd) {
case 'CONNECTED':
// //NEW_ACCOUNT
// //{"len":30,"packet":[30,0,3,0,6,102,97,110,102,97,110,6,102,97,110,102,97,110,0,0,0,0,0,0,0,0,0,0,0,0],"cmdName":"NEW_ACCOUNT","res":{"AccountID":"fanfan","Password":"fanfan","DateTime":0,"UserName":"","SecretQuestion":"","SecretAnswer":"","EMailAddress":""}}
// $client->send(bytesToString([30, 0, 3, 0, 6, 102, 97, 110, 102, 97, 110, 6, 102, 97, 110, 102, 97, 110, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]));
//LOGIN
//{"len":18,"cmd":1005,"packet":[18,0,5,0,6,102,97,110,102,97,110,6,102,97,110,102,97,110],"cmdName":"LOGIN","res":{"AccountID":"fanfan","Password":"fanfan"}}
$client->send(bytesToString([18, 0, 5, 0, 6, 102, 97, 110, 102, 97, 110, 6, 102, 97, 110, 102, 97, 110]));
break;
case 'LOGIN_SUCCESS':
// //NEW_CHARACTER
// // {"len":13,"cmd":1006,"packet":[13,0,6,0,6,97,115,100,97,115,100,0,0],"cmdName":"NEW_CHARACTER","res":{"Name":"asdasd","Gender":0,"Class":0}}
// $client->send(bytesToString([13, 0, 6, 0, 6, 97, 115, 100, 97, 115, 100, 0, 0]));
// $this->unPacketData($client->recv());
//START_game
// {"len":8,"cmd":1008,"packet":[8,0,8,0,14,0,0,0],"cmdName":"START_GAME","res":{"CharacterIndex":1}}
$client->send(bytesToString([8, 0, 8, 0, 01, 0, 0, 0]));
break;
case 'START_GAME':
$client->send(bytesToString([16,0,41,0,196,134,1,0,7,91,64,77,97,105,110,93]));
default:
# code...
break;
}
}
}
}
}
public function unPacketData(string $packet)
{
sleep(1);
$packetBytes = stringToBytes($packet);
$cmdInfo = bytesToString(array_slice($packetBytes, 0, 4));
$paramInfo = bytesToString(array_slice($packetBytes, 4));
$param = unpack('slen/scmd/', $cmdInfo);
$param['cmd'] += 2000;
$param['packet'] = $packetBytes;
$param['cmdName'] = $this->CodeMap->getCmdName($param['cmd']);
EchoLog(sprintf('收到服务端信息: [%s] ', json_encode($param, JSON_UNESCAPED_UNICODE)), 'i');
return $param['cmdName'];
}
}
new Client();