@Override public DatagramPacket replace(ByteBuf content) { return new DatagramPacket(content, recipient(), sender()); }
@Override protected void encode(ChannelHandlerContext channelHandlerContext, LogEvent logEvent, List<Object> out) throws Exception { byte[] file = logEvent.getLogfile().getBytes(CharsetUtil.UTF_8); byte[] msg = logEvent.getMsg().getBytes(CharsetUtil.UTF_8); ByteBuf buf = channelHandlerContext.alloc() .buffer(file.length + msg.length + 1); buf.writeBytes(file); buf.writeByte(LogEvent.SEPARATOR); buf.writeBytes(msg); out.add(new DatagramPacket(buf, remoteAddress)); } }
@Override public DatagramPacket replace(ByteBuf content) { return new DatagramPacket(content, recipient(), sender()); }
void sendPacket(byte[] wire, InetSocketAddress address) { DatagramPacket packet = new DatagramPacket(Unpooled.copiedBuffer(wire), address); channel.write(packet); channel.flush(); }
@Override public void broadcast(String subject, byte[] payload) { if (enabled) { Message message = new Message(subject, payload); byte[] bytes = SERIALIZER.encode(message); ByteBuf buf = serverChannel.alloc().buffer(4 + bytes.length); buf.writeInt(bytes.length).writeBytes(bytes); serverChannel.writeAndFlush(new DatagramPacket(buf, groupAddress)); } }
@Override public DatagramPacket replace(ByteBuf content) { return new DatagramPacket(content, recipient(), sender()); }
private void doSend(Buffer packet, InetSocketAddress addr, Handler<AsyncResult<DatagramSocket>> handler) { ChannelFuture future = channel.writeAndFlush(new DatagramPacket(packet.getByteBuf(), addr)); addListener(future, handler); }
private void sendPacket(byte[] payload, InetSocketAddress address) { DatagramPacket packet = new DatagramPacket(Unpooled.copiedBuffer(payload), address); System.out.println("Sending message from " + clientAddr + ":" + clientPort + " to " + address.getHostString() + ":" + address.getPort()); channel.writeAndFlush(packet); }
@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) { // Replace the ByteBuf with a DatagramPacket. out.set(0, new DatagramPacket((ByteBuf) content, msg.recipient(), msg.sender())); } else { throw new EncoderException( StringUtil.simpleClassName(encoder) + " must produce only ByteBuf."); } }
@Override public void unicast(Address address, String subject, byte[] payload) { Message message = new Message(this.address, subject, payload); byte[] bytes = SERIALIZER.encode(message); ByteBuf buf = channel.alloc().buffer(4 + bytes.length); buf.writeInt(bytes.length).writeBytes(bytes); channel.writeAndFlush(new DatagramPacket(buf, new InetSocketAddress(address.address(), address.port()))); }
@Override public Object toFrame(Channel channel) { int capacity = cmd == HEARTBEAT.cmd ? 1 : HEADER_LEN + getBodyLength(); ByteBuf out = channel.alloc().buffer(capacity, capacity); encodePacket(this, out); return new DatagramPacket(out, sender()); } }
@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) { // Replace the ByteBuf with a DatagramPacket. out.set(0, new DatagramPacket((ByteBuf) content, msg.recipient(), msg.sender())); } else { throw new EncoderException( StringUtil.simpleClassName(encoder) + " must produce only ByteBuf."); } }
@Override protected void encode( ChannelHandlerContext ctx, AddressedEnvelope<DnsQuery, InetSocketAddress> in, List<Object> out) throws Exception { final InetSocketAddress recipient = in.recipient(); final DnsQuery query = in.content(); final ByteBuf buf = allocateBuffer(ctx, in); boolean success = false; try { encodeHeader(query, buf); encodeQuestions(query, buf); encodeRecords(query, DnsSection.ADDITIONAL, buf); success = true; } finally { if (!success) { buf.release(); } } out.add(new DatagramPacket(buf, recipient, null)); }
@Override protected int doReadMessages(List<Object> buf) throws Exception { DatagramChannel ch = javaChannel(); DatagramChannelConfig config = config(); RecvByteBufAllocator.Handle allocHandle = unsafe().recvBufAllocHandle(); ByteBuf data = allocHandle.allocate(config.getAllocator()); allocHandle.attemptedBytesRead(data.writableBytes()); boolean free = true; try { ByteBuffer nioData = data.internalNioBuffer(data.writerIndex(), data.writableBytes()); int pos = nioData.position(); InetSocketAddress remoteAddress = (InetSocketAddress) ch.receive(nioData); if (remoteAddress == null) { return 0; } allocHandle.lastBytesRead(nioData.position() - pos); buf.add(new DatagramPacket(data.writerIndex(data.writerIndex() + allocHandle.lastBytesRead()), localAddress(), remoteAddress)); free = false; return 1; } catch (Throwable cause) { PlatformDependent.throwException(cause); return -1; } finally { if (free) { data.release(); } } }
buf.add(new DatagramPacket(data.writerIndex(allocHandle.lastBytesRead()), localAddress(), remoteAddr)); free = false; return 1;
@Override protected int doReadMessages(List<Object> buf) throws Exception { DatagramChannel ch = javaChannel(); DatagramChannelConfig config = config(); RecvByteBufAllocator.Handle allocHandle = unsafe().recvBufAllocHandle(); ByteBuf data = allocHandle.allocate(config.getAllocator()); allocHandle.attemptedBytesRead(data.writableBytes()); boolean free = true; try { ByteBuffer nioData = data.internalNioBuffer(data.writerIndex(), data.writableBytes()); int pos = nioData.position(); InetSocketAddress remoteAddress = (InetSocketAddress) ch.receive(nioData); if (remoteAddress == null) { return 0; } allocHandle.lastBytesRead(nioData.position() - pos); buf.add(new DatagramPacket(data.writerIndex(data.writerIndex() + allocHandle.lastBytesRead()), localAddress(), remoteAddress)); free = false; return 1; } catch (Throwable cause) { PlatformDependent.throwException(cause); return -1; } finally { if (free) { data.release(); } } }
@Override protected void encode(ChannelHandlerContext ctx, AddressedEnvelope<DnsResponse, InetSocketAddress> in, List<Object> out) throws Exception { final InetSocketAddress recipient = in.recipient(); final DnsResponse response = in.content(); final ByteBuf buf = allocateBuffer(ctx, in); boolean success = false; try { encodeHeader(response, buf); encodeQuestions(response, buf); encodeRecords(response, DnsSection.ANSWER, buf); encodeRecords(response, DnsSection.AUTHORITY, buf); encodeRecords(response, DnsSection.ADDITIONAL, buf); success = true; } finally { if (!success) { buf.release(); } } out.add(new DatagramPacket(buf, recipient, null)); }
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); }