scoop:scoop.platform:0.1.0
src/net/udp.scoop
Selected production source · src/net/udp.scoop
1
package scoop.platform.net3
import scoop.platform.io.*4
import scoop.platform.memory.NativeByteBuffer5
import scoop.platform.system.*7
/**8
* A received datagram's sender, delivered byte count, and whether its payload was truncated.9
*/10
public struct Datagram(val source: SocketAddress, val count: Long, val truncated: Boolean)12
private class ReceiveResult {13
var value: NativeDatagram? = None14
}16
/**17
* An owned blocking datagram socket preserving message boundaries.18
*/19
public class UdpSocket private constructor(private val owner: SocketOwner): Closeable {20
/**21
* Queries the actual bound UDP endpoint.22
*/23
public fun localAddress(): SocketAddress = owner.socketAddress(false)24
/**25
* Whether this owned resource has been closed.26
*/27
public val isClosed: Boolean get() = owner.isClosed29
/**30
* Binds UDP sockets with explicit address-reuse selection.31
*/32
public companion object {33
/**34
* Binds an owned UDP socket to the specified endpoint.35
*/36
public fun bind(address: SocketAddress, reuseAddress: Boolean = false): UdpSocket {37
val owner = SocketOwner()38
val socket = UdpSocket(owner)39
@Unsafe40
{41
owner.adopt(42
nativeSocketBind(nativeAddress(address), true, reuseAddress, 0),43
"UDP bind"44
)45
}46
return socket47
}48
}50
/**51
* Sends one datagram from count readable bytes and advances the buffer by the sent count.52
*/53
public fun sendTo(54
buffer: NativeByteBuffer,55
destination: SocketAddress,56
count: Long = buffer.readableBytes57
): Long =58
owner.withHandle {59
descriptor ->60
requireTransfer(count, buffer.readableBytes)61
@Unsafe62
{63
val sent =64
buffer.withReadableBytes {65
pointer,66
_ -> @Unsafe67
{68
return@withReadableBytes nativeSend(69
descriptor,70
pointer,71
count,72
nativeAddress(destination)73
).checked("UDP send")74
}75
}76
buffer.skip(sent)77
if (sent != count)78
throw PlatformException("incomplete UDP send", PlatformErrorKind.Other)79
return@withHandle sent80
}81
}83
/**84
* Receives one datagram into the writable region, returning sender, byte count, and truncation85
* status.86
*/87
public fun receiveFrom(buffer: NativeByteBuffer, count: Long = buffer.writableBytes): Datagram =88
owner.withHandle {89
descriptor ->90
requireTransfer(count, buffer.writableBytes)91
if (count == 0L)92
throw IllegalArgumentException(Some("UDP receive requires positive capacity"))93
val received = ReceiveResult()94
@Unsafe95
{96
buffer.withWritableBytes {97
pointer,98
_ -> @Unsafe99
{100
val result = nativeReceive(descriptor, pointer!!, count)101
checkSocketError("UDP receive", result.error)102
received.value = Some(result.packet)103
return@withWritableBytes result.packet.count104
}105
}106
}107
val packet = received.value!!108
Datagram(socketAddress(packet.source, 0), packet.count, packet.truncated)109
}111
/**112
* Closes the owned native descriptor; subsequent I/O through this owner is invalid.113
*/114
public override fun close() {115
owner.close()116
}117
}119
@Extern(name = "scoop_platform_udp_send")120
private fun nativeSend(121
descriptor: NativeSocketHandle,122
bytes: Ptr<Byte>?,123
count: Long,124
destination: NativeSocketAddress125
): NativeSocketResult127
@Extern(name = "scoop_platform_udp_receive")128
private fun nativeReceive(129
descriptor: NativeSocketHandle,130
bytes: Ptr<Byte>,131
count: Long132
): NativeSocketReceived