use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf in project vert.x by eclipse.
the class DatagramTest method testSendReceive.
@Test
public void testSendReceive() {
peer1 = vertx.createDatagramSocket(new DatagramSocketOptions());
peer2 = vertx.createDatagramSocket(new DatagramSocketOptions());
peer2.exceptionHandler(t -> fail(t.getMessage()));
peer2.listen(1234, "127.0.0.1", ar -> {
assertTrue(ar.succeeded());
Buffer buffer = TestUtils.randomBuffer(128);
peer2.handler(packet -> {
Buffer data = packet.data();
ByteBuf buff = data.getByteBuf();
while (buff != buff.unwrap() && buff.unwrap() != null) {
buff = buff.unwrap();
}
assertTrue("Was expecting an unpooled buffer instead of " + buff.getClass().getSimpleName(), buff.getClass().getSimpleName().contains("Unpooled"));
assertEquals(buffer, data);
testComplete();
});
peer1.send(buffer, 1234, "127.0.0.1", ar2 -> assertTrue(ar2.succeeded()));
});
await();
}
use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf in project elasticsearch by elastic.
the class Netty4MessageChannelHandler method channelRead.
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
Transports.assertTransportThread();
if (!(msg instanceof ByteBuf)) {
ctx.fireChannelRead(msg);
return;
}
final ByteBuf buffer = (ByteBuf) msg;
final int remainingMessageSize = buffer.getInt(buffer.readerIndex() - TcpHeader.MESSAGE_LENGTH_SIZE);
final int expectedReaderIndex = buffer.readerIndex() + remainingMessageSize;
try {
InetSocketAddress remoteAddress = (InetSocketAddress) ctx.channel().remoteAddress();
// netty always copies a buffer, either in NioWorker in its read handler, where it copies to a fresh
// buffer, or in the cumulative buffer, which is cleaned each time so it could be bigger than the actual size
BytesReference reference = Netty4Utils.toBytesReference(buffer, remainingMessageSize);
transport.messageReceived(reference, ctx.channel(), profileName, remoteAddress, remainingMessageSize);
} finally {
// Set the expected position of the buffer, no matter what happened
buffer.readerIndex(expectedReaderIndex);
}
}
use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf in project elasticsearch by elastic.
the class Netty4SizeHeaderFrameDecoder method decode.
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
try {
boolean continueProcessing = TcpTransport.validateMessageHeader(Netty4Utils.toBytesReference(in));
final ByteBuf message = in.skipBytes(TcpHeader.MARKER_BYTES_SIZE + TcpHeader.MESSAGE_LENGTH_SIZE);
if (!continueProcessing)
return;
out.add(message);
} catch (IllegalArgumentException ex) {
throw new TooLongFrameException(ex);
} catch (IllegalStateException ex) {
/* decode will be called until the ByteBuf is fully consumed; when it is fully
* consumed, transport#validateMessageHeader will throw an IllegalStateException which
* is okay, it means we have finished consuming the ByteBuf and we can get out
*/
}
}
use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf in project elasticsearch by elastic.
the class Netty4HttpClient method processRequestsWithBody.
private Collection<FullHttpResponse> processRequestsWithBody(HttpMethod method, SocketAddress remoteAddress, Tuple<String, CharSequence>... urisAndBodies) throws InterruptedException {
Collection<HttpRequest> requests = new ArrayList<>(urisAndBodies.length);
for (Tuple<String, CharSequence> uriAndBody : urisAndBodies) {
ByteBuf content = Unpooled.copiedBuffer(uriAndBody.v2(), StandardCharsets.UTF_8);
HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, method, uriAndBody.v1(), content);
request.headers().add(HttpHeaderNames.HOST, "localhost");
request.headers().add(HttpHeaderNames.CONTENT_LENGTH, content.readableBytes());
request.headers().add(HttpHeaderNames.CONTENT_TYPE, "application/json");
requests.add(request);
}
return sendRequests(remoteAddress, requests);
}
use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf in project elasticsearch by elastic.
the class Netty4UtilsTests method testToChannelBuffer.
public void testToChannelBuffer() throws IOException {
BytesReference ref = getRandomizedBytesReference(randomIntBetween(1, 3 * PAGE_SIZE));
ByteBuf buffer = Netty4Utils.toByteBuf(ref);
BytesReference bytesReference = Netty4Utils.toBytesReference(buffer);
if (ref instanceof ByteBufBytesReference) {
assertEquals(buffer, ((ByteBufBytesReference) ref).toByteBuf());
} else if (AbstractBytesReferenceTestCase.getNumPages(ref) > 1) {
// we gather the buffers into a channel buffer
assertTrue(buffer instanceof CompositeByteBuf);
}
assertArrayEquals(BytesReference.toBytes(ref), BytesReference.toBytes(bytesReference));
}
Aggregations