Skip to content

Commit 8f19d80

Browse files
Lukasarnro
andauthored
Strict concurrency for the chat examples (apple#3104)
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. Co-authored-by: Rick Newton-Rogers <rnro@apple.com>
1 parent 0bc48eb commit 8f19d80

File tree

3 files changed

+13
-18
lines changed

3 files changed

+13
-18
lines changed

Package.swift

+6-3
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,8 @@ let package = Package(
334334
"NIOCore",
335335
"NIOConcurrencyHelpers",
336336
],
337-
exclude: ["README.md"]
337+
exclude: ["README.md"],
338+
swiftSettings: strictConcurrencySettings
338339
),
339340
.executableTarget(
340341
name: "NIOChatClient",
@@ -343,7 +344,8 @@ let package = Package(
343344
"NIOCore",
344345
"NIOConcurrencyHelpers",
345346
],
346-
exclude: ["README.md"]
347+
exclude: ["README.md"],
348+
swiftSettings: strictConcurrencySettings
347349
),
348350
.executableTarget(
349351
name: "NIOWebSocketServer",
@@ -370,7 +372,8 @@ let package = Package(
370372
dependencies: [
371373
"NIOPosix",
372374
"NIOCore",
373-
]
375+
],
376+
swiftSettings: strictConcurrencySettings
374377
),
375378
.executableTarget(
376379
name: "NIOUDPEchoServer",

Sources/NIOChatClient/main.swift

+5-13
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,9 @@ private final class ChatHandler: ChannelInboundHandler {
1818
public typealias InboundIn = ByteBuffer
1919
public typealias OutboundOut = ByteBuffer
2020

21-
private func printByte(_ byte: UInt8) {
22-
#if os(Android)
23-
print(Character(UnicodeScalar(byte)), terminator: "")
24-
#else
25-
fputc(Int32(byte), stdout)
26-
#endif
27-
}
28-
2921
public func channelRead(context: ChannelHandlerContext, data: NIOAny) {
30-
var buffer = Self.unwrapInboundIn(data)
31-
while let byte: UInt8 = buffer.readInteger() {
32-
printByte(byte)
33-
}
22+
let buffer = Self.unwrapInboundIn(data)
23+
print(String(buffer: buffer))
3424
}
3525

3626
public func errorCaught(context: ChannelHandlerContext, error: Error) {
@@ -47,7 +37,9 @@ let bootstrap = ClientBootstrap(group: group)
4737
// Enable SO_REUSEADDR.
4838
.channelOption(.socketOption(.so_reuseaddr), value: 1)
4939
.channelInitializer { channel in
50-
channel.pipeline.addHandler(ChatHandler())
40+
channel.eventLoop.makeCompletedFuture {
41+
try channel.pipeline.syncOperations.addHandler(ChatHandler())
42+
}
5143
}
5244
defer {
5345
try! group.syncShutdownGracefully()

Sources/NIOMulticastChat/main.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
7272
var datagramBootstrap = DatagramBootstrap(group: group)
7373
.channelOption(.socketOption(.so_reuseaddr), value: 1)
7474
.channelInitializer { channel in
75-
channel.pipeline.addHandler(ChatMessageEncoder()).flatMap {
76-
channel.pipeline.addHandler(ChatMessageDecoder())
75+
channel.eventLoop.makeCompletedFuture {
76+
try channel.pipeline.syncOperations.addHandlers(ChatMessageEncoder(), ChatMessageDecoder())
7777
}
7878
}
7979

0 commit comments

Comments
 (0)