summaryrefslogtreecommitdiff
path: root/src/main/kotlin/TransportLayer.kt
blob: d4a9b12c9b335d0455a58528fcac5ccc8f2c4252 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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 [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: Array<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>
}