-
-
Save mrmitew/8b690bbdb7f7a996af6c7d4006d66f28 to your computer and use it in GitHub Desktop.
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
fun main(args: Array<String>) { | |
if (args[0] == "start") { | |
launch(CommonPool) { | |
println("Launching") | |
SerialisationHelper.test() | |
println("Finished") | |
} | |
} | |
else { | |
val coro = SerialisationHelper.deserialiseCoro() | |
coro.resume(Unit) | |
} | |
while (true) {} | |
} | |
object SerialisationHelper { | |
val kryo = Kryo() | |
init { | |
kryo.instantiatorStrategy = Kryo.DefaultInstantiatorStrategy(StdInstantiatorStrategy()) | |
} | |
suspend fun test(): Unit = suspendCoroutine { | |
println("Serialising") | |
testSerialise(it) | |
} | |
fun <T: Any> serialiseCoro(coro: Continuation<T>) { | |
val output = Output(FileOutputStream("coro.bin")) | |
kryo.writeClassAndObject(output, coro) | |
output.close() | |
} | |
fun deserialiseCoro(): Continuation<Any> { | |
println("Deserialising") | |
val fis = Input(FileInputStream("coro.bin")) | |
val coro = kryo.readClassAndObject(fis) as Continuation<Any> | |
fis.close() | |
coro::class.java.getDeclaredField("result").apply { | |
isAccessible = true | |
set(coro, COROUTINE_SUSPENDED) | |
} | |
return coro | |
} | |
fun <T: Any> testSerialise(coro: Continuation<T>) { | |
serialiseCoro(coro) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment