Scoop API

scoop:scoop.platform:0.1.0

src/net/tcp.scoop

Selected production source · src/net/tcp.scoop

1package scoop.platform.net
3import scoop.platform.io.*
4import scoop.platform.memory.NativeByteBuffer
6/**
7 * An owned blocking TCP connection. Input and output can be half-closed separately; close releases
8 * the entire connection.
9 */
10public class TcpStream internal constructor(
11 private val owner: SocketOwner
12): ByteSource, ByteSink, Closeable {
13 /**
14 * Whether this owned resource has been closed.
15 */
16 public val isClosed: Boolean get() = owner.isClosed
17 /**
18 * Queries the local endpoint of this connection.
19 */
20 public fun localAddress(): SocketAddress = owner.socketAddress(false)
21 /**
22 * Queries the remote endpoint of this connection.
23 */
24 public fun peerAddress(): SocketAddress = owner.socketAddress(true)
25 /**
26 * Queries the TCP_NODELAY setting.
27 */
28 public fun noDelay(): Boolean = option(true, false, false)
29 /**
30 * Queries whether TCP keepalive is enabled.
31 */
32 public fun keepAlive(): Boolean = option(false, false, false)
34 /**
35 * Creates owned blocking TCP connections to resolved endpoints.
36 */
37 public companion object {
38 /**
39 * Connects to the specified numeric endpoint or reports a platform failure.
40 */
41 public fun connect(address: SocketAddress): TcpStream {
42 val owner = SocketOwner()
43 val stream = TcpStream(owner)
44 @Unsafe
45 {
46 owner.adopt(nativeConnect(nativeAddress(address)), "TCP connect")
47 }
48 return stream
49 }
50 }
52 /**
53 * Reads into the selected writable region, returning actual bytes and permitting short reads.
54 * Zero for a positive request denotes EOF.
55 */
56 public override fun readInto(buffer: NativeByteBuffer, count: Long): Long =
57 owner.withHandle {
58 handle ->
59 requireTransfer(count, buffer.writableBytes)
60 if (count == 0L)
61 return@withHandle 0L
62 @Unsafe
63 {
64 return@withHandle buffer.withWritableBytes {
65 pointer,
66 _ -> @Unsafe
67 {
68 return@withWritableBytes nativeRead(handle, pointer!!, count).checked(
69 "TCP read"
70 )
71 }
72 }
73 }
74 }
75 /**
76 * Consumes the selected readable region, returning the actual accepted byte count and
77 * permitting short writes.
78 */
79 public override fun writeFrom(buffer: NativeByteBuffer, count: Long): Long =
80 owner.withHandle {
81 descriptor ->
82 requireTransfer(count, buffer.readableBytes)
83 if (count == 0L)
84 return@withHandle 0L
85 @Unsafe
86 {
87 val written =
88 buffer.withReadableBytes {
89 pointer,
90 _ -> @Unsafe
91 {
92 return@withReadableBytes nativeWrite(
93 descriptor,
94 pointer!!,
95 count
96 ).checked("TCP write")
97 }
98 }
99 if (written == 0L)
100 throw WriteZeroException()
101 buffer.skip(written)
102 return@withHandle written
103 }
104 }
106 /**
107 * Enables or disables TCP_NODELAY.
108 */
109 public fun setNoDelay(value: Boolean) {
110 option(true, true, value)
111 }
112 /**
113 * Enables or disables operating-system TCP keepalive.
114 */
115 public fun setKeepAlive(value: Boolean) {
116 option(false, true, value)
117 }
118 private fun option(noDelay: Boolean, set: Boolean, value: Boolean): Boolean =
119 owner.withHandle {
120 descriptor -> @Unsafe
121 {
122 return@withHandle nativeOption(descriptor, noDelay, set, value).checked(
123 "socket option"
124 ) !=
125 0L
126 }
127 }
129 /**
130 * Shuts down the connection's input direction.
131 */
132 public fun shutdownInput() {
133 shutdown(false)
134 }
135 /**
136 * Shuts down output while preserving the input direction.
137 */
138 public fun shutdownOutput() {
139 shutdown(true)
140 }
141 private fun shutdown(output: Boolean) {
142 owner.withHandle {
143 descriptor -> @Unsafe
144 {
145 nativeShutdown(descriptor, output).checked("TCP shutdown")
146 }
147 }
148 }
149 /**
150 * Checks the unbuffered connection state; it does not wait for peer acknowledgement.
151 */
152 public override fun flush() {
153 owner.requireOpen()
154 }
155 /**
156 * Closes the owned native descriptor; subsequent I/O through this owner is invalid.
157 */
158 public override fun close() {
159 owner.close()
160 }
163/**
164 * An owned blocking TCP listener; accepted streams have independent ownership.
165 */
166public class TcpListener private constructor(private val owner: SocketOwner): Closeable {
167 /**
168 * Queries the actual bound address, including any ephemeral port.
169 */
170 public fun localAddress(): SocketAddress = owner.socketAddress(false)
171 /**
172 * Whether this owned resource has been closed.
173 */
174 public val isClosed: Boolean get() = owner.isClosed
176 /**
177 * Binds TCP listening sockets with explicit backlog and reuse policy.
178 */
179 public companion object {
180 /**
181 * Binds and listens on the selected address with the requested backlog.
182 */
183 public fun bind(
184 address: SocketAddress,
185 backlog: Int = 128,
186 reuseAddress: Boolean = false
187 ): TcpListener {
188 if (backlog <= 0)
189 throw IllegalArgumentException(Some("backlog must be positive"))
190 val owner = SocketOwner()
191 val listener = TcpListener(owner)
192 @Unsafe
193 {
194 owner.adopt(
195 nativeSocketBind(nativeAddress(address), false, reuseAddress, backlog),
196 "TCP bind/listen"
197 )
198 }
199 return listener
200 }
201 }
203 /**
204 * Blocks until a connection is accepted, returning an owned stream.
205 */
206 public fun accept(): TcpStream =
207 owner.withHandle {
208 listener ->
209 val accepted = SocketOwner()
210 val stream = TcpStream(accepted)
211 @Unsafe
212 {
213 accepted.adopt(nativeAccept(listener), "TCP accept")
214 }
215 stream
216 }
217 /**
218 * Closes the owned native descriptor; subsequent I/O through this owner is invalid.
219 */
220 public override fun close() {
221 owner.close()
222 }
225@Extern(name = "scoop_platform_tcp_connect")
226private fun nativeConnect(address: NativeSocketAddress): NativeSocketOpened
228@Extern(name = "scoop_platform_tcp_accept")
229private fun nativeAccept(listener: NativeSocketHandle): NativeSocketOpened
231@Extern(name = "scoop_platform_tcp_write")
232private fun nativeWrite(
233 descriptor: NativeSocketHandle,
234 bytes: Ptr<Byte>,
235 count: Long
236): NativeSocketResult
238@Extern(name = "scoop_platform_socket_option")
239private fun nativeOption(
240 descriptor: NativeSocketHandle,
241 noDelay: Boolean,
242 set: Boolean,
243 value: Boolean
244): NativeSocketResult
246@Extern(name = "scoop_platform_tcp_shutdown")
247private fun nativeShutdown(descriptor: NativeSocketHandle, output: Boolean): NativeSocketResult
249@Extern(name = "scoop_platform_tcp_read")
250private fun nativeRead(
251 handle: NativeSocketHandle,
252 bytes: Ptr<Byte>,
253 count: Long
254): NativeSocketResult