summaryrefslogtreecommitdiff
path: root/src/main/kotlin/net/TransportLayer.kt
diff options
context:
space:
mode:
authorEdoardo La Greca2025-08-12 18:02:04 +0200
committerEdoardo La Greca2025-08-12 18:08:23 +0200
commit1e50cf9c224d03896f176f3718ff80ef1659e9c2 (patch)
treea4eff2d5acf5db1780e34f49daba566ed2ce3fa4 /src/main/kotlin/net/TransportLayer.kt
parentc03cb8abf4cb48e9816d8f2642e6e60823627689 (diff)
move InMessage, OutMessage, TransportLayer, and TransportLayerJavaNet to net package
Diffstat (limited to 'src/main/kotlin/net/TransportLayer.kt')
-rw-r--r--src/main/kotlin/net/TransportLayer.kt59
1 files changed, 59 insertions, 0 deletions
diff --git a/src/main/kotlin/net/TransportLayer.kt b/src/main/kotlin/net/TransportLayer.kt
new file mode 100644
index 0000000..90dcd11
--- /dev/null
+++ b/src/main/kotlin/net/TransportLayer.kt
@@ -0,0 +1,59 @@
+package net
+
+import java.io.Closeable
+
+/**
+ * [TransportLayer] is an interface for network transport-layer operations. A class that implements these methods, once
+ * instantiated, establishes and manages a connection with a remote endpoint defined by an address and a port and allows
+ * to send and receive network messages (also called "payloads").
+ *
+ * The address of the remote endpoint can be an IP address (v4 or v6) or a domain name, in which case it is resolved to
+ * an IP address right before initializing the connection. Every constructor should throw an
+ * [except.UnresolvableHostException] if the remote address is formatted as an actual domain name, but it cannot be
+ * resolved (e.g. it doesn't exist, or it contains forbidden characters).
+ *
+ * Depending on the specific given implementation, the constructor of this class might throw other exceptions (e.g. the
+ * [java.net.Socket] constructor in [TransportLayerJavaNet]).
+ */
+interface TransportLayer : Closeable {
+ /**
+ * Close the connection.
+ */
+ abstract override fun close()
+
+ /**
+ * Transmit a payload.
+ *
+ * @throws java.io.IOException if the message could not be correctly transmitted.
+ */
+ fun transmit(payload: Iterable<UByte>)
+/*
+ /**
+ * Receive a payload until a byte occurs, which marks the end of the message. The byte is discarded after being read
+ * and is not returned.
+ *
+ * If you know both which byte marks the end of the message and the message length, it is advised to use
+ * [receiveFixed] instead, which is usually more efficient.
+ *
+ * @param untilByte The byte that marks the end of the message.
+ * @return the received payload.
+ * @throws java.io.IOException if the message could not be correctly received.
+ */
+ abstract fun receiveUntil(untilByte: UByte): Array<UByte>
+*/
+ /**
+ * Receive a payload with fixed length. If zero, nothing is read.
+ *
+ * @param length The length of the message in bytes.
+ * @return the received payload.
+ * @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