1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
|
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
@_implementationOnly import CoreFoundation
internal extension UInt {
init(_ status: CFStreamStatus) {
self.init(status.rawValue)
}
}
extension Stream {
public struct PropertyKey : RawRepresentable, Equatable, Hashable, Sendable {
public let rawValue: String
public init(_ rawValue: String) {
self.rawValue = rawValue
}
public init(rawValue: String) {
self.rawValue = rawValue
}
}
public enum Status : UInt, Sendable {
case notOpen
case opening
case open
case reading
case writing
case atEnd
case closed
case error
}
public struct Event : OptionSet, Sendable {
public let rawValue : UInt
public init(rawValue: UInt) { self.rawValue = rawValue }
// NOTE: on darwin these are vars
public static let openCompleted = Event(rawValue: 1 << 0)
public static let hasBytesAvailable = Event(rawValue: 1 << 1)
public static let hasSpaceAvailable = Event(rawValue: 1 << 2)
public static let errorOccurred = Event(rawValue: 1 << 3)
public static let endEncountered = Event(rawValue: 1 << 4)
}
}
// Stream is an abstract class encapsulating the common API to InputStream and OutputStream.
// Subclassers of InputStream and OutputStream must also implement these methods.
//@_nonSendable - TODO: Mark with attribute to indicate this pure abstract class defers Sendable annotation to its subclasses.
open class Stream: NSObject {
public override init() {
if type(of: self) == Stream.self {
NSRequiresConcreteImplementation()
}
}
open func open() {
NSRequiresConcreteImplementation()
}
open func close() {
NSRequiresConcreteImplementation()
}
open weak var delegate: StreamDelegate?
// By default, a stream is its own delegate, and subclassers of InputStream and OutputStream must maintain this contract. [someStream setDelegate:nil] must restore this behavior. As usual, delegates are not retained.
open func property(forKey key: PropertyKey) -> AnyObject? {
NSRequiresConcreteImplementation()
}
open func setProperty(_ property: AnyObject?, forKey key: PropertyKey) -> Bool {
NSRequiresConcreteImplementation()
}
open func schedule(in aRunLoop: RunLoop, forMode mode: RunLoop.Mode) {
NSRequiresConcreteImplementation()
}
open func remove(from aRunLoop: RunLoop, forMode mode: RunLoop.Mode) {
NSRequiresConcreteImplementation()
}
open var streamStatus: Status {
NSRequiresConcreteImplementation()
}
open var streamError: Error? {
NSRequiresConcreteImplementation()
}
}
@available(*, unavailable)
extension InputStream : @unchecked Sendable { }
// InputStream is an abstract class representing the base functionality of a read stream.
// Subclassers are required to implement these methods.
open class InputStream: Stream {
enum _Error: Error {
case cantSeekInputStream
}
internal let _streamStorage: AnyObject!
internal final var _stream: CFReadStream { unsafeBitCast(_streamStorage, to: CFReadStream.self) }
// reads up to length bytes into the supplied buffer, which must be at least of size len. Returns the actual number of bytes read.
open func read(_ buffer: UnsafeMutablePointer<UInt8>, maxLength len: Int) -> Int {
return CFReadStreamRead(_stream, buffer, len)
}
// returns in O(1) a pointer to the buffer in 'buffer' and by reference in 'len' how many bytes are available. This buffer is only valid until the next stream operation. Subclassers may return NO for this if it is not appropriate for the stream type. This may return NO if the buffer is not available.
open func getBuffer(_ buffer: UnsafeMutablePointer<UnsafeMutablePointer<UInt8>?>, length len: UnsafeMutablePointer<Int>) -> Bool {
if let result = UnsafeMutablePointer(mutating: CFReadStreamGetBuffer(_stream, 0, len)) {
buffer.pointee = result
return true
} else {
return false
}
}
// returns YES if the stream has bytes available or if it impossible to tell without actually doing the read.
open var hasBytesAvailable: Bool {
return CFReadStreamHasBytesAvailable(_stream)
}
public init(data: Data) {
_streamStorage = CFReadStreamCreateWithData(kCFAllocatorSystemDefault, data._cfObject)
}
public init?(url: URL) {
_streamStorage = CFReadStreamCreateWithFile(kCFAllocatorDefault, url._cfObject)
}
public convenience init?(fileAtPath path: String) {
self.init(url: URL(fileURLWithPath: path))
}
open override func open() {
CFReadStreamOpen(_stream)
}
open override func close() {
CFReadStreamClose(_stream)
}
open override var streamStatus: Status {
return Stream.Status(rawValue: UInt(CFReadStreamGetStatus(_stream)))!
}
open override var streamError: Error? {
return CFReadStreamCopyError(_stream)._nsObject
}
open override func property(forKey key: PropertyKey) -> AnyObject? {
return CFReadStreamCopyProperty(_stream, key.rawValue._cfObject)
}
open override func setProperty(_ property: AnyObject?, forKey key: PropertyKey) -> Bool {
return CFReadStreamSetProperty(_stream, key.rawValue._cfObject, property)
}
open override func schedule(in aRunLoop: RunLoop, forMode mode: RunLoop.Mode) {
CFReadStreamScheduleWithRunLoop(_stream, aRunLoop.currentCFRunLoop, mode.rawValue._cfObject)
}
open override func remove(from aRunLoop: RunLoop, forMode mode: RunLoop.Mode) {
CFReadStreamUnscheduleFromRunLoop(_stream, aRunLoop.currentCFRunLoop, mode.rawValue._cfObject)
}
}
@available(*, unavailable)
extension OutputStream : @unchecked Sendable { }
// OutputStream is an abstract class representing the base functionality of a write stream.
// Subclassers are required to implement these methods.
// Currently this is left as named OutputStream due to conflicts with the standard library's text streaming target protocol named OutputStream (which ideally should be renamed)
open class OutputStream : Stream {
internal let _streamStorage: AnyObject!
internal final var _stream: CFWriteStream { unsafeBitCast(_streamStorage, to: CFWriteStream.self) }
// writes the bytes from the specified buffer to the stream up to len bytes. Returns the number of bytes actually written.
open func write(_ buffer: UnsafePointer<UInt8>, maxLength len: Int) -> Int {
return CFWriteStreamWrite(_stream, buffer, len)
}
// returns YES if the stream can be written to or if it is impossible to tell without actually doing the write.
open var hasSpaceAvailable: Bool {
return CFWriteStreamCanAcceptBytes(_stream)
}
// NOTE: on Darwin this is 'open class func toMemory() -> Self'
required public init(toMemory: ()) {
_streamStorage = CFWriteStreamCreateWithAllocatedBuffers(kCFAllocatorDefault, kCFAllocatorDefault)
}
// TODO: this should use the real buffer API
public init(toBuffer buffer: UnsafeMutablePointer<UInt8>, capacity: Int) {
_streamStorage = CFWriteStreamCreateWithBuffer(kCFAllocatorSystemDefault, buffer, capacity)
}
public init?(url: URL, append shouldAppend: Bool) {
_streamStorage = CFWriteStreamCreateWithFile(kCFAllocatorSystemDefault, url._cfObject)
super.init()
CFWriteStreamSetProperty(_stream, kCFStreamPropertyAppendToFile, shouldAppend._cfObject)
}
public convenience init?(toFileAtPath path: String, append shouldAppend: Bool) {
self.init(url: URL(fileURLWithPath: path), append: shouldAppend)
}
open override func open() {
CFWriteStreamOpen(_stream)
}
open override func close() {
CFWriteStreamClose(_stream)
}
open override var streamStatus: Status {
return Stream.Status(rawValue: UInt(CFWriteStreamGetStatus(_stream)))!
}
open class func toMemory() -> Self {
return self.init(toMemory: ())
}
open override func property(forKey key: PropertyKey) -> AnyObject? {
return CFWriteStreamCopyProperty(_stream, key.rawValue._cfObject)
}
open override func setProperty(_ property: AnyObject?, forKey key: PropertyKey) -> Bool {
return CFWriteStreamSetProperty(_stream, key.rawValue._cfObject, property)
}
open override var streamError: Error? {
return CFWriteStreamCopyError(_stream)._nsObject
}
open override func schedule(in aRunLoop: RunLoop, forMode mode: RunLoop.Mode) {
CFWriteStreamScheduleWithRunLoop(_stream, aRunLoop.currentCFRunLoop, mode.rawValue._cfObject)
}
open override func remove(from aRunLoop: RunLoop, forMode mode: RunLoop.Mode) {
CFWriteStreamUnscheduleFromRunLoop(_stream, aRunLoop.currentCFRunLoop, mode.rawValue._cfObject)
}
}
@available(*, unavailable)
extension _InputStreamSPIForFoundationNetworkingUseOnly : Sendable { }
public struct _InputStreamSPIForFoundationNetworkingUseOnly {
var inputStream: InputStream
public init(_ inputStream: InputStream) {
self.inputStream = inputStream
}
public func seek(to position: UInt64) throws {
try inputStream.seek(to: position)
}
}
extension InputStream {
func seek(to position: UInt64) throws {
guard position > 0 else {
return
}
guard position < Int.max else { throw _Error.cantSeekInputStream }
let bufferSize = 1024
var remainingBytes = Int(position)
let buffer = UnsafeMutableRawBufferPointer.allocate(byteCount: bufferSize, alignment: MemoryLayout<UInt8>.alignment)
guard let pointer = buffer.baseAddress?.assumingMemoryBound(to: UInt8.self) else {
buffer.deallocate()
throw _Error.cantSeekInputStream
}
if self.streamStatus == .notOpen {
self.open()
}
while remainingBytes > 0 && self.hasBytesAvailable {
let read = self.read(pointer, maxLength: min(bufferSize, remainingBytes))
if read == -1 {
throw _Error.cantSeekInputStream
}
remainingBytes -= read
}
buffer.deallocate()
if remainingBytes != 0 {
throw _Error.cantSeekInputStream
}
}
}
// Discussion of this API is ongoing for its usage of AutoreleasingUnsafeMutablePointer
#if false
extension Stream {
open class func getStreamsToHost(withName hostname: String, port: Int, inputStream: AutoreleasingUnsafeMutablePointer<InputStream?>?, outputStream: AutoreleasingUnsafeMutablePointer<OutputStream?>?) {
NSUnsupported()
}
}
extension Stream {
open class func getBoundStreams(withBufferSize bufferSize: Int, inputStream: AutoreleasingUnsafeMutablePointer<InputStream?>?, outputStream: AutoreleasingUnsafeMutablePointer<OutputStream?>?) {
NSUnsupported()
}
}
#endif
public protocol StreamDelegate: AnyObject {
func stream(_ aStream: Stream, handle eventCode: Stream.Event)
}
extension StreamDelegate {
public func stream(_ aStream: Stream, handle eventCode: Stream.Event) { }
}
// MARK: -
extension Stream.PropertyKey {
public static let socketSecurityLevelKey = Stream.PropertyKey(rawValue: "kCFStreamPropertySocketSecurityLevel")
public static let socksProxyConfigurationKey = Stream.PropertyKey(rawValue: "kCFStreamPropertySOCKSProxy")
public static let dataWrittenToMemoryStreamKey = Stream.PropertyKey(rawValue: "kCFStreamPropertyDataWritten")
public static let fileCurrentOffsetKey = Stream.PropertyKey(rawValue: "kCFStreamPropertyFileCurrentOffset")
public static let networkServiceType = Stream.PropertyKey(rawValue: "kCFStreamNetworkServiceType")
}
// MARK: -
public struct StreamSocketSecurityLevel : RawRepresentable, Equatable, Hashable, Sendable {
public let rawValue: String
public init(rawValue: String) {
self.rawValue = rawValue
}
}
extension StreamSocketSecurityLevel {
public static let none = StreamSocketSecurityLevel(rawValue: "kCFStreamSocketSecurityLevelNone")
public static let ssLv2 = StreamSocketSecurityLevel(rawValue: "NSStreamSocketSecurityLevelSSLv2")
public static let ssLv3 = StreamSocketSecurityLevel(rawValue: "NSStreamSocketSecurityLevelSSLv3")
public static let tlSv1 = StreamSocketSecurityLevel(rawValue: "kCFStreamSocketSecurityLevelTLSv1")
public static let negotiatedSSL = StreamSocketSecurityLevel(rawValue: "kCFStreamSocketSecurityLevelNegotiatedSSL")
}
// MARK: -
public struct StreamSOCKSProxyConfiguration : RawRepresentable, Equatable, Hashable, Sendable {
public let rawValue: String
public init(rawValue: String) {
self.rawValue = rawValue
}
}
extension StreamSOCKSProxyConfiguration {
public static let hostKey = StreamSOCKSProxyConfiguration(rawValue: "NSStreamSOCKSProxyKey")
public static let portKey = StreamSOCKSProxyConfiguration(rawValue: "NSStreamSOCKSPortKey")
public static let versionKey = StreamSOCKSProxyConfiguration(rawValue: "kCFStreamPropertySOCKSVersion")
public static let userKey = StreamSOCKSProxyConfiguration(rawValue: "kCFStreamPropertySOCKSUser")
public static let passwordKey = StreamSOCKSProxyConfiguration(rawValue: "kCFStreamPropertySOCKSPassword")
}
// MARK: -
public struct StreamSOCKSProxyVersion : RawRepresentable, Equatable, Hashable, Sendable {
public let rawValue: String
public init(rawValue: String) {
self.rawValue = rawValue
}
}
extension StreamSOCKSProxyVersion {
public static let version4 = StreamSOCKSProxyVersion(rawValue: "kCFStreamSocketSOCKSVersion4")
public static let version5 = StreamSOCKSProxyVersion(rawValue: "kCFStreamSocketSOCKSVersion5")
}
// MARK: - Supported network service types
public struct StreamNetworkServiceTypeValue : RawRepresentable, Equatable, Hashable, Sendable {
public let rawValue: String
public init(rawValue: String) {
self.rawValue = rawValue
}
}
extension StreamNetworkServiceTypeValue {
public static let voIP = StreamNetworkServiceTypeValue(rawValue: "kCFStreamNetworkServiceTypeVoIP")
public static let video = StreamNetworkServiceTypeValue(rawValue: "kCFStreamNetworkServiceTypeVideo")
public static let background = StreamNetworkServiceTypeValue(rawValue: "kCFStreamNetworkServiceTypeBackground")
public static let voice = StreamNetworkServiceTypeValue(rawValue: "kCFStreamNetworkServiceTypeVoice")
public static let callSignaling = StreamNetworkServiceTypeValue(rawValue: "kCFStreamNetworkServiceTypeVoice")
}
// MARK: - Error Domains
// String constants for error domains.
public let NSStreamSocketSSLErrorDomain: String = "NSStreamSocketSSLErrorDomain"
// SSL errors are to be interpreted via <Security/SecureTransport.h>
public let NSStreamSOCKSErrorDomain: String = "NSStreamSOCKSErrorDomain"
|