From 9bcce9d8cc2986bb39618c78e6127fc50f5696f0 Mon Sep 17 00:00:00 2001 From: Cory Benfield Date: Mon, 3 Feb 2025 14:00:30 +0000 Subject: [PATCH 1/2] Strict concurrency for the chat examples Motivation: Our next suite of examples for strict concurrency are the chat ones. These are very similar to the rest: either no violation at all, or just a need to use the sync operations on pipeline setup. Modifications: - Use sync operations - Lock in strict concurrency checking Result: We continue our march. --- Package.swift | 9 ++++++--- Sources/NIOChatClient/main.swift | 4 +++- Sources/NIOMulticastChat/main.swift | 4 ++-- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/Package.swift b/Package.swift index 863c3f4016..af2504e7c8 100644 --- a/Package.swift +++ b/Package.swift @@ -331,7 +331,8 @@ let package = Package( "NIOCore", "NIOConcurrencyHelpers", ], - exclude: ["README.md"] + exclude: ["README.md"], + swiftSettings: strictConcurrencySettings ), .executableTarget( name: "NIOChatClient", @@ -340,7 +341,8 @@ let package = Package( "NIOCore", "NIOConcurrencyHelpers", ], - exclude: ["README.md"] + exclude: ["README.md"], + swiftSettings: strictConcurrencySettings ), .executableTarget( name: "NIOWebSocketServer", @@ -367,7 +369,8 @@ let package = Package( dependencies: [ "NIOPosix", "NIOCore", - ] + ], + swiftSettings: strictConcurrencySettings ), .executableTarget( name: "NIOUDPEchoServer", diff --git a/Sources/NIOChatClient/main.swift b/Sources/NIOChatClient/main.swift index c41250e39d..273ff993f9 100644 --- a/Sources/NIOChatClient/main.swift +++ b/Sources/NIOChatClient/main.swift @@ -47,7 +47,9 @@ let bootstrap = ClientBootstrap(group: group) // Enable SO_REUSEADDR. .channelOption(.socketOption(.so_reuseaddr), value: 1) .channelInitializer { channel in - channel.pipeline.addHandler(ChatHandler()) + channel.eventLoop.makeCompletedFuture { + try channel.pipeline.syncOperations.addHandler(ChatHandler()) + } } defer { try! group.syncShutdownGracefully() diff --git a/Sources/NIOMulticastChat/main.swift b/Sources/NIOMulticastChat/main.swift index 7d3298ee7e..94316e5ecd 100644 --- a/Sources/NIOMulticastChat/main.swift +++ b/Sources/NIOMulticastChat/main.swift @@ -72,8 +72,8 @@ let group = MultiThreadedEventLoopGroup(numberOfThreads: 1) var datagramBootstrap = DatagramBootstrap(group: group) .channelOption(.socketOption(.so_reuseaddr), value: 1) .channelInitializer { channel in - channel.pipeline.addHandler(ChatMessageEncoder()).flatMap { - channel.pipeline.addHandler(ChatMessageDecoder()) + channel.eventLoop.makeCompletedFuture { + try channel.pipeline.syncOperations.addHandlers(ChatMessageEncoder(), ChatMessageDecoder()) } } From d230884964a1ff5fe228b792e63e1173bcdd0b41 Mon Sep 17 00:00:00 2001 From: Cory Benfield Date: Mon, 3 Feb 2025 15:40:19 +0000 Subject: [PATCH 2/2] Remove byte-by-byte printing --- Sources/NIOChatClient/main.swift | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/Sources/NIOChatClient/main.swift b/Sources/NIOChatClient/main.swift index 273ff993f9..a350bf14bf 100644 --- a/Sources/NIOChatClient/main.swift +++ b/Sources/NIOChatClient/main.swift @@ -18,19 +18,9 @@ private final class ChatHandler: ChannelInboundHandler { public typealias InboundIn = ByteBuffer public typealias OutboundOut = ByteBuffer - private func printByte(_ byte: UInt8) { - #if os(Android) - print(Character(UnicodeScalar(byte)), terminator: "") - #else - fputc(Int32(byte), stdout) - #endif - } - public func channelRead(context: ChannelHandlerContext, data: NIOAny) { - var buffer = Self.unwrapInboundIn(data) - while let byte: UInt8 = buffer.readInteger() { - printByte(byte) - } + let buffer = Self.unwrapInboundIn(data) + print(String(buffer: buffer)) } public func errorCaught(context: ChannelHandlerContext, error: Error) {