use of io.netty.channel.AddressedEnvelope in project netty by netty.
the class EpollDatagramChannel method filterOutboundMessage.
@Override
protected Object filterOutboundMessage(Object msg) {
if (msg instanceof DatagramPacket) {
DatagramPacket packet = (DatagramPacket) msg;
ByteBuf content = packet.content();
if (content.hasMemoryAddress()) {
return msg;
}
if (content.isDirect() && content instanceof CompositeByteBuf) {
// Special handling of CompositeByteBuf to reduce memory copies if some of the Components
// in the CompositeByteBuf are backed by a memoryAddress.
CompositeByteBuf comp = (CompositeByteBuf) content;
if (comp.isDirect() && comp.nioBufferCount() <= Native.IOV_MAX) {
return msg;
}
}
// passed to write.
return new DatagramPacket(newDirectBuffer(packet, content), packet.recipient());
}
if (msg instanceof ByteBuf) {
ByteBuf buf = (ByteBuf) msg;
if (!buf.hasMemoryAddress() && (PlatformDependent.hasUnsafe() || !buf.isDirect())) {
if (buf instanceof CompositeByteBuf) {
// Special handling of CompositeByteBuf to reduce memory copies if some of the Components
// in the CompositeByteBuf are backed by a memoryAddress.
CompositeByteBuf comp = (CompositeByteBuf) buf;
if (!comp.isDirect() || comp.nioBufferCount() > Native.IOV_MAX) {
// more then 1024 buffers for gathering writes so just do a memory copy.
buf = newDirectBuffer(buf);
assert buf.hasMemoryAddress();
}
} else {
// We can only handle buffers with memory address so we need to copy if a non direct is
// passed to write.
buf = newDirectBuffer(buf);
assert buf.hasMemoryAddress();
}
}
return 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();
if (content.hasMemoryAddress()) {
return e;
}
if (content instanceof CompositeByteBuf) {
// Special handling of CompositeByteBuf to reduce memory copies if some of the Components
// in the CompositeByteBuf are backed by a memoryAddress.
CompositeByteBuf comp = (CompositeByteBuf) content;
if (comp.isDirect() && comp.nioBufferCount() <= Native.IOV_MAX) {
return e;
}
}
// passed to write.
return new DefaultAddressedEnvelope<ByteBuf, InetSocketAddress>(newDirectBuffer(e, content), (InetSocketAddress) e.recipient());
}
}
throw new UnsupportedOperationException("unsupported message type: " + StringUtil.simpleClassName(msg) + EXPECTED_TYPES);
}
use of io.netty.channel.AddressedEnvelope in project camel by apache.
the class DatagramPacketByteArrayCodecTest method testDecoder.
@Test
public void testDecoder() {
ByteBuf buf = Unpooled.buffer();
buf.writeBytes(VALUE.getBytes());
ByteBuf input = buf.duplicate();
AddressedEnvelope<Object, InetSocketAddress> addressedEnvelop = new DefaultAddressedEnvelope<Object, InetSocketAddress>(input, new InetSocketAddress(8888));
EmbeddedChannel channel = new EmbeddedChannel(ChannelHandlerFactories.newByteArrayDecoder("udp").newChannelHandler());
Assert.assertTrue(channel.writeInbound(addressedEnvelop));
Assert.assertTrue(channel.finish());
AddressedEnvelope<Object, InetSocketAddress> result = (AddressedEnvelope) channel.readInbound();
Assert.assertEquals(result.recipient().getPort(), addressedEnvelop.recipient().getPort());
Assert.assertTrue(result.content() instanceof byte[]);
Assert.assertEquals(VALUE, new String((byte[]) result.content()));
Assert.assertNull(channel.readInbound());
}
use of io.netty.channel.AddressedEnvelope in project camel by apache.
the class DatagramPacketByteArrayCodecTest method testEncoder.
@Test
public void testEncoder() {
ByteBuf buf = Unpooled.buffer();
buf.writeBytes(VALUE.getBytes());
AddressedEnvelope<Object, InetSocketAddress> addressedEnvelop = new DefaultAddressedEnvelope<Object, InetSocketAddress>(VALUE.getBytes(), new InetSocketAddress(8888));
EmbeddedChannel channel = new EmbeddedChannel(ChannelHandlerFactories.newByteArrayEncoder("udp").newChannelHandler());
Assert.assertTrue(channel.writeOutbound(addressedEnvelop));
Assert.assertTrue(channel.finish());
AddressedEnvelope output = (AddressedEnvelope) channel.readOutbound();
Assert.assertTrue(output.content() instanceof ByteBuf);
ByteBuf resultContent = (ByteBuf) output.content();
Assert.assertEquals(VALUE, new String(resultContent.array()));
Assert.assertNull(channel.readOutbound());
}
use of io.netty.channel.AddressedEnvelope in project netty by netty.
the class EpollDatagramChannel method doWriteMessage.
private boolean doWriteMessage(Object msg) throws Exception {
final ByteBuf data;
InetSocketAddress remoteAddress;
if (msg instanceof AddressedEnvelope) {
@SuppressWarnings("unchecked") AddressedEnvelope<ByteBuf, InetSocketAddress> envelope = (AddressedEnvelope<ByteBuf, InetSocketAddress>) msg;
data = envelope.content();
remoteAddress = envelope.recipient();
} else {
data = (ByteBuf) msg;
remoteAddress = null;
}
final int dataLen = data.readableBytes();
if (dataLen == 0) {
return true;
}
if (remoteAddress == null) {
remoteAddress = remote;
if (remoteAddress == null) {
throw new NotYetConnectedException();
}
}
final int writtenBytes;
if (data.hasMemoryAddress()) {
long memoryAddress = data.memoryAddress();
writtenBytes = fd().sendToAddress(memoryAddress, data.readerIndex(), data.writerIndex(), remoteAddress.getAddress(), remoteAddress.getPort());
} else if (data instanceof CompositeByteBuf) {
IovArray array = ((EpollEventLoop) eventLoop()).cleanArray();
array.add(data);
int cnt = array.count();
assert cnt != 0;
writtenBytes = fd().sendToAddresses(array.memoryAddress(0), cnt, remoteAddress.getAddress(), remoteAddress.getPort());
} else {
ByteBuffer nioData = data.internalNioBuffer(data.readerIndex(), data.readableBytes());
writtenBytes = fd().sendTo(nioData, nioData.position(), nioData.limit(), remoteAddress.getAddress(), remoteAddress.getPort());
}
return writtenBytes > 0;
}
use of io.netty.channel.AddressedEnvelope in project netty by netty.
the class DnsNameResolverContext method finishResolve.
private void finishResolve(Promise<T> promise) {
if (!queriesInProgress.isEmpty()) {
// If there are queries in progress, we should cancel it because we already finished the resolution.
for (Iterator<Future<AddressedEnvelope<DnsResponse, InetSocketAddress>>> i = queriesInProgress.iterator(); i.hasNext(); ) {
Future<AddressedEnvelope<DnsResponse, InetSocketAddress>> f = i.next();
i.remove();
if (!f.cancel(false)) {
f.addListener(RELEASE_RESPONSE);
}
}
}
if (resolvedEntries != null) {
// Found at least one resolved address.
for (InternetProtocolFamily f : resolvedInternetProtocolFamilies) {
if (finishResolve(f.addressType(), resolvedEntries, promise)) {
return;
}
}
}
// No resolved address found.
final int tries = maxAllowedQueries - allowedQueries;
final StringBuilder buf = new StringBuilder(64);
buf.append("failed to resolve '");
if (pristineHostname != null) {
buf.append(pristineHostname);
} else {
buf.append(hostname);
}
buf.append('\'');
if (tries > 1) {
if (tries < maxAllowedQueries) {
buf.append(" after ").append(tries).append(" queries ");
} else {
buf.append(". Exceeded max queries per resolve ").append(maxAllowedQueries).append(' ');
}
}
if (trace != null) {
buf.append(':').append(trace);
}
final UnknownHostException cause = new UnknownHostException(buf.toString());
resolveCache.cache(hostname, additionals, cause, parent.ch.eventLoop());
promise.tryFailure(cause);
}
Aggregations