使用ChannelHandlerContext

This commit is contained in:
Guoqs
2024-08-01 11:35:31 +08:00
parent 1099874666
commit 1cec3f64c5
2 changed files with 5 additions and 5 deletions

View File

@@ -97,11 +97,11 @@ public abstract class AbstractHandler implements InitializingBean {
* 阻止重复帧
* @return true 重复
*/
protected boolean verifyTheDuplicateRequest(YKCDataProtocol ykcDataProtocol, Channel channel) {
protected boolean verifyTheDuplicateRequest(YKCDataProtocol ykcDataProtocol, ChannelHandlerContext ctx) {
// 获取序列号域
int serialNumber = BytesUtil.bytesToIntLittle(ykcDataProtocol.getSerialNumber());
// 获取channelId
String channelId = channel.id().asShortText();
String channelId = ctx.channel().id().asShortText();
String redisKey = "Request_" + channelId + "_" + serialNumber;
Boolean result = redisCache.setnx(redisKey, ykcDataProtocol.getHEXString(), 30);
// result返回false说明没有设置成功就是说已经有相同请求了所以返回true重复

View File

@@ -106,7 +106,7 @@ public class LoginRequestHandler extends AbstractHandler {
}
@Override
public byte[] supplyProcess(YKCDataProtocol ykcDataProtocol, ChannelHandlerContext channel) {
public byte[] supplyProcess(YKCDataProtocol ykcDataProtocol, ChannelHandlerContext ctx) {
// 获取消息体
byte[] msgBody = ykcDataProtocol.getMsgBody();
@@ -118,7 +118,7 @@ public class LoginRequestHandler extends AbstractHandler {
String pileSn = BytesUtil.binary(pileSnByte, 16);
// 保存时间
saveLastTimeAndCheckChannel(pileSn, channel);
saveLastTimeAndCheckChannel(pileSn, ctx);
// 桩类型 0 表示直流桩, 1 表示交流桩
startIndex += length;
@@ -164,7 +164,7 @@ public class LoginRequestHandler extends AbstractHandler {
String business = BytesUtil.bcd2Str(businessTypeByteArr);
// *********************** 字段解析完成,下面进行逻辑处理 *********************** //
if (verifyTheDuplicateRequest(ykcDataProtocol, channel)) {
if (verifyTheDuplicateRequest(ykcDataProtocol, ctx)) {
return null;
}