移除报文日志,编码接口增加session
parent
c756450c5c
commit
244ca3483f
|
@ -2,13 +2,10 @@ package io.github.yezhihao.netmc.codec;
|
|||
|
||||
import io.github.yezhihao.netmc.session.Session;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.ByteBufUtil;
|
||||
import io.netty.channel.ChannelHandler;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.ChannelInboundHandlerAdapter;
|
||||
import io.netty.handler.codec.DecoderException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* 基础消息解码
|
||||
|
@ -18,8 +15,6 @@ import org.slf4j.LoggerFactory;
|
|||
@ChannelHandler.Sharable
|
||||
public class MessageDecoderWrapper extends ChannelInboundHandlerAdapter {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(MessageDecoderWrapper.class.getSimpleName());
|
||||
|
||||
private MessageDecoder decoder;
|
||||
|
||||
public MessageDecoderWrapper(MessageDecoder decoder) {
|
||||
|
@ -31,14 +26,6 @@ public class MessageDecoderWrapper extends ChannelInboundHandlerAdapter {
|
|||
if (msg instanceof ByteBuf) {
|
||||
ByteBuf buf = (ByteBuf) msg;
|
||||
try {
|
||||
if (log.isInfoEnabled()) {
|
||||
String hex;
|
||||
if (buf.readableBytes() < 1048)
|
||||
hex = ByteBufUtil.hexDump(buf);
|
||||
else
|
||||
hex = ByteBufUtil.hexDump(buf.slice(0, 32)) + "..." + ByteBufUtil.hexDump(buf.slice(buf.readableBytes() - 32, 32));
|
||||
log.info("<<<<<[ip={}],payload={}", ctx.channel().remoteAddress(), hex);
|
||||
}
|
||||
Object message = decoder.decode(buf, ctx.channel().attr(Session.KEY).get());
|
||||
if (message != null)
|
||||
ctx.fireChannelRead(message);
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package io.github.yezhihao.netmc.codec;
|
||||
|
||||
import io.github.yezhihao.netmc.session.Session;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
|
||||
/**
|
||||
|
@ -11,4 +12,6 @@ public interface MessageEncoder<T> {
|
|||
|
||||
ByteBuf encode(T message);
|
||||
|
||||
ByteBuf encode(T message, Session session);
|
||||
|
||||
}
|
|
@ -1,15 +1,13 @@
|
|||
package io.github.yezhihao.netmc.codec;
|
||||
|
||||
import io.github.yezhihao.netmc.session.Session;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.ByteBufUtil;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import io.netty.channel.ChannelHandler;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.ChannelOutboundHandlerAdapter;
|
||||
import io.netty.channel.ChannelPromise;
|
||||
import io.netty.handler.codec.EncoderException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* 基础消息编码
|
||||
|
@ -19,8 +17,6 @@ import org.slf4j.LoggerFactory;
|
|||
@ChannelHandler.Sharable
|
||||
public class MessageEncoderWrapper extends ChannelOutboundHandlerAdapter {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(MessageEncoderWrapper.class.getSimpleName());
|
||||
|
||||
private MessageEncoder encoder;
|
||||
|
||||
public MessageEncoderWrapper(MessageEncoder encoder) {
|
||||
|
@ -34,9 +30,7 @@ public class MessageEncoderWrapper extends ChannelOutboundHandlerAdapter {
|
|||
if (msg instanceof ByteBuf)
|
||||
buf = (ByteBuf) msg;
|
||||
else
|
||||
buf = encoder.encode(msg);
|
||||
if (log.isInfoEnabled())
|
||||
log.info(">>>>>[ip={}],payload={}", ctx.channel().remoteAddress(), ByteBufUtil.hexDump(buf));
|
||||
buf = encoder.encode(msg, ctx.channel().attr(Session.KEY).get());
|
||||
|
||||
if (buf.isReadable()) {
|
||||
ctx.write(buf, promise);
|
||||
|
|
Loading…
Reference in New Issue