Skip to content

Commit c055f24

Browse files
committed
update
1 parent ef267db commit c055f24

File tree

4 files changed

+104
-3
lines changed

4 files changed

+104
-3
lines changed

examples/client.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Workbunny\MysqlProtocol\Packets\HandshakeInitialization;
6+
use Workbunny\MysqlProtocol\Packets\HandshakeResponse;
7+
use Workbunny\MysqlProtocol\Utils\Binary;
8+
use Workbunny\MysqlProtocol\Utils\Packet;
9+
use Workbunny\MysqlProtocol\Packets\Error;
10+
use Workbunny\MysqlProtocol\Packets\Command;
11+
use Workbunny\MysqlProtocol\Packets\Ok;
12+
use Workerman\Connection\TcpConnection;
13+
14+
require_once __DIR__ . '/../vendor/autoload.php';
15+
16+
$server = new \Workerman\Worker();
17+
$server->name = 'workbunny-mysql-client';
18+
$server->onWorkerStart = function () {
19+
global $clientMysqlHandshakeStatus;
20+
$clientMysqlHandshakeStatus = $clientMysqlHandshakeStatus ?: 0;
21+
$client = new \Workerman\Connection\AsyncTcpConnection("MySQL://host.docker.internal:3306");
22+
23+
$client->onConnect = function (TcpConnection $connection) use (&$clientMysqlHandshakeStatus) {
24+
$connection->errorHandler = function (Throwable $exception) {
25+
dump($exception);
26+
};
27+
// 模拟心跳
28+
\Workerman\Timer::add(30, function () use ($connection, &$clientMysqlHandshakeStatus) {
29+
if ($clientMysqlHandshakeStatus > 1) {
30+
$connection->send(Command::pack([
31+
'command' => Command::COM_QUERY,
32+
'data' => 'SELECT 1'
33+
]));
34+
}
35+
});
36+
};
37+
38+
$client->onMessage = function (TcpConnection $connection, Binary $binary) use (&$clientMysqlHandshakeStatus) {
39+
dump($binary->dump());
40+
// 还未握手
41+
if ($clientMysqlHandshakeStatus < 1) {
42+
// 解析握手包
43+
$handshakeInitialization = HandshakeInitialization::unpack($binary);
44+
$clientMysqlHandshakeStatus = 1;
45+
$connection->send(HandshakeResponse::pack([
46+
'packet_id' => $handshakeInitialization['packet_id'] + 1,
47+
"capability_flags" => $handshakeInitialization['capability_flags'],
48+
"max_packet_size" => 1073741824,
49+
"character_set" => 33,
50+
"username" => "root",
51+
"database" => null,
52+
"auth_plugin" => $handshakeInitialization['auth_plugin_name'],
53+
"auth_response" => "",
54+
]));
55+
}
56+
// 握手还未确认
57+
elseif ($clientMysqlHandshakeStatus < 2) {
58+
if (!$class = Packet::getPacketClass($binary)) {
59+
echo "Error!\n";
60+
return;
61+
}
62+
$result = $class::unpack($binary);
63+
dump($result);
64+
// todo判定是否握手成功
65+
66+
}
67+
// 握手成功
68+
else {
69+
70+
dump($binary->dump());
71+
}
72+
};
73+
74+
$client->connect();
75+
};
76+
77+
\Workerman\Worker::runAll();

examples/server.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
if ($connection->mysql_handshake_status < 1) {
6060
// 握手响应信息获取
6161
$handshakeResponse = HandshakeResponse::unpack($binary);
62-
62+
dump($handshakeResponse);
6363
// todo 可以对 数据信息进行验证,这里暂时不验证用户信息等
6464

6565
// 状态机:1 已经握手

src/Packets/Error.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class Error implements PacketInterface
1818
*
1919
* @param Binary $binary
2020
* @return array
21-
* - header: int 固定 0xFF
21+
* - flag: int 固定 0xFF
2222
* - error_code: int
2323
* - sql_state: string (5字节)
2424
* - error_message: string

src/Utils/Packet.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
use Workbunny\MysqlProtocol\Constants\ExceptionCode;
99
use Workbunny\MysqlProtocol\Exceptions\Exception;
1010
use Workbunny\MysqlProtocol\Exceptions\PacketException;
11+
use Workbunny\MysqlProtocol\Packets\EOF;
12+
use Workbunny\MysqlProtocol\Packets\Error;
13+
use Workbunny\MysqlProtocol\Packets\OK;
14+
use Workbunny\MysqlProtocol\Packets\PacketInterface;
1115

1216
class Packet
1317
{
@@ -55,11 +59,31 @@ public static function parser(?Closure $closure, Binary $binary): array
5559
], $result);
5660
}
5761

62+
/**
63+
* 根据包头获取包类型
64+
*
65+
* @param Binary $binary
66+
* @return class-string<PacketInterface>|null
67+
*/
68+
public static function getPacketClass(Binary $binary): ?string
69+
{
70+
$readCursor = $binary->getReadCursor();
71+
$binary->setReadCursor(4);
72+
$header = $binary->readByte();
73+
$binary->setReadCursor($readCursor);
74+
return match ($header) {
75+
Ok::PACKET_FLAG => OK::class,
76+
Error::PACKET_FLAG => Error::class,
77+
EOF::PACKET_FLAG => EOF::class,
78+
default => null,
79+
};
80+
}
81+
5882
/**
5983
* 生成认证数据
6084
*
6185
* @param int $bytesCount
62-
* @return array
86+
* @return array<int>
6387
*/
6488
public static function authData(int $bytesCount = 8): array
6589
{

0 commit comments

Comments
 (0)