Scoop API

scoop:scoop.asyncio:0.1.0

src/tcp_create.scoop

Selected production source · src/tcp_create.scoop

1package scoop.asyncio
3import scoop.platform.net.SocketAddress
5internal suspend fun connectTcpOnLoop(loop: LoopOwner, address: SocketAddress): AsyncTcpStream {
6 val job = loop.requireJob()
7 val target = ioAddress(address)
8 return suspendCoroutine<AsyncTcpStream> {
9 continuation ->
10 val operation =
11 CallbackOperation(
12 loop,
13 loop.nextOperation(),
14 {
15 native,
16 id -> @Unsafe
17 {
18 return@CallbackOperation nativeStatus(
19 id,
20 nativeTcpConnect(native, id, target)
21 )
22 }
23 },
24 {
25 native,
26 id -> @Unsafe
27 {
28 nativeTcpConnectCancel(native, id)
29 }
30 },
31 {
32 event,
33 selected -> finishEndpointCreation<AsyncTcpStream>(
34 loop,
35 EndpointKind.Tcp,
36 event.result,
37 selected,
38 continuation,
39 {
40 owner -> TcpStream(
41 owner,
42 tcpAddress(owner.pointer, false),
43 tcpAddress(owner.pointer, true)
44 )
45 }
46 )
47 }
48 )
49 operation.subscribe(job)
50 loop.submit(operation)
51 }
54internal suspend fun listenTcpOnLoop(
55 loop: LoopOwner,
56 address: SocketAddress,
57 backlog: Int,
58 reuse: Boolean
59): AsyncTcpListener {
60 val job = loop.requireJob()
61 if (backlog <= 0)
62 throw IllegalArgumentException(Some("backlog must be positive"))
63 val target = ioAddress(address)
64 return suspendCoroutine<AsyncTcpListener> {
65 continuation ->
66 val operation =
67 CallbackOperation(
68 loop,
69 loop.nextOperation(),
70 {
71 native,
72 id -> @Unsafe
73 {
74 return@CallbackOperation nativeStatus(
75 id,
76 nativeTcpListen(native, id, target, backlog, reuse)
77 )
78 }
79 },
80 {
81 native,
82 id -> @Unsafe
83 {
84 nativeTcpConnectCancel(native, id)
85 }
86 },
87 {
88 event,
89 selected -> finishEndpointCreation<AsyncTcpListener>(
90 loop,
91 EndpointKind.Tcp,
92 event.result,
93 selected,
94 continuation,
95 { owner -> TcpListener(owner, tcpAddress(owner.pointer, false)) }
96 )
97 }
98 )
99 operation.subscribe(job)
100 loop.submit(operation)
101 }