scoop:scoop.platform:0.1.0
src/net/native_types.scoop
Selected production source · src/net/native_types.scoop
1
package scoop.platform.net3
/**4
* The native socket-address bridge representation, including family, port, IPv6 scope, and address5
* words.6
*/7
@CLayout8
public 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: UInt16
)18
@CLayout19
internal struct NativeDatagram(20
val source: NativeSocketAddress,21
val count: Long,22
val truncated: Boolean23
)25
@CLayout26
internal struct NativeSocketReceived(val packet: NativeDatagram, val error: Int)28
@CLayout29
internal struct NativeSocketAddressResult(val address: NativeSocketAddress, val error: Int)31
@CLayout32
internal struct NativeResolution(33
val head: Ptr<Unit>?,34
val code: Int,35
val systemCode: Int,36
val hasSystemCode: Boolean37
)39
internal fun nativeAddress(address: SocketAddress): NativeSocketAddress {40
val value = address.address.native41
return NativeSocketAddress(42
value.family,43
address.port,44
value.scopeId,45
value.a,46
value.b,47
value.c,48
value.d49
)50
}52
internal fun socketAddress(value: NativeSocketAddress, error: Int): SocketAddress {53
checkSocketError("socket address", error)54
return SocketAddress(IpAddress.fromNative(value), value.port)55
}57
internal fun SocketOwner.socketAddress(peer: Boolean): SocketAddress =58
withHandle {59
handle -> @Unsafe60
{61
val result = nativeSocketAddress(handle, peer)62
return@withHandle socketAddress(result.address, result.error)63
}64
}66
internal fun requirePort(port: Int) {67
if (port < 0 || port > 65535)68
throw IllegalArgumentException(Some("port is outside 0..65535"))69
}71
@Extern(name = "scoop_platform_socket_address")72
private fun nativeSocketAddress(73
handle: NativeSocketHandle,74
peer: Boolean75
): NativeSocketAddressResult77
@Extern(name = "scoop_platform_socket_bind_owned")78
internal fun nativeSocketBind(79
address: NativeSocketAddress,80
datagram: Boolean,81
reuse: Boolean,82
backlog: Int83
): NativeSocketOpened