Scoop API

scoop:scoop.asyncio:0.1.0

src/udp_send.scoop

Selected production source · src/udp_send.scoop

1package scoop.asyncio
3import scoop.platform.memory.NativeByteBuffer
4import scoop.platform.net.SocketAddress
6private class SendProgress {
7 var count: Long = 0L
8}
10internal suspend fun sendUdp(
11 owner: OwnedEndpoint,
12 buffer: NativeByteBuffer,
13 destination: SocketAddress,
14 count: Long
15): Long {
16 val job = owner.loop.requireJob()
17 val reservation = owner.reserve(true)
18 try {
19 @Unsafe
20 {
21 return sendBorrowed(owner, buffer, destination, count, job)
22 }
23 } finally {
24 reservation.dispose()
25 }
28@Unsafe
29private suspend fun sendBorrowed(
30 owner: OwnedEndpoint,
31 buffer: NativeByteBuffer,
32 destination: SocketAddress,
33 count: Long,
34 job: Job
35): Long {
36 val target = ioAddress(destination)
37 val borrowed = buffer.borrowReadable(count)
38 val progress = SendProgress()
39 try {
40 return suspendCoroutine<Long> {
41 continuation ->
42 val operation =
43 CallbackOperation(
44 owner.loop,
45 owner.loop.nextOperation(),
46 {
47 _,
48 id -> @Unsafe
49 {
50 return@CallbackOperation nativeStatus(
51 id,
52 nativeUdpSend(
53 owner.pointer,
54 id,
55 borrowed.pointer(),
56 borrowed.length,
57 target
58 )
59 )
60 }
61 },
62 {
63 _,
64 id -> @Unsafe
65 {
66 nativeUdpSendCancel(owner.pointer, id)
67 }
68 },
69 {
70 event, selected ->
71 progress.count = resultProgress(event.result)
72 when (completionFailure("UDP send", event.result, selected)) {
73 case Some(val error) -> continuation.resumeWithException(error)
74 case None -> continuation.resume(progress.count)
75 }
76 }
77 )
78 operation.subscribe(job)
79 owner.attach(operation, true)
80 owner.loop.submit(operation)
81 }
82 } finally {
83 borrowed.complete(progress.count)
84 }