Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrmitew/8b690bbdb7f7a996af6c7d4006d66f28 to your computer and use it in GitHub Desktop.
Save mrmitew/8b690bbdb7f7a996af6c7d4006d66f28 to your computer and use it in GitHub Desktop.
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