Search in sources :

Example 46 with BinaryWebSocketFrame

use of io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame in project netty by netty.

the class DeflateEncoder method encode.

@Override
protected void encode(ChannelHandlerContext ctx, WebSocketFrame msg, List<Object> out) throws Exception {
    final ByteBuf compressedContent;
    if (msg.content().isReadable()) {
        compressedContent = compressContent(ctx, msg);
    } else if (msg.isFinalFragment()) {
        // Set empty DEFLATE block manually for unknown buffer size
        // https://tools.ietf.org/html/rfc7692#section-7.2.3.6
        compressedContent = EMPTY_DEFLATE_BLOCK.duplicate();
    } else {
        throw new CodecException("cannot compress content buffer");
    }
    final WebSocketFrame outMsg;
    if (msg instanceof TextWebSocketFrame) {
        outMsg = new TextWebSocketFrame(msg.isFinalFragment(), rsv(msg), compressedContent);
    } else if (msg instanceof BinaryWebSocketFrame) {
        outMsg = new BinaryWebSocketFrame(msg.isFinalFragment(), rsv(msg), compressedContent);
    } else if (msg instanceof ContinuationWebSocketFrame) {
        outMsg = new ContinuationWebSocketFrame(msg.isFinalFragment(), rsv(msg), compressedContent);
    } else {
        throw new CodecException("unexpected frame type: " + msg.getClass().getName());
    }
    out.add(outMsg);
}
Also used : ContinuationWebSocketFrame(io.netty.handler.codec.http.websocketx.ContinuationWebSocketFrame) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) BinaryWebSocketFrame(io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame) CodecException(io.netty.handler.codec.CodecException) WebSocketFrame(io.netty.handler.codec.http.websocketx.WebSocketFrame) ContinuationWebSocketFrame(io.netty.handler.codec.http.websocketx.ContinuationWebSocketFrame) BinaryWebSocketFrame(io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf)

Example 47 with BinaryWebSocketFrame

use of io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame in project netty by netty.

the class PerFrameDeflateEncoderTest method testCompressedFrame.

@Test
public void testCompressedFrame() {
    EmbeddedChannel encoderChannel = new EmbeddedChannel(new PerFrameDeflateEncoder(9, 15, false));
    EmbeddedChannel decoderChannel = new EmbeddedChannel(ZlibCodecFactory.newZlibDecoder(ZlibWrapper.NONE));
    // initialize
    byte[] payload = new byte[300];
    random.nextBytes(payload);
    BinaryWebSocketFrame frame = new BinaryWebSocketFrame(true, WebSocketExtension.RSV3, Unpooled.wrappedBuffer(payload));
    // execute
    assertTrue(encoderChannel.writeOutbound(frame));
    BinaryWebSocketFrame compressedFrame = encoderChannel.readOutbound();
    // test
    assertNotNull(compressedFrame);
    assertNotNull(compressedFrame.content());
    assertEquals(WebSocketExtension.RSV1 | WebSocketExtension.RSV3, compressedFrame.rsv());
    assertTrue(decoderChannel.writeInbound(compressedFrame.content()));
    assertTrue(decoderChannel.writeInbound(DeflateDecoder.FRAME_TAIL.duplicate()));
    ByteBuf uncompressedPayload = decoderChannel.readInbound();
    assertEquals(300, uncompressedPayload.readableBytes());
    byte[] finalPayload = new byte[300];
    uncompressedPayload.readBytes(finalPayload);
    assertArrayEquals(finalPayload, payload);
    uncompressedPayload.release();
}
Also used : EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) BinaryWebSocketFrame(io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

Example 48 with BinaryWebSocketFrame

use of io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame in project netty by netty.

the class PerFrameDeflateEncoderTest method testCompressionSkip.

@Test
public void testCompressionSkip() {
    EmbeddedChannel encoderChannel = new EmbeddedChannel(new PerFrameDeflateEncoder(9, 15, false, ALWAYS_SKIP));
    byte[] payload = new byte[300];
    random.nextBytes(payload);
    BinaryWebSocketFrame binaryFrame = new BinaryWebSocketFrame(true, 0, Unpooled.wrappedBuffer(payload));
    // execute
    assertTrue(encoderChannel.writeOutbound(binaryFrame.copy()));
    BinaryWebSocketFrame outboundFrame = encoderChannel.readOutbound();
    // test
    assertNotNull(outboundFrame);
    assertNotNull(outboundFrame.content());
    assertArrayEquals(payload, ByteBufUtil.getBytes(outboundFrame.content()));
    assertEquals(0, outboundFrame.rsv());
    assertTrue(outboundFrame.release());
    assertFalse(encoderChannel.finish());
}
Also used : EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) BinaryWebSocketFrame(io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame) Test(org.junit.jupiter.api.Test)

Example 49 with BinaryWebSocketFrame

use of io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame in project netty by netty.

the class PerFrameDeflateEncoderTest method testFramementedFrame.

@Test
public void testFramementedFrame() {
    EmbeddedChannel encoderChannel = new EmbeddedChannel(new PerFrameDeflateEncoder(9, 15, false));
    EmbeddedChannel decoderChannel = new EmbeddedChannel(ZlibCodecFactory.newZlibDecoder(ZlibWrapper.NONE));
    // initialize
    byte[] payload1 = new byte[100];
    random.nextBytes(payload1);
    byte[] payload2 = new byte[100];
    random.nextBytes(payload2);
    byte[] payload3 = new byte[100];
    random.nextBytes(payload3);
    BinaryWebSocketFrame frame1 = new BinaryWebSocketFrame(false, WebSocketExtension.RSV3, Unpooled.wrappedBuffer(payload1));
    ContinuationWebSocketFrame frame2 = new ContinuationWebSocketFrame(false, WebSocketExtension.RSV3, Unpooled.wrappedBuffer(payload2));
    ContinuationWebSocketFrame frame3 = new ContinuationWebSocketFrame(true, WebSocketExtension.RSV3, Unpooled.wrappedBuffer(payload3));
    // execute
    assertTrue(encoderChannel.writeOutbound(frame1));
    assertTrue(encoderChannel.writeOutbound(frame2));
    assertTrue(encoderChannel.writeOutbound(frame3));
    BinaryWebSocketFrame compressedFrame1 = encoderChannel.readOutbound();
    ContinuationWebSocketFrame compressedFrame2 = encoderChannel.readOutbound();
    ContinuationWebSocketFrame compressedFrame3 = encoderChannel.readOutbound();
    // test
    assertNotNull(compressedFrame1);
    assertNotNull(compressedFrame2);
    assertNotNull(compressedFrame3);
    assertEquals(WebSocketExtension.RSV1 | WebSocketExtension.RSV3, compressedFrame1.rsv());
    assertEquals(WebSocketExtension.RSV1 | WebSocketExtension.RSV3, compressedFrame2.rsv());
    assertEquals(WebSocketExtension.RSV1 | WebSocketExtension.RSV3, compressedFrame3.rsv());
    assertFalse(compressedFrame1.isFinalFragment());
    assertFalse(compressedFrame2.isFinalFragment());
    assertTrue(compressedFrame3.isFinalFragment());
    assertTrue(decoderChannel.writeInbound(compressedFrame1.content()));
    assertTrue(decoderChannel.writeInbound(DeflateDecoder.FRAME_TAIL.duplicate()));
    ByteBuf uncompressedPayload1 = decoderChannel.readInbound();
    byte[] finalPayload1 = new byte[100];
    uncompressedPayload1.readBytes(finalPayload1);
    assertArrayEquals(finalPayload1, payload1);
    uncompressedPayload1.release();
    assertTrue(decoderChannel.writeInbound(compressedFrame2.content()));
    assertTrue(decoderChannel.writeInbound(DeflateDecoder.FRAME_TAIL.duplicate()));
    ByteBuf uncompressedPayload2 = decoderChannel.readInbound();
    byte[] finalPayload2 = new byte[100];
    uncompressedPayload2.readBytes(finalPayload2);
    assertArrayEquals(finalPayload2, payload2);
    uncompressedPayload2.release();
    assertTrue(decoderChannel.writeInbound(compressedFrame3.content()));
    assertTrue(decoderChannel.writeInbound(DeflateDecoder.FRAME_TAIL.duplicate()));
    ByteBuf uncompressedPayload3 = decoderChannel.readInbound();
    byte[] finalPayload3 = new byte[100];
    uncompressedPayload3.readBytes(finalPayload3);
    assertArrayEquals(finalPayload3, payload3);
    uncompressedPayload3.release();
}
Also used : ContinuationWebSocketFrame(io.netty.handler.codec.http.websocketx.ContinuationWebSocketFrame) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) BinaryWebSocketFrame(io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

Example 50 with BinaryWebSocketFrame

use of io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame in project autobahn-java by crossbario.

the class NettyWebSocket method send.

@Override
public void send(byte[] payload, boolean isBinary) {
    WebSocketFrame frame;
    if (isBinary) {
        frame = new BinaryWebSocketFrame(toByteBuf(payload));
    } else {
        frame = new TextWebSocketFrame(toByteBuf(payload));
    }
    mChannel.writeAndFlush(frame);
}
Also used : TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) BinaryWebSocketFrame(io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame) WebSocketFrame(io.netty.handler.codec.http.websocketx.WebSocketFrame) BinaryWebSocketFrame(io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame)

Aggregations

BinaryWebSocketFrame (io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame)51 ByteBuf (io.netty.buffer.ByteBuf)24 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)24 Test (org.junit.jupiter.api.Test)23 TextWebSocketFrame (io.netty.handler.codec.http.websocketx.TextWebSocketFrame)19 ContinuationWebSocketFrame (io.netty.handler.codec.http.websocketx.ContinuationWebSocketFrame)17 WebSocketFrame (io.netty.handler.codec.http.websocketx.WebSocketFrame)11 FrameChecker (io.undertow.websockets.utils.FrameChecker)8 WebSocketTestClient (io.undertow.websockets.utils.WebSocketTestClient)8 URI (java.net.URI)8 FutureResult (org.xnio.FutureResult)8 CloseWebSocketFrame (io.netty.handler.codec.http.websocketx.CloseWebSocketFrame)7 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 PingWebSocketFrame (io.netty.handler.codec.http.websocketx.PingWebSocketFrame)6 PongWebSocketFrame (io.netty.handler.codec.http.websocketx.PongWebSocketFrame)6 ServerWebSocketContainer (io.undertow.websockets.jsr.ServerWebSocketContainer)6 UndertowSession (io.undertow.websockets.jsr.UndertowSession)6 AnnotatedClientEndpoint (io.undertow.websockets.jsr.test.annotated.AnnotatedClientEndpoint)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 Endpoint (javax.websocket.Endpoint)6