List of usage examples for io.netty.buffer ByteBuf copy
public abstract ByteBuf copy();
From source file:io.vertx.test.core.Http2ServerTest.java
License:Open Source License
@Test public void testSendPing() throws Exception { waitFor(2);//from w w w.j ava2s .com Buffer expected = TestUtils.randomBuffer(8); Context ctx = vertx.getOrCreateContext(); server.close(); server.connectionHandler(conn -> { conn.ping(expected, ar -> { assertSame(ctx, Vertx.currentContext()); assertTrue(ar.succeeded()); assertEquals(expected, ar.result()); complete(); }); }); server.requestHandler(req -> fail()); startServer(ctx); TestClient client = new TestClient(); ChannelFuture fut = client.connect(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, request -> { request.decoder.frameListener(new Http2EventAdapter() { @Override public void onPingRead(ChannelHandlerContext ctx, ByteBuf data) throws Http2Exception { Buffer buffer = Buffer.buffer(data.copy()); vertx.runOnContext(v -> { assertEquals(expected, buffer); complete(); }); } }); }); fut.sync(); await(); }
From source file:k4unl.minecraft.Hydraulicraft.network.PacketPipeline.java
License:LGPL
@Override protected void encode(ChannelHandlerContext ctx, AbstractPacket msg, List<Object> out) throws Exception { ByteBuf buffer = Unpooled.buffer(); Class<? extends AbstractPacket> clazz = msg.getClass(); if (!packets.contains(msg.getClass())) { throw new NullPointerException("No Packet Registered for: " + msg.getClass().getCanonicalName()); }/*from w w w .ja v a 2 s . co m*/ byte discriminator = (byte) packets.indexOf(clazz); buffer.writeByte(discriminator); msg.encodeInto(ctx, buffer); FMLProxyPacket proxyPacket = new FMLProxyPacket(buffer.copy(), ctx.channel().attr(NetworkRegistry.FML_CHANNEL).get()); out.add(proxyPacket); }
From source file:net.dries007.races.network.PacketPipeline.java
License:Open Source License
@Override protected void encode(ChannelHandlerContext ctx, AbstractPacket msg, List<Object> out) throws Exception { ByteBuf buffer = Unpooled.buffer(); Class<? extends AbstractPacket> clazz = msg.getClass(); if (!this.packets.contains(msg.getClass())) { throw new NullPointerException("No Packet Registered for: " + msg.getClass().getCanonicalName()); }/*w ww. ja va2s. c o m*/ byte discriminator = (byte) this.packets.indexOf(clazz); buffer.writeByte(discriminator); msg.encodeInto(ctx, buffer); FMLProxyPacket proxyPacket = new FMLProxyPacket(buffer.copy(), ctx.channel().attr(NetworkRegistry.FML_CHANNEL).get()); out.add(proxyPacket); }
From source file:net.openhft.fix.transport.test.NettyFrameDecoderTest.java
License:Apache License
@Test public void testDecode() { ByteBuf message = newLogonMessage(); ByteBuf msgcopy = message.copy(); EmbeddedChannel channel = new EmbeddedChannel(new NettyFrameDecoder()); // raw write write Assert.assertTrue(channel.writeInbound(message)); Assert.assertTrue(channel.finish()); // read/*ww w . j a v a 2 s. c o m*/ byte[] result = (byte[]) channel.readInbound(); Assert.assertNotNull(result); if (msgcopy.hasArray()) { Assert.assertArrayEquals(msgcopy.array(), result); } }
From source file:org.aotorrent.client.PeerRequestDecoder.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { if (in.readableBytes() < 4) { return;/*from w w w .ja v a 2 s . com*/ } final int messageLength = in.readInt(); if (messageLength == 0) { return; } final byte messageTypeId = in.readByte(); switch (messageTypeId) { case 0: out.add(new ChokeRequest()); break; case 1: out.add(new UnChokeRequest()); break; case 2: out.add(new InterestedRequest()); break; case 3: out.add(new NotInterestedRequest()); break; case 4: out.add(new HaveRequest(in.readInt())); break; case 5: out.add(new BitFieldRequest(in, messageLength - 1)); break; case 6: out.add(new RequestRequest(in.readInt(), in.readInt(), in.readInt())); break; case 7: out.add(new PieceRequest(in.readInt(), in.readInt(), in.copy())); break; case 8: break; //TODO CancelRequest. make it case 9: break; //TODO PortRequest. make it too } //ReferenceCountUtil.release(in); }
From source file:org.apache.qpid.jms.provider.amqp.message.AmqpJmsMessageBuilder.java
License:Apache License
private static JmsObjectMessage createObjectMessage(AmqpConsumer consumer, Message message, ByteBuf messageBytes) { return new JmsObjectMessage(new AmqpJmsObjectMessageFacade(consumer, message, messageBytes.copy())); }
From source file:org.apache.qpid.jms.transports.netty.NettyTcpTransportTest.java
License:Apache License
@Test(timeout = 60 * 1000) public void testMultipleConnectionsSendReceive() throws Exception { final int CONNECTION_COUNT = 10; final int FRAME_SIZE = 8; ByteBuf sendBuffer = Unpooled.buffer(FRAME_SIZE); for (int i = 0; i < 8; ++i) { sendBuffer.writeByte('A'); }//www.j a v a 2 s. c o m try (NettyEchoServer server = createEchoServer(createServerOptions())) { server.start(); int port = server.getServerPort(); URI serverLocation = new URI("tcp://localhost:" + port); List<Transport> transports = new ArrayList<Transport>(); for (int i = 0; i < CONNECTION_COUNT; ++i) { Transport transport = createTransport(serverLocation, testListener, createClientOptions()); try { transport.connect(null); transport.send(sendBuffer.copy()); transports.add(transport); } catch (Exception e) { fail("Should have connected to the server at " + serverLocation + " but got exception: " + e); } } assertTrue(Wait.waitFor(new Wait.Condition() { @Override public boolean isSatisified() throws Exception { LOG.debug("Checking completion: read {} expecting {}", bytesRead.get(), (FRAME_SIZE * CONNECTION_COUNT)); return bytesRead.get() == (FRAME_SIZE * CONNECTION_COUNT); } })); for (Transport transport : transports) { transport.close(); } } assertTrue(exceptions.isEmpty()); }
From source file:org.apache.qpid.jms.transports.netty.NettyTcpTransportTest.java
License:Apache License
public void doMultipleDataPacketsSentAndReceive(final int byteCount, final int iterations) throws Exception { try (NettyEchoServer server = createEchoServer(createServerOptions())) { server.start();//w ww . ja va 2 s.c om int port = server.getServerPort(); URI serverLocation = new URI("tcp://localhost:" + port); Transport transport = createTransport(serverLocation, testListener, createClientOptions()); try { transport.connect(null); LOG.info("Connected to server:{} as expected.", serverLocation); } catch (Exception e) { fail("Should have connected to the server at " + serverLocation + " but got exception: " + e); } assertTrue(transport.isConnected()); ByteBuf sendBuffer = Unpooled.buffer(byteCount); for (int i = 0; i < byteCount; ++i) { sendBuffer.writeByte('A'); } for (int i = 0; i < iterations; ++i) { transport.send(sendBuffer.copy()); } assertTrue(Wait.waitFor(new Wait.Condition() { @Override public boolean isSatisified() throws Exception { return bytesRead.get() == (byteCount * iterations); } })); transport.close(); } assertTrue(!transportClosed); // Normal shutdown does not trigger the event. assertTrue(exceptions.isEmpty()); }
From source file:org.apache.qpid.jms.transports.netty.NettyWsTransportTest.java
License:Apache License
@Test(timeout = 60000) public void testConnectionsSendReceiveLargeDataWhenFrameSizeAllowsIt() throws Exception { final int FRAME_SIZE = 8192; ByteBuf sendBuffer = Unpooled.buffer(FRAME_SIZE); for (int i = 0; i < FRAME_SIZE; ++i) { sendBuffer.writeByte('A'); }/*from w ww .j a va 2 s. co m*/ try (NettyEchoServer server = createEchoServer(createServerOptions())) { // Server should pass the data through without issue with this size server.setMaxFrameSize(FRAME_SIZE); server.start(); int port = server.getServerPort(); URI serverLocation = new URI("tcp://localhost:" + port); List<Transport> transports = new ArrayList<Transport>(); Transport transport = createTransport(serverLocation, testListener, createClientOptions()); try { // The transport should allow for the size of data we sent. transport.setMaxFrameSize(FRAME_SIZE); transport.connect(null); transports.add(transport); transport.send(sendBuffer.copy()); } catch (Exception e) { fail("Should have connected to the server at " + serverLocation + " but got exception: " + e); } assertTrue(Wait.waitFor(new Wait.Condition() { @Override public boolean isSatisfied() throws Exception { LOG.debug("Checking completion: read {} expecting {}", bytesRead.get(), FRAME_SIZE); return bytesRead.get() == FRAME_SIZE || !transport.isConnected(); } }, 10000, 50)); assertTrue("Connection failed while receiving.", transport.isConnected()); transport.close(); } assertTrue(exceptions.isEmpty()); }
From source file:org.apache.qpid.jms.transports.netty.NettyWsTransportTest.java
License:Apache License
@Test(timeout = 20000) public void testConnectionReceivesFragmentedData() throws Exception { final int FRAME_SIZE = 5317; ByteBuf sendBuffer = Unpooled.buffer(FRAME_SIZE); for (int i = 0; i < FRAME_SIZE; ++i) { sendBuffer.writeByte('A' + (i % 10)); }/*from ww w . j a va 2s .co m*/ try (NettyEchoServer server = createEchoServer(createServerOptions())) { server.setMaxFrameSize(FRAME_SIZE); // Server should fragment the data as it goes through server.setFragmentWrites(true); server.start(); int port = server.getServerPort(); URI serverLocation = new URI("tcp://localhost:" + port); List<Transport> transports = new ArrayList<Transport>(); TransportOptions createClientOptions = createClientOptions(); createClientOptions.setTraceBytes(true); NettyTransportListener wsListener = new NettyTransportListener(true); Transport transport = createTransport(serverLocation, wsListener, createClientOptions); try { transport.setMaxFrameSize(FRAME_SIZE); transport.connect(null); transports.add(transport); transport.send(sendBuffer.copy()); } catch (Exception e) { fail("Should have connected to the server at " + serverLocation + " but got exception: " + e); } assertTrue(Wait.waitFor(new Wait.Condition() { @Override public boolean isSatisfied() throws Exception { LOG.debug("Checking completion: read {} expecting {}", bytesRead.get(), FRAME_SIZE); return bytesRead.get() == FRAME_SIZE || !transport.isConnected(); } }, 10000, 50)); assertTrue("Connection failed while receiving.", transport.isConnected()); transport.close(); assertEquals("Expected 2 data packets due to seperate websocket frames", 2, data.size()); ByteBuf receivedBuffer = Unpooled.buffer(FRAME_SIZE); for (ByteBuf buf : data) { buf.readBytes(receivedBuffer, buf.readableBytes()); } assertEquals("Unexpected data length", FRAME_SIZE, receivedBuffer.readableBytes()); assertTrue("Unexpected data", ByteBufUtil.equals(sendBuffer, 0, receivedBuffer, 0, FRAME_SIZE)); } finally { for (ByteBuf buf : data) { buf.release(); } } assertTrue(exceptions.isEmpty()); }