-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added AsynchronousServerSocketChannel suspending extension functions
Closes #3
- Loading branch information
Showing
2 changed files
with
21 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
lib/src/main/kotlin/io/github/agcom/knio2/AsyncServerSocketChannel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package io.github.agcom.knio2 | ||
|
||
import java.nio.channels.AsynchronousServerSocketChannel | ||
import java.nio.channels.AsynchronousSocketChannel | ||
import kotlin.coroutines.suspendCoroutine | ||
|
||
/** | ||
* Suspending version of [accept][AsynchronousServerSocketChannel.accept] function. | ||
* | ||
* The call is not cancellable (suspends until success or failure), because the underlying channel ([AsynchronousServerSocketChannel]) provides no guarantee for cancellation. | ||
* Note that closing the channel (probably) continues every call with a failure (and that covers most use cases). | ||
* | ||
* However, you can mimic cancellation by ignoring the call (hence, ignoring the results). | ||
*/ | ||
public suspend fun AsynchronousServerSocketChannel.acceptAwait(): AsynchronousSocketChannel = suspendCoroutine { | ||
accept(Unit, it.asCompletionHandler()) | ||
} |