您将如何扩展频道?

发布于 2024-12-27 04:31:18 字数 90 浏览 0 评论 0原文

我需要一些有关 Netty 的帮助。我想知道如何扩展 Channel 接口并向其中添加我自己的方法并使 Netty 使用它(或强制转换为它)?

马特。

I need a little help with Netty. I was wondering how would you extend the Channel interface and add my own methods to it and make Netty use it (or cast to it)?

Matt.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

热血少△年 2025-01-03 04:31:18

答案对于 netty 4.1.25.Final 来说是实际的。

首先扩展您的通道类型,

public class ExtendedNioChannel extends NioSocketChannel implements Channel {
    private volatile boolean handshakeComplete;
    // more custom fields and methods
}

如果您想将其用于客户端引导,只需将该类传递给它即可。

 Bootstrap b = new Bootstrap();
                b.group(workerGroup);
                b.channel(ExtendedNioChannel.class);

对于服务器引导程序,您需要覆盖 NioServerSocketChannel 并将 NioServerSocketChannel.class 传递给 ServerBootstrap。

public class ExtendedNioServerChannel extends NioServerSocketChannel {
    private static final InternalLogger logger = InternalLoggerFactory.getInstance(NioServerSocketChannel.class);

    @Override
    protected int doReadMessages(List<Object> buf) throws Exception {
        SocketChannel ch = SocketUtils.accept(javaChannel());

        try {
            if (ch != null) {
                buf.add(new ExtendedNioChannel(this, ch));
                return 1;
            }
        } catch (Throwable t) {
            logger.warn("Failed to create a new channel from an accepted socket.", t);

            try {
                ch.close();
            } catch (Throwable t2) {
                logger.warn("Failed to close a socket.", t2);
            }
        }

        return 0;
    }
}

 ServerBootstrap b = new ServerBootstrap();
            b.group(acceptGroup, workerGroup)
                    .channel(BtcNioServerChannel.class)

The answer is actual for netty 4.1.25.Final.

First extend your channel type

public class ExtendedNioChannel extends NioSocketChannel implements Channel {
    private volatile boolean handshakeComplete;
    // more custom fields and methods
}

If you want to use it for client bootstap just pass that class to it.

 Bootstrap b = new Bootstrap();
                b.group(workerGroup);
                b.channel(ExtendedNioChannel.class);

For server bootstrap you need to override NioServerSocketChannel and pass NioServerSocketChannel.class to ServerBootstrap.

public class ExtendedNioServerChannel extends NioServerSocketChannel {
    private static final InternalLogger logger = InternalLoggerFactory.getInstance(NioServerSocketChannel.class);

    @Override
    protected int doReadMessages(List<Object> buf) throws Exception {
        SocketChannel ch = SocketUtils.accept(javaChannel());

        try {
            if (ch != null) {
                buf.add(new ExtendedNioChannel(this, ch));
                return 1;
            }
        } catch (Throwable t) {
            logger.warn("Failed to create a new channel from an accepted socket.", t);

            try {
                ch.close();
            } catch (Throwable t2) {
                logger.warn("Failed to close a socket.", t2);
            }
        }

        return 0;
    }
}

 ServerBootstrap b = new ServerBootstrap();
            b.group(acceptGroup, workerGroup)
                    .channel(BtcNioServerChannel.class)
粉红×色少女 2025-01-03 04:31:18

我想更好的解决方案是将 Channel“包装”到您的类中,将该类存储在 ChannelLocal 或 ChannelHandlerContext 中。然后只需检索您的实现并使用它。这将允许您在 NIO 和 OIO 之间切换,而无需担心实现。

我在 niosmtp 中做了类似的事情:
https://github.com/normanmaurer/niosmtp/blob/master/src/main/java/me/normanmaurer/niosmtp/transport/netty/NettySMTPClientSession.java

这也可以帮助您解耦你的代码。

要提供您自己的通道,您需要破解套接字实现。但我认为你应该尽可能避免它。

I guess a better solution would be to "wrap" the Channel into your class store the class in a ChannelLocal or in the ChannelHandlerContext. Then jsut retrieve your implementation and use it. This will allow you to switch between NIO and OIO without the need to worry about the implementation.

I'm doing something similar in niosmtp:
https://github.com/normanmaurer/niosmtp/blob/master/src/main/java/me/normanmaurer/niosmtp/transport/netty/NettySMTPClientSession.java

This also helps you to decouble your code.

To provide your own channel you would need to hack the socket implementation. But as I think you should avoid it whenever possible.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文