Search in sources :

Example 11 with BinaryWebSocketFrame

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

the class JsrWebSocketServer07Test method testBinaryWithByteArray.

@org.junit.Test
public void testBinaryWithByteArray() throws Exception {
    final byte[] payload = "payload".getBytes();
    final AtomicReference<Throwable> cause = new AtomicReference<>();
    final AtomicBoolean connected = new AtomicBoolean(false);
    final FutureResult latch = new FutureResult();
    class TestEndPoint extends Endpoint {

        @Override
        public void onOpen(final Session session, EndpointConfig config) {
            connected.set(true);
            session.addMessageHandler(new MessageHandler.Whole<byte[]>() {

                @Override
                public void onMessage(byte[] message) {
                    session.getAsyncRemote().sendBinary(ByteBuffer.wrap(message.clone()));
                }
            });
        }
    }
    ServerWebSocketContainer builder = new ServerWebSocketContainer(TestClassIntrospector.INSTANCE, DefaultServer.getWorker(), DefaultServer.getBufferPool(), Collections.EMPTY_LIST, false, false);
    builder.addEndpoint(ServerEndpointConfig.Builder.create(TestEndPoint.class, "/").configurator(new InstanceConfigurator(new TestEndPoint())).build());
    deployServlet(builder);
    WebSocketTestClient client = new WebSocketTestClient(getVersion(), new URI("ws://" + DefaultServer.getHostAddress("default") + ":" + DefaultServer.getHostPort("default") + "/"));
    client.connect();
    client.send(new BinaryWebSocketFrame(Unpooled.wrappedBuffer(payload)), new FrameChecker(BinaryWebSocketFrame.class, payload, latch));
    latch.getIoFuture().get();
    Assert.assertNull(cause.get());
    client.destroy();
}
Also used : MessageHandler(javax.websocket.MessageHandler) ServerWebSocketContainer(io.undertow.websockets.jsr.ServerWebSocketContainer) BinaryWebSocketFrame(io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame) AtomicReference(java.util.concurrent.atomic.AtomicReference) URI(java.net.URI) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) WebSocketTestClient(io.undertow.websockets.utils.WebSocketTestClient) FutureResult(org.xnio.FutureResult) Endpoint(javax.websocket.Endpoint) AnnotatedClientEndpoint(io.undertow.websockets.jsr.test.annotated.AnnotatedClientEndpoint) FrameChecker(io.undertow.websockets.utils.FrameChecker) ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) EndpointConfig(javax.websocket.EndpointConfig) Session(javax.websocket.Session) UndertowSession(io.undertow.websockets.jsr.UndertowSession) Test(org.junit.Test)

Example 12 with BinaryWebSocketFrame

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

the class JsrWebSocketServer07Test method testBinaryWithByteBufferByCompletion.

@org.junit.Test
public void testBinaryWithByteBufferByCompletion() throws Exception {
    final byte[] payload = "payload".getBytes();
    final AtomicReference<SendResult> sendResult = new AtomicReference<>();
    final AtomicBoolean connected = new AtomicBoolean(false);
    final FutureResult latch = new FutureResult();
    final FutureResult latch2 = new FutureResult();
    class TestEndPoint extends Endpoint {

        @Override
        public void onOpen(final Session session, EndpointConfig config) {
            connected.set(true);
            session.addMessageHandler(new MessageHandler.Whole<ByteBuffer>() {

                @Override
                public void onMessage(ByteBuffer message) {
                    ByteBuffer buf = ByteBuffer.allocate(message.remaining());
                    buf.put(message);
                    buf.flip();
                    session.getAsyncRemote().sendBinary(buf, new SendHandler() {

                        @Override
                        public void onResult(SendResult result) {
                            sendResult.set(result);
                            if (result.getException() != null) {
                                latch2.setException(new IOException(result.getException()));
                            } else {
                                latch2.setResult(null);
                            }
                        }
                    });
                }
            });
        }
    }
    ServerWebSocketContainer builder = new ServerWebSocketContainer(TestClassIntrospector.INSTANCE, DefaultServer.getWorker(), DefaultServer.getBufferPool(), Collections.EMPTY_LIST, false, false);
    builder.addEndpoint(ServerEndpointConfig.Builder.create(TestEndPoint.class, "/").configurator(new InstanceConfigurator(new TestEndPoint())).build());
    deployServlet(builder);
    WebSocketTestClient client = new WebSocketTestClient(getVersion(), new URI("ws://" + DefaultServer.getHostAddress("default") + ":" + DefaultServer.getHostPort("default") + "/"));
    client.connect();
    client.send(new BinaryWebSocketFrame(Unpooled.wrappedBuffer(payload)), new FrameChecker(BinaryWebSocketFrame.class, payload, latch));
    latch.getIoFuture().get();
    latch2.getIoFuture().get();
    SendResult result = sendResult.get();
    Assert.assertNotNull(result);
    Assert.assertNull(result.getException());
    client.destroy();
}
Also used : SendHandler(javax.websocket.SendHandler) MessageHandler(javax.websocket.MessageHandler) ServerWebSocketContainer(io.undertow.websockets.jsr.ServerWebSocketContainer) BinaryWebSocketFrame(io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) URI(java.net.URI) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) WebSocketTestClient(io.undertow.websockets.utils.WebSocketTestClient) FutureResult(org.xnio.FutureResult) Endpoint(javax.websocket.Endpoint) AnnotatedClientEndpoint(io.undertow.websockets.jsr.test.annotated.AnnotatedClientEndpoint) SendResult(javax.websocket.SendResult) FrameChecker(io.undertow.websockets.utils.FrameChecker) ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) EndpointConfig(javax.websocket.EndpointConfig) Session(javax.websocket.Session) UndertowSession(io.undertow.websockets.jsr.UndertowSession) Test(org.junit.Test)

Example 13 with BinaryWebSocketFrame

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

the class JsrWebSocketServer07Test method testBinaryWithByteBufferAsync.

@org.junit.Test
public void testBinaryWithByteBufferAsync() throws Exception {
    final byte[] payload = "payload".getBytes();
    final AtomicReference<Throwable> cause = new AtomicReference<>();
    final AtomicBoolean connected = new AtomicBoolean(false);
    final FutureResult latch = new FutureResult();
    class TestEndPoint extends Endpoint {

        @Override
        public void onOpen(final Session session, EndpointConfig config) {
            connected.set(true);
            session.addMessageHandler(new MessageHandler.Partial<ByteBuffer>() {

                @Override
                public void onMessage(ByteBuffer message, boolean last) {
                    Assert.assertTrue(last);
                    ByteBuffer buf = ByteBuffer.allocate(message.remaining());
                    buf.put(message);
                    buf.flip();
                    session.getAsyncRemote().sendBinary(buf);
                }
            });
        }
    }
    ServerWebSocketContainer builder = new ServerWebSocketContainer(TestClassIntrospector.INSTANCE, DefaultServer.getWorker(), DefaultServer.getBufferPool(), Collections.EMPTY_LIST, false, false);
    builder.addEndpoint(ServerEndpointConfig.Builder.create(TestEndPoint.class, "/").configurator(new InstanceConfigurator(new TestEndPoint())).build());
    deployServlet(builder);
    WebSocketTestClient client = new WebSocketTestClient(getVersion(), new URI("ws://" + DefaultServer.getHostAddress("default") + ":" + DefaultServer.getHostPort("default") + "/"));
    client.connect();
    client.send(new BinaryWebSocketFrame(Unpooled.wrappedBuffer(payload)), new FrameChecker(BinaryWebSocketFrame.class, payload, latch));
    latch.getIoFuture().get();
    Assert.assertNull(cause.get());
    client.destroy();
}
Also used : MessageHandler(javax.websocket.MessageHandler) ServerWebSocketContainer(io.undertow.websockets.jsr.ServerWebSocketContainer) BinaryWebSocketFrame(io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteBuffer(java.nio.ByteBuffer) URI(java.net.URI) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) WebSocketTestClient(io.undertow.websockets.utils.WebSocketTestClient) FutureResult(org.xnio.FutureResult) Endpoint(javax.websocket.Endpoint) AnnotatedClientEndpoint(io.undertow.websockets.jsr.test.annotated.AnnotatedClientEndpoint) FrameChecker(io.undertow.websockets.utils.FrameChecker) ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) EndpointConfig(javax.websocket.EndpointConfig) Session(javax.websocket.Session) UndertowSession(io.undertow.websockets.jsr.UndertowSession) Test(org.junit.Test)

Example 14 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 {
    if (encoder == null) {
        encoder = new EmbeddedChannel(ZlibCodecFactory.newZlibEncoder(ZlibWrapper.NONE, compressionLevel, windowSize, 8));
    }
    encoder.writeOutbound(msg.content().retain());
    CompositeByteBuf fullCompressedContent = ctx.alloc().compositeBuffer();
    for (; ; ) {
        ByteBuf partCompressedContent = encoder.readOutbound();
        if (partCompressedContent == null) {
            break;
        }
        if (!partCompressedContent.isReadable()) {
            partCompressedContent.release();
            continue;
        }
        fullCompressedContent.addComponent(true, partCompressedContent);
    }
    if (fullCompressedContent.numComponents() <= 0) {
        fullCompressedContent.release();
        throw new CodecException("cannot read compressed buffer");
    }
    if (msg.isFinalFragment() && noContext) {
        cleanup();
    }
    ByteBuf compressedContent;
    if (removeFrameTail(msg)) {
        int realLength = fullCompressedContent.readableBytes() - FRAME_TAIL.length;
        compressedContent = fullCompressedContent.slice(0, realLength);
    } else {
        compressedContent = fullCompressedContent;
    }
    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 : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ContinuationWebSocketFrame(io.netty.handler.codec.http.websocketx.ContinuationWebSocketFrame) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) 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 15 with BinaryWebSocketFrame

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

the class PerMessageDeflateDecoderTest method testFramementedFrame.

@Test
public void testFramementedFrame() {
    EmbeddedChannel encoderChannel = new EmbeddedChannel(ZlibCodecFactory.newZlibEncoder(ZlibWrapper.NONE, 9, 15, 8));
    EmbeddedChannel decoderChannel = new EmbeddedChannel(new PerMessageDeflateDecoder(false));
    // initialize
    byte[] payload = new byte[300];
    random.nextBytes(payload);
    encoderChannel.writeOutbound(Unpooled.wrappedBuffer(payload));
    ByteBuf compressedPayload = encoderChannel.readOutbound();
    compressedPayload = compressedPayload.slice(0, compressedPayload.readableBytes() - 4);
    int oneThird = compressedPayload.readableBytes() / 3;
    BinaryWebSocketFrame compressedFrame1 = new BinaryWebSocketFrame(false, WebSocketExtension.RSV1 | WebSocketExtension.RSV3, compressedPayload.slice(0, oneThird));
    ContinuationWebSocketFrame compressedFrame2 = new ContinuationWebSocketFrame(false, WebSocketExtension.RSV3, compressedPayload.slice(oneThird, oneThird));
    ContinuationWebSocketFrame compressedFrame3 = new ContinuationWebSocketFrame(true, WebSocketExtension.RSV3, compressedPayload.slice(oneThird * 2, compressedPayload.readableBytes() - oneThird * 2));
    // execute
    decoderChannel.writeInbound(compressedFrame1.retain());
    decoderChannel.writeInbound(compressedFrame2.retain());
    decoderChannel.writeInbound(compressedFrame3);
    BinaryWebSocketFrame uncompressedFrame1 = decoderChannel.readInbound();
    ContinuationWebSocketFrame uncompressedFrame2 = decoderChannel.readInbound();
    ContinuationWebSocketFrame uncompressedFrame3 = decoderChannel.readInbound();
    // test
    assertNotNull(uncompressedFrame1);
    assertNotNull(uncompressedFrame2);
    assertNotNull(uncompressedFrame3);
    assertEquals(WebSocketExtension.RSV3, uncompressedFrame1.rsv());
    assertEquals(WebSocketExtension.RSV3, uncompressedFrame2.rsv());
    assertEquals(WebSocketExtension.RSV3, uncompressedFrame3.rsv());
    ByteBuf finalPayloadWrapped = Unpooled.wrappedBuffer(uncompressedFrame1.content(), uncompressedFrame2.content(), uncompressedFrame3.content());
    assertEquals(300, finalPayloadWrapped.readableBytes());
    byte[] finalPayload = new byte[300];
    finalPayloadWrapped.readBytes(finalPayload);
    assertTrue(Arrays.equals(finalPayload, payload));
    finalPayloadWrapped.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.Test)

Aggregations

BinaryWebSocketFrame (io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame)28 Test (org.junit.Test)20 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)15 ByteBuf (io.netty.buffer.ByteBuf)12 FrameChecker (io.undertow.websockets.utils.FrameChecker)7 WebSocketTestClient (io.undertow.websockets.utils.WebSocketTestClient)7 URI (java.net.URI)7 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 FutureResult (org.xnio.FutureResult)7 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 EndpointConfig (javax.websocket.EndpointConfig)6 MessageHandler (javax.websocket.MessageHandler)6 Session (javax.websocket.Session)6 ServerEndpointConfig (javax.websocket.server.ServerEndpointConfig)6 ContinuationWebSocketFrame (io.netty.handler.codec.http.websocketx.ContinuationWebSocketFrame)5 TextWebSocketFrame (io.netty.handler.codec.http.websocketx.TextWebSocketFrame)5