forked from Kotlin/kotlinx.coroutines
-
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.
IO: byte channel reader and writer coroutines support
- Loading branch information
Sergey Mashkov
committed
Aug 24, 2017
1 parent
9d5152b
commit 25b2bc5
Showing
3 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
...-coroutines-io/src/main/kotlin/kotlinx/coroutines/experimental/io/ByteChannelCoroutine.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 kotlinx.coroutines.experimental.io | ||
|
||
import kotlinx.coroutines.experimental.* | ||
import kotlin.coroutines.experimental.* | ||
|
||
internal open class ByteChannelCoroutine( | ||
parentContext: CoroutineContext, | ||
open val channel: ByteChannel | ||
) : AbstractCoroutine<Unit>(parentContext, active = true) { | ||
override fun afterCompletion(state: Any?, mode: Int) { | ||
val cause = (state as? JobSupport.CompletedExceptionally)?.cause | ||
if (!channel.close(cause) && cause != null) | ||
handleCoroutineException(context, cause) | ||
|
||
super.afterCompletion(state, mode) | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...ion/kotlinx-coroutines-io/src/main/kotlin/kotlinx/coroutines/experimental/io/ReaderJob.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,34 @@ | ||
package kotlinx.coroutines.experimental.io | ||
|
||
import kotlinx.coroutines.experimental.* | ||
import kotlin.coroutines.experimental.* | ||
|
||
/** | ||
* A coroutine job that is reading from a byte channel | ||
*/ | ||
interface ReaderJob : Job { | ||
/** | ||
* A reference to the channel that this coroutine is reading from | ||
*/ | ||
val channel: ByteWriteChannel | ||
} | ||
|
||
interface ReaderScope : CoroutineScope { | ||
val channel: ByteReadChannel | ||
} | ||
|
||
fun reader(coroutineContext: CoroutineContext, | ||
channel: ByteChannel, | ||
block: suspend ReaderScope.() -> Unit): ReaderJob { | ||
val coroutine = ReaderCoroutine(newCoroutineContext(coroutineContext), channel) | ||
coroutine.initParentJob(coroutineContext[Job]) | ||
block.startCoroutine(coroutine, coroutine) | ||
return coroutine | ||
} | ||
|
||
fun reader(coroutineContext: CoroutineContext, | ||
autoFlush: Boolean = false, | ||
block: suspend ReaderScope.() -> Unit): ReaderJob = reader(coroutineContext, ByteChannel(autoFlush), block) | ||
|
||
private class ReaderCoroutine(context: CoroutineContext, channel: ByteChannel) | ||
: ByteChannelCoroutine(context, channel), ReaderJob, ReaderScope |
35 changes: 35 additions & 0 deletions
35
...ion/kotlinx-coroutines-io/src/main/kotlin/kotlinx/coroutines/experimental/io/WriterJob.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,35 @@ | ||
package kotlinx.coroutines.experimental.io | ||
|
||
import kotlinx.coroutines.experimental.* | ||
import kotlin.coroutines.experimental.* | ||
|
||
/** | ||
* A coroutine job that is writing to a byte channel | ||
*/ | ||
interface WriterJob : Job { | ||
/** | ||
* A reference to the channel that this coroutine is writing to | ||
*/ | ||
val channel: ByteReadChannel | ||
} | ||
|
||
interface WriterScope : CoroutineScope { | ||
val channel: ByteWriteChannel | ||
} | ||
|
||
fun writer(coroutineContext: CoroutineContext, | ||
channel: ByteChannel, | ||
block: suspend CoroutineScope.() -> Unit): WriterJob { | ||
val coroutine = WriterCoroutine(newCoroutineContext(coroutineContext), channel) | ||
coroutine.initParentJob(coroutineContext[Job]) | ||
block.startCoroutine(coroutine, coroutine) | ||
return coroutine | ||
} | ||
|
||
fun writer(coroutineContext: CoroutineContext, | ||
autoFlush: Boolean = false, | ||
block: suspend CoroutineScope.() -> Unit): WriterJob = writer(coroutineContext, ByteChannel(autoFlush), block) | ||
|
||
private class WriterCoroutine(ctx: CoroutineContext, channel: ByteChannel) | ||
: ByteChannelCoroutine(ctx, channel), WriterScope, WriterJob | ||
|