Peter 4459dc5b47 Add 'submodules/Postbox/' from commit '534443c710e63ff4ea595b5dc7be94550c467734'
git-subtree-dir: submodules/Postbox
git-subtree-mainline: 373769682ef152a8d5ef41ccb064a8387b2ca6f0
git-subtree-split: 534443c710e63ff4ea595b5dc7be94550c467734
2019-06-11 18:56:39 +01:00

61 lines
1.8 KiB
Swift

import Foundation
#if os(macOS)
import SwiftSignalKitMac
#else
import SwiftSignalKit
#endif
func ipcNotify(basePath: String, data: Int64) {
DispatchQueue.global(qos: .default).async {
let path = basePath + ".ipc"
let fd = open(path, open(path, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR))
if fd != -1 {
var value = data
write(fd, &value, 8)
close(fd)
}
}
}
func ipcNotifications(basePath: String) -> Signal<Int64, Void> {
return Signal { subscriber in
let queue = Queue()
let disposable = MetaDisposable()
queue.async {
let path = basePath + ".ipc"
let fd = open(path, open(path, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR))
if fd != -1 {
let readSource = DispatchSource.makeFileSystemObjectSource(fileDescriptor: fd, eventMask: [.write])
var previousValue: Int64 = 0
readSource.setEventHandler(handler: {
subscriber.putNext(Int64.max)
/*lseek(fd, 0, SEEK_SET)
var value: Int64 = 0
if read(fd, &value, 8) == 8 {
if previousValue != value {
previousValue = value
subscriber.putNext(value)
}
}*/
})
readSource.resume()
disposable.set(ActionDisposable {
queue.async {
readSource.cancel()
close(fd)
}
})
} else {
subscriber.putError(Void())
}
}
return disposable
}
}