Created
October 24, 2015 20:46
-
-
Save alien11689/67ac3edbe562555acc7f 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
interface PacketTranscoder | |
open class Packet | |
class StringPacket(val body: String) : Packet() | |
interface PacketDecoder<out T : Packet> : PacketTranscoder { | |
fun decode(client: String, buf: Array<String>): T | |
} | |
interface PacketEncoder<in T : Packet> : PacketTranscoder { | |
fun encode(packet: T, buf: Array<String>) | |
} | |
class PacketHandler : PacketDecoder<StringPacket>, PacketEncoder<StringPacket> { | |
override fun decode(client: String, buf: Array<String>) = StringPacket(buf[0]) | |
override fun encode(packet: StringPacket, buf: Array<String>) { | |
buf[0] = packet.body | |
} | |
} | |
fun main(args: Array<String>) { | |
val inbuf = Array(1, { "test" }) | |
val p = PacketHandler() | |
val packet = p.decode("", inbuf) | |
val outBuf = Array(1, { "" }) | |
p.encode(packet, outBuf) | |
assert(inbuf[0] == outBuf[0]) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment