Indeed, thanks Cory. Link to @ktoso (et al)'s task executor proposal.
2 Likes
Thanks for linking it Johannes. Weβre still fleshing out some details before pitching but Iβd be happy to take early comments already. I believe it would be very useful for NIO and similar systems.
3 Likes
I need a second pair of eyes here, please. I don't understand why this code is not working. I can see the connection established and also can print the content of the buffer. But the data is not echo back.
Code
import NIO
import NIOCore
import Foundation
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: System.coreCount)
let serverChannel = try await ServerBootstrap(group: eventLoopGroup)
.bind(
host: "127.0.0.1",
port: 9010
) { childChannel in
// This closure is called for every inbound connection
childChannel.eventLoop.makeCompletedFuture {
print("connection received")
return try NIOAsyncChannel<ByteBuffer, ByteBuffer>(
wrappingChannelSynchronously: childChannel
)
}
}
try await withThrowingDiscardingTaskGroup { group in
try await serverChannel.executeThenClose { serverChannelInbound in
for try await connectionChannel in serverChannelInbound {
group.addTask {
do {
try await connectionChannel.executeThenClose { connectionChannelInbound, connectionChannelOutbound in
for try await inboundData in connectionChannelInbound {
// Let's echo back all inbound data
try await connectionChannelOutbound.write(inboundData)
print(inboundData)
}
}
} catch {
// Handle errors
}
}
}
}
}
Test
# I expect to get hello back but I don't get it
echo "Hello" | nc 127.0.0.1 9010
# But the logs suggest that the connection is being established and the data received in inbound data.
connection received
ByteBuffer { readerIndex: 0, writerIndex: 6, readableBytes: 6, capacity: 2048, storageCapacity: 2048, slice: _ByteBufferSlice { 0..<2048 }, storage: 0x000000012380d000 (2048 bytes) }
I have hard time to see what' s wrong. I wanted to try quickly what I can do with Swift before starting to learn it properly.
Nevermind, I saw the issue. The code works but I am not testing the right way.