Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
83 additions
and
81 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,102 +1,39 @@ | ||
package com.koushikdutta.scratch | ||
|
||
import com.koushikdutta.scratch.async.startSafeCoroutine | ||
import com.koushikdutta.scratch.buffers.Buffers | ||
import com.koushikdutta.scratch.buffers.ByteBufferList | ||
import com.koushikdutta.scratch.buffers.ReadableBuffers | ||
import com.koushikdutta.scratch.buffers.WritableBuffers | ||
import kotlin.coroutines.Continuation | ||
import kotlin.coroutines.resume | ||
import kotlin.coroutines.suspendCoroutine | ||
|
||
/** | ||
* Manages filtering and buffering of data through a pipe. | ||
*/ | ||
abstract class AsyncFilter { | ||
internal val unfiltered = ByteBufferList() | ||
// need separate buffers for filtered and outgoing as filtering may take place during | ||
// read/write operations | ||
internal val filtered = ByteBufferList() | ||
internal val outgoing = ByteBufferList() | ||
|
||
protected abstract fun filter(unfiltered: Buffers, filtered: Buffers) | ||
|
||
// invoke a filter on the pending data, even if it is empty. | ||
protected fun invokeFilter() { | ||
filter(unfiltered, filtered) | ||
} | ||
} | ||
|
||
/** | ||
* Create an AsyncRead that can be interrupted. | ||
* The interrupted read will return true to indicate more data is present, | ||
* and the WritableBuffers will be unchanged. | ||
*/ | ||
class InterruptibleRead(private val input: AsyncRead) { | ||
private var readResume: Continuation<ReadResult>? = null | ||
private val transient = ByteBufferList() | ||
private var reading = false | ||
|
||
fun readTransient() { | ||
val exit = synchronized(this) { | ||
if (reading) | ||
return@synchronized true | ||
reading = true | ||
false | ||
} | ||
if (exit) | ||
return | ||
private val pipe = PipeSocket() | ||
|
||
init { | ||
startSafeCoroutine { | ||
val result = try { | ||
try { | ||
val ret = input(transient) | ||
ReadResult(true, ret, null) | ||
} finally { | ||
reading = false | ||
val buffer = ByteBufferList() | ||
try { | ||
pipe.write(ByteBufferList()) | ||
|
||
while (buffer.hasRemaining() || input(buffer)) { | ||
if (buffer.hasRemaining()) | ||
pipe.write(buffer) | ||
} | ||
} catch (t: Throwable) { | ||
ReadResult(true, null, t) | ||
} | ||
|
||
val resume: Continuation<ReadResult>? = synchronized(this) { | ||
val resume = readResume | ||
readResume = null | ||
resume | ||
catch (throwable: Throwable) { | ||
pipe.close(throwable) | ||
return@startSafeCoroutine | ||
} | ||
resume?.resume(result) | ||
} | ||
} | ||
|
||
private data class ReadResult(val succeeded: Boolean, val result: Boolean? = null, val throwable: Throwable? = null) | ||
|
||
suspend fun read(buffer: WritableBuffers): Boolean { | ||
check(readResume == null) { "read already in progress" } | ||
|
||
if (transient.hasRemaining()) { | ||
transient.read(buffer) | ||
return true | ||
} | ||
|
||
val result = suspendCoroutine<ReadResult> read@{ | ||
readResume = it | ||
readTransient() | ||
pipe.close() | ||
} | ||
|
||
if (!result.succeeded) | ||
return true | ||
if (result.throwable != null) | ||
throw result.throwable | ||
transient.read(buffer) | ||
return result.result!! | ||
} | ||
|
||
fun interrupt() { | ||
val resume: Continuation<ReadResult>? = synchronized(this) { | ||
val resume = readResume | ||
readResume = null | ||
resume | ||
} | ||
resume?.resume(ReadResult(false)) | ||
} | ||
} | ||
suspend fun read(buffer: WritableBuffers) = pipe.read(buffer) | ||
fun interrupt() = pipe.interruptRead() | ||
fun readTransient() = pipe.interruptWrite() | ||
} |
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
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