Skip to content

NSFileHandle/NSPipe compatibility #1659

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 13, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 13 additions & 22 deletions Foundation/FileHandle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,14 @@ open class FileHandle : NSObject, NSSecureCoding {
internal var _fd: Int32
internal var _closeOnDealloc: Bool
internal var _closed: Bool = false


open var readabilityHandler: ((FileHandle) -> Void)? = {
(FileHandle) -> Void in NSUnimplemented()
}
open var writeabilityHandler: ((FileHandle) -> Void)? = {
(FileHandle) -> Void in NSUnimplemented()
}

open var availableData: Data {
return _readDataOfLength(Int.max, untilEOF: false)
}
Expand Down Expand Up @@ -346,14 +353,6 @@ extension FileHandle {
open func waitForDataInBackgroundAndNotify() {
NSUnimplemented()
}

open var readabilityHandler: ((FileHandle) -> Void)? {
NSUnimplemented()
}

open var writeabilityHandler: ((FileHandle) -> Void)? {
NSUnimplemented()
}
}

extension FileHandle {
Expand All @@ -367,9 +366,9 @@ extension FileHandle {
}

open class Pipe: NSObject {
private let readHandle: FileHandle
private let writeHandle: FileHandle
open let fileHandleForReading: FileHandle
open let fileHandleForWriting: FileHandle

public override init() {
/// the `pipe` system call creates two `fd` in a malloc'ed area
var fds = UnsafeMutablePointer<Int32>.allocate(capacity: 2)
Expand All @@ -383,19 +382,11 @@ open class Pipe: NSObject {
/// don't need to add a `deinit` to this class

/// Create the read handle from the first fd in `fds`
self.readHandle = FileHandle(fileDescriptor: fds.pointee, closeOnDealloc: true)
self.fileHandleForReading = FileHandle(fileDescriptor: fds.pointee, closeOnDealloc: true)

/// Advance `fds` by one to create the write handle from the second fd
self.writeHandle = FileHandle(fileDescriptor: fds.successor().pointee, closeOnDealloc: true)
self.fileHandleForWriting = FileHandle(fileDescriptor: fds.successor().pointee, closeOnDealloc: true)

super.init()
}

open var fileHandleForReading: FileHandle {
return self.readHandle
}

open var fileHandleForWriting: FileHandle {
return self.writeHandle
}
}