@@ -5,31 +5,39 @@ import bytearray
5
5
import io/error
6
6
7
7
8
- /// A tcp handle. Should not be inspected.
8
+ /// A TCP handle. Should not be inspected.
9
9
type Connection = Int
10
10
11
- def connect(host: String, port: Int): Connection / Exception[IOError] =
12
- internal::checkResult(internal::connect(host, port))
13
-
11
+ /// Reads data from a TCP connection into a buffer at the given offset.
14
12
def read(handle: Connection, buffer: ByteArray, offset: Int, size: Int): Int / Exception[IOError] =
15
13
internal::checkResult(internal::read(handle, buffer, offset, size))
16
14
15
+ /// Writes data from a buffer at the given offset to a TCP connection.
17
16
def write(handle: Connection, buffer: ByteArray, offset: Int, size: Int): Int / Exception[IOError] =
18
17
internal::checkResult(internal::write(handle, buffer, offset, size))
19
18
19
+ /// Establishes a TCP connection to the specified host and port.
20
+ def connect(host: String, port: Int): Connection / Exception[IOError] =
21
+ internal::checkResult(internal::connect(host, port))
22
+
23
+ /// Closes a TCP connection and releases associated resources.
20
24
def close(handle: Connection): Unit =
21
25
internal::close(handle)
22
26
23
- /// A tcp listener. Should not be inspected.
27
+ /// A TCP listener. Should not be inspected.
24
28
type Listener = Int
25
29
30
+ /// Creates a TCP listener bound to the specified host and port.
26
31
def bind(host: String, port: Int): Listener / Exception[IOError] =
27
32
internal::checkResult(internal::bind(host, port))
28
33
34
+ /// Starts listening for incoming connections and handles them with the provided handler function.
35
+ /// Runs until `shutdown` is called on this `Listener`.
29
36
def listen(listener: Listener, handler: Connection => Unit at {io, async, global}): Unit / Exception[IOError] = {
30
37
internal::checkResult(internal::listen(listener, handler)); ()
31
38
}
32
39
40
+ /// Stops a TCP listener and releases associated resources.
33
41
def shutdown(listener: Listener): Unit =
34
42
internal::shutdown(listener)
35
43
0 commit comments