From f2cd571d7afedff496767ba3cbf0546cc3abe87f Mon Sep 17 00:00:00 2001 From: Amir Abbas Date: Wed, 25 Apr 2018 13:23:15 +0430 Subject: [PATCH] Fixed LocalFileMonitor dir not refreshing, Fixed FTP connection refused error by retrying --- Sources/FPSStreamTask.swift | 21 +++++++++++++++++++-- Sources/LocalHelper.swift | 4 +++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/Sources/FPSStreamTask.swift b/Sources/FPSStreamTask.swift index d4af1a6..5581790 100644 --- a/Sources/FPSStreamTask.swift +++ b/Sources/FPSStreamTask.swift @@ -790,10 +790,27 @@ public class FileProviderStreamTask: URLSessionTask, StreamDelegate { } } + private var _retriesForInputStream: Int = 0 + private var _retriesForOutputStream: Int = 0 open func stream(_ aStream: Stream, handle eventCode: Stream.Event) { if eventCode.contains(.errorOccurred) { - self._error = aStream.streamError - streamDelegate?.urlSession?(_underlyingSession, task: self, didCompleteWithError: error) + let retries: Int + if aStream == self.inputStream { + retries = _retriesForInputStream + _retriesForInputStream += 1 + } else if aStream == self.outputStream { + retries = _retriesForOutputStream + _retriesForOutputStream += 1 + } else { + return + } + + if retries < 3 { + aStream.open() + } else { + self._error = aStream.streamError + streamDelegate?.urlSession?(_underlyingSession, task: self, didCompleteWithError: error) + } } if aStream == self.inputStream && eventCode.contains(.hasBytesAvailable) { diff --git a/Sources/LocalHelper.swift b/Sources/LocalHelper.swift index a77fc2a..bc26ed3 100644 --- a/Sources/LocalHelper.swift +++ b/Sources/LocalHelper.swift @@ -101,7 +101,9 @@ public final class LocalFileMonitor { public init(url: URL, handler: @escaping ()->Void) { self.url = url descriptor = open((url.absoluteURL as NSURL).fileSystemRepresentation, O_EVTONLY) - source = DispatchSource.makeFileSystemObjectSource(fileDescriptor: descriptor, eventMask: [.write, .extend, .attrib, .delete, .rename], queue: qq) + let event: DispatchSource.FileSystemEvent = url.fileIsDirectory ? [.write] : .all + source = DispatchSource.makeFileSystemObjectSource(fileDescriptor: descriptor, eventMask: event, queue: qq) + // Folder monitoring is recursive and deep. Monitoring a root folder may be very costly // We have a 0.2 second delay to ensure we wont call handler 1000s times when there is // a huge file operation. This ensures app will work smoothly while this 250 milisec won't