@Test public void testEncode() { InetSocketAddress recipient = SocketUtils.socketAddress("127.0.0.1", 10000); InetSocketAddress sender = SocketUtils.socketAddress("127.0.0.1", 20000); assertTrue(channel.writeOutbound( new DefaultAddressedEnvelope<String, InetSocketAddress>("netty", recipient, sender))); DatagramPacket packet = channel.readOutbound(); try { assertEquals("netty", packet.content().toString(CharsetUtil.UTF_8)); assertEquals(recipient, packet.recipient()); assertEquals(sender, packet.sender()); } finally { packet.release(); } }
@Override protected void encode( ChannelHandlerContext ctx, AddressedEnvelope<M, InetSocketAddress> msg, List<Object> out) throws Exception { assert out.isEmpty(); encoder.encode(ctx, msg.content(), out); if (out.size() != 1) { throw new EncoderException( StringUtil.simpleClassName(encoder) + " must produce only one message."); } Object content = out.get(0); if (content instanceof ByteBuf) { out.set(0, new DatagramPacket((ByteBuf) content, msg.recipient(), msg.sender())); } else { throw new EncoderException( StringUtil.simpleClassName(encoder) + " must produce only ByteBuf."); } }
DatagramPacketEncoder extends MessageToMessageEncoder<AddressedEnvelope<M, InetSocketAddress>> { @Override protected void encode( ChannelHandlerContext ctx, AddressedEnvelope<M, InetSocketAddress> msg, List<Object> out) throws Exception { assert out.isEmpty(); encoder.encode(ctx, msg.content(), out); if (out.size() != 1) { throw new EncoderException( StringUtil.simpleClassName(encoder) + " must produce only one message."); } Object content = out.get(0); if (content instanceof ByteBuf) { out.set(0, new DatagramPacket((ByteBuf) content, msg.recipient(), msg.sender())); } else { throw new EncoderException( StringUtil.simpleClassName(encoder) + " must produce only ByteBuf."); } } }
DatagramPacketEncoder extends MessageToMessageEncoder<AddressedEnvelope<M, InetSocketAddress>> { @Override protected void encode( ChannelHandlerContext ctx, AddressedEnvelope<M, InetSocketAddress> msg, List<Object> out) throws Exception { assert out.isEmpty(); encoder.encode(ctx, msg.content(), out); if (out.size() != 1) { throw new EncoderException( StringUtil.simpleClassName(encoder) + " must produce only one message."); } Object content = out.get(0); if (content instanceof ByteBuf) { out.set(0, new DatagramPacket((ByteBuf) content, msg.recipient(), msg.sender())); } else { throw new EncoderException( StringUtil.simpleClassName(encoder) + " must produce only ByteBuf."); } } DatagramPacketEncoder(MessageToMessageEncoder<? super M> encoder); }
DatagramPacketEncoder extends MessageToMessageEncoder<AddressedEnvelope<M, InetSocketAddress>> { @Override protected void encode( ChannelHandlerContext ctx, AddressedEnvelope<M, InetSocketAddress> msg, List<Object> out) throws Exception { assert out.isEmpty(); encoder.encode(ctx, msg.content(), out); if (out.size() != 1) { throw new EncoderException( StringUtil.simpleClassName(encoder) + " must produce only one message."); } Object content = out.get(0); if (content instanceof ByteBuf) { out.set(0, new DatagramPacket((ByteBuf) content, msg.recipient(), msg.sender())); } else { throw new EncoderException( StringUtil.simpleClassName(encoder) + " must produce only ByteBuf."); } } DatagramPacketEncoder(MessageToMessageEncoder<? super M> encoder); @Override boolean acceptOutboundMessage(Object msg); @Override void bind(ChannelHandlerContext ctx, SocketAddress localAddress, ChannelPromise promise); @Override void connect(
            ChannelHandlerContext ctx, SocketAddress remoteAddress,
            SocketAddress localAddress, ChannelPromise promise); @Override void disconnect(ChannelHandlerContext ctx, ChannelPromise promise); @Override void close(ChannelHandlerContext ctx, ChannelPromise promise); @Override void deregister(ChannelHandlerContext ctx, ChannelPromise promise); @Override void read(ChannelHandlerContext ctx); @Override void flush(ChannelHandlerContext ctx); @Override void handlerAdded(ChannelHandlerContext ctx); @Override void handlerRemoved(ChannelHandlerContext ctx); @Override void exceptionCaught(ChannelHandlerContext ctx, Throwable cause); @Override boolean isSharable(); }
DatagramPacketEncoder extends MessageToMessageEncoder<AddressedEnvelope<M, InetSocketAddress>> { @Override protected void encode( ChannelHandlerContext ctx, AddressedEnvelope<M, InetSocketAddress> msg, List<Object> out) throws Exception { assert out.isEmpty(); encoder.encode(ctx, msg.content(), out); if (out.size() != 1) { throw new EncoderException( StringUtil.simpleClassName(encoder) + " must produce only one message."); } Object content = out.get(0); if (content instanceof ByteBuf) { out.set(0, new DatagramPacket((ByteBuf) content, msg.recipient(), msg.sender())); } else { throw new EncoderException( StringUtil.simpleClassName(encoder) + " must produce only ByteBuf."); } } DatagramPacketEncoder(MessageToMessageEncoder<? super M> encoder); @Override boolean acceptOutboundMessage(Object msg); @Override void bind(ChannelHandlerContext ctx, SocketAddress localAddress, ChannelPromise promise); @Override void connect(
            ChannelHandlerContext ctx, SocketAddress remoteAddress,
            SocketAddress localAddress, ChannelPromise promise); @Override void disconnect(ChannelHandlerContext ctx, ChannelPromise promise); @Override void close(ChannelHandlerContext ctx, ChannelPromise promise); @Override void deregister(ChannelHandlerContext ctx, ChannelPromise promise); @Override void read(ChannelHandlerContext ctx); @Override void flush(ChannelHandlerContext ctx); @Override void handlerAdded(ChannelHandlerContext ctx); @Override void handlerRemoved(ChannelHandlerContext ctx); @Override void exceptionCaught(ChannelHandlerContext ctx, Throwable cause); @Override boolean isSharable();  }