Skip to content

Commit 8d7a7ef

Browse files
committed
WIP: add FileDescriptor forwarding methods
1 parent 6ebed8f commit 8d7a7ef

File tree

2 files changed

+96
-6
lines changed

2 files changed

+96
-6
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
This source file is part of the Swift System open source project
3+
4+
Copyright (c) 2021 Apple Inc. and the Swift System project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See https://swift.org/LICENSE.txt for license information
8+
*/
9+
10+
extension SocketDescriptor {
11+
/// Writes a sequence of bytes to the socket
12+
///
13+
/// This is equivalent to calling `fileDescriptor.writeAll(_:)`
14+
///
15+
/// - Parameter sequence: The bytes to write.
16+
/// - Returns: The number of bytes written, equal to the number of elements in `sequence`.
17+
@_alwaysEmitIntoClient
18+
@discardableResult
19+
public func writeAll<S: Sequence>(
20+
_ sequence: S
21+
) throws -> Int where S.Element == UInt8 {
22+
try fileDescriptor.writeAll(sequence)
23+
}
24+
25+
/// Runs a closure and then closes the socket, even if an error occurs.
26+
///
27+
/// This is equivalent to calling `fileDescriptor.closeAfter(_:)`
28+
///
29+
/// - Parameter body: The closure to run.
30+
/// If the closure throws an error,
31+
/// this method closes the socket before it rethrows that error.
32+
///
33+
/// - Returns: The value returned by the closure.
34+
///
35+
/// If `body` throws an error
36+
/// or an error occurs while closing the socket,
37+
/// this method rethrows that error.
38+
public func closeAfter<R>(_ body: () throws -> R) throws -> R {
39+
try fileDescriptor.closeAfter(body)
40+
}
41+
}
42+

Sources/System/Sockets/SocketOperations.swift

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,6 @@ extension SocketDescriptor {
4848
}.map(SocketDescriptor.init(rawValue:))
4949
}
5050

51-
/// Deletes a socket's file descriptor.
52-
///
53-
/// This is equivalent to `socket.fileDescriptor.close()`
54-
@_alwaysEmitIntoClient
55-
public func close() throws { try fileDescriptor.close() }
56-
5751
/// Shutdown part of a full-duplex connection
5852
///
5953
/// The corresponding C function is `shutdown`
@@ -87,3 +81,57 @@ extension SocketDescriptor {
8781

8882

8983
}
84+
85+
// MARK: - Forward FileDescriptor methods
86+
extension SocketDescriptor {
87+
/// Deletes a socket's file descriptor.
88+
///
89+
/// This is equivalent to calling `fileDescriptor.close()`
90+
@_alwaysEmitIntoClient
91+
public func close() throws { try fileDescriptor.close() }
92+
93+
/// Reads bytes from a socket.
94+
///
95+
/// This is equivalent to calling `fileDescriptor.read(into:retryOnInterrupt:)`
96+
///
97+
/// - Parameters:
98+
/// - buffer: The region of memory to read into.
99+
/// - retryOnInterrupt: Whether to retry the read operation
100+
/// if it throws ``Errno/interrupted``.
101+
/// The default is `true`.
102+
/// Pass `false` to try only once and throw an error upon interruption.
103+
/// - Returns: The number of bytes that were read.
104+
///
105+
/// The corresponding C function is `read`.
106+
@_alwaysEmitIntoClient
107+
public func read(
108+
into buffer: UnsafeMutableRawBufferPointer, retryOnInterrupt: Bool = true
109+
) throws -> Int {
110+
try fileDescriptor.read(into: buffer, retryOnInterrupt: retryOnInterrupt)
111+
}
112+
113+
/// Writes the contents of a buffer to the socket.
114+
///
115+
/// This is equivalent to `fileDescriptor.write(_:retryOnInterrupt:)`
116+
///
117+
/// - Parameters:
118+
/// - buffer: The region of memory that contains the data being written.
119+
/// - retryOnInterrupt: Whether to retry the write operation
120+
/// if it throws ``Errno/interrupted``.
121+
/// The default is `true`.
122+
/// Pass `false` to try only once and throw an error upon interruption.
123+
/// - Returns: The number of bytes that were written.
124+
///
125+
/// After writing,
126+
/// this method increments the file's offset by the number of bytes written.
127+
/// To change the file's offset,
128+
/// call the ``seek(offset:from:)`` method.
129+
///
130+
/// The corresponding C function is `write`.
131+
@_alwaysEmitIntoClient
132+
public func write(
133+
_ buffer: UnsafeRawBufferPointer, retryOnInterrupt: Bool = true
134+
) throws -> Int {
135+
try fileDescriptor.write(buffer, retryOnInterrupt: retryOnInterrupt)
136+
}
137+
}

0 commit comments

Comments
 (0)