Search in sources :

Example 1 with WebSocketFrameImpl

use of io.vertx.core.http.impl.ws.WebSocketFrameImpl in project vert.x by eclipse.

the class WebSocketImplBase method writePartialMessage.

/**
   * Splits the provided buffer into multiple frames (which do not exceed the maximum web socket frame size)
   * and writes them in order to the socket.
   */
private void writePartialMessage(FrameType frameType, Buffer data, int offset) {
    int end = offset + maxWebSocketFrameSize;
    boolean isFinal;
    if (end >= data.length()) {
        end = data.length();
        isFinal = true;
    } else {
        isFinal = false;
    }
    Buffer slice = data.slice(offset, end);
    WebSocketFrame frame;
    if (offset == 0 || !supportsContinuation) {
        frame = new WebSocketFrameImpl(frameType, slice.getByteBuf(), isFinal);
    } else {
        frame = WebSocketFrame.continuationFrame(slice, isFinal);
    }
    writeFrame(frame);
    int newOffset = offset + maxWebSocketFrameSize;
    if (!isFinal) {
        writePartialMessage(frameType, data, newOffset);
    }
}
Also used : Buffer(io.vertx.core.buffer.Buffer) WebSocketFrameImpl(io.vertx.core.http.impl.ws.WebSocketFrameImpl) WebSocketFrame(io.vertx.core.http.WebSocketFrame)

Example 2 with WebSocketFrameImpl

use of io.vertx.core.http.impl.ws.WebSocketFrameImpl in project vert.x by eclipse.

the class WebSocketImplBase method writeBinaryFrameInternal.

private void writeBinaryFrameInternal(Buffer data) {
    ByteBuf buf = data.getByteBuf();
    WebSocketFrame frame = new WebSocketFrameImpl(FrameType.BINARY, buf);
    writeFrame(frame);
}
Also used : WebSocketFrameImpl(io.vertx.core.http.impl.ws.WebSocketFrameImpl) WebSocketFrame(io.vertx.core.http.WebSocketFrame) ByteBuf(io.netty.buffer.ByteBuf)

Example 3 with WebSocketFrameImpl

use of io.vertx.core.http.impl.ws.WebSocketFrameImpl in project vertx-web by vert-x3.

the class SockJSHandlerTest method testCombineTextFrameSockJs.

@Test
public void testCombineTextFrameSockJs() throws InterruptedException {
    String serverPath = "/text-combine-sockjs";
    setupSockJsServer(serverPath, this::echoRequest);
    List<Buffer> receivedMessages = new ArrayList<>();
    WebSocket openedWebSocket = setupSockJsClient(serverPath, receivedMessages);
    Buffer largeMessage = Buffer.buffer("[\"" + TestUtils.randomAlphaString(30) + "\"]");
    WebSocketFrame frame1 = new WebSocketFrameImpl(FrameType.TEXT, largeMessage.slice(0, 10).getByteBuf(), false);
    WebSocketFrame frame2 = WebSocketFrame.continuationFrame(largeMessage.slice(10, 20), false);
    WebSocketFrame frame3 = WebSocketFrame.continuationFrame(largeMessage.slice(20, largeMessage.length()), true);
    log.debug("Client sending " + frame1.textData());
    openedWebSocket.writeFrame(frame1);
    log.debug("Client sending " + frame2.textData());
    openedWebSocket.writeFrame(frame2);
    log.debug("Client sending " + frame3.textData());
    openedWebSocket.writeFrame(frame3);
    await(5, TimeUnit.SECONDS);
    assertEquals("Client should have received 2 messages: the reply and the close.", 2, receivedMessages.size());
    Buffer expectedReply = Buffer.buffer("a" + largeMessage.toString());
    assertEquals("Client reply should have matched request", expectedReply, receivedMessages.get(0));
    assertEquals("Final message should have been a close", SOCKJS_CLOSE_REPLY, receivedMessages.get(1));
}
Also used : Buffer(io.vertx.core.buffer.Buffer) WebSocketFrameImpl(io.vertx.core.http.impl.ws.WebSocketFrameImpl) ArrayList(java.util.ArrayList) WebSocketFrame(io.vertx.core.http.WebSocketFrame) WebSocket(io.vertx.core.http.WebSocket) Test(org.junit.Test)

Example 4 with WebSocketFrameImpl

use of io.vertx.core.http.impl.ws.WebSocketFrameImpl in project vert.x by eclipse.

the class WebSocketImplBase method writePartialMessage.

/**
 * Splits the provided buffer into multiple frames (which do not exceed the maximum web socket frame size)
 * and writes them in order to the socket.
 */
private Future<Void> writePartialMessage(WebSocketFrameType frameType, Buffer data, int offset) {
    int end = offset + maxWebSocketFrameSize;
    boolean isFinal;
    if (end >= data.length()) {
        end = data.length();
        isFinal = true;
    } else {
        isFinal = false;
    }
    Buffer slice = data.slice(offset, end);
    WebSocketFrame frame;
    if (offset == 0 || !supportsContinuation) {
        frame = new WebSocketFrameImpl(frameType, slice.getByteBuf(), isFinal);
    } else {
        frame = WebSocketFrame.continuationFrame(slice, isFinal);
    }
    int newOffset = offset + maxWebSocketFrameSize;
    if (isFinal) {
        return writeFrame(frame);
    } else {
        writeFrame(frame);
        return writePartialMessage(frameType, data, newOffset);
    }
}
Also used : InboundBuffer(io.vertx.core.streams.impl.InboundBuffer) VertxHandler.safeBuffer(io.vertx.core.net.impl.VertxHandler.safeBuffer) Buffer(io.vertx.core.buffer.Buffer) WebSocketFrameImpl(io.vertx.core.http.impl.ws.WebSocketFrameImpl) PongWebSocketFrame(io.netty.handler.codec.http.websocketx.PongWebSocketFrame) CloseWebSocketFrame(io.netty.handler.codec.http.websocketx.CloseWebSocketFrame) ContinuationWebSocketFrame(io.netty.handler.codec.http.websocketx.ContinuationWebSocketFrame) PingWebSocketFrame(io.netty.handler.codec.http.websocketx.PingWebSocketFrame) BinaryWebSocketFrame(io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) WebSocketFrame(io.vertx.core.http.WebSocketFrame)

Example 5 with WebSocketFrameImpl

use of io.vertx.core.http.impl.ws.WebSocketFrameImpl in project vert.x by eclipse.

the class WebSocketTest method testServerWebSocketReceivePingExceedsMaxFrameSize.

@Test
public void testServerWebSocketReceivePingExceedsMaxFrameSize() {
    String pingBody = randomAlphaString(113);
    Integer maxFrameSize = 64;
    Buffer ping1 = Buffer.buffer(Buffer.buffer(pingBody.getBytes()).getBytes(0, maxFrameSize));
    Buffer ping2 = Buffer.buffer(Buffer.buffer(pingBody.getBytes()).getBytes(maxFrameSize, pingBody.length()));
    server = vertx.createHttpServer(new HttpServerOptions().setIdleTimeout(1).setPort(DEFAULT_HTTP_PORT).setHost(HttpTestBase.DEFAULT_HTTP_HOST).setMaxWebSocketFrameSize(maxFrameSize));
    server.webSocketHandler(ws -> {
    }).listen(onSuccess(v -> {
        client = vertx.createHttpClient();
        client.webSocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/", onSuccess(ws -> {
            List<Buffer> pongs = new ArrayList<>();
            ws.pongHandler(pong -> {
                pongs.add(pong);
                if (pongs.size() == 2) {
                    assertEquals(pongs, Arrays.asList(ping1, ping2));
                    testComplete();
                }
            });
            try {
                ws.writeFrame(new WebSocketFrameImpl(WebSocketFrameType.PING, ping1.copy().getByteBuf(), false));
                ws.writeFrame(new WebSocketFrameImpl(WebSocketFrameType.PING, ping2.copy().getByteBuf(), true));
            } catch (Throwable t) {
                fail(t);
            }
        }));
    }));
    await();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Buffer(io.vertx.core.buffer.Buffer) WebSocketInternal(io.vertx.core.http.impl.WebSocketInternal) MultiMap(io.vertx.core.MultiMap) Context(io.vertx.core.Context) Matcher(java.util.regex.Matcher) PlatformDependent(io.netty.util.internal.PlatformDependent) TestUtils(io.vertx.test.core.TestUtils) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DEFAULT_HTTP_HOST(io.vertx.core.http.HttpTestBase.DEFAULT_HTTP_HOST) ReadStream(io.vertx.core.streams.ReadStream) HAProxy(io.vertx.test.proxy.HAProxy) WebSocketFrameImpl(io.vertx.core.http.impl.ws.WebSocketFrameImpl) CheckingSender(io.vertx.test.core.CheckingSender) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) VertxOptions(io.vertx.core.VertxOptions) Trust(io.vertx.test.tls.Trust) BlockingQueue(java.util.concurrent.BlockingQueue) Future(io.vertx.core.Future) StandardCharsets(java.nio.charset.StandardCharsets) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) CountDownLatch(java.util.concurrent.CountDownLatch) Certificate(java.security.cert.Certificate) Buffer(io.vertx.core.buffer.Buffer) ReferenceCountUtil(io.netty.util.ReferenceCountUtil) AbstractVerticle(io.vertx.core.AbstractVerticle) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Cert(io.vertx.test.tls.Cert) Pattern(java.util.regex.Pattern) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NetSocketInternal(io.vertx.core.net.impl.NetSocketInternal) NetSocket(io.vertx.core.net.NetSocket) java.util(java.util) MessageDigest(java.security.MessageDigest) DEFAULT_HTTP_PORT(io.vertx.core.http.HttpTestBase.DEFAULT_HTTP_PORT) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) TestUtils.assertNullPointerException(io.vertx.test.core.TestUtils.assertNullPointerException) VertxTestBase(io.vertx.test.core.VertxTestBase) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) DEFAULT_HTTPS_HOST(io.vertx.core.http.HttpTestBase.DEFAULT_HTTPS_HOST) BiConsumer(java.util.function.BiConsumer) CloseWebSocketFrame(io.netty.handler.codec.http.websocketx.CloseWebSocketFrame) WebSocket13FrameDecoder(io.netty.handler.codec.http.websocketx.WebSocket13FrameDecoder) AsyncResult(io.vertx.core.AsyncResult) SocketAddress(io.vertx.core.net.SocketAddress) DEFAULT_TEST_URI(io.vertx.core.http.HttpTestBase.DEFAULT_TEST_URI) ConcurrentHashSet(io.vertx.core.impl.ConcurrentHashSet) Promise(io.vertx.core.Promise) TestUtils.randomAlphaString(io.vertx.test.core.TestUtils.randomAlphaString) Vertx(io.vertx.core.Vertx) Test(org.junit.Test) IOException(java.io.IOException) Http1xServerConnection(io.vertx.core.http.impl.Http1xServerConnection) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) WebSocket13FrameEncoder(io.netty.handler.codec.http.websocketx.WebSocket13FrameEncoder) DeploymentOptions(io.vertx.core.DeploymentOptions) NetServer(io.vertx.core.net.NetServer) DEFAULT_HTTPS_PORT(io.vertx.core.http.HttpTestBase.DEFAULT_HTTPS_PORT) HttpHeaderNames(io.netty.handler.codec.http.HttpHeaderNames) Handler(io.vertx.core.Handler) TestUtils.assertIllegalStateException(io.vertx.test.core.TestUtils.assertIllegalStateException) Http1xClientConnection(io.vertx.core.http.impl.Http1xClientConnection) WebSocketFrameImpl(io.vertx.core.http.impl.ws.WebSocketFrameImpl) TestUtils.randomAlphaString(io.vertx.test.core.TestUtils.randomAlphaString) Test(org.junit.Test)

Aggregations

WebSocketFrameImpl (io.vertx.core.http.impl.ws.WebSocketFrameImpl)12 Buffer (io.vertx.core.buffer.Buffer)8 CloseWebSocketFrame (io.netty.handler.codec.http.websocketx.CloseWebSocketFrame)6 HttpHeaderNames (io.netty.handler.codec.http.HttpHeaderNames)4 WebSocket13FrameDecoder (io.netty.handler.codec.http.websocketx.WebSocket13FrameDecoder)4 WebSocket13FrameEncoder (io.netty.handler.codec.http.websocketx.WebSocket13FrameEncoder)4 ReferenceCountUtil (io.netty.util.ReferenceCountUtil)4 PlatformDependent (io.netty.util.internal.PlatformDependent)4 AbstractVerticle (io.vertx.core.AbstractVerticle)4 AsyncResult (io.vertx.core.AsyncResult)4 Context (io.vertx.core.Context)4 DeploymentOptions (io.vertx.core.DeploymentOptions)4 Future (io.vertx.core.Future)4 Handler (io.vertx.core.Handler)4 MultiMap (io.vertx.core.MultiMap)4 Promise (io.vertx.core.Promise)4 Vertx (io.vertx.core.Vertx)4 VertxOptions (io.vertx.core.VertxOptions)4 DEFAULT_HTTPS_HOST (io.vertx.core.http.HttpTestBase.DEFAULT_HTTPS_HOST)4 DEFAULT_HTTPS_PORT (io.vertx.core.http.HttpTestBase.DEFAULT_HTTPS_PORT)4