diff --git a/Sources/AsyncConnection.swift b/Sources/AsyncConnection.swift index 9ad3c81..31ba4ee 100644 --- a/Sources/AsyncConnection.swift +++ b/Sources/AsyncConnection.swift @@ -1,9 +1,9 @@ public protocol AsyncConnection: AsyncStream { - func open(timingOut deadline: Double, completion: ((Void) throws -> AsyncConnection) -> Void) throws + func open(deadline: Double, completion: ((Void) throws -> AsyncConnection) -> Void) throws } extension AsyncConnection { public func open(completion: ((Void) throws -> AsyncConnection) -> Void) throws { - try open(timingOut: .never, completion: completion) + try open(deadline: .never, completion: completion) } } diff --git a/Sources/AsyncDrain.swift b/Sources/AsyncDrain.swift index 7b83b8f..4d686f6 100644 --- a/Sources/AsyncDrain.swift +++ b/Sources/AsyncDrain.swift @@ -10,10 +10,10 @@ public final class AsyncDrain: DataRepresentable, AsyncStream { } public convenience init() { - self.init(for: []) + self.init(buffer: []) } - public init(for stream: AsyncReceivingStream, timingOut deadline: Double = .never, completion: @escaping ((Void) throws -> AsyncDrain) -> Void) { + public init(stream: AsyncInputStream, deadline: Double = .never, completion: @escaping ((Void) throws -> AsyncDrain) -> Void) { var buffer: Data = [] if stream.closed { @@ -24,7 +24,7 @@ public final class AsyncDrain: DataRepresentable, AsyncStream { return } - stream.receive(upTo: 1024, timingOut: deadline) { [unowned self] getData in + stream.read(upTo: 1024, deadline: deadline) { [unowned self] getData in do { let chunk = try getData() buffer.bytes += chunk.bytes @@ -43,12 +43,12 @@ public final class AsyncDrain: DataRepresentable, AsyncStream { } } - public init(for buffer: Data) { + public init(buffer: Data) { self.buffer = buffer } - public convenience init(for buffer: DataRepresentable) { - self.init(for: buffer.data) + public convenience init(buffer: DataRepresentable) { + self.init(buffer: buffer.data) } public func close() throws { @@ -58,7 +58,7 @@ public final class AsyncDrain: DataRepresentable, AsyncStream { closed = true } - public func receive(upTo byteCount: Int, timingOut deadline: Double = .never, completion: ((Void) throws -> Data) -> Void) { + public func read(upTo byteCount: Int, deadline: Double = .never, completion: @escaping ((Void) throws -> Data) -> Void) { if byteCount >= buffer.count { completion { [unowned self] in try self.close() @@ -75,12 +75,12 @@ public final class AsyncDrain: DataRepresentable, AsyncStream { } } - public func send(_ data: Data, timingOut deadline: Double = .never, completion: ((Void) throws -> Void) -> Void) { + public func write(_ data: Data, deadline: Double = .never, completion: @escaping ((Void) throws -> Void) -> Void) { buffer += data.bytes completion {} } - public func flush(timingOut deadline: Double = .never, completion: ((Void) throws -> Void) -> Void) { + public func flush(deadline: Double = .never, completion: @escaping ((Void) throws -> Void) -> Void) { buffer = [] completion {} } diff --git a/Sources/AsyncHost.swift b/Sources/AsyncHost.swift index 6e136c8..4ceb639 100644 --- a/Sources/AsyncHost.swift +++ b/Sources/AsyncHost.swift @@ -1,9 +1,9 @@ public protocol AsyncHost { - func accept(timingOut deadline: Double, completion: ((Void) throws -> AsyncStream) -> Void) + func accept(deadline: Double, completion: ((Void) throws -> AsyncStream) -> Void) } extension AsyncHost { public func accept(completion: ((Void) throws -> AsyncStream) -> Void) { - accept(timingOut: .never, completion: completion) + accept(deadline: .never, completion: completion) } } diff --git a/Sources/AsyncStream.swift b/Sources/AsyncStream.swift index 132023a..ffc637b 100644 --- a/Sources/AsyncStream.swift +++ b/Sources/AsyncStream.swift @@ -1,27 +1,32 @@ -public protocol AsyncSending { - func send(_ data: Data, timingOut deadline: Double, completion: ((Void) throws -> Void) -> Void) - func flush(timingOut deadline: Double, completion: ((Void) throws -> Void) -> Void) +public protocol AsyncWritable { + func write(_ data: Data, deadline: Double, completion: @escaping ((Void) throws -> Void) -> Void) + func flush(deadline: Double, completion: @escaping ((Void) throws -> Void) -> Void) } -public protocol AsyncReceiving { - func receive(upTo byteCount: Int, timingOut deadline: Double, completion: ((Void) throws -> Data) -> Void) -} - -public protocol AsyncSendingStream: Closable, AsyncSending {} -public protocol AsyncReceivingStream: Closable, AsyncReceiving {} -public protocol AsyncStream: AsyncSendingStream, AsyncReceivingStream {} +extension AsyncWritable { + public func write(_ data: Data, completion: @escaping ((Void) throws -> Void) -> Void) { + write(data, deadline: .never, completion: completion) + } -extension AsyncSending { - public func send(_ data: Data, completion: ((Void) throws -> Void) -> Void) { - send(data, timingOut: .never, completion: completion) + public func write(_ convertible: DataConvertible, deadline: Double = .never, completion: @escaping ((Void) throws -> Void) -> Void) { + write(convertible.data, deadline: deadline, completion: completion) } - public func flush(completion: ((Void) throws -> Void) -> Void) { - flush(timingOut: .never, completion: completion) + + public func flush(completion: @escaping ((Void) throws -> Void) -> Void) { + flush(deadline: .never, completion: completion) } } -extension AsyncReceiving { - public func receive(upTo byteCount: Int, completion: ((Void) throws -> Data) -> Void) { - receive(upTo: byteCount, timingOut: .never, completion: completion) +public protocol AsyncReadable { + func read(upTo byteCount: Int, deadline: Double, completion: @escaping ((Void) throws -> Data) -> Void) +} + +extension AsyncReadable { + public func read(upTo byteCount: Int, completion: @escaping ((Void) throws -> Data) -> Void) { + read(upTo: byteCount, deadline: .never, completion: completion) } } + +public protocol AsyncOutputStream: Closable, AsyncWritable {} +public protocol AsyncInputStream: Closable, AsyncReadable {} +public protocol AsyncStream: AsyncOutputStream, AsyncInputStream {} diff --git a/Sources/C7.swift b/Sources/C7.swift index 51ed470..4a69a90 100644 --- a/Sources/C7.swift +++ b/Sources/C7.swift @@ -1,6 +1,6 @@ #if swift(>=3.0) #else - public typealias ErrorProtocol = ErrorType + public typealias Error = ErrorType public typealias RangeReplaceableCollection = RangeReplaceableCollectionType public typealias MutableCollection = MutableCollectionType public typealias Sequence = SequenceType diff --git a/Sources/Closable.swift b/Sources/Closable.swift deleted file mode 100644 index 4fcf564..0000000 --- a/Sources/Closable.swift +++ /dev/null @@ -1,8 +0,0 @@ -public protocol Closable { - var closed: Bool { get } - func close() throws -} - -public enum ClosableError: Error { - case alreadyClosed -} diff --git a/Sources/Connection.swift b/Sources/Connection.swift index a406835..788a70e 100644 --- a/Sources/Connection.swift +++ b/Sources/Connection.swift @@ -1,9 +1,9 @@ public protocol Connection: Stream { - func open(timingOut deadline: Double) throws + func open(deadline: Double) throws } extension Connection { public func open() throws { - try open(timingOut: .never) + try open(deadline: .never) } } diff --git a/Sources/Data.swift b/Sources/Data.swift index c5846fb..5802162 100644 --- a/Sources/Data.swift +++ b/Sources/Data.swift @@ -184,7 +184,7 @@ extension String: DataConvertible { } #else public init(data: Data) throws { - struct Error: ErrorProtocol {} + struct Error: Error {} var string = "" var decoder = UTF8() var generator = data.generate() diff --git a/Sources/Drain.swift b/Sources/Drain.swift index cfd86a1..d528c29 100644 --- a/Sources/Drain.swift +++ b/Sources/Drain.swift @@ -3,17 +3,14 @@ public final class Drain: DataRepresentable, Stream { public var closed = false public var data: Data { - if !closed { - return buffer - } - return [] + return buffer } public convenience init() { - self.init(for: []) + self.init(buffer: []) } - public init(for stream: ReceivingStream, timingOut deadline: Double = .never) { + public init(stream: InputStream, deadline: Double = .never) { var buffer: Data = [] if stream.closed { @@ -21,7 +18,7 @@ public final class Drain: DataRepresentable, Stream { } while !stream.closed { - if let chunk = try? stream.receive(upTo: 1024, timingOut: deadline) { + if let chunk = try? stream.read(upTo: 1024, deadline: deadline) { buffer.bytes += chunk.bytes } else { break @@ -31,12 +28,12 @@ public final class Drain: DataRepresentable, Stream { self.buffer = buffer } - public init(for buffer: Data) { + public init(buffer: Data) { self.buffer = buffer } - public convenience init(for buffer: DataRepresentable) { - self.init(for: buffer.data) + public convenience init(buffer: DataRepresentable) { + self.init(buffer: buffer.data) } public func close() throws { @@ -46,23 +43,21 @@ public final class Drain: DataRepresentable, Stream { closed = true } - public func receive(upTo byteCount: Int, timingOut deadline: Double = .never) throws -> Data { + public func read(upTo byteCount: Int, deadline: Double = .never) throws -> Data { if byteCount >= buffer.count { try close() return buffer } - let data = buffer[0.. Data + func readAll(deadline: Double) throws -> Data +} + +extension File { + public func read(_ byteCount: Int) throws -> Data { + return try read(byteCount, deadline: .never) + } + + public func readAll() throws -> Data { + return try readAll(deadline: .never) + } +} + +extension File { + public init(path: String) throws { + try self.init(path: path, mode: .read) + } +} diff --git a/Sources/Host.swift b/Sources/Host.swift index f596cea..918b6af 100644 --- a/Sources/Host.swift +++ b/Sources/Host.swift @@ -1,9 +1,9 @@ public protocol Host { - func accept(timingOut deadline: Double) throws -> Stream + func accept(deadline: Double) throws -> Stream } extension Host { - func accept() throws -> Stream { - return try accept(timingOut: .never) + public func accept() throws -> Stream { + return try accept(deadline: .never) } } diff --git a/Sources/Stream.swift b/Sources/Stream.swift index 0ce40f7..9e4d744 100644 --- a/Sources/Stream.swift +++ b/Sources/Stream.swift @@ -1,44 +1,57 @@ -public protocol Sending: AsyncSending { - func send(_ data: Data, timingOut deadline: Double) throws - func flush(timingOut deadline: Double) throws +public protocol Closable { + var closed: Bool { get } + func close() throws } -public protocol Receiving: AsyncReceiving { - func receive(upTo byteCount: Int, timingOut deadline: Double) throws -> Data +public enum ClosableError: Error { + case alreadyClosed } -public protocol SendingStream: Closable, Sending {} -public protocol ReceivingStream: Closable, Receiving {} -public protocol Stream: SendingStream, ReceivingStream {} +public protocol Writable: AsyncWritable { + func write(_ data: Data, deadline: Double) throws + func flush(deadline: Double) throws +} -extension Sending { - public func send(_ data: Data, timingOut deadline: Double, completion: ((Void) throws -> Void) -> Void) { - completion { try self.send(data, timingOut: deadline) } +extension Writable { + public func write(_ data: Data) throws { + try write(data, deadline: .never) } - public func flush(timingOut deadline: Double, completion: ((Void) throws -> Void) -> Void) { - completion { try self.flush(timingOut: deadline) } + public func write(_ convertible: DataConvertible, deadline: Double = .never) throws { + try write(convertible.data, deadline: deadline) + } + + public func flush() throws { + try flush(deadline: .never) } } -extension Sending { - public func send(_ data: Data) throws { - try send(data, timingOut: .never) +extension Writable { + public func write(_ data: Data, deadline: Double, completion: @escaping ((Void) throws -> Void) -> Void) { + completion { try self.write(data, deadline: deadline) } } - - public func flush() throws { - try flush(timingOut: .never) + + public func flush(deadline: Double, completion: @escaping ((Void) throws -> Void) -> Void) { + completion { try self.flush(deadline: deadline) } } } -extension Receiving { - public func receive(upTo byteCount: Int, timingOut deadline: Double, completion: ((Void) throws -> Data) -> Void) { - completion { try self.receive(upTo: byteCount, timingOut: deadline) } +public protocol Readable: AsyncReadable { + func read(upTo byteCount: Int, deadline: Double) throws -> Data +} + +extension Readable { + public func read(upTo byteCount: Int) throws -> Data { + return try read(upTo: byteCount, deadline: .never) } } -extension Receiving { - public func receive(upTo byteCount: Int) throws -> Data { - return try receive(upTo: byteCount, timingOut: .never) +extension Readable { + public func read(upTo byteCount: Int, deadline: Double, completion: @escaping ((Void) throws -> Data) -> Void) { + completion { try self.read(upTo: byteCount, deadline: deadline) } } -} \ No newline at end of file +} + +public protocol OutputStream: Closable, Writable, AsyncOutputStream {} +public protocol InputStream: Closable, Readable, AsyncInputStream {} +public protocol Stream: OutputStream, InputStream, AsyncStream {} diff --git a/Sources/StreamSequence.swift b/Sources/StreamSequence.swift deleted file mode 100644 index 7c21046..0000000 --- a/Sources/StreamSequence.swift +++ /dev/null @@ -1,39 +0,0 @@ -#if swift(>=3.0) - public final class StreamSequence: Sequence { - public let stream: Stream - public let deadline: Double - - public init(for stream: Stream, timingOut deadline: Double = .never) { - self.stream = stream - self.deadline = deadline - } - - public func makeIterator() -> AnyIterator { - return AnyIterator { - if self.stream.closed { - return nil - } - return try? self.stream.receive(upTo: 1024, timingOut: self.deadline) - } - } - } -#else - public final class StreamSequence: SequenceType { - public let stream: Stream - public let deadline: Double - - public init(for stream: Stream, timingOut deadline: Double = .never) { - self.stream = stream - self.deadline = deadline - } - - public func generate() -> AnyGenerator { - return AnyGenerator { - if self.stream.closed { - return nil - } - return try? self.stream.receive(upTo: 1024, timingOut: self.deadline) - } - } - } -#endif