Example usage for io.vertx.core.parsetools RecordParser newFixed

List of usage examples for io.vertx.core.parsetools RecordParser newFixed

Introduction

In this page you can find the example usage for io.vertx.core.parsetools RecordParser newFixed.

Prototype

static RecordParser newFixed(int size, ReadStream<Buffer> stream) 

Source Link

Document

Like #newFixed(int) but wraps the stream .

Usage

From source file:examples.ParseToolsExamples.java

License:Open Source License

public void recordParserExample2() {
    RecordParser.newFixed(4, h -> {
        System.out.println(h.toString());
    });
}

From source file:net.kuujo.copycat.vertx.VertxTcpProtocolClient.java

License:Apache License

@Override
public CompletableFuture<Void> connect() {
    final CompletableFuture<Void> future = new CompletableFuture<>();

    if (vertx == null)
        vertx = protocol.getVertx();//from  w  w w .  ja  va  2s  .  c o m
    if (vertx == null)
        vertx = Vertx.vertx();

    if (client == null) {
        NetClientOptions options = new NetClientOptions().setTcpKeepAlive(true).setTcpNoDelay(true)
                .setSendBufferSize(protocol.getSendBufferSize())
                .setReceiveBufferSize(protocol.getReceiveBufferSize()).setSsl(protocol.isSsl())
                .setTrustAll(protocol.isClientTrustAll()).setUsePooledBuffers(true);
        client = vertx.createNetClient(options);
        client.connect(port, host, result -> {
            if (result.failed()) {
                future.completeExceptionally(result.cause());
            } else {
                socket = result.result();
                RecordParser parser = RecordParser.newFixed(4, null);
                Handler<Buffer> handler = new Handler<Buffer>() {
                    int length = -1;

                    @Override
                    public void handle(Buffer buffer) {
                        if (length == -1) {
                            length = buffer.getInt(0);
                            parser.fixedSizeMode(length + 8);
                        } else {
                            handleResponse(buffer.getLong(0),
                                    buffer.getBuffer(8, length + 8).getByteBuf().nioBuffer());
                            length = -1;
                            parser.fixedSizeMode(4);
                        }
                    }
                };
                parser.setOutput(handler);
                socket.handler(parser);
                future.complete(null);
            }
        });
    } else {
        future.complete(null);
    }
    return future;
}

From source file:net.kuujo.copycat.vertx.VertxTcpProtocolServer.java

License:Apache License

@Override
public CompletableFuture<Void> listen() {
    final CompletableFuture<Void> future = new CompletableFuture<>();

    if (vertx == null)
        vertx = protocol.getVertx();//  ww w . j a  va2s  .c o m
    if (vertx == null)
        vertx = Vertx.vertx();

    if (server == null) {
        NetServerOptions options = new NetServerOptions().setTcpKeepAlive(true).setTcpNoDelay(true)
                .setReuseAddress(true).setAcceptBacklog(protocol.getAcceptBacklog())
                .setSendBufferSize(protocol.getSendBufferSize())
                .setReceiveBufferSize(protocol.getReceiveBufferSize()).setSsl(protocol.isSsl())
                .setClientAuthRequired(protocol.isClientAuthRequired()).setUsePooledBuffers(true);
        server = vertx.createNetServer(options);
        server.connectHandler(socket -> {
            RecordParser parser = RecordParser.newFixed(4, null);
            Handler<Buffer> handler = new Handler<Buffer>() {
                int length = -1;

                @Override
                public void handle(Buffer buffer) {
                    if (length == -1) {
                        length = buffer.getInt(0);
                        parser.fixedSizeMode(length + 8);
                    } else {
                        handleRequest(buffer.getLong(0), socket,
                                buffer.getBuffer(8, length + 8).getByteBuf().nioBuffer());
                        length = -1;
                        parser.fixedSizeMode(4);
                    }
                }
            };
            parser.setOutput(handler);
            socket.handler(parser);
        }).listen(port, host, result -> {
            if (result.failed()) {
                future.completeExceptionally(result.cause());
            } else {
                future.complete(null);
            }
        });
    } else {
        future.complete(null);
    }
    return future;
}

From source file:org.apache.servicecomb.foundation.vertx.server.TcpParser.java

License:Apache License

/**
 * ??parser?//from  www  .j a  va  2 s.  c o  m
 */
protected void reset() {
    parser = RecordParser.newFixed(TCP_HEADER_LENGTH, this::onParse);
    status = ParseStatus.TCP_HEADER;

    parser.handle(Buffer.buffer(0));
}