Skip to content

Commit

Permalink
Added AsynchronousServerSocketChannel suspending extension functions
Browse files Browse the repository at this point in the history
Closes #3
  • Loading branch information
agcom committed Feb 7, 2021
1 parent f7e7bbc commit ae39292
Showing 1 changed file with 17 additions and 0 deletions.
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())
}

0 comments on commit ae39292

Please sign in to comment.