Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
apache
Shardingsphere
提交
4dcaa7b2
Shardingsphere
项目概览
apache
/
Shardingsphere
通知
56
Star
3
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
Shardingsphere
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
4dcaa7b2
编写于
9月 25, 2019
作者:
K
KomachiSion
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Refactor MysqlCommandPacketDecoder to improve readability
上级
03ddad93
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
103 addition
and
48 deletion
+103
-48
src/main/java/info/avalon566/shardingscaling/sync/mysql/binlog/codec/AuthenticationMethod.java
...scaling/sync/mysql/binlog/codec/AuthenticationMethod.java
+22
-0
src/main/java/info/avalon566/shardingscaling/sync/mysql/binlog/codec/MysqlBinlogEventPacketDecoder.java
...ync/mysql/binlog/codec/MysqlBinlogEventPacketDecoder.java
+0
-1
src/main/java/info/avalon566/shardingscaling/sync/mysql/binlog/codec/MysqlCommandPacketDecoder.java
...ng/sync/mysql/binlog/codec/MysqlCommandPacketDecoder.java
+64
-47
src/main/java/info/avalon566/shardingscaling/sync/mysql/binlog/codec/PacketConstants.java
...rdingscaling/sync/mysql/binlog/codec/PacketConstants.java
+17
-0
未找到文件。
src/main/java/info/avalon566/shardingscaling/sync/mysql/binlog/codec/AuthenticationMethod.java
0 → 100644
浏览文件 @
4dcaa7b2
package
info.avalon566.shardingscaling.sync.mysql.binlog.codec
;
/**
* MySQL client/server protocol Authentication Method
*
* MySQL Internals Manual / MySQL Client/Server Protocol / Authentication Method / SHA256
* https://dev.mysql.com/doc/internals/en/sha256.html
*
* @author yangyi
*/
public
final
class
AuthenticationMethod
{
public
static
final
String
OLD_PASSWORD_AUTHENTICATION
=
"mysql_old_password"
;
public
static
final
String
SECURE_PASSWORD_AUTHENTICATION
=
"mysql_native_password"
;
public
static
final
String
CLEAR_TEXT_AUTHENTICATION
=
"mysql_clear_password"
;
public
static
final
String
WINDOWS_NATIVE_AUTHENTICATION
=
"authentication_windows_client"
;
public
static
final
String
SHA256
=
"sha256_password"
;
}
src/main/java/info/avalon566/shardingscaling/sync/mysql/binlog/codec/MysqlBinlogEventPacketDecoder.java
浏览文件 @
4dcaa7b2
...
...
@@ -24,6 +24,5 @@ public class MysqlBinlogEventPacketDecoder extends ByteToMessageDecoder {
LOGGER
.
info
(
Byte
.
toString
(
eventHeader
.
getTypeCode
()));
LOGGER
.
info
(
"readable:{},length:{}"
,
in
.
readableBytes
(),
eventHeader
.
getEventLength
()
-
19
);
in
.
readBytes
(
eventHeader
.
getEventLength
()
-
19
);
return
;
}
}
src/main/java/info/avalon566/shardingscaling/sync/mysql/binlog/codec/MysqlCommandPacketDecoder.java
浏览文件 @
4dcaa7b2
...
...
@@ -5,79 +5,96 @@ import info.avalon566.shardingscaling.sync.mysql.binlog.packet.response.*;
import
io.netty.buffer.ByteBuf
;
import
io.netty.channel.ChannelHandlerContext
;
import
io.netty.handler.codec.ByteToMessageDecoder
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.var
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
java.util.List
;
/**
* MySQL Command Packet decoder
*
* @author avalon566
* @author yangyi
*/
@Slf4j
public
class
MysqlCommandPacketDecoder
extends
ByteToMessageDecoder
{
private
Logger
LOGGER
=
LoggerFactory
.
getLogger
(
MysqlCommandPacketDecoder
.
class
);
private
enum
States
{
OkOrError
,
FieldPacket
,
RowDataPacket
}
private
boolean
initiated
=
false
;
private
States
expectedState
=
States
.
OkOrError
;
private
enum
States
{
Initiate
,
ResponsePacket
,
FieldPacket
,
RowDataPacket
}
private
States
currentState
=
States
.
Initiate
;
private
InternalResultSet
internalResultSet
=
null
;
@Override
protected
void
decode
(
ChannelHandlerContext
ctx
,
ByteBuf
in
,
List
out
)
{
protected
void
decode
(
ChannelHandlerContext
ctx
,
ByteBuf
in
,
List
<
Object
>
out
)
{
// first packet from server is handshake initialization packet
if
(!
initiated
)
{
var
handshake
=
new
HandshakeInitializationPacket
();
handshake
.
fromByteBuf
(
in
);
if
(
handshake
.
getProtocolVersion
()
!=
0x0a
)
{
throw
new
UnsupportedOperationException
();
}
if
(!
"mysql_native_password"
.
equals
(
handshake
.
getAuthPluginName
()))
{
throw
new
UnsupportedOperationException
();
}
out
.
add
(
handshake
);
initiated
=
true
;
if
(
States
.
Initiate
.
equals
(
currentState
))
{
out
.
add
(
decodeHandshakeInitializationPacket
(
in
));
currentState
=
States
.
ResponsePacket
;
return
;
}
if
(
States
.
FieldPacket
.
equals
(
expectedState
))
{
if
(-
2
!=
in
.
getByte
(
0
))
{
var
fieldPacket
=
new
FieldPacket
();
fieldPacket
.
fromByteBuf
(
in
);
internalResultSet
.
getFieldDescriptors
().
add
(
fieldPacket
);
}
else
{
var
eofPacket
=
new
EofPacket
();
eofPacket
.
fromByteBuf
(
in
);
expectedState
=
States
.
RowDataPacket
;
}
if
(
States
.
FieldPacket
.
equals
(
currentState
))
{
decodeFieldPacket
(
in
);
return
;
}
if
(
States
.
RowDataPacket
.
equals
(
expectedState
))
{
if
(-
2
!=
in
.
getByte
(
0
))
{
var
rowDataPacket
=
new
RowDataPacket
();
rowDataPacket
.
fromByteBuf
(
in
);
internalResultSet
.
getFieldValues
().
add
(
rowDataPacket
);
}
else
{
var
eofPacket
=
new
EofPacket
();
eofPacket
.
fromByteBuf
(
in
);
out
.
add
(
internalResultSet
);
expectedState
=
States
.
OkOrError
;
internalResultSet
=
null
;
}
if
(
States
.
RowDataPacket
.
equals
(
currentState
))
{
decodeRowDataPacket
(
in
,
out
);
return
;
}
if
(-
1
==
in
.
getByte
(
0
))
{
decodeResponsePacket
(
in
,
out
);
}
private
HandshakeInitializationPacket
decodeHandshakeInitializationPacket
(
final
ByteBuf
in
)
{
var
result
=
new
HandshakeInitializationPacket
();
result
.
fromByteBuf
(
in
);
if
(
PacketConstants
.
PROTOCOL_VERSION
!=
result
.
getProtocolVersion
())
{
throw
new
UnsupportedOperationException
();
}
if
(!
AuthenticationMethod
.
SECURE_PASSWORD_AUTHENTICATION
.
equals
(
result
.
getAuthPluginName
()))
{
throw
new
UnsupportedOperationException
();
}
return
result
;
}
private
void
decodeFieldPacket
(
final
ByteBuf
in
)
{
if
(
PacketConstants
.
EOF_PACKET_MARK
!=
in
.
getByte
(
0
))
{
var
fieldPacket
=
new
FieldPacket
();
fieldPacket
.
fromByteBuf
(
in
);
internalResultSet
.
getFieldDescriptors
().
add
(
fieldPacket
);
}
else
{
var
eofPacket
=
new
EofPacket
();
eofPacket
.
fromByteBuf
(
in
);
currentState
=
States
.
RowDataPacket
;
}
}
private
void
decodeRowDataPacket
(
final
ByteBuf
in
,
final
List
<
Object
>
out
)
{
if
(
PacketConstants
.
EOF_PACKET_MARK
!=
in
.
getByte
(
0
))
{
var
rowDataPacket
=
new
RowDataPacket
();
rowDataPacket
.
fromByteBuf
(
in
);
internalResultSet
.
getFieldValues
().
add
(
rowDataPacket
);
}
else
{
var
eofPacket
=
new
EofPacket
();
eofPacket
.
fromByteBuf
(
in
);
out
.
add
(
internalResultSet
);
currentState
=
States
.
ResponsePacket
;
internalResultSet
=
null
;
}
}
private
void
decodeResponsePacket
(
final
ByteBuf
in
,
final
List
<
Object
>
out
)
{
if
(
PacketConstants
.
ERR_PACKET_MARK
==
in
.
getByte
(
0
))
{
var
error
=
new
ErrorPacket
();
error
.
fromByteBuf
(
(
ByteBuf
)
in
);
error
.
fromByteBuf
(
in
);
out
.
add
(
error
);
}
else
if
(
0
==
in
.
getByte
(
0
))
{
}
else
if
(
PacketConstants
.
OK_PACKET_MARK
==
in
.
getByte
(
0
))
{
var
ok
=
new
OkPacket
();
ok
.
fromByteBuf
(
in
);
out
.
add
(
ok
);
}
else
{
var
resultSetHeaderPacket
=
new
ResultSetHeaderPacket
();
resultSetHeaderPacket
.
fromByteBuf
(
in
);
expected
State
=
States
.
FieldPacket
;
current
State
=
States
.
FieldPacket
;
internalResultSet
=
new
InternalResultSet
(
resultSetHeaderPacket
);
}
}
...
...
src/main/java/info/avalon566/shardingscaling/sync/mysql/binlog/codec/PacketConstants.java
0 → 100644
浏览文件 @
4dcaa7b2
package
info.avalon566.shardingscaling.sync.mysql.binlog.codec
;
/**
* MySQL protocol constants.
*
* @author yangyi
*/
public
final
class
PacketConstants
{
public
static
final
byte
PROTOCOL_VERSION
=
0x0a
;
public
static
final
byte
OK_PACKET_MARK
=
0x00
;
public
static
final
byte
EOF_PACKET_MARK
=
(
byte
)
0xfe
;
public
static
final
byte
ERR_PACKET_MARK
=
(
byte
)
0xff
;
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录