在使用非阻塞数据报通道时,扩展哪些解码器是安全的?本质上,我需要从*ByteBuff转到String,然后我就有了将该string转换为对象的代码。此外,这也需要用解码器来完成。从object到string,最后返回到*ByteBuff。
我尝试过扩展ByteToMessageDecoder,但似乎Netty从未调用过decode方法。所以我不确定这主要是数据报通道的问题,还是我对解码器的原理理解的问题……
为了以防万一,下面是我的一些代码
初始化器:
public class Initializer extends ChannelInitializer<NioDatagramChannel> {
private SimpleChannelInboundHandler<Packet> sipHandler;
public Initializer(SimpleChannelInboundHandler<Packet> handler) {
sipHandler = handler;
}
@Override
protected void initChannel(NioDatagramChannel chan) throws Exception {
ChannelPipeline pipe = chan.pipeline();
pipe.addLast("decoder", new SipDecoder());
pipe.addLast("handler", sipHandler);
pipe.addLast("encoder", new SipEncoder());
}
}我的解码器开始:
public class SipDecoder extends ByteToMessageDecoder {
private Packet sip;
@Override
protected void decode(ChannelHandlerContext context, ByteBuf byteBuf, List<Object> objects) throws Exception {
System.out.println("got hit...");
String data = new String(byteBuf.array());
sip = new Packet();
// [...]
}
}发布于 2013-08-26 14:29:14
要处理DatagramPacket,您需要使用MessageToMessageDecoder,因为ByteToMessageDecoder只适用于ByteBuf。
https://stackoverflow.com/questions/18393007
复制相似问题