Skip to content

Commit ae39292

Browse files
committed
Added AsynchronousServerSocketChannel suspending extension functions
Closes #3
1 parent f7e7bbc commit ae39292

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package io.github.agcom.knio2
2+
3+
import java.nio.channels.AsynchronousServerSocketChannel
4+
import java.nio.channels.AsynchronousSocketChannel
5+
import kotlin.coroutines.suspendCoroutine
6+
7+
/**
8+
* Suspending version of [accept][AsynchronousServerSocketChannel.accept] function.
9+
*
10+
* The call is not cancellable (suspends until success or failure), because the underlying channel ([AsynchronousServerSocketChannel]) provides no guarantee for cancellation.
11+
* Note that closing the channel (probably) continues every call with a failure (and that covers most use cases).
12+
*
13+
* However, you can mimic cancellation by ignoring the call (hence, ignoring the results).
14+
*/
15+
public suspend fun AsynchronousServerSocketChannel.acceptAwait(): AsynchronousSocketChannel = suspendCoroutine {
16+
accept(Unit, it.asCompletionHandler())
17+
}

0 commit comments

Comments
 (0)