当前位置: > Linux新闻 >

smart-socket v1.4.9 发布,重构 UDP 通信

时间:2020-03-20 13:30来源:linux.it.net.cn 作者:IT

smart-socket 是一款国产开源的 Java AIO 框架,追求代码量、性能、稳定性、接口设计各方面都达到极致。如果 smart-socket 对您有一丝帮助,请 Star 一下我们的项目并持续关注;如果您对 smart-socket 并不满意,那请多一些耐心,smart-socket 一直在努力变得更好。

更新内容

  1. bug修复:修复内存池特定场景下数组越界问题,影响范围:1.4.0+。
  2. bug修复:修复WriteBuffer#write,WriteBuffer#writeByte并发条件下偶发异常问题,影响范围:1.4.0+。
  3. 更新:重构UDP代码。

smart-socket自 v1.4.4 版本开始提供UDP的支持,不过代码设计的比较复杂,有违 smart-socket 一贯强调极简、易用、高性能的原则。故此次大刀阔斧进行了一番重构,其中参考了部分 Java AIO 的内核设计思路,仅用 5 个Java 源文件便达到了预期的效果。

原先熟悉 smart-socket TCP 开发的用户可以比较快的上手 UDP 开发,都是基于 Protocol、MessageProcessor 两个接口进行开发。同时也意味着如果你是采用 smart-socket 进行通信开发,只需少量的调整便可在 TCP 和 UDP 之间进行切换。

Maven

<dependency>
    <groupId>org.smartboot.socket</groupId>
    <artifactId>aio-pro</artifactId>
    <version>1.4.9</version>
</dependency>

示例demo

public class UdpDemo {
    public static void main(String[] args) throws IOException {
        AbstractMessageProcessor<String> processor = new AbstractMessageProcessor<String>() {
            @Override
            public void process0(AioSession channel, String msg) {
                try {
                    InetSocketAddress remoteAddress = channel.getRemoteAddress();
                    if (remoteAddress.getPort() == 9999) {
                        System.out.println(channel + " receive response:" + msg);
                    } else {
                        System.out.println("server receive request:" + msg);
                        WriteBuffer buffer = channel.writeBuffer();
                        byte[] bytes = msg.getBytes();
                        buffer.writeInt(bytes.length);
                        buffer.write(bytes);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void stateEvent0(AioSession session, StateMachineEnum stateMachineEnum, Throwable throwable) {
                if (throwable != null) {
                    throwable.printStackTrace();
                }
            }
        };
        //服务端
        final UdpBootstrap<String> bootstrap = new UdpBootstrap<String>(new StringProtocol(), processor);
        bootstrap.setThreadNum(Runtime.getRuntime().availableProcessors());
        bootstrap.setReadBufferSize(1024);
        bootstrap.open(9999);
        System.out.println("启动成功");

        //客户端
        int i = 10;
        final SocketAddress remote = new InetSocketAddress("localhost", 9999);
        while (i-- > 0) {
            new Thread(() -> {
                try {
                    int count = 100;
                    UdpChannel<String> channel = bootstrap.open();
                    AioSession<String> aioSession = channel.connect(remote);
                    WriteBuffer writeBuffer = aioSession.writeBuffer();
                    while (count-- > 0) {
                        byte[] msg = ("HelloWorld" + count).getBytes();
                        writeBuffer.writeInt(msg.length);
                        writeBuffer.write(msg);
                        writeBuffer.flush();
                    }
                    System.out.println("发送完毕");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }).start();
        }
    }
}

当然,还是要负责任的提醒各位,现阶段不保证 UDP 功能无 bug,但一旦发现 bug 保证会在第一时间解决。

最后

无论你是从事物联网还是互联网,需要采用TCP或者UDP进行通信框架,smart-socket 都会是比较不错的选择。另外,一款基于 smart-socket 开发的首款商业化产品即将面世,请关注 smart-socket gitee仓库。

 


(责任编辑:IT)
------分隔线----------------------------
栏目列表
推荐内容