/** * Try to add the given {@link DatagramPacket}. Returns {@code true} on success, * {@code false} otherwise. */ boolean add(DatagramPacket packet) { if (count == packets.length) { return false; } ByteBuf content = packet.content(); int len = content.readableBytes(); if (len == 0) { return true; } NativeDatagramPacket p = packets[count]; InetSocketAddress recipient = packet.recipient(); if (!p.init(content, recipient)) { return false; } count++; return true; }
@Override public DatagramPacket replace(ByteBuf content) { return new DatagramPacket(content, recipient(), sender()); }
@Override public DatagramPacket replace(ByteBuf content) { return new DatagramPacket(content, recipient(), sender()); }
/** * Try to add the given {@link DatagramPacket}. Returns {@code true} on success, * {@code false} otherwise. */ boolean add(DatagramPacket packet) { if (count == packets.length) { return false; } ByteBuf content = packet.content(); int len = content.readableBytes(); if (len == 0) { return true; } NativeDatagramPacket p = packets[count]; InetSocketAddress recipient = packet.recipient(); if (!p.init(content, recipient)) { return false; } count++; return true; }
@Override public DatagramPacket replace(ByteBuf content) { return new DatagramPacket(content, recipient(), sender()); }
private static DnsQuery newQuery(DatagramPacket packet, ByteBuf buf) { final int id = buf.readUnsignedShort(); final int flags = buf.readUnsignedShort(); if (flags >> 15 == 1) { throw new CorruptedFrameException("not a query"); } final DnsQuery query = new DatagramDnsQuery( packet.sender(), packet.recipient(), id, DnsOpCode.valueOf((byte) (flags >> 11 & 0xf))); query.setRecursionDesired((flags >> 8 & 1) == 1); query.setZ(flags >> 4 & 0x7); return query; }
return p; return new DatagramPacket(newDirectBuffer(p, content), p.recipient());
private static DnsQuery newQuery(DatagramPacket packet, ByteBuf buf) { final int id = buf.readUnsignedShort(); final int flags = buf.readUnsignedShort(); if (flags >> 15 == 1) { throw new CorruptedFrameException("not a query"); } final DnsQuery query = new DatagramDnsQuery( packet.sender(), packet.recipient(), id, DnsOpCode.valueOf((byte) (flags >> 11 & 0xf))); query.setRecursionDesired((flags >> 8 & 1) == 1); query.setZ(flags >> 4 & 0x7); return query; }
return p; return new DatagramPacket(newDirectBuffer(p, content), p.recipient());
@Override protected Object filterOutboundMessage(Object msg) { if (msg instanceof DatagramPacket) { DatagramPacket packet = (DatagramPacket) msg; ByteBuf content = packet.content(); return UnixChannelUtil.isBufferCopyNeededForWrite(content) ? new DatagramPacket(newDirectBuffer(packet, content), packet.recipient()) : msg; } if (msg instanceof ByteBuf) { ByteBuf buf = (ByteBuf) msg; return UnixChannelUtil.isBufferCopyNeededForWrite(buf)? newDirectBuffer(buf) : buf; } if (msg instanceof AddressedEnvelope) { @SuppressWarnings("unchecked") AddressedEnvelope<Object, SocketAddress> e = (AddressedEnvelope<Object, SocketAddress>) msg; if (e.content() instanceof ByteBuf && (e.recipient() == null || e.recipient() instanceof InetSocketAddress)) { ByteBuf content = (ByteBuf) e.content(); return UnixChannelUtil.isBufferCopyNeededForWrite(content)? new DefaultAddressedEnvelope<ByteBuf, InetSocketAddress>( newDirectBuffer(e, content), (InetSocketAddress) e.recipient()) : e; } } throw new UnsupportedOperationException( "unsupported message type: " + StringUtil.simpleClassName(msg) + EXPECTED_TYPES); }
@Override protected Object filterOutboundMessage(Object msg) { if (msg instanceof DatagramPacket) { DatagramPacket packet = (DatagramPacket) msg; ByteBuf content = packet.content(); return UnixChannelUtil.isBufferCopyNeededForWrite(content)? new DatagramPacket(newDirectBuffer(packet, content), packet.recipient()) : msg; } if (msg instanceof ByteBuf) { ByteBuf buf = (ByteBuf) msg; return UnixChannelUtil.isBufferCopyNeededForWrite(buf)? newDirectBuffer(buf) : buf; } if (msg instanceof AddressedEnvelope) { @SuppressWarnings("unchecked") AddressedEnvelope<Object, SocketAddress> e = (AddressedEnvelope<Object, SocketAddress>) msg; if (e.content() instanceof ByteBuf && (e.recipient() == null || e.recipient() instanceof InetSocketAddress)) { ByteBuf content = (ByteBuf) e.content(); return UnixChannelUtil.isBufferCopyNeededForWrite(content)? new DefaultAddressedEnvelope<ByteBuf, InetSocketAddress>( newDirectBuffer(e, content), (InetSocketAddress) e.recipient()) : e; } } throw new UnsupportedOperationException( "unsupported message type: " + StringUtil.simpleClassName(msg) + EXPECTED_TYPES); }
private static DnsResponse newResponse(DatagramPacket packet, ByteBuf buf) { final int id = buf.readUnsignedShort(); final int flags = buf.readUnsignedShort(); if (flags >> 15 == 0) { throw new CorruptedFrameException("not a response"); } final DnsResponse response = new DatagramDnsResponse( packet.sender(), packet.recipient(), id, DnsOpCode.valueOf((byte) (flags >> 11 & 0xf)), DnsResponseCode.valueOf((byte) (flags & 0xf))); response.setRecursionDesired((flags >> 8 & 1) == 1); response.setAuthoritativeAnswer((flags >> 10 & 1) == 1); response.setTruncated((flags >> 9 & 1) == 1); response.setRecursionAvailable((flags >> 7 & 1) == 1); response.setZ(flags >> 4 & 0x7); return response; }
return p; return new DatagramPacket(newDirectBuffer(p, content), p.recipient());
private static DnsResponse newResponse(DatagramPacket packet, ByteBuf buf) { final int id = buf.readUnsignedShort(); final int flags = buf.readUnsignedShort(); if (flags >> 15 == 0) { throw new CorruptedFrameException("not a response"); } final DnsResponse response = new DatagramDnsResponse( packet.sender(), packet.recipient(), id, DnsOpCode.valueOf((byte) (flags >> 11 & 0xf)), DnsResponseCode.valueOf((byte) (flags & 0xf))); response.setRecursionDesired((flags >> 8 & 1) == 1); response.setAuthoritativeAnswer((flags >> 10 & 1) == 1); response.setTruncated((flags >> 9 & 1) == 1); response.setRecursionAvailable((flags >> 7 & 1) == 1); response.setZ(flags >> 4 & 0x7); return response; }
@Override protected Object filterOutboundMessage(Object msg) { if (msg instanceof DatagramPacket) { DatagramPacket packet = (DatagramPacket) msg; ByteBuf content = packet.content(); return UnixChannelUtil.isBufferCopyNeededForWrite(content) ? new DatagramPacket(newDirectBuffer(packet, content), packet.recipient()) : msg; } if (msg instanceof ByteBuf) { ByteBuf buf = (ByteBuf) msg; return UnixChannelUtil.isBufferCopyNeededForWrite(buf)? newDirectBuffer(buf) : buf; } if (msg instanceof AddressedEnvelope) { @SuppressWarnings("unchecked") AddressedEnvelope<Object, SocketAddress> e = (AddressedEnvelope<Object, SocketAddress>) msg; if (e.content() instanceof ByteBuf && (e.recipient() == null || e.recipient() instanceof InetSocketAddress)) { ByteBuf content = (ByteBuf) e.content(); return UnixChannelUtil.isBufferCopyNeededForWrite(content)? new DefaultAddressedEnvelope<ByteBuf, InetSocketAddress>( newDirectBuffer(e, content), (InetSocketAddress) e.recipient()) : e; } } throw new UnsupportedOperationException( "unsupported message type: " + StringUtil.simpleClassName(msg) + EXPECTED_TYPES); }
@Override protected Object filterOutboundMessage(Object msg) { if (msg instanceof DatagramPacket) { DatagramPacket packet = (DatagramPacket) msg; ByteBuf content = packet.content(); return UnixChannelUtil.isBufferCopyNeededForWrite(content)? new DatagramPacket(newDirectBuffer(packet, content), packet.recipient()) : msg; } if (msg instanceof ByteBuf) { ByteBuf buf = (ByteBuf) msg; return UnixChannelUtil.isBufferCopyNeededForWrite(buf)? newDirectBuffer(buf) : buf; } if (msg instanceof AddressedEnvelope) { @SuppressWarnings("unchecked") AddressedEnvelope<Object, SocketAddress> e = (AddressedEnvelope<Object, SocketAddress>) msg; if (e.content() instanceof ByteBuf && (e.recipient() == null || e.recipient() instanceof InetSocketAddress)) { ByteBuf content = (ByteBuf) e.content(); return UnixChannelUtil.isBufferCopyNeededForWrite(content)? new DefaultAddressedEnvelope<ByteBuf, InetSocketAddress>( newDirectBuffer(e, content), (InetSocketAddress) e.recipient()) : e; } } throw new UnsupportedOperationException( "unsupported message type: " + StringUtil.simpleClassName(msg) + EXPECTED_TYPES); }
@Override protected void decode(ChannelHandlerContext ctx, DatagramPacket msg, List<Object> out) throws Exception { // decode the DatagramPackage to AddressedEnvelope DefaultAddressedEnvelope<Object, InetSocketAddress> addressEvelop = new DefaultAddressedEnvelope<>(msg.content().retain(), msg.recipient(), msg.sender()); out.add(addressEvelop); }
@Override public DatagramPacket replace(ByteBuf content) { return new DatagramPacket(content, recipient(), sender()); }
@Override protected void decode(final ChannelHandlerContext ctx, final DatagramPacket msg, final List<Object> out) throws Exception { final Session session = this.sessionManager.getSession(msg.sender(), msg.recipient()); final ByteBuf buf = msg.content(); final ByteBuffer headerBuffer = buf.readSlice(Header.SIZE).nioBuffer(); final Header header = new Header(headerBuffer); final ByteBuffer payloadBuffer = buf.nioBuffer(); final Packet packet = new Packet(session, header, payloadBuffer); out.add(new DefaultAddressedEnvelope<>(packet, msg.recipient(), msg.sender())); } }
@Override protected void decode(final ChannelHandlerContext ctx, final DatagramPacket msg, final List<Object> out) throws Exception { final Session session = this.sessionManager.getSession(msg.sender(), msg.recipient()); final ByteBuf buf = msg.content(); final ByteBuffer headerBuffer = buf.readSlice(Header.SIZE).nioBuffer(); final Header header = new Header(headerBuffer); final ByteBuffer payloadBuffer = buf.readSlice(header.length - Header.SIZE).nioBuffer(); final Packet packet = new Packet(session, msg.sender(), header, payloadBuffer); out.add(new DefaultAddressedEnvelope<>(packet, msg.recipient(), msg.sender())); } }