summaryrefslogtreecommitdiff
path: root/src/main/kotlin
diff options
context:
space:
mode:
authorEdoardo La Greca2025-08-05 21:23:50 +0200
committerEdoardo La Greca2025-08-05 21:23:50 +0200
commitfc7c0ddc5b939bf5d2b44a36ab14508d36fee3ba (patch)
tree266d45dbc37e9a58c082430c989314033137a823 /src/main/kotlin
parent445cbf2eacd4c59dfb6662fff03723e18b40639a (diff)
add receiver method to TransportLayer
Diffstat (limited to 'src/main/kotlin')
-rw-r--r--src/main/kotlin/TransportLayer.kt7
-rw-r--r--src/main/kotlin/TransportLayerJavaNet.kt14
2 files changed, 21 insertions, 0 deletions
diff --git a/src/main/kotlin/TransportLayer.kt b/src/main/kotlin/TransportLayer.kt
index d4a9b12..8778592 100644
--- a/src/main/kotlin/TransportLayer.kt
+++ b/src/main/kotlin/TransportLayer.kt
@@ -47,4 +47,11 @@ interface TransportLayer : Closeable {
* @throws java.io.IOException if the message could not be correctly received.
*/
fun receive(length: ULong): Array<UByte>
+
+ /**
+ * Gives the caller a "receiver" (i.e. an instance of Iterable) from which raw data of any length can be read.
+ *
+ * @return The receiver.
+ */
+ fun receiver(): Iterable<UByte>
} \ No newline at end of file
diff --git a/src/main/kotlin/TransportLayerJavaNet.kt b/src/main/kotlin/TransportLayerJavaNet.kt
index 1085100..d2ee2af 100644
--- a/src/main/kotlin/TransportLayerJavaNet.kt
+++ b/src/main/kotlin/TransportLayerJavaNet.kt
@@ -30,6 +30,16 @@ class TransportLayerJavaNet : TransportLayer {
constructor(address: String, port: UShort)
constructor(fullAddress: String)
+ private class InStreamIterator(val inStream: InputStream) : Iterator<UByte> {
+ override fun next(): UByte {
+ return this.inStream.readNBytes(1).first().toUByte()
+ }
+
+ override fun hasNext(): Boolean {
+ return this.inStream.available() > 0
+ }
+ }
+
override fun close() {
if (this.socket.isClosed) {
return
@@ -75,4 +85,8 @@ class TransportLayerJavaNet : TransportLayer {
}
return Array(bytes.size) { i -> bytes[i].toUByte() }
}
+
+ override fun receiver(): Iterable<UByte> {
+ return Iterable { InStreamIterator(this.inStream) }
+ }
} \ No newline at end of file