我对netty有点问题。 目前,netty没有处理消息,channelRead()方法没有被执行,但是logger说netty收到了消息,并且channelReadComplete()方法也被执行了。
try {
//logger
Logger.getRootLogger().debug("initialize channel pipeline.");
//use gzip compression
//pipeline.addLast("deflater", ZlibCodecFactory.newZlibEncoder(ZlibWrapper.GZIP));
//pipeline.addLast("inflater", ZlibCodecFactory.newZlibDecoder(ZlibWrapper.GZIP));
//log events
pipeline.addLast("logger", new LoggingHandler(LogLevel.DEBUG));
//message encoder and decoder
pipeline.addLast("encoder", new MessageEncoder());
pipeline.addLast("decoder", new MessageDecoder());
//create message handler
final MessageLookupHandler handler = new MessageLookupHandler() {
@Override
protected void initActions(Map<Byte, MessageAction> map) {
if (map == null) {
Logger.getRootLogger().warn("LookUp Table is null.");
}
try {
Logger.getRootLogger().debug("initialize actions.");
//login action
map.put(LoginAction.MESSAGE_TYPE, new LoginAction(LoginServer.this.userSystem, LoginServer.this.sessionClient));
//register action
map.put(RegisterAction.MESSAGE_TYPE, new RegisterAction(LoginServer.this.userSystem));
//mail activation action
map.put(ActivationAction.MESSAGE_TYPE, new ActivationAction(LoginServer.this.userSystem));
for (Map.Entry<Byte,MessageAction> entry : map.entrySet()) {
Logger.getRootLogger().debug("registered action: " + entry.getKey());
}
} catch (Exception e) {
//because netty doesnt catch exception in constructors, print stacktrace here
e.printStackTrace();
}
}
@Override
public void exceptionCaught (ChannelHandlerContext ctx, Throwable cause) {
//exception handling
cause.printStackTrace();
}
};
//add handler
pipeline.addLast(new ChannelInboundHandlerAdapter() {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
Logger.getRootLogger().debug("channel read.");
super.channelRead(ctx, msg);
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
java.util.logging.Logger.getLogger(MessageLookupHandler.class.getName()).info("channel read complete.");
//ctx.flush();
}
});
pipeline.addLast("handler", handler);
} catch (Exception e) {
e.printStackTrace();
}
MessageEncoder,具体:
public class MessageEncoder extends MessageToByteEncoder<Message>
{
@Override
protected void encode(ChannelHandlerContext ctx, Message msg, ByteBuf out) throws Exception
{
out.writeInt(Message.HEADER_LENGHT + msg.content().readableBytes());
out.writeByte(msg.getType());
out.writeByte(msg.getVersion());
out.writeBytes(msg.content());
}
}
MessageDecoder:
public class MessageDecoder extends ByteToMessageDecoder
{
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception
{
if (in.readableBytes() < 4) return;
in.markReaderIndex();
int length = in.readInt();
if (in.readableBytes() < length)
{
in.resetReaderIndex();
return;
}
byte type = in.readByte();
byte version = in.readByte();
ByteBuf content = in.slice(in.readerIndex(), length - Message.HEADER_LENGHT);
in.skipBytes(length - Message.HEADER_LENGHT);
// increment refcount since content is a slice of in and ByteToMessageDecoder will release in
content.retain();
Message message = new Message(type, version, content);
out.add(message);
}
}
记录器输出:
33808 [nioEventLoopGroup-3-1] DEBUG root - initialize channel pipeline.
33808 [nioEventLoopGroup-3-1] DEBUG root - initialize actions.
33808 [nioEventLoopGroup-3-1] DEBUG root - registered action: 1
33809 [nioEventLoopGroup-3-1] DEBUG root - registered action: 2
33809 [nioEventLoopGroup-3-1] DEBUG root - registered action: 3
33810 [nioEventLoopGroup-4-2] DEBUG io.netty.handler.logging.LoggingHandler - [id: 0x31c22087, /127.0.0.1:53164 => /127.0.0.1:3001] REGISTERED
33810 [nioEventLoopGroup-4-2] DEBUG io.netty.handler.logging.LoggingHandler - [id: 0x31c22087, /127.0.0.1:53164 => /127.0.0.1:3001] ACTIVE
33883 [nioEventLoopGroup-4-2] DEBUG io.netty.handler.logging.LoggingHandler - [id: 0x31c22087, /127.0.0.1:53164 => /127.0.0.1:3001] RECEIVED: 200B
+-------------------------------------------------+
| 0 1 2 3 4 5 6 7 8 9 a b c d e f |
+--------+-------------------------------------------------+----------------+
|00000000| 7b 22 62 69 72 74 68 64 61 79 22 3a 7b 22 6d 6f |{"birthday":{"mo|
|00000010| 6e 74 68 22 3a 31 2c 22 79 65 61 72 22 3a 31 39 |nth":1,"year":19|
|00000020| 39 36 2c 22 64 61 79 22 3a 31 7d 2c 22 6d 61 69 |96,"day":1},"mai|
|00000030| 6c 22 3a 22 74 65 73 74 40 70 65 6e 74 61 71 75 |l":"test@*******|
|00000040| 69 6e 2e 63 6f 6d 22 2c 22 70 61 73 73 77 6f 72 |******","passwor|
|00000050| 64 48 61 73 68 22 3a 22 36 65 59 7a 43 58 71 35 |dHash":"********|
|00000060| 7a 72 50 6b 6a 73 50 33 44 75 4b 2b 75 6b 48 51 |****************|
|00000070| 58 56 51 67 37 2b 35 64 71 46 2b 58 32 58 41 46 |****************|
|00000080| 63 6e 57 48 2f 61 4d 2b 39 50 38 6a 49 67 69 50 |****************|
|00000090| 54 48 6e 6f 45 7a 7a 4a 7a 5a 38 31 45 76 54 54 |****************|
|000000a0| 6f 77 50 4c 32 31 76 46 68 55 46 61 41 41 3d 3d |****************|
|000000b0| 22 2c 22 75 73 65 72 6e 61 6d 65 22 3a 22 74 65 |","username":"te|
|000000c0| 73 74 75 73 65 72 22 7d |stuser"} |
+--------+-------------------------------------------------+----------------+
Okt 19, 2015 9:00:35 PM com.pentaquin.core.network.MessageLookupHandler channelReadComplete
INFORMATION: channel read complete.
有没有人有想法? 谢谢你的帮助!