@Override public void encodeToWire(Buffer buffer, Byte b) { buffer.appendByte(b); }
@Override public void encodeToWire(Buffer buffer, ReplyException body) { buffer.appendByte((byte)body.failureType().toInt()); buffer.appendInt(body.failureCode()); if (body.getMessage() == null) { buffer.appendByte((byte)0); } else { buffer.appendByte((byte)1); byte[] encoded = body.getMessage().getBytes(CharsetUtil.UTF_8); buffer.appendInt(encoded.length); buffer.appendBytes(encoded); } }
@Override public void encodeToWire(Buffer buffer, MyReplyException body) { buffer.appendInt(body.failureCode()); if (body.getMessage() == null) { buffer.appendByte((byte)0); } else { buffer.appendByte((byte)1); byte[] encoded = body.getMessage().getBytes(CharsetUtil.UTF_8); buffer.appendInt(encoded.length); buffer.appendBytes(encoded); } }
@Override public void encodeToWire(Buffer buffer, Boolean b) { buffer.appendByte((byte)(b ? 0 : 1)); }
@Override public void encodeToWire(Buffer buffer, ReplyException body) { buffer.appendByte((byte)body.failureType().toInt()); buffer.appendInt(body.failureCode()); if (body.getMessage() == null) { buffer.appendByte((byte)0); } else { buffer.appendByte((byte)1); byte[] encoded = body.getMessage().getBytes(CharsetUtil.UTF_8); buffer.appendInt(encoded.length); buffer.appendBytes(encoded); } }
@Test /* Test parsing with delimiters */ public void testDelimited() { delimited(Buffer.buffer().appendByte((byte)'\n')); delimited(Buffer.buffer().appendByte((byte) '\r').appendByte((byte) '\n')); delimited(Buffer.buffer(new byte[]{0, 3, 2, 5, 6, 4, 6})); }
@Override public void encodeToWire(Buffer buffer, MyReplyException body) { buffer.appendInt(body.failureCode()); if (body.getMessage() == null) { buffer.appendByte((byte)0); } else { buffer.appendByte((byte)1); byte[] encoded = body.getMessage().getBytes(CharsetUtil.UTF_8); buffer.appendInt(encoded.length); buffer.appendBytes(encoded); } }
public Buffer encodeToWire() { int length = 1024; // TODO make this configurable Buffer buffer = Buffer.buffer(length); buffer.appendInt(0); buffer.appendByte(WIRE_PROTOCOL_VERSION); byte systemCodecID = messageCodec.systemCodecID(); buffer.appendByte(systemCodecID); if (systemCodecID == -1) { // User codec writeString(buffer, messageCodec.name()); } buffer.appendByte(send ? (byte)0 : (byte)1); writeString(buffer, address); if (replyAddress != null) { writeString(buffer, replyAddress); } else { buffer.appendInt(0); } buffer.appendInt(sender.port); writeString(buffer, sender.host); encodeHeaders(buffer); writeBody(buffer); buffer.setInt(0, buffer.length() - 4); return buffer; }
@Override public BatchStream write(Batch batch) { if (batch == null) { if (exceptionHandler != null) { exceptionHandler.handle(new NullPointerException()); } } else { Buffer protocol = Buffer.buffer(); protocol.appendInt(0); protocol.appendByte((byte) batch.getType()); protocol.appendBuffer(batch.getRaw()); protocol.setInt(0, protocol.length() - 4); writeStream.write(protocol); } return this; }
@Test public void testReportProtocolViolationOnServer() { server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)).websocketHandler(ws -> { AtomicReference<Throwable> failure = new AtomicReference<>(); ws.closeHandler(v -> { assertNotNull(failure.get()); testComplete(); }); ws.exceptionHandler(failure::set); }); server.listen(ar -> { assertTrue(ar.succeeded()); handshake(sock -> { // Let's write an invalid frame Buffer buff = Buffer.buffer(); buff.appendByte((byte)(0x8)).appendByte((byte)0); // Violates protocol with V13 (final control frame) sock.write(buff); }); }); await(); }
@Test public void testReportProtocolViolationOnClient() { server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)).requestHandler(req -> { NetSocket sock = getUpgradedNetSocket(req, "/some/path"); // Let's write an invalid frame Buffer buff = Buffer.buffer(); buff.appendByte((byte)(0x8)).appendByte((byte)0); // Violates protocol with V13 (final control frame) sock.write(buff); }); server.listen(ar -> { assertTrue(ar.succeeded()); client.websocketStream(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/some/path", null, WebsocketVersion.V13). handler(ws -> { AtomicReference<Throwable> failure = new AtomicReference<>(); ws.closeHandler(v -> { assertNotNull(failure.get()); testComplete(); }); ws.exceptionHandler(failure::set); }); }); await(); }
@Test public void testAppendByte() throws Exception { int bytesLen = 100; byte[] bytes = TestUtils.randomByteArray(bytesLen); Buffer b = Buffer.buffer(); for (int i = 0; i < bytesLen; i++) { b.appendByte(bytes[i]); } assertEquals(b.length(), bytes.length); assertTrue(TestUtils.byteArraysEqual(bytes, b.getBytes())); for (int i = 0; i < bytesLen; i++) { b.appendByte(bytes[i]); } assertEquals(b.length(), 2 * bytes.length); }
buff.appendByte((byte) 0x01); // Incomplete Text frame buff.appendByte((byte) firstFrame.length()); buff.appendString(firstFrame); sock.write(buff); buff.appendByte((byte) (0x00 | 0x80)); // Complete continuation frame buff.appendByte((byte) continuationFrame.length()); buff.appendString(continuationFrame); sock.write(buff);
@Test // Let's manually handle the websocket handshake and write a frame to the client public void testHandleWSManually() throws Exception { String path = "/some/path"; String message = "here is some text data"; server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)).requestHandler(req -> { NetSocket sock = getUpgradedNetSocket(req, path); // Let's write a Text frame raw Buffer buff = Buffer.buffer(); buff.appendByte((byte)129); // Text frame buff.appendByte((byte)message.length()); buff.appendString(message); sock.write(buff); }); server.listen(ar -> { assertTrue(ar.succeeded()); client.websocketStream(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, path). handler(ws -> { ws.handler(buff -> { assertEquals(message, buff.toString("UTF-8")); testComplete(); }); }); }); await(); }
@Override public void encodeToWire(Buffer buffer, Byte b) { buffer.appendByte(b); }
@Override public void encodeToWire(Buffer buffer, Boolean b) { buffer.appendByte((byte)(b ? 0 : 1)); }
Buffer authReply = Buffer.buffer(new byte[] { 1, (byte) username.length() }); authReply.appendString(username); authReply.appendByte((byte) username.length()); authReply.appendString(username); if (!buffer3.equals(authReply)) {
@Override public void write(int b) throws IOException { if (buffer.length() >= chunkSize - 1) { flush(); } buffer.appendByte((byte) b); }
@Test /* Test parsing with delimiters */ public void testDelimited() { delimited(Buffer.buffer().appendByte((byte)'\n')); delimited(Buffer.buffer().appendByte((byte) '\r').appendByte((byte) '\n')); delimited(Buffer.buffer(new byte[]{0, 3, 2, 5, 6, 4, 6})); }
@Test public void testAppendByte() throws Exception { int bytesLen = 100; byte[] bytes = TestUtils.randomByteArray(bytesLen); Buffer b = Buffer.buffer(); for (int i = 0; i < bytesLen; i++) { b.appendByte(bytes[i]); } assertEquals(b.length(), bytes.length); assertTrue(TestUtils.byteArraysEqual(bytes, b.getBytes())); for (int i = 0; i < bytesLen; i++) { b.appendByte(bytes[i]); } assertEquals(b.length(), 2 * bytes.length); }