Scoop API

scoop:scoop.platform:0.1.0

src/net/native_types.scoop

Selected production source · src/net/native_types.scoop

1package scoop.platform.net
3/**
4 * The native socket-address bridge representation, including family, port, IPv6 scope, and address
5 * words.
6 */
7@CLayout
8public struct NativeSocketAddress(
9 val family: Int,
10 val port: Int,
11 val scopeId: UInt,
12 val a: UInt,
13 val b: UInt,
14 val c: UInt,
15 val d: UInt
18@CLayout
19internal struct NativeDatagram(
20 val source: NativeSocketAddress,
21 val count: Long,
22 val truncated: Boolean
25@CLayout
26internal struct NativeSocketReceived(val packet: NativeDatagram, val error: Int)
28@CLayout
29internal struct NativeSocketAddressResult(val address: NativeSocketAddress, val error: Int)
31@CLayout
32internal struct NativeResolution(
33 val head: Ptr<Unit>?,
34 val code: Int,
35 val systemCode: Int,
36 val hasSystemCode: Boolean
39internal fun nativeAddress(address: SocketAddress): NativeSocketAddress {
40 val value = address.address.native
41 return NativeSocketAddress(
42 value.family,
43 address.port,
44 value.scopeId,
45 value.a,
46 value.b,
47 value.c,
48 value.d
49 )
52internal fun socketAddress(value: NativeSocketAddress, error: Int): SocketAddress {
53 checkSocketError("socket address", error)
54 return SocketAddress(IpAddress.fromNative(value), value.port)
57internal fun SocketOwner.socketAddress(peer: Boolean): SocketAddress =
58 withHandle {
59 handle -> @Unsafe
60 {
61 val result = nativeSocketAddress(handle, peer)
62 return@withHandle socketAddress(result.address, result.error)
63 }
64 }
66internal fun requirePort(port: Int) {
67 if (port < 0 || port > 65535)
68 throw IllegalArgumentException(Some("port is outside 0..65535"))
71@Extern(name = "scoop_platform_socket_address")
72private fun nativeSocketAddress(
73 handle: NativeSocketHandle,
74 peer: Boolean
75): NativeSocketAddressResult
77@Extern(name = "scoop_platform_socket_bind_owned")
78internal fun nativeSocketBind(
79 address: NativeSocketAddress,
80 datagram: Boolean,
81 reuse: Boolean,
82 backlog: Int
83): NativeSocketOpened