ChannelInboundHandler 中的 writeDataUnsupported (Swift-NIO)
writeDataUnsupported in ChannelInboundHandler (Swift-NIO)
我正在尝试制作一个简单的 echo UDP 服务器,它发回所有以 UTF8 字符串为前缀的传入数据报。
在我试图达到这个目标的过程中,我成功地发回了传入的数据,但是当我尝试用字符串 "You sent: "
作为此数据的前缀时,我收到错误 writeDataUnsupported
这是我的代码:
我制作了一个名为 Echo
的 ChannelInboundHandler
它所做的只是:对于每个传入的数据报,它发送字符串 "You sent: "
,然后是传入数据报的数据。
final class Echo: ChannelInboundHandler {
typealias InboundIn = ByteBuffer
typealias OutboundOut = ByteBuffer
var wroteResponse = false
static let response = "You sent: ".data(using: .utf8)!
func channelRead(ctx: ChannelHandlerContext, data: NIOAny) {
if !wroteResponse {
var buffer = ctx.channel.allocator.buffer(capacity: Echo.response.count)
buffer.write(bytes: Echo.response)
ctx.write(self.wrapOutboundOut(buffer), promise: nil)
wroteResponse = true
}
ctx.write(data, promise: nil)
}
func channelReadComplete(ctx: ChannelHandlerContext) {
ctx.flush()
wroteResponse = false
}
}
然后我制作了一个单线程事件循环组并为其分配了一个数据报引导程序。然后我将 bootstrap 绑定到端口 4065.
let = MultiThreadedEventLoopGroup(numThreads: 1)
let bootstrap = DatagramBootstrap(group: )
.channelOption(ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1)
.channelInitializer { [=12=].pipeline.add(handler: Echo()) }
defer {
try! .syncShutdownGracefully()
}
try bootstrap
.bind(host: "127.0.0.1", port: 4065)
.wait()
.closeFuture
.wait()
为什么我在尝试发送字符串时总是收到此 writeDataUnsupported
:"You sent: "
?
对于 DatagramChannel
,您需要将 ByteBuffer
包装成 AddressEnvelope
。这也意味着您的 ChannelInboundHandler 应该在 AddressedEnvelope<ByteBuffer>
.
上运行
要使 ChannelInboundHandler
在 AddressedEnvelope<ByteBuffer>
上运行,正如 Norman Maurer 所建议的,您可以重写 Echo
,使其看起来更像:
final class Echo: ChannelInboundHandler {
typealias InboundIn = AddressedEnvelope<ByteBuffer>
typealias OutboundOut = AddressedEnvelope<ByteBuffer>
static let response = "You sent: ".data(using: .utf8)!
func channelRead(ctx: ChannelHandlerContext, data: NIOAny) {
var incomingEnvelope = unwrapInboundIn(data)
var buffer = ctx.channel.allocator.buffer(capacity: Echo.response.count + incomingEnvelope.data.readableBytes)
buffer.write(bytes: Echo.response)
buffer.write(buffer: &incomingEnvelope.data)
let envelope = AddressedEnvelope(remoteAddress: incomingEnvelope.remoteAddress, data: buffer)
ctx.write(wrapOutboundOut(envelope), promise: nil)
}
func channelReadComplete(ctx: ChannelHandlerContext) {
ctx.flush()
}
}
我正在尝试制作一个简单的 echo UDP 服务器,它发回所有以 UTF8 字符串为前缀的传入数据报。
在我试图达到这个目标的过程中,我成功地发回了传入的数据,但是当我尝试用字符串 "You sent: "
作为此数据的前缀时,我收到错误 writeDataUnsupported
这是我的代码:
我制作了一个名为 Echo
的 ChannelInboundHandler
它所做的只是:对于每个传入的数据报,它发送字符串 "You sent: "
,然后是传入数据报的数据。
final class Echo: ChannelInboundHandler {
typealias InboundIn = ByteBuffer
typealias OutboundOut = ByteBuffer
var wroteResponse = false
static let response = "You sent: ".data(using: .utf8)!
func channelRead(ctx: ChannelHandlerContext, data: NIOAny) {
if !wroteResponse {
var buffer = ctx.channel.allocator.buffer(capacity: Echo.response.count)
buffer.write(bytes: Echo.response)
ctx.write(self.wrapOutboundOut(buffer), promise: nil)
wroteResponse = true
}
ctx.write(data, promise: nil)
}
func channelReadComplete(ctx: ChannelHandlerContext) {
ctx.flush()
wroteResponse = false
}
}
然后我制作了一个单线程事件循环组并为其分配了一个数据报引导程序。然后我将 bootstrap 绑定到端口 4065.
let = MultiThreadedEventLoopGroup(numThreads: 1)
let bootstrap = DatagramBootstrap(group: )
.channelOption(ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1)
.channelInitializer { [=12=].pipeline.add(handler: Echo()) }
defer {
try! .syncShutdownGracefully()
}
try bootstrap
.bind(host: "127.0.0.1", port: 4065)
.wait()
.closeFuture
.wait()
为什么我在尝试发送字符串时总是收到此 writeDataUnsupported
:"You sent: "
?
对于 DatagramChannel
,您需要将 ByteBuffer
包装成 AddressEnvelope
。这也意味着您的 ChannelInboundHandler 应该在 AddressedEnvelope<ByteBuffer>
.
要使 ChannelInboundHandler
在 AddressedEnvelope<ByteBuffer>
上运行,正如 Norman Maurer 所建议的,您可以重写 Echo
,使其看起来更像:
final class Echo: ChannelInboundHandler {
typealias InboundIn = AddressedEnvelope<ByteBuffer>
typealias OutboundOut = AddressedEnvelope<ByteBuffer>
static let response = "You sent: ".data(using: .utf8)!
func channelRead(ctx: ChannelHandlerContext, data: NIOAny) {
var incomingEnvelope = unwrapInboundIn(data)
var buffer = ctx.channel.allocator.buffer(capacity: Echo.response.count + incomingEnvelope.data.readableBytes)
buffer.write(bytes: Echo.response)
buffer.write(buffer: &incomingEnvelope.data)
let envelope = AddressedEnvelope(remoteAddress: incomingEnvelope.remoteAddress, data: buffer)
ctx.write(wrapOutboundOut(envelope), promise: nil)
}
func channelReadComplete(ctx: ChannelHandlerContext) {
ctx.flush()
}
}