gpt4 book ai didi

netty - AbstractBootstrap#handler vs. ServerBootstrap#childHandler for ServerBootstrap?

转载 作者:行者123 更新时间:2023-12-01 10:36:45 26 4
gpt4 key购买 nike

我要复活this一和Manish Maheshwari的回答,尤其是。哪里有文件证明

The handler, which is defined in the AbstractBootstrap is used when writing Netty based clients.





When writing Netty based servers [use] childHandler as defined in the ServerBootstrap.



换句话说,差异在哪里

val b = new ServerBootstrap()
b.group(boss, wrkr)
.channel(classOf[NioServerSocketChannel])
.handler(new LoggingHandler(LogLevel.INFO))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.childHandler(new ChannelInitializer[SocketChannel]() {
override def initChannel(ch: SocketChannel): Unit =
ch.pipeline()
.addLast(new LoggingHandler(LogLevel.INFO))
.addLast(new StringDecoder())
.addLast(new StringEncoder())
})



val b = new ServerBootstrap()
b.group(boss, wrkr)
.channel(classOf[NioServerSocketChannel])
.childHandler(new ChannelInitializer[SocketChannel]() {
override def initChannel(ch: SocketChannel): Unit =
ch.pipeline()
.addLast(new LoggingHandler(LogLevel.INFO))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.addLast(new StringDecoder())
.addLast(new StringEncoder())
})

最佳答案

handler 注册一个 channel 处理程序家长 channel childHandler 注册一个 channel 处理程序 child channel
看似相同的处理程序在不同的 channel 上监听事件并具有不同的行为。

LoggingHandler 的情况下,第一个记录在父 channel 中发生的事件,包括端口绑定(bind)和接受新连接。所以它产生如下日志(简化和注释):

// parent channel registered
INFO - [id: 0xb94a8e7c] REGISTERED
// parent channel binds to localhost:8009
INFO - [id: 0xb94a8e7c] BIND: 0.0.0.0/0.0.0.0:8009
// parent channel active
INFO - [id: 0xb94a8e7c, L:/0:0:0:0:0:0:0:0:8009] ACTIVE
// parent channel accepts new connection, child channel with id 0xe507ce8f created
INFO - [id: 0xb94a8e7c, L:/0:0:0:0:0:0:0:0:8009] RECEIVED: [id: 0xe507ce8f, L:/0:0:0:0:0:0:0:1:8009 - R:/0:0:0:0:0:0:0:1:54398]



假设子 channel 读取请求中的数据,您的第二个记录器将产生如下内容:

// child channel registered
INFO - [id: 0x15fee362, L:/0:0:0:0:0:0:0:1:8009 - R:/0:0:0:0:0:0:0:1:55459] REGISTERED
// child channel active
INFO - [id: 0x15fee362, L:/0:0:0:0:0:0:0:1:8009 - R:/0:0:0:0:0:0:0:1:55459] ACTIVE
// child channel received 7 bytes of data, "hello\r\n"
INFO - [id: 0x15fee362, L:/0:0:0:0:0:0:0:1:8009 - R:/0:0:0:0:0:0:0:1:55459] RECEIVED: 7B
// logs the hex dump of the received data

+-------------------------------------------------+
| 0 1 2 3 4 5 6 7 8 9 a b c d e f |
+--------+-------------------------------------------------+----------------+
|00000000| 68 65 6c 6c 6f 0d 0a |hello.. |
+--------+-------------------------------------------------+----------------+

关于netty - AbstractBootstrap#handler vs. ServerBootstrap#childHandler for ServerBootstrap?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34069487/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com