Introduction

UDT gives you two choices, byte stream or message, which one to choose? Experience tells us that only primary school students do multiple-choice questions, and we should have them all!

type definition

How are the two types of UDT defined?

Looking at the com.barchart.udt package, you can find that these two types are defined in the TypeUDT enumeration class.

    STREAM(1),
    DATAGRAM(2), 

One is called STREAM, and its code is 1. One is called DATAGRAM, and his code is 2.

We can create different selectorProvider and channelFactory according to two different types. And these two are exactly what you need to build a netty service.

In the tool class NioUdtProvider, netty provides us with six combinations of TypeUDT and KindUDT ChannelFactory, they are:

For Stream: BYTE_ACCEPTOR, BYTE_CONNECTOR, BYTE_RENDEZVOUS.

And for Message: MESSAGE_ACCEPTOR, MESSAGE_CONNECTOR and MESSAGE_RENDEZVOUS.

Similarly, there are two corresponding SelectorProviders, namely:

BYTE_PROVIDER 和 MESSAGE_PROVIDER.

Build a UDT stream server

If you want to build a UDT stream server, you first need to use NioUdtProvider.BYTE_PROVIDER to create a NioEventLoopGroup:

        final NioEventLoopGroup acceptGroup = new NioEventLoopGroup(1, acceptFactory, NioUdtProvider.BYTE_PROVIDER);
        final NioEventLoopGroup connectGroup = new NioEventLoopGroup(1, connectFactory, NioUdtProvider.BYTE_PROVIDER);

Here, we create two eventLoops, acceptLoop and connectLoop.

The next step is to bind the above two groups in ServerBootstrap and specify the channelFactory. Here we need NioUdtProvider.BYTE_ACCEPTOR:

final ServerBootstrap boot = new ServerBootstrap();
            boot.group(acceptGroup, connectGroup)
                    .channelFactory(NioUdtProvider.BYTE_ACCEPTOR)
                    .option(ChannelOption.SO_BACKLOG, 10)
                    .handler(new LoggingHandler(LogLevel.INFO))
                    .childHandler(new ChannelInitializer<UdtChannel>() {
                        @Override
                        public void initChannel(final UdtChannel ch) {
                            ch.pipeline().addLast(
                                    new LoggingHandler(LogLevel.INFO),
                                    new UDTByteEchoServerHandler());
                        }
                    });

It's that simple.

Build UDT message server

The steps to build a UDT message server are similar to those of stream, except that NioUdtProvider.MESSAGE_PROVIDER needs to be used as the selectorProvider:

        final NioEventLoopGroup acceptGroup =
                new NioEventLoopGroup(1, acceptFactory, NioUdtProvider.MESSAGE_PROVIDER);
        final NioEventLoopGroup connectGroup =
                new NioEventLoopGroup(1, connectFactory, NioUdtProvider.MESSAGE_PROVIDER);

Then use NioUdtProvider.MESSAGE_ACCEPTOR as channelFactory when binding ServerBootstrap:

final ServerBootstrap boot = new ServerBootstrap();
            boot.group(acceptGroup, connectGroup)
                    .channelFactory(NioUdtProvider.MESSAGE_ACCEPTOR)
                    .option(ChannelOption.SO_BACKLOG, 10)
                    .handler(new LoggingHandler(LogLevel.INFO))
                    .childHandler(new ChannelInitializer<UdtChannel>() {
                        @Override
                        public void initChannel(final UdtChannel ch)
                                throws Exception {
                            ch.pipeline().addLast(
                                    new LoggingHandler(LogLevel.INFO),
                                    new UDTMsgEchoServerHandler());
                        }
                    });

Equally simple.

Stream and Message handlers

Different UDT types require different handlers.

For Stream, its bottom layer is byte, so our message processing is also carried out in the form of byte, we construct the message in the following way:

private final ByteBuf message;
message = Unpooled.buffer(UDTByteEchoClient.SIZE);
        message.writeBytes("www.flydean.com".getBytes(StandardCharsets.UTF_8));

Then use ctx.writeAndFlush(message) to write it to the channel.

For message, it is actually the encapsulation of ByteBuf in format. There is a corresponding class in netty called UdtMessage:

public final class UdtMessage extends DefaultByteBufHolder

UdtMessage is a ByteBufHolder, so it is actually a ByteBuf wrapper.

We need to encapsulate ByteBuf into UdtMessage:

private final UdtMessage message;
final ByteBuf byteBuf = Unpooled.buffer(UDTMsgEchoClient.SIZE);
        byteBuf.writeBytes("www.flydean.com".getBytes(StandardCharsets.UTF_8));
        message = new UdtMessage(byteBuf);

Then send this UdtMessage to the channel:

ctx.writeAndFlush(message);

In this way, you have learned to use two data types, stream and message, in the UDT protocol.

Summarize

You may think that different data types are so simple to implement. This is all thanks to the excellent packaging and design of netty.

Thanks netty!

Examples of this article can refer to: learn-netty4

This article has been included in http://www.flydean.com/40-netty-udt-support-2/

The most popular interpretation, the most profound dry goods, the most concise tutorials, and many tricks you don't know are waiting for you to discover!

Welcome to pay attention to my official account: "Program those things", understand technology, understand you better!


flydean
890 声望437 粉丝

欢迎访问我的个人网站:www.flydean.com