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

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

Introduction

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

Prototype

void setOutput(Handler<Buffer> output);

Source Link

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();/*from  w w w.  j ava2  s  .  com*/
    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();//w ww .  j a va 2  s .co 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;
}