如何让Vertx Reactive RecordParser发出没有长度的消息?

时间:2018-01-17 05:07:59

标签: java rx-java2 vert.x

我有一个反应式vertx TCP服务器,它使用RecordParser来处理长度为2字节的消息。 RecordParser完成了解长度部分和消息部分的工作。

问题1
recordparser正确解释recordparser中的数据和sysout。但是当我添加以下行时,sysouts不再打印。它可能与它在不同的线程上执行有关吗?我不确定。

frameParser.toFlowable()
         .subscribe(onNext -> System.out.println(onNext));  

问题2
一旦添加了问题1中的行,onNext就会打印以块的形式收到的所有数据。据我所知,它收到的转发数据 输出

  

5bd34970-da5a-44e1-b167-0d32d2e70bb2
  的onSuccess
  ? [它是一个不可打印的字节4]
  12个
  34

我的最终目标是只获取没有长度标题的消息。任何指针都将受到赞赏。

代码

// data that is sent by client byte[] b = { 0, 4, 0x31, 0x32, 0x33, 0x34 };

import io.reactivex.Flowable;
import io.reactivex.Single;
import io.vertx.core.net.NetServerOptions;
import io.vertx.reactivex.RxHelper;
import io.vertx.reactivex.core.AbstractVerticle;
import io.vertx.reactivex.core.Vertx;
import io.vertx.reactivex.core.buffer.Buffer;
import io.vertx.reactivex.core.net.NetServer;
import io.vertx.reactivex.core.net.NetSocket;
import io.vertx.reactivex.core.parsetools.RecordParser;

public class TCPRxServer extends AbstractVerticle {

    Single<NetSocket>        observableNetSocket;
    private FrameToken       expectedToken = FrameToken.SIZE;
    RecordParser             parser, frameParser = null;
    static Flowable<Buffer>  y;
    static Single<NetServer> observable;

    @Override
    public void start() throws Exception {
        NetServerOptions opt = new NetServerOptions();
        opt.setIdleTimeout(500); // large to assist in debugging for now
        observable = vertx.createNetServer(opt)
                          .connectHandler(socket -> {
                              frameParser = get2byteFrameParser();
//                              frameParser.toFlowable()
//                                         .subscribe(onNext -> System.out.println(onNext));

                              socket.handler(frameParser);

                          })
                          .rxListen(8888);

        observable.subscribe(onSuccess -> System.out.println("OnSuccess"), onError -> onError.printStackTrace());
    }

    protected RecordParser get2byteFrameParser() {
        return RecordParser.newFixed(2, buffer -> {

            switch (expectedToken) {

                case SIZE:

                    byte[] b = buffer.getString(0, 2)
                                     .getBytes();
                    int frameSize = (((b[0]) & 0xFF) << 8) | ((b[1]) & 0xFF);
                    System.out.println("size: " + frameSize);
                    frameParser.fixedSizeMode(frameSize);
                    expectedToken = FrameToken.PAYLOAD;
                    break;

                case PAYLOAD:
                    System.out.println("Payload: |" + buffer + "|");
                    frameParser.fixedSizeMode(2);
                    expectedToken = FrameToken.SIZE;
                vertx.eventBus() // this solves the sending messsge in the non rx api. When I turn the recordparser to a flowable I still cant get the parser logic to parse.
                     .rxSend("MESSAGE", buffer.toString())
                     .subscribe();

                    break;
            }

        });
    }

    public static void main(String[] args) throws InterruptedException {

        TCPRxServer server = new TCPRxServer();
        Single<String> deployment = io.vertx.reactivex.core.RxHelper.deployVerticle(Vertx.vertx(), server);

        deployment.subscribe(id -> {
            // Deployed
            System.out.println(id);
            //
        }, err -> {
            // Could not deploy
            err.printStackTrace();
        });

    }

}  

P.S。
我根据How to use vert.x-rx to create reactive client-server TCP communication 的SO响应定制了LengthPrefixedStream,但行为没有差异。

    observable = vertx.createNetServer(opt)
                      .connectHandler(socket -> {
                          new LengthPrefixedStream(socket).toFlowable()
                                                          .subscribe(onNext -> System.out.println(onNext));
                      })
                      .rxListen(8888);

3 个答案:

答案 0 :(得分:2)

来自vertx google用户组@tsegismont提供了适用于我的代码。

<强> LengthPrefixedStream.java

import io.vertx.core.Handler;
import io.vertx.reactivex.core.buffer.Buffer;
import io.vertx.reactivex.core.parsetools.RecordParser;
import io.vertx.reactivex.core.streams.ReadStream;

public class LengthPrefixedStream implements io.vertx.core.streams.ReadStream<Buffer> {
    private final RecordParser recordParser;

    private FrameToken         expectedToken = FrameToken.SIZE;

    public LengthPrefixedStream(ReadStream<Buffer> stream) {
        recordParser = RecordParser.newFixed(2, stream);
    }

    @Override
    public io.vertx.core.streams.ReadStream<Buffer> exceptionHandler(Handler<Throwable> handler) {
        recordParser.exceptionHandler(handler);
        return this;
    }

    @Override
    public io.vertx.core.streams.ReadStream<Buffer> handler(Handler<Buffer> handler) {

        if (handler == null) {
            recordParser.handler(null);
            return this;
        }
        recordParser.handler(buffer -> {
            switch (expectedToken) {

                case SIZE:

                    byte[] b = buffer.getString(0, 2)
                                     .getBytes();
                    int frameSize = (((b[0]) & 0xFF) << 8) | ((b[1]) & 0xFF);
                    System.out.println("size: " + frameSize);
                    recordParser.fixedSizeMode(frameSize);
                    expectedToken = FrameToken.PAYLOAD;
                    break;

                case PAYLOAD:
                    System.out.println("Payload: |" + buffer + "|");
                    recordParser.fixedSizeMode(2);
                    expectedToken = FrameToken.SIZE;
                    handler.handle(buffer);

                    break;
            }
        });
        return this;
    }

    @Override
    public io.vertx.core.streams.ReadStream<Buffer> pause() {
        recordParser.pause();
        return this;
    }

    @Override
    public io.vertx.core.streams.ReadStream<Buffer> resume() {
        recordParser.resume();
        return this;
    }

    @Override
    public io.vertx.core.streams.ReadStream<Buffer> endHandler(Handler<Void> endHandler) {
        recordParser.endHandler(endHandler);
        return this;
    }
}

<强> TCPRxServer.java

import io.reactivex.Single;
import io.reactivex.disposables.Disposable;
import io.vertx.core.VertxOptions;
import io.vertx.core.net.NetServerOptions;
import io.vertx.reactivex.FlowableHelper;
import io.vertx.reactivex.core.AbstractVerticle;
import io.vertx.reactivex.core.RxHelper;
import io.vertx.reactivex.core.Vertx;
import io.vertx.reactivex.core.net.NetServer;
import io.vertx.reactivex.core.parsetools.RecordParser;

public class TCPRxServer extends AbstractVerticle {

    RecordParser       frameParser = null;
    private NetServer  server;
    private Disposable disposable;

    @Override
    public void start() throws Exception {

        NetServerOptions opt = new NetServerOptions();
        opt.setIdleTimeout(500); // large to assist in debugging for now
        server = vertx.createNetServer(opt);// Create a TCP server

        Single<NetServer> rxSingleServer = server.connectHandler(socket -> {
            LengthPrefixedStream frameParser = new LengthPrefixedStream(socket);
            FlowableHelper.toFlowable(frameParser)
                          .subscribe(onNext -> System.out.println(onNext));

        })
                                                 .rxListen(8888, "localhost");
        disposable = rxSingleServer.subscribe(s -> System.out.println("Server is listening"),
                                              onError -> System.out.println(onError));

    }

    @Override
    public void stop() throws Exception {

        disposable.dispose();
        server.close(completionHandler -> {
            if (completionHandler.succeeded()) {
                System.out.println("TCPRxServer stopped");
            }
            else {
                System.out.println(completionHandler.cause());
            }
        });
    }

    public static void main(String[] args) {

        VertxOptions ops = new VertxOptions();

        // We are using clustered as we have verticles in different VMs and we
        // want to make sure when something is placed on the eventbus, a
        // verticle on a different VM receives it.
        ops.setClustered(true);
        Vertx.clusteredVertx(ops, result -> {
            if (result.succeeded()) {

                Single<String> deployment = RxHelper.deployVerticle(result.result(), new TCPRxServer());

                deployment.subscribe(id -> {
                    System.out.println(id);
                    // Deployed
                }, err -> {
                    err.printStackTrace();
                });

            }
        });

    }

}

答案 1 :(得分:1)

我无法运行您粘贴的代码。如果您的目标只是获取没有长度标题的消息,请执行以下操作:(我不使用RecordParser)

import io.reactivex.Flowable;
import io.reactivex.Single;
import io.vertx.core.net.NetServerOptions;
import io.vertx.reactivex.core.AbstractVerticle;
import io.vertx.reactivex.core.Vertx;
import io.vertx.reactivex.core.buffer.Buffer;
import io.vertx.reactivex.core.net.NetClient;
import io.vertx.reactivex.core.net.NetServer;
import io.vertx.reactivex.core.net.NetSocket;
import io.vertx.reactivex.core.parsetools.RecordParser;

public class NewTCPRxServer extends AbstractVerticle {

    Single<NetSocket>        observableNetSocket;
    RecordParser             parser, frameParser = null;
    static Flowable<Buffer>  y;
    static Single<NetServer> observable;

    @Override
    public void start() throws Exception {
        NetServerOptions opt = new NetServerOptions();
        opt.setIdleTimeout(500); // large to assist in debugging for now
        observable = vertx.createNetServer(opt)
                          .connectHandler(socket -> {
                    socket.handler(buffer -> onNewframe(buffer));
                          })
                          .rxListen(8888);

        observable.subscribe(onSuccess -> System.out.println("OnSuccess"), onError -> onError.printStackTrace());
    }

    private void onNewframe(Buffer buffer) {
        int size = buffer.getInt(0);
        Buffer content = buffer.getBuffer(4, size + 4);
        vertx.eventBus().rxSend("MESSAGE", content.toString()).subscribe();
    }

    public static void main(String[] args) {
        NewTCPRxServer server = new NewTCPRxServer();
        Vertx vertxx = Vertx.vertx();
        Single<String> deployment = io.vertx.reactivex.core.RxHelper.deployVerticle(vertxx, server);

        deployment.subscribe(id -> {
            // Deployed
            System.out.println(id);
            //
        }, err -> {
            // Could not deploy
            err.printStackTrace();
        });

        // a test client
        io.vertx.reactivex.core.RxHelper.deployVerticle(vertxx, new TestTCPClient()).subscribe();
    }

    static class TestTCPClient extends AbstractVerticle {

        @Override
        public void start() throws Exception {

            vertx.eventBus().consumer("MESSAGE", message -> {
                Object body = message.body();
                // we receive 'hello world' here means the TCP server processed
                // it successfully!
                System.out.println("[Message] - " + body);
            });

            NetClient netclient = vertx.createNetClient();
            netclient.connect(8888, "localhost", result -> {

                if (result.failed()) {
                    System.err.println("Client unable to connect");
                    return;
                }
                NetSocket socket = result.result();

                // sending "hello world" every second
                vertx.periodicStream(1000).handler(tick -> {
                    String msg = "Hello world";
                    Buffer frame = Buffer.buffer(msg.length() + 4);
                    frame.appendInt(msg.length());
                    frame.appendString(msg);
                    socket.write(frame);
                });

            });
        }

    }

}

我在最后部署了同一个vertx实例的测试客户端。

答案 2 :(得分:0)

我发现使用RecordParser.fixedSizeMode()方法将传入的数据包聚合到一条消息时相当复杂。

对于寻求更简单解决方案的人,我建议使用 RecordParser.newDelimited

如果您可以操作发送到TCP接收器的数据,那么您可以为每个发送的消息添加一个唯一的分隔符,而不是将消息长度的前几个字节前缀添加到每个消息的末尾。

代码示例 :(我没有使用RxJava语法)

<强>发送

byte[] serialized = SerializationUtils.serialize(o);
senderSocket.write(Buffer.buffer(serialized).appendString(MSG_DELIMITER));
System.out.println("I sent some bytes: " + serialized.length);

<强>读取

protected static String MSG_DELIMITER = "_I_Am_so0o_UNIQUEQE_";
protected Vertx vertx;
protected final RecordParser parser = RecordParser.newDelimited(MSG_DELIMITER, new Output());

public static main(String[] args) {
    this.vertx = Vertx.vertx();
    this.vertx.deployVerticle(new Receiver());
}

public class Receiver extends AbstractVerticle {
    @Override
    public void start() {

        NetServer receiver = this.vertx.createNetServer();

        receiver.connectHandler(socket ->

                socket.handler(parser::handle));

        receiver.listen(port, host, res -> {
            if (res.succeeded()) {
                System.out.println("Receiver is now listening!");
            } else {
                System.out.println("Failed to bind!");
            }
        });
    }

    @Override
    public void stop() {
        System.out.println("Receiver stopped");
    }
}

public class Output implements Handler<Buffer> {

    @Override
    public void handle(Buffer event) {
        try {
            E deserialized = (E) SerializationUtils.deserialize(event.getBytes());
            queue.put(deserialized);
            System.out.println("I received some bytes: " + event.length());

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}