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

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

Introduction

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

Prototype

void fixedSizeMode(int size);

Source Link

Document

Flip the parser into fixed size mode, where the record size is specified by size in bytes.

Usage

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();/*  w  w  w .  j a  v a2s. 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();/*from  w  ww .j  a  v  a 2 s  . 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;
}