Skip to content

Commit ef267db

Browse files
committed
update
1 parent be57a55 commit ef267db

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

examples/server.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
declare(strict_types=1);
44

55
use Workbunny\MysqlProtocol\Packets\HandshakeInitialization;
6+
use Workbunny\MysqlProtocol\Packets\HandshakeResponse;
67
use Workbunny\MysqlProtocol\Utils\Binary;
78
use Workbunny\MysqlProtocol\Utils\Packet;
89
use Workbunny\MysqlProtocol\Packets\Error;
@@ -56,17 +57,20 @@
5657
}
5758
// 状态机:0 握手阶段,1 已经握手
5859
if ($connection->mysql_handshake_status < 1) {
60+
// 握手响应信息获取
61+
$handshakeResponse = HandshakeResponse::unpack($binary);
62+
5963
// todo 可以对 数据信息进行验证,这里暂时不验证用户信息等
60-
// \Workbunny\MysqlProtocol\Packets\HandshakeResponse::unpack($binary);
6164

6265
// 状态机:1 已经握手
6366
$connection->mysql_handshake_status = 1;
64-
$connection->send(Ok::pack([]));
67+
$connection->send(Ok::pack([
68+
'packet_id' => $handshakeResponse['packet_id'] + 1
69+
]));
6570
} else { // command包
6671
$command = Command::unpack($binary);
67-
dump($command);
6872
$connection->send(Ok::pack([
69-
'packet_id' => $command['packet_id'] ?? 0,
73+
'packet_id' => $command['packet_id'] + 1,
7074
]));
7175
}
7276
};

src/Utils/Packet.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,18 @@ public static function binary(Closure $closure, int $packetId = 0): Binary
3434
/**
3535
* 快速解析包头和包体
3636
*
37-
* @param Closure $closure
37+
* @param Closure|null $closure
3838
* @param Binary $binary
3939
* @return array
4040
*/
41-
public static function parser(Closure $closure, Binary $binary): array
41+
public static function parser(?Closure $closure, Binary $binary): array
4242
{
4343
// 重置读指针
4444
$binary->setReadCursor(0);
4545
// 包头
4646
$packetLength = $binary->readUB(Binary::UB3);
4747
$packetId = $binary->readByte();
48-
$result = $closure($binary);
48+
$result = $closure ? $closure($binary) : [];
4949
if (!is_array($result)) {
5050
throw new PacketException('Packet parser must return array', ExceptionCode::ERROR);
5151
}

0 commit comments

Comments
 (0)