Scoop API

scoop:scoop.asyncio:0.1.0

src/platform_event_loop.scoop

Selected production source · src/platform_event_loop.scoop

1package scoop.asyncio
3import scoop.asyncio.host.EventLoopHost
4import scoop.time.Duration
5import scoop.platform.net.*
6import scoop.platform.system.OsString
7import scoop.platform.fs.*
8import scoop.platform.process.Command
10/**
11 * A single-use platform event loop. Run it from outside a Job and outside another event loop; I/O
12 * requires an active job in this loop's context.
13 */
14public class PlatformEventLoop internal constructor(
15 config: LoopBlockingConfig
16) : EventLoop {
17 private val lifetime = SingleLoop(config)
18 internal val state: LoopOwner get() = lifetime.state
20 /** The owner executor for this active loop lifetime. */
21 public override val executor: CoroutineExecutor get() = state.activeExecutor()
23 /** Runs a root scope on the caller's thread until all children finish. */
24 public fun <T> run(block: suspend CoroutineScope.() -> T): T = lifetime.run(this, block)
26 /** Drives this scope from the existing host until shutdown and watch removal. */
27 public fun attach(host: EventLoopHost, onClosed: (Throwable?) -> Unit): EventLoopSession =
28 lifetime.attach(this, host, onClosed)
30 /**
31 * Suspends for a nonnegative duration and follows current-job cancellation.
32 */
33 public override suspend fun sleep(duration: Duration) {
34 sleepOnLoop(state, duration)
35 }
37 /** Registers a cancellable ordinary timer notification on this loop's owner. */
38 public override fun schedule(duration: Duration, completion: (Throwable?) -> Unit): Disposable =
39 scheduleOnLoop(state, duration, completion)
41 /**
42 * Resolves the host and port, optionally restricted to one address family.
43 */
44 public override suspend fun resolve(host: OsString, port: Int, family: AddressFamily?): Array<
45 SocketAddress
46 > = resolveOnLoop(state, host, port, family)
48 /**
49 * Opens an owned asynchronous file using explicit access, creation, and truncation options.
50 */
51 public override suspend fun openFile(path: Path, options: OpenOptions): AsyncFile =
52 openFileOnLoop(state, path, options)
53 /**
54 * Opens an owned asynchronous terminal handle.
55 */
56 public override suspend fun openTty(): AsyncTty = openTtyOnLoop(state)
57 public override suspend fun spawnPty(
58 command: Command,
59 columns: Long,
60 rows: Long,
61 searchPath: Boolean
62 ): AsyncPty = spawnPtyOnLoop(state, command, columns, rows, searchPath)
64 /**
65 * Queries filesystem metadata, optionally following symbolic links.
66 */
67 public override suspend fun metadata(path: Path, followLinks: Boolean): FileMetadata =
68 metadataOnLoop(state, path, followLinks)
70 /**
71 * Moves a filesystem entry, replacing the destination only when requested.
72 */
73 public override suspend fun moveFile(
74 source: Path,
75 destination: Path,
76 replaceExisting: Boolean
77 ) {
78 moveFileOnLoop(state, source, destination, replaceExisting)
79 }
80 /**
81 * Deletes the selected filesystem entry.
82 */
83 public override suspend fun deleteFile(path: Path) {
84 deleteFileOnLoop(state, path)
85 }
86 /**
87 * Creates missing parent directories with the requested mode, subject to the process umask.
88 */
89 public override suspend fun createDirectories(path: Path, mode: Int) {
90 createDirectoriesOnLoop(state, path, mode)
91 }
92 /**
93 * Creates a unique temporary file and returns both its path and open handle.
94 */
95 public override suspend fun createTempFile(parent: Path?, prefix: String): TemporaryFile =
96 createTemporaryFileOnLoop(state, parent, prefix)
97 /**
98 * Opens an owned directory enumeration cursor.
99 */
100 public override suspend fun openDirectory(path: Path): AsyncDirectoryEntries =
101 openDirectoryOnLoop(state, path)
103 /**
104 * Connects to an IP endpoint, or resolves and races candidates using the supplied host
105 * connection options. The returned stream has independent ownership.
106 */
107 public override suspend fun connectTcp(address: SocketAddress): AsyncTcpStream =
108 connectTcpOnLoop(state, address)
109 /**
110 * Connects to an IP endpoint, or resolves and races candidates using the supplied host
111 * connection options. The returned stream has independent ownership.
112 */
113 public override suspend fun connectTcp(
114 host: OsString,
115 port: Int,
116 options: TcpConnectOptions
117 ): AsyncTcpStream = connectTcpHostOnLoop(state, host, port, options)
118 /**
119 * Binds and listens for TCP connections with the requested backlog and address-reuse setting.
120 */
121 public override suspend fun listenTcp(
122 address: SocketAddress,
123 backlog: Int,
124 reuseAddress: Boolean
125 ): AsyncTcpListener = listenTcpOnLoop(state, address, backlog, reuseAddress)
127 /**
128 * Binds an owned UDP socket, optionally enabling address reuse.
129 */
130 public override suspend fun bindUdp(
131 address: SocketAddress,
132 reuseAddress: Boolean
133 ): AsyncUdpSocket = bindUdpOnLoop(state, address, reuseAddress)