Skip to content

Reliable UDP 轻量级可靠性UDP

RockyLOMO edited this page Apr 14, 2022 · 1 revision

3种ACK

  • 无ACK:调用完send方法后就会返回,也没有补发机制
  • 半同步:调用完send后等待remote接收到package后响应ACK
  • 全同步:调用完send后等待remote接收到package后并且callback业务事件无异常则响应ACK

补发

  • local 根据 waitAckTimeout / resend 时间间隔重发package
  • remote 根据package sequenceId 做幂等。
@SneakyThrows
    @Test
    public synchronized void udpRpc() {
        UdpClient c1 = new UdpClient(endpoint0.getPort());
        c1.onReceive = (s, e) -> System.out.println("c1: " + toJsonString(e));
        UdpClient c2 = new UdpClient(endpoint1.getPort());
        AtomicInteger count = new AtomicInteger();
        c2.onReceive = (s, e) -> {
            System.out.println("c2:" + toJsonString(e));
            if (count.incrementAndGet() < 2) {
                throw new InvalidException("error");
            }
        };

        c1.sendAsync(endpoint0, "我是1");
        for (int i = 0; i < 10; i++) {
            int finalI = i;
            Tasks.run(() -> c2.sendAsync(endpoint0, "我是2 + " + finalI));
        }

        c1.sendAsync(endpoint1, "wlz", 15000, true);
        System.out.println("done");
        wait();
    }